Skip to content

Commit

Permalink
Switch test suite to default Rack Test (#82)
Browse files Browse the repository at this point in the history
* 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);

* delete unnecessary Capybara configuration code (rack_test and puma are already the defaults); remove cuprite from gemspec;
  • Loading branch information
strouptl authored Jun 4, 2024
1 parent c7b15ae commit 2376e15
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 77 deletions.
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"

0 comments on commit 2376e15

Please sign in to comment.