Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch test suite to default Rack Test #82

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion devise-otp.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "rotp", ">= 2.0.0"

gem.add_development_dependency "capybara"
gem.add_development_dependency "cuprite"
gem.add_development_dependency "minitest-reporters", ">= 0.5.0"
gem.add_development_dependency "puma"
gem.add_development_dependency "rdoc"
Expand Down
9 changes: 3 additions & 6 deletions test/integration/persistence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ def teardown
visit user_otp_token_path
assert_equal user_otp_token_path, current_path

enable_chrome_headless_downloads(page, "/tmp/devise-otp")
click_link("Download recovery codes")

DownloadHelper.wait_for_download(count: 1) do
click_link("Download recovery codes")
end

assert_equal 1, DownloadHelper.downloads.size
assert current_path.match?(/recovery\.text/)
assert page.body.match?(user.next_otp_recovery_tokens.values.join("\n"))
end

test "trusted status should expire" do
Expand Down
70 changes: 0 additions & 70 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
require "orm/#{DEVISE_ORM}"
require "rails/test_help"
require "capybara/rails"
require "capybara/cuprite"
require "minitest/reporters"

Minitest::Reporters.use!
Expand All @@ -15,77 +14,8 @@

# ActiveSupport::Deprecation.silenced = true

# Use a module to not pollute the global namespace
module CapybaraHelper
def self.register_driver(driver_name, args = [])
opts = {headless: true, js_errors: true, window_size: [1920, 1200], browser_options: {}}
args.each do |arg|
opts[:browser_options][arg] = nil
end

Capybara.register_driver(driver_name) do |app|
Capybara::Cuprite::Driver.new(app, opts)
end
end
end

# Register our own custom drivers
CapybaraHelper.register_driver(:headless_chrome, %w[disable-gpu no-sandbox disable-dev-shm-usage])

# Configure Capybara JS driver
Capybara.current_driver = :headless_chrome
Capybara.javascript_driver = :headless_chrome

# Configure Capybara server
Capybara.run_server = true
Capybara.server = :puma, {Silent: true}

class ActionDispatch::IntegrationTest
include Capybara::DSL

# What capybara calls a "page" in its DSL is actually a Capybara::Session
# and doesn't know about the *command* method that allows us to play with
# the Chrome API.
# See: https://rubydoc.info/github/jnicklas/capybara/master/Capybara/Session
#
# To enable downloads we need to do it on the browser's page object, so fetch it
# from this long method chain.
# See: https://github.com/rubycdp/ferrum/blob/master/lib/ferrum/page.rb
def enable_chrome_headless_downloads(session, directory)
page = session.driver.browser.page
page.command("Page.setDownloadBehavior", behavior: "allow", downloadPath: directory)
end
end

# From https://collectiveidea.com/blog/archives/2012/01/27/testing-file-downloads-with-capybara-and-chromedriver
module DownloadHelper
extend self

TIMEOUT = 10

def downloads
Dir["/tmp/devise-otp/*"]
end

def wait_for_download(count: 1)
yield if block_given?

Timeout.timeout(TIMEOUT) do
sleep 0.2 until downloaded?(count)
end
end

def downloaded?(count)
!downloading? && downloads.size == count
end

def downloading?
downloads.grep(/\.crdownload$/).any?
end

def clear_downloads
FileUtils.rm_f(downloads)
end
end

require "devise-otp"
Loading