From a52dee2de50ee391ec3977a3eee7b24d7a418558 Mon Sep 17 00:00:00 2001 From: Laney Stroup Date: Tue, 4 Jun 2024 11:07:32 +0900 Subject: [PATCH 1/2] switch to :rack_test driver (prepares for for warden.redirect!); test download URL and content directly via default :rack_test functionality; remove custom driver and download helper (no longer needed); --- test/integration/persistence_test.rb | 9 ++-- test/test_helper.rb | 66 +--------------------------- 2 files changed, 5 insertions(+), 70 deletions(-) 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..af0251e 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,26 +14,9 @@ # 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 +Capybara.current_driver = :rack_test +Capybara.javascript_driver = :rack_test # Configure Capybara server Capybara.run_server = true @@ -42,50 +24,6 @@ def self.register_driver(driver_name, args = []) 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" From 63202bf74ef19f68be4f3b60bbbb8bc90fa87119 Mon Sep 17 00:00:00 2001 From: Laney Stroup Date: Tue, 4 Jun 2024 15:09:01 +0900 Subject: [PATCH 2/2] delete unnecessary Capybara configuration code (rack_test and puma are already the defaults); remove cuprite from gemspec; --- devise-otp.gemspec | 1 - test/test_helper.rb | 8 -------- 2 files changed, 9 deletions(-) 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/test_helper.rb b/test/test_helper.rb index af0251e..dbb3f93 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -14,14 +14,6 @@ # ActiveSupport::Deprecation.silenced = true -# Configure Capybara JS driver -Capybara.current_driver = :rack_test -Capybara.javascript_driver = :rack_test - -# Configure Capybara server -Capybara.run_server = true -Capybara.server = :puma, {Silent: true} - class ActionDispatch::IntegrationTest include Capybara::DSL end