From 7c4381402b8607981246350866ca997ff42ebbfe Mon Sep 17 00:00:00 2001 From: Mark Taylor <138604938+mtaylorgds@users.noreply.github.com> Date: Tue, 16 Jul 2024 17:34:11 +0100 Subject: [PATCH] Add a new integration test helper Add an integration test helper for use in integration tests for pages that have been transitioned to the GOV.UK Design System. Has a minimal implementation for now, which is copied from the legacy integration test helper. --- test/integration/downtime_integration_test.rb | 4 +- .../downtime_with_invalid_dates_test.rb | 4 +- test/integration_test_helper.rb | 62 +++++++++++++++++++ 3 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 test/integration_test_helper.rb diff --git a/test/integration/downtime_integration_test.rb b/test/integration/downtime_integration_test.rb index d133a4c1f..1639c4fcc 100644 --- a/test/integration/downtime_integration_test.rb +++ b/test/integration/downtime_integration_test.rb @@ -1,6 +1,6 @@ -require "legacy_integration_test_helper" +require "integration_test_helper" -class DowntimeIntegrationTest < LegacyJavascriptIntegrationTest +class DowntimeIntegrationTest < JavascriptIntegrationTest setup do setup_users diff --git a/test/integration/downtime_with_invalid_dates_test.rb b/test/integration/downtime_with_invalid_dates_test.rb index 4fdcee7c2..b6df0fe7e 100644 --- a/test/integration/downtime_with_invalid_dates_test.rb +++ b/test/integration/downtime_with_invalid_dates_test.rb @@ -1,6 +1,6 @@ -require "legacy_integration_test_helper" +require "integration_test_helper" -class DowntimeWithInvalidDates < LegacyPublisherIntegrationTest +class DowntimeWithInvalidDates < PublisherIntegrationTest setup do setup_users diff --git a/test/integration_test_helper.rb b/test/integration_test_helper.rb new file mode 100644 index 000000000..4486b7453 --- /dev/null +++ b/test/integration_test_helper.rb @@ -0,0 +1,62 @@ +require "test_helper" +require "capybara/rails" +require "capybara-select-2" +require "support/govuk_test" + +class PublisherIntegrationTest < ActionDispatch::IntegrationTest + include Capybara::DSL + include CapybaraSelect2 + include CapybaraSelect2::Helpers + include Warden::Test::Helpers + + teardown do + Capybara.reset_sessions! # Forget the (simulated) browser state + Capybara.use_default_driver # Revert Capybara.current_driver to Capybara.default_driver + GDS::SSO.test_user = nil + end + + def setup_users + # This may not be the right way to do things. We rely on the gds-sso + # having a strategy that uses the first user. We probably want some + # tests that cover the oauth interaction properly + @author = FactoryBot.create(:user, :govuk_editor, name: "Author", email: "test@example.com") + @reviewer = FactoryBot.create(:user, :govuk_editor, name: "Reviewer", email: "test@example.com") + end + + def login_as(user) + GDS::SSO.test_user = user + super(user) + end +end + +class JavascriptIntegrationTest < PublisherIntegrationTest + setup do + Capybara.current_driver = Capybara.javascript_driver + end + + # Get a single user by their name. If the user doesn't exist, return nil. + def get_user(name) + User.where(name:).first + end + + # Set the given user to be the current user + # Accepts either a User object or a user's name + def login_as(user) + unless user.is_a?(User) + user = get_user(user) + end + clear_cookies + GDS::SSO.test_user = user + end + + def clear_cookies + browser = Capybara.current_session.driver.browser + if browser.respond_to?(:clear_cookies) + # Rack::MockSession + browser.clear_cookies + elsif browser.respond_to?(:manage) && browser.manage.respond_to?(:delete_all_cookies) + # Selenium::WebDriver + browser.manage.delete_all_cookies + end + end +end