Skip to content
Snippets Groups Projects
Commit c31dd86b authored by Dean Lovett's avatar Dean Lovett
Browse files

fix: fixed issue with cached_permissions retaining state from different models...

fix: fixed issue with cached_permissions retaining state from different models within the same request
parent bd0f0ccd
No related branches found
Tags 0.3.0
No related merge requests found
Pipeline #37793 passed
......@@ -120,7 +120,7 @@ module NexusCqrs
# @param [ApplicationRecord] permission_model Permission model
# @see PermissionProvider#for_user
def cached_permissions(permission_model)
::RequestStore.store[:permissions] ||= for_user(permission_model)
::RequestStore.store["permissions_#{permission_model.class.name}"] ||= for_user(permission_model)
end
end
end
......
......@@ -11,10 +11,36 @@ class EntityPermission
attr_accessor :permission
end
class Entity1Permission
def initialize(entity_id:, permission:)
self.entity_id = entity_id
self.permission = permission
end
attr_accessor :entity_id
attr_accessor :permission
end
class Entity2Permission
def initialize(entity_id:, permission:)
self.entity_id = entity_id
self.permission = permission
end
attr_accessor :entity_id
attr_accessor :permission
end
describe NexusCqrs::Auth::PermissionProvider do
subject { NexusCqrs::Auth::PermissionProvider.new(1, {'global' => ['test']}) }
before do
RequestStore.clear!
end
it "retreives permissions for user in correct format" do
model = double("EntityPermissions")
......@@ -52,4 +78,43 @@ describe NexusCqrs::Auth::PermissionProvider do
[{:global => true, :key => "global:test"}]
)
end
it "correctly caches permissions across entity models" do
model1 = double("Entity1Permissions")
model2 = double("Entity2Permissions")
allow(model1).to receive_message_chain("class.name").and_return("Entity1Permission")
allow(model2).to receive_message_chain("class.name").and_return("Entity2Permission")
expect(model1).to receive(:where).with(user_id: 1).and_return([
Entity1Permission.new(entity_id: 1, permission: 'test:permission'),
Entity1Permission.new(entity_id: 2, permission: 'test:permission')
])
expect(model2).to receive(:where).with(user_id: 1).and_return([
Entity2Permission.new(entity_id: 3, permission: 'test:permission'),
Entity2Permission.new(entity_id: 4, permission: 'test:permission')
])
# Check two different models are being cached correctly
expect(subject.for_user_on_entity(model1, 1)).to eq(
[{:global => true, :key => "global:test"},
{:global => false, :key => "test:permission"}]
)
expect(subject.for_user_on_entity(model2, 4)).to eq(
[{:global => true, :key => "global:test"},
{:global => false, :key => "test:permission"}]
)
# Check state hasn't been left from previous model call
expect(subject.for_user_on_entity(model1, 3)).to eq(
[{:global => true, :key => "global:test"}]
)
expect(subject.for_user_on_entity(model2, 1)).to eq(
[{:global => true, :key => "global:test"}]
)
end
end
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment