Module: StandardId::Testing::RequestHelpers
- Defined in:
- lib/standard_id/testing/request_helpers.rb
Overview
Integration test helpers for signing in accounts and making authenticated requests.
Usage in rails_helper.rb:
require "standard_id/testing"
RSpec.configure do |config|
config.include StandardId::Testing::RequestHelpers, type: :request
end
Instance Method Summary collapse
-
#bearer_auth_header(token) ⇒ Hash
Returns an Authorization header hash for Bearer token authentication.
-
#build_jwt(account: nil, sub: nil, client_id: "test-client", scope: "openid", grant_type: "authorization_code", extra: {}) ⇒ String
Build a JWT token for API/service authentication.
-
#create_browser_session(account, user_agent: "RSpec") ⇒ StandardId::BrowserSession
Create a browser session record for integration tests.
Instance Method Details
#bearer_auth_header(token) ⇒ Hash
Returns an Authorization header hash for Bearer token authentication.
55 56 57 |
# File 'lib/standard_id/testing/request_helpers.rb', line 55 def bearer_auth_header(token) { "Authorization" => "Bearer #{token}" } end |
#build_jwt(account: nil, sub: nil, client_id: "test-client", scope: "openid", grant_type: "authorization_code", extra: {}) ⇒ String
Build a JWT token for API/service authentication.
41 42 43 44 45 46 47 48 |
# File 'lib/standard_id/testing/request_helpers.rb', line 41 def build_jwt(account: nil, sub: nil, client_id: "test-client", scope: "openid", grant_type: "authorization_code", extra: {}) sub ||= account&.id raise ArgumentError, "account or sub must be provided" if sub.nil? claims = { sub: sub, client_id: client_id, scope: scope, grant_type: grant_type }.merge(extra) StandardId::JwtService.encode(claims) end |
#create_browser_session(account, user_agent: "RSpec") ⇒ StandardId::BrowserSession
Create a browser session record for integration tests.
For a simpler approach, use stub_web_authentication from AuthenticationHelpers instead.
22 23 24 25 26 27 28 29 |
# File 'lib/standard_id/testing/request_helpers.rb', line 22 def create_browser_session(account, user_agent: "RSpec") StandardId::BrowserSession.create!( account: account, ip_address: "127.0.0.1", user_agent: user_agent, expires_at: StandardId::BrowserSession.expiry ) end |