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

Refactor specs for allowed_to in nested contexts

Klaus Zanders130021f7cc24
diff --git a/spec/models/users/allowed_to_spec.rb b/spec/models/users/allowed_to_spec.rb
index 1790b25149c..6e3c6904a03 100644
--- a/spec/models/users/allowed_to_spec.rb
+++ b/spec/models/users/allowed_to_spec.rb
@@ -55,6 +55,10 @@ RSpec.describe User, 'allowed_to?' do
let(:permission) { :add_work_packages }
let(:final_setup_step) {}
+ before do
+ project.save
+ end
+
context 'with the user being admin' do
before { user.update(admin: true) }
@@ -82,7 +86,6 @@ RSpec.describe User, 'allowed_to?' do
let(:permission) { :create_meetings } # pick a permission from a module
before do
- project.save
project.enabled_module_names -= ['meetings']
final_setup_step
@@ -95,8 +98,6 @@ RSpec.describe User, 'allowed_to?' do
let(:permission) { :work_package_assigned } # permission that is not automatically granted to admins
before do
- project.save
-
final_setup_step
end
@@ -104,318 +105,284 @@ RSpec.describe User, 'allowed_to?' do
end
end
- context 'w/ the user being a member in the project
- w/o the role having the necessary permission' do
- before do
- member.save!
-
- final_setup_step
- end
-
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, project)
- end
- end
-
- context 'w/ the user being a member in the project
- w/ the role having the necessary permission' do
- before do
- role.add_permission! permission
-
- member.save!
+ context 'without the user being a member in the project' do
+ context 'with the project being private' do
+ before { project.update(public: false) }
- final_setup_step
- end
+ context 'and the permission being assigend to the non-member role' do
+ before do
+ non_member = Role.non_member
+ non_member.add_permission! permission
- it 'is true' do
- expect(user).to be_allowed_to(permission, project)
- end
- end
+ final_setup_step
+ end
- context 'w/ the user being a member in the project
- w/ the role having the necessary permission
- w/o the module being active' do
- let(:permission) { :view_news }
+ it do
+ expect(user).not_to be_allowed_to(permission, project)
+ end
+ end
- before do
- role.add_permission! permission
- project.enabled_module_names = []
+ context 'and requesting a public permission' do
+ let(:permission) { :view_project } # a permission defined as public
- member.save!
+ before do
+ final_setup_step
+ end
- final_setup_step
+ it { expect(user).not_to be_allowed_to(permission, project) }
+ end
end
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, project)
- end
- end
+ context 'and the project being public' do
+ before { project.update(public: true) }
- context 'w/ the user being a member in the project
- w/ the role having the necessary permission
- w/ asking for a controller/action hash
- w/o the module being active' do
- let(:permission) { { controller: 'news', action: 'show' } }
+ context 'and the permission not being assigend to the non-member role' do
+ before do
+ non_member = Role.non_member
+ non_member.remove_permission! permission
- before do
- role.add_permission! permission
- project.enabled_module_names = []
+ final_setup_step
+ end
- member.save!
+ it { expect(user).not_to be_allowed_to(permission, project) }
+ end
- final_setup_step
- end
+ context 'and the permission being assigend to the non-member role' do
+ before do
+ non_member = Role.non_member
+ non_member.add_permission! permission
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, project)
- end
- end
+ final_setup_step
+ end
- context 'w/ the user being a member in the project
- w/o the role having the necessary permission
- w/ non members having the necessary permission' do
- before do
- project.public = false
+ it do
+ expect(user).to be_allowed_to(permission, project)
+ end
+ end
- non_member = Role.non_member
- non_member.add_permission! permission
+ context 'and requesting a public permission' do
+ let(:permission) { :view_project } # a permission defined as public
- member.save!
+ before do
+ final_setup_step
+ end
- final_setup_step
- end
-
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, project)
+ it { expect(user).to be_allowed_to(permission, project) }
+ end
end
end
- context 'w/ the user being a member in the project
- w/o the role having the necessary permission
- w/ inquiring for a permission that is public' do
- let(:permission) { :view_project }
-
- before do
- project.public = false
+ context 'with the user being a member in the project' do
+ before { member.save! }
- member.save!
+ context 'without the role granting the requested permission' do
+ before do
+ role.remove_permission!(permission)
+ end
- final_setup_step
- end
+ context 'and no permissions being assigned to the non-member role' do
+ before { final_setup_step }
- it 'is true' do
- expect(user).to be_allowed_to(permission, project)
- end
- end
+ it { expect(user).not_to be_allowed_to(permission, project) }
+ end
- context 'w/o the user being member in the project
- w/ non member being allowed the action
- w/ the project being private' do
- before do
- project.public = false
- project.save!
+ context 'and the permission being assigend to the non-member role' do
+ before do
+ non_member = Role.non_member
+ non_member.add_permission! permission
+
+ final_setup_step
+ end
+
+ it do
+ # TODO: Figure this one out. In the documentation it says:
+ #
+ # Surprisingly, when looking up permissions, the non member role is always factored in, even it the user
+ # does have other roles within the project as well. That means that if the user has a role in a project
+ # not granting "Create new work package", but the non member role is granting the permission, the user
+ # will in effect have that permission.
+ #
+ # So, in my eyes, with the permission being granted to the non-member role, it should be allowed to do the
+ # requested action.
+
+ skip "Have to figure out why this is not working"
+ expect(user).to be_allowed_to(permission, project)
+ end
+ end
- non_member = Role.non_member
+ context 'and requesting a public permission' do
+ let(:permission) { :view_project } # a permission defined as public
- non_member.add_permission! permission
+ before do
+ project.update(public: false)
+ final_setup_step
+ end
- final_setup_step
+ it { expect(user).to be_allowed_to(permission, project) }
+ end
end
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, project)
- end
- end
+ context 'with the role granting the requested permission' do
+ let(:permission) { :view_news }
- context 'w/o the user being member in the project
- w/ the project being public
- w/ non members being allowed the action' do
- before do
- project.public = true
- project.save!
-
- non_member = Role.non_member
-
- non_member.add_permission! permission
+ before do
+ role.add_permission!(permission)
+ end
- final_setup_step
- end
+ context 'with the module being active' do
+ before do
+ project.enabled_module_names += ['news']
- it 'is true' do
- expect(user).to be_allowed_to(permission, project)
- end
- end
+ final_setup_step
+ end
- context 'w/ the user being member in the project
- w/ the project being public
- w/ non members being allowed the action
- w/o the role being allowed the action' do
- before do
- project.public = true
- project.save!
+ context 'and the permission being requested with the permission name' do
+ it { expect(user).to be_allowed_to(permission, project) }
+ end
- non_member = Role.non_member
- non_member.add_permission! permission
+ context 'and the permission being requested with the controller name and action' do
+ it { expect(user).to be_allowed_to({ controller: 'news', action: 'show' }, project) }
+ end
+ end
- member.save!
+ context 'without the module being active' do
+ before do
+ project.enabled_module_names -= ['news']
- final_setup_step
- end
+ final_setup_step
+ end
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, project)
- end
- end
-
- context 'w/ the user being anonymous
- w/ the project being public
- w/ anonymous being allowed the action' do
- before do
- project.public = true
- project.save!
+ context 'and the permission being requested with the permission name' do
+ it { expect(user).not_to be_allowed_to(permission, project) }
+ end
- anonymous_role.add_permission! permission
-
- final_setup_step
- end
-
- it 'is true' do
- expect(anonymous).to be_allowed_to(permission, project)
+ context 'and the permission being requested with the controller name and action' do
+ it { expect(user).not_to be_allowed_to({ controller: 'news', action: 'show' }, project) }
+ end
+ end
end
end
- context 'w/ the user being anonymous
- w/ the project being public
- w/ querying for a public permission' do
- let(:permission) { :view_project }
-
- before do
- project.public = true
- project.save!
+ context 'with the user being anonymous' do
+ context 'with the project being public' do
+ before { project.update(public: true) }
- anonymous_role.save!
+ context 'without the anonymous role being given the permission' do
+ before do
+ anonymous_role.remove_permission!(permission)
+ final_setup_step
+ end
- final_setup_step
- end
+ it { expect(anonymous).not_to be_allowed_to(permission, project) }
+ end
- it 'is true' do
- expect(anonymous).to be_allowed_to(permission, project)
- end
- end
+ context 'with the anonymous role being given the permission' do
+ before do
+ anonymous_role.add_permission!(permission)
+ final_setup_step
+ end
- context 'w/ the user being anonymous
- w/ requesting a controller and action allowed by multiple permissions
- w/ the project being public
- w/ anonymous being allowed the action' do
- let(:permission) { { controller: '/projects/settings/categories', action: 'show' } }
+ it { expect(anonymous).to be_allowed_to(permission, project) }
+ end
- before do
- project.public = true
- project.save!
+ context 'with a public permission' do
+ let(:permission) { :view_project }
- anonymous_role.add_permission! :manage_categories
+ before { final_setup_step }
- final_setup_step
- end
+ it { expect(anonymous).to be_allowed_to(permission, project) }
+ end
- it 'is true' do
- expect(anonymous)
- .to be_allowed_to(permission, project)
- end
- end
+ context 'with a controller and action that is allowed via multiple permissions' do
+ let(:permission) { :manage_categories }
- context 'w/ the user being anonymous
- w/ the project being public
- w/ anonymous being not allowed the action' do
- before do
- project.public = true
- project.save!
+ before do
+ anonymous_role.add_permission! permission
+ final_setup_step
+ end
- final_setup_step
- end
-
- it 'is false' do
- expect(anonymous).not_to be_allowed_to(permission, project)
+ it { expect(anonymous).to be_allowed_to({ controller: '/projects/settings/categories', action: 'show' }, project) }
+ end
end
end
- context 'w/ the user being a member in two projects
- w/ the user being allowed the action in both projects' do
- before do
- role.add_permission! permission
- role2.add_permission! permission
+ context 'when requesting permission for multiple projects' do
+ context 'with the user being a member of multiple projects' do
+ before do
+ member.save
+ member2.save
+ end
- member.save!
- member2.save!
+ context 'with the permission being granted in both projects' do
+ before do
+ role.add_permission! permission
+ role2.add_permission! permission
- final_setup_step
- end
+ final_setup_step
+ end
- it 'is true' do
- expect(user).to be_allowed_to(permission, [project, project2])
+ it { expect(user).to be_allowed_to(permission, [project, project2]) }
+ end
end
- end
- context 'w/ the user being a member in two projects
- w/ the user being allowed in only one project' do
- before do
- role.add_permission! permission
+ context 'with the permission being granted in only one of the two projects' do
+ before do
+ role.add_permission! permission
+ role2.remove_permission! permission
- member.save!
- member2.save!
+ final_setup_step
+ end
- final_setup_step
+ it { expect(user).not_to be_allowed_to(permission, [project, project2]) }
end
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, [project, project2])
- end
- end
+ context 'with the user not being member of any projects, but both projects being public' do
+ before do
+ project.update(public: true)
+ project2.update(public: true)
+ end
- context 'w/o the user being a member in the two projects
- w/ both projects being public
- w/ non member being allowed the action' do
- before do
- non_member = Role.non_member
- non_member.add_permission! permission
+ context 'with non-member role having the permission' do
+ before do
+ non_member = Role.non_member
+ non_member.add_permission! permission
+ final_setup_step
+ end
- project.update(public: true)
- project2.update(public: true)
+ it { expect(user).to be_allowed_to(permission, [project, project2]) }
+ end
- final_setup_step
- end
+ context 'without non-member role having the permission' do
+ before do
+ non_member = Role.non_member
+ non_member.remove_permission! permission
+ final_setup_step
+ end
- it 'is true' do
- expect(user).to be_allowed_to(permission, [project, project2])
+ it { expect(user).not_to be_allowed_to(permission, [project, project2]) }
+ end
end
- end
- context 'w/o the user being a member in the two projects
- w/ only one project being public
- w/ non member being allowed the action' do
- before do
- non_member = Role.non_member
- non_member.add_permission! permission
-
- project.update(public: true)
- project2.update(public: false)
+ context 'with the user not being member of any projects, but one of the projects being public' do
+ before do
+ project.update(public: true)
+ project2.update(public: false)
+ end
- final_setup_step
- end
+ context 'with non-member role having the permission' do
+ before do
+ non_member = Role.non_member
+ non_member.add_permission! permission
+ final_setup_step
+ end
- it 'is false' do
- expect(user).not_to be_allowed_to(permission, [project, project2])
+ it { expect(user).not_to be_allowed_to(permission, [project, project2]) }
+ end
end
end
- context "w/o the user being member in a project
- w/ the user having the global role
- w/ the global role having the necessary permission" do
+ context 'when requesting a global permission, but with the project as a context' do
before do
- project.save!
-
- global_role.save!
-
global_member.save!
end
@@ -423,42 +390,6 @@ RSpec.describe User, 'allowed_to?' do
expect(user).not_to be_allowed_to(global_permission.name, project)
end
end
-
- context 'w/ requesting a controller and action
- w/ the user being allowed the action' do
- let(:permission) { :view_wiki_pages }
-
- before do
- role.add_permission! permission
-
- member.save!
-
- final_setup_step
- end
-
- it 'is true' do
- expect(user)
- .to be_allowed_to({ controller: 'wiki', action: 'show' }, project)
- end
- end
-
- context 'w/ requesting a controller and action allowed by multiple permissions
- w/ the user being allowed the action' do
- let(:permission) { :manage_categories }
-
- before do
- role.add_permission! permission
-
- member.save!
-
- final_setup_step
- end
-
- it 'is true' do
- expect(user)
- .to be_allowed_to({ controller: 'projects', action: 'show' }, project)
- end
- end
end
shared_examples_for 'w/ inquiring globally' do