diff --git a/devise-otp.gemspec b/devise-otp.gemspec index f2146d9..05d77f5 100644 --- a/devise-otp.gemspec +++ b/devise-otp.gemspec @@ -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" diff --git a/test/integration/persistence_test.rb b/test/integration/persistence_test.rb index 4c320ea..f2bf702 100644 --- a/test/integration/persistence_test.rb +++ b/test/integration/persistence_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index a28db81..dbb3f93 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -6,7 +6,6 @@ require "orm/#{DEVISE_ORM}" require "rails/test_help" require "capybara/rails" -require "capybara/cuprite" require "minitest/reporters" Minitest::Reporters.use! @@ -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"