Module: Verikloak::Rails::Testing::MiddlewareStub

Included in:
Helpers
Defined in:
lib/verikloak/rails/testing/middleware_stub.rb

Overview

Stubs the Verikloak middleware stack so authenticated request specs can run without contacting an OIDC provider or signing real JWTs.

stub_verikloak_middleware patches #call on the relevant middlewares so they inject the supplied claims into env['verikloak.user'] and pass through to the next middleware. The companion verikloak.token env key is also set when available so controller helpers like current_token work. Custom user_env_key / token_env_key settings on Verikloak::Rails.config are honored automatically.

Requires RSpec-style mocks (allow_any_instance_of). Load verikloak/rails/testing/rspec to wire things up automatically, or include this module directly into your example groups.

Constant Summary collapse

DEFAULT_STUB_TOKEN =
'verikloak-test-token'

Instance Method Summary collapse

Instance Method Details

#stub_verikloak_middleware(claims, token: DEFAULT_STUB_TOKEN) ⇒ void

This method returns an undefined value.

Stub all loaded Verikloak middlewares to populate the request environment with the supplied claims.

Parameters:

  • claims (Hash)

    value placed into env['verikloak.user']

  • token (String) (defaults to: DEFAULT_STUB_TOKEN)

    value placed into env['verikloak.token']



29
30
31
32
33
# File 'lib/verikloak/rails/testing/middleware_stub.rb', line 29

def stub_verikloak_middleware(claims, token: DEFAULT_STUB_TOKEN)
  stub_core_middleware(claims, token)
  stub_bff_middleware(claims, token)           if bff_middleware_loaded?
  stub_audience_middleware(claims, token)      if audience_middleware_loaded?
end