From 6587a30b113772e61cf4d571d198c48cde7449cc Mon Sep 17 00:00:00 2001 From: Dombi Attila <83396+dombesz@users.noreply.github.com> Date: Wed, 18 Dec 2024 12:16:19 +0200 Subject: [PATCH] Mock browser login for integration specs. --- spec/features/my/sessions_management_spec.rb | 2 +- spec/features/oauth/authorization_code_flow_spec.rb | 2 +- spec/support/authentication_helpers.rb | 7 ++++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/spec/features/my/sessions_management_spec.rb b/spec/features/my/sessions_management_spec.rb index b72d30c7a71b..011144981042 100644 --- a/spec/features/my/sessions_management_spec.rb +++ b/spec/features/my/sessions_management_spec.rb @@ -28,7 +28,7 @@ require "spec_helper" -RSpec.describe "My account session management", :js do +RSpec.describe "My account session management", :js, :with_cookies do include Redmine::I18n let(:user) { create(:user) } diff --git a/spec/features/oauth/authorization_code_flow_spec.rb b/spec/features/oauth/authorization_code_flow_spec.rb index 84ffc46df8ef..f595b4b646b1 100644 --- a/spec/features/oauth/authorization_code_flow_spec.rb +++ b/spec/features/oauth/authorization_code_flow_spec.rb @@ -144,7 +144,7 @@ def get_and_test_token(code) end # Selenium can't return response headers - context "in browser that can log response headers", js: false do + context "in browser that can log response headers", :with_cookies, js: false do before do login_as user end diff --git a/spec/support/authentication_helpers.rb b/spec/support/authentication_helpers.rb index 4921b3498199..6879e8eaa06b 100644 --- a/spec/support/authentication_helpers.rb +++ b/spec/support/authentication_helpers.rb @@ -34,7 +34,7 @@ def self.included(base) end def login_as(user) - if is_a?(RSpec::Rails::FeatureExampleGroup) + if using_browser_cookies? && is_a?(RSpec::Rails::FeatureExampleGroup) # If we want to mock having finished the login process # we must set the user_id in rack.session accordingly # Otherwise e.g. calls to Warden will behave unexpectantly @@ -49,6 +49,7 @@ def login_as(user) end end + allow(User).to receive(:current).and_return user allow(RequestStore).to receive(:[]).and_call_original allow(RequestStore).to receive(:[]).with(:current_user).and_return(user) end @@ -94,6 +95,10 @@ def session_value_for(user) { user_id: user.id, updated_at: Time.current } end + def using_browser_cookies? + RSpec.current_example.metadata[:with_cookies] + end + module ClassMethods # Sets the current user. #