Module: CurrentScope::TestHelpers
- Defined in:
- lib/current_scope/test_helpers.rb
Overview
Test support for host apps:
include CurrentScope::TestHelpers
with_current_user(users(:alice)) do
assert component_allows_approve?
end
with_current_user(users(:bob), actor: users(:admin)) do # act-as
assert impersonating?
end
CurrentAttributes resets between examples, so nothing set here can leak.
Instance Method Summary collapse
-
#grant_role!(subject, role:) ⇒ Object
Seed a real org-wide grant for request/system specs.
-
#grant_scoped_role!(subject, role:, record:) ⇒ Object
The scoped-grant companion: seed a role held on ONE specific record.
-
#with_current_user(user, actor: nil) ⇒ Object
Snapshot/restore the RAW attributes rather than using Current.set: the actor reader falls back to user, and Object#with (which set uses) would snapshot that fallback and restore a stale actor.
Instance Method Details
#grant_role!(subject, role:) ⇒ Object
Seed a real org-wide grant for request/system specs. Unlike with_current_user (which only sets Current.user in-process, and is overwritten by Context's before_action on a real request), this persists a RoleAssignment row that survives the request cycle, so a host can test its own controllers behind the gate. It does NOT authenticate — the host still signs the subject in through its own auth. Bang-suffixed like the engine's other DB-mutating helpers (seed_defaults!, Event.record!). Returns the assignment.
43 44 45 |
# File 'lib/current_scope/test_helpers.rb', line 43 def grant_role!(subject, role:) CurrentScope::RoleAssignment.create!(subject: subject, role: role) end |
#grant_scoped_role!(subject, role:, record:) ⇒ Object
The scoped-grant companion: seed a role held on ONE specific record. Returns the scoped assignment.
49 50 51 |
# File 'lib/current_scope/test_helpers.rb', line 49 def grant_scoped_role!(subject, role:, record:) CurrentScope::ScopedRoleAssignment.create!(subject: subject, role: role, resource: record) end |
#with_current_user(user, actor: nil) ⇒ Object
Snapshot/restore the RAW attributes rather than using Current.set: the actor reader falls back to user, and Object#with (which set uses) would snapshot that fallback and restore a stale actor. Saving the underlying hash restores the true prior state.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/current_scope/test_helpers.rb', line 20 def with_current_user(user, actor: nil) # dup: on Rails 8.0 `attributes` returns the LIVE hash, so without a copy # the assignments below would mutate our own snapshot. previous = CurrentScope::Current.attributes.dup CurrentScope::Current.user = user CurrentScope::Current.actor = actor yield ensure # Version-robust restore: `attributes = previous` clears absent keys on # Rails 8.1 but NOT on 8.0 (leaving the block's user set), so reset first, # then re-apply the snapshot. Works identically on the whole >= 8.0 floor. CurrentScope::Current.reset previous.each { |name, value| CurrentScope::Current.public_send(:"#{name}=", value) } end |