From 096202cdc2c36d4835b176c611b6213e99aa6c91 Mon Sep 17 00:00:00 2001 From: Lauri Ojansivu Date: Sat, 2 Nov 2024 05:41:29 +0200 Subject: [PATCH] Fix tests. --- test/application_system_test_case.rb | 5 --- test/support/authentication_helpers.rb | 27 ++++++++++++++++ test/test_helper.rb | 45 +------------------------- 3 files changed, 28 insertions(+), 49 deletions(-) create mode 100644 test/support/authentication_helpers.rb diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb index a2bafa0..0276266 100644 --- a/test/application_system_test_case.rb +++ b/test/application_system_test_case.rb @@ -18,9 +18,4 @@ class ApplicationSystemTestCase < ActionDispatch::SystemTestCase url: "http://selenium:4444", timeout: 120 } - - # Remove duplicate methods since they're now in AuthenticationHelpers - remove_method :sign_in_as if method_defined?(:sign_in_as) - remove_method :current_user if method_defined?(:current_user) - remove_method :teardown if method_defined?(:teardown) end diff --git a/test/support/authentication_helpers.rb b/test/support/authentication_helpers.rb new file mode 100644 index 0000000..d950211 --- /dev/null +++ b/test/support/authentication_helpers.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +module AuthenticationHelpers + def sign_in_as(user) + if respond_to?(:visit) + visit sign_in_url + fill_in :email, with: user.email + fill_in :password, with: "Secret1*3*5*" + click_on "Login to your account" + elsif respond_to?(:session) + session[:user_id] = user.id + else + post sign_in_url, params: { email: user.email, password: "Secret1*3*5*" } + end + @current_user = user + end + + def current_user + @current_user + end + + def teardown + super if defined?(super) + @current_user = nil + session[:user_id] = nil if respond_to?(:session) + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 171d141..a27dd40 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,6 +8,7 @@ require_relative "../config/environment" require "rails/test_help" require "minitest/reporters" +require_relative "support/authentication_helpers" Minitest::Reporters.use!( Minitest::Reporters::ProgressReporter.new(color: true), @@ -15,64 +16,23 @@ Minitest.backtrace_filter ) -module AuthenticationHelpers - def sign_in_as(user) - if respond_to?(:visit) - visit sign_in_url - fill_in :email, with: user.email - fill_in :password, with: "Secret1*3*5*" - click_on "Login to your account" - elsif respond_to?(:session) - session[:user_id] = user.id - else - post sign_in_url, params: { email: user.email, password: "Secret1*3*5*" } - end - @current_user = user - end - - def current_user - @current_user - end - - def teardown - super if defined?(super) - @current_user = nil - session[:user_id] = nil if respond_to?(:session) - end -end - module ActiveSupport class TestCase include AuthenticationHelpers # Run tests in parallel with specified workers parallelize(workers: :number_of_processors) - # Fix simplecov failing when run in parallel. - # https://github.com/simplecov-ruby/simplecov/issues/718 - # If it still has problems then disable parallelize. - # parallelize_setup do |worker| - # SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}" - # end - # - # parallelize_teardown do |_worker| - # SimpleCov.result - # end - # Activate FactoryBot methods include FactoryBot::Syntax::Methods - # Add more helper methods to be used by all tests here... - # Pundit helpers def assert_permit(user, record, action) msg = "User #{user.inspect} should be permitted to #{action} #{record}, but isn't permitted" - assert permit(user, record, action), msg end def refute_permit(user, record, action) msg = "User #{user.inspect} should NOT be permitted to #{action} #{record}, but is permitted" - assert_not permit(user, record, action), msg end @@ -86,9 +46,6 @@ def permit(user, record, action) class ActionDispatch::IntegrationTest include AuthenticationHelpers - - # remove_method :teardown if method_defined?(:teardown) - remove_method :sign_in_as if method_defined?(:sign_in_as) end Shoulda::Matchers.configure do |config|