Skip to content

Commit

Permalink
Mock browser login for integration specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dombesz committed Dec 20, 2024
1 parent 7512be8 commit 6587a30
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
2 changes: 1 addition & 1 deletion spec/features/my/sessions_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }

Expand Down
2 changes: 1 addition & 1 deletion spec/features/oauth/authorization_code_flow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/support/authentication_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
#
Expand Down

0 comments on commit 6587a30

Please sign in to comment.