Add some more test cases

This commit is contained in:
Klaus Zanders
2023-09-27 18:22:08 +02:00
parent e3257c7667
commit f4043e5fa3
2 changed files with 31 additions and 5 deletions
+11 -4
View File
@@ -35,9 +35,7 @@ class PermissionMock
def initialize(user)
@user = user
@permitted_entities = Hash.new do |hash, entity_project_or_global|
hash[entity_project_or_global] = Array.new
end
reset_permitted_entities
@allow_all_permissions = false
end
@@ -47,7 +45,7 @@ class PermissionMock
def forbid_everything!
@allow_all_permissions = false
@permitted_entites = {}
reset_permitted_entities
end
def in_project(*permissions, project:)
@@ -74,6 +72,14 @@ class PermissionMock
end
permitted_entities[:global] += permissions
end
private
def reset_permitted_entities
@permitted_entities = Hash.new do |hash, entity_project_or_global|
hash[entity_project_or_global] = Array.new
end
end
end
module MockedPermissionHelper
@@ -85,6 +91,7 @@ module MockedPermissionHelper
# Instead of mocking directly on the user, we mock on the UserPermissibleService
# Advantage is that we can handle the `allowed_in_entity?` calls correctly without needing to write
# a mock for each of them
permissible_service = user.send(:user_permissible_service) # access the private instance
# Permission is allowed globally, when it has been given globally
@@ -74,7 +74,8 @@ RSpec.describe MockedPermissionHelper do
before do
mock_permissions_for(user) do |mock|
mock.all_permissions_allowed!
mock.in_project :add_work_packages, project:
mock.globally :add_project
mock.in_project(:add_work_packages, project:)
# this removes all permissions previously set
mock.forbid_everything!
@@ -109,6 +110,24 @@ RSpec.describe MockedPermissionHelper do
end
end
context 'when running the mock service multiple times' do
before do
mock_permissions_for(user) do |mock|
mock.globally :add_project
end
# this will overwrite the mocks from the first run
mock_permissions_for(user) do |mock|
mock.globally :manage_user
end
end
it 'only allows the permissions from the last run' do
expect(user).not_to be_allowed_globally(:add_project)
expect(user).to be_allowed_globally(:manage_user)
end
end
context 'when mocking a global permission' do
before do
mock_permissions_for(user) do |mock|