Module: Hanko::TestHelper
- Defined in:
- lib/hanko/test_helper.rb
Overview
Test utilities for generating JWT tokens and stubbing Hanko endpoints.
Provides helpers that make it easy to test Hanko authentication without hitting a real Hanko API.
Defined Under Namespace
Classes: StubVerifier
Class Method Summary collapse
-
.generate_test_token(sub:, exp:, **extra_claims) ⇒ String
Generates a signed JWT test token using an ephemeral RSA key.
-
.stub_jwks(api_url:) ⇒ WebMock::RequestStub
Stubs the JWKS endpoint using WebMock so tokens from TestHelper.generate_test_token can be verified.
-
.stub_session(sub:, exp:, **extra_claims) ⇒ StubVerifier
Creates a StubVerifier that returns a fixed session payload without cryptographic verification.
-
.test_jwks_response ⇒ String
Returns a JSON string containing the test JWKS (public key set).
Class Method Details
.generate_test_token(sub:, exp:, **extra_claims) ⇒ String
Generates a signed JWT test token using an ephemeral RSA key.
44 45 46 47 |
# File 'lib/hanko/test_helper.rb', line 44 def generate_test_token(sub:, exp:, **extra_claims) payload = { 'sub' => sub, 'exp' => exp }.merge(extra_claims.transform_keys(&:to_s)) JWT.encode(payload, test_key, 'RS256', kid: test_kid) end |
.stub_jwks(api_url:) ⇒ WebMock::RequestStub
Stubs the JWKS endpoint using WebMock so tokens from generate_test_token can be verified.
64 65 66 67 |
# File 'lib/hanko/test_helper.rb', line 64 def stub_jwks(api_url:) WebMock::API.stub_request(:get, "#{api_url}/.well-known/jwks.json") .to_return(status: 200, body: test_jwks_response) end |
.stub_session(sub:, exp:, **extra_claims) ⇒ StubVerifier
Creates a StubVerifier that returns a fixed session payload without cryptographic verification.
79 80 81 82 |
# File 'lib/hanko/test_helper.rb', line 79 def stub_session(sub:, exp:, **extra_claims) payload = { 'sub' => sub, 'exp' => exp }.merge(extra_claims.transform_keys(&:to_s)) StubVerifier.new(payload) end |
.test_jwks_response ⇒ String
Returns a JSON string containing the test JWKS (public key set).
52 53 54 55 |
# File 'lib/hanko/test_helper.rb', line 52 def test_jwks_response jwk = JWT::JWK.new(test_key, kid: test_kid) { keys: [jwk.export] }.to_json end |