Skip to content

Commit d6dbc83

Browse files
committed
Fix OmniAuth 2.x CSRF check in test mode stubs
OmniAuth 2.1.4 still runs request_validation_phase inside mock_request_call even when test_mode is true. With omniauth-rails_csrf_protection installed this raises InvalidAuthenticityToken on the mocked POST, breaking feature specs that drive the SSO button flow. Disable request_validation_phase while stubs are active and restore the original value in reset_oidc_stubs.
1 parent 7151fe5 commit d6dbc83

File tree

1 file changed

+9
-0
lines changed

1 file changed

+9
-0
lines changed

lib/activeadmin/oidc/test_helpers.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ module TestHelpers
2929
def stub_oidc_sign_in(sub: 'alice-sub', claims: {})
3030
merged = DEFAULT_CLAIMS.merge(claims.transform_keys(&:to_s))
3131
OmniAuth.config.test_mode = true
32+
# OmniAuth 2.x still runs request_validation_phase in test mode
33+
# (mock_request_call, line 325 of strategy.rb). Disable it so
34+
# the CSRF check from omniauth-rails_csrf_protection doesn't
35+
# reject the mocked request.
36+
@_oidc_saved_request_validation_phase = OmniAuth.config.request_validation_phase
37+
OmniAuth.config.request_validation_phase = nil
3238
OmniAuth.config.mock_auth[:oidc] = OmniAuth::AuthHash.new(
3339
provider: 'oidc',
3440
uid: sub,
@@ -45,13 +51,16 @@ def stub_oidc_sign_in(sub: 'alice-sub', claims: {})
4551
# Stubs OmniAuth to simulate a strategy failure.
4652
def stub_oidc_failure(message_key = :invalid_credentials)
4753
OmniAuth.config.test_mode = true
54+
@_oidc_saved_request_validation_phase = OmniAuth.config.request_validation_phase
55+
OmniAuth.config.request_validation_phase = nil
4856
OmniAuth.config.mock_auth[:oidc] = message_key
4957
end
5058

5159
# Resets OmniAuth test mode. Call in an `after` hook.
5260
def reset_oidc_stubs
5361
OmniAuth.config.mock_auth[:oidc] = nil
5462
OmniAuth.config.test_mode = false
63+
OmniAuth.config.request_validation_phase = @_oidc_saved_request_validation_phase if defined?(@_oidc_saved_request_validation_phase)
5564
end
5665
end
5766

0 commit comments

Comments
 (0)