Module: Hanko::Rails::TestHelper

Defined in:
lib/hanko/rails/test_helper.rb

Overview

Test helpers for simulating Hanko authentication in integration tests.

Include this module in your test classes to sign in and out of Hanko sessions without hitting the real Hanko API.

Examples:

Using in a Rails integration test

class PostsTest < ActionDispatch::IntegrationTest
  include Hanko::Rails::TestHelper

  test 'authenticated user can view posts' do
    ('user-uuid-123')
    get posts_path
    assert_response :success
  end
end

Instance Method Summary collapse

Instance Method Details

#sign_in_as_hanko_user(user_id) ⇒ void

This method returns an undefined value.

Signs in as a Hanko user by generating a test JWT and setting it as a cookie.

Stubs the JWKS endpoint so the middleware can verify the test token.

Examples:

('user-uuid-123')

Parameters:

  • user_id (String)

    the Hanko user ID to authenticate as



30
31
32
33
34
35
# File 'lib/hanko/rails/test_helper.rb', line 30

def (user_id)
  Hanko::TestHelper.stub_jwks(api_url: Hanko.configuration.api_url)
  token = Hanko::TestHelper.generate_test_token(sub: user_id, exp: Time.now.to_i + 3600)
  cookie_name = Hanko::Rails.configuration.cookie_name
  set_cookie "#{cookie_name}=#{token}"
end

#sign_out_hanko_uservoid

This method returns an undefined value.

Signs out the current Hanko user by clearing the session cookie.

Examples:

sign_out_hanko_user


43
44
45
46
# File 'lib/hanko/rails/test_helper.rb', line 43

def sign_out_hanko_user
  cookie_name = Hanko::Rails.configuration.cookie_name
  set_cookie "#{cookie_name}="
end