Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add some more test cases

Klaus Zandersf4043e5fa34c
diff --git a/spec/support/mocked_permission_helper.rb b/spec/support/mocked_permission_helper.rb
index 1d55263688d..ed0d9ea2c97 100644
--- a/spec/support/mocked_permission_helper.rb
+++ b/spec/support/mocked_permission_helper.rb
@@ -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
diff --git a/spec/support_spec/mocked_permission_helper_spec.rb b/spec/support_spec/mocked_permission_helper_spec.rb
index 555973aa385..96dd86ba10a 100644
--- a/spec/support_spec/mocked_permission_helper_spec.rb
+++ b/spec/support_spec/mocked_permission_helper_spec.rb
@@ -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|