Module: Machina::TestHelpers
- Defined in:
- lib/machina/test_helpers.rb
Overview
Test helpers for applications integrating with the machina-auth gem.
Include this module in your test framework to get convenience methods for setting up authenticated contexts in controller, request, and integration tests.
# RSpec
RSpec.configure do |config|
config.include Machina::TestHelpers
end
# Minitest
class ActiveSupport::TestCase
include Machina::TestHelpers
end
Instance Method Summary collapse
-
#sign_in_as_machina(tenant_ref = nil, user: {}, organization: {}, workspace: {}, session: {}, permissions: nil) ⇒ Machina::Authorized
Sets
Machina::Current.authorizedto a fully-populatedAuthorizedinstance built from the provided overrides merged onto realistic defaults. -
#sign_out_machina ⇒ Object
Clears the current authenticated context.
Instance Method Details
#sign_in_as_machina(tenant_ref = nil, user: {}, organization: {}, workspace: {}, session: {}, permissions: nil) ⇒ Machina::Authorized
Sets Machina::Current.authorized to a fully-populated Authorized instance built from the provided overrides merged onto realistic defaults. Returns the Authorized object.
The first argument is tenant_ref — the workspace ID that scopes all multi-tenant queries. This is the value you’ll use most often, so it’s positional to keep tests concise.
rubocop:disable Metrics/AbcSize, Metrics/ParameterLists
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/machina/test_helpers.rb', line 55 def sign_in_as_machina( tenant_ref = nil, user: {}, organization: {}, workspace: {}, session: {}, permissions: nil ) data = default_session_data data['user'].merge!(stringify(user)) data['session'].merge!(stringify(session)) data['workspace'].merge!(stringify(workspace)) data['organization'].merge!(stringify(organization)) data['workspace']['id'] = tenant_ref.to_s if tenant_ref data['permissions'] = .map(&:to_s) if = Machina::Authorized.new(data) Machina::Current. = end |