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: Make spec example names more descriptive

Christophe Bliard3cd78260c0a4
diff --git a/spec/models/capabilities/scopes/default_spec.rb b/spec/models/capabilities/scopes/default_spec.rb
index 4b589ac1af2..8f78c69c420 100644
--- a/spec/models/capabilities/scopes/default_spec.rb
+++ b/spec/models/capabilities/scopes/default_spec.rb
@@ -32,7 +32,7 @@ describe Capabilities::Scopes::Default do
# we focus on the non current user capabilities to make the tests easier to understand
subject(:scope) { Capability.default.where(principal_id: user.id) }
- shared_let(:project) { create(:project) }
+ shared_let(:project) { create(:project, enabled_module_names: []) }
shared_let(:user) { create(:user) }
let(:permissions) { %i[] }
@@ -64,21 +64,14 @@ describe Capabilities::Scopes::Default do
create(:anonymous_role,
permissions: anonymous_permissions)
end
- let(:own_role) { create(:role, permissions: []) }
- let(:own_member) do
- create(:member,
- principal: current_user,
- roles: [own_role],
- project:)
- end
let(:members) { [] }
shared_current_user do
create(:admin)
end
- shared_examples_for 'consists of contract actions' do
- it 'includes the expected for the scoped to user' do
+ shared_examples_for 'consists of contract actions' do |with: 'the expected actions'|
+ it "includes #{with} for the scoped to user" do
expect(scope.pluck(:action, :principal_id, :context_id))
.to match_array(expected)
end
@@ -97,81 +90,75 @@ describe Capabilities::Scopes::Default do
end
context 'without any members and non member roles' do
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
- context 'with a member without a permission' do
+ context 'with a member without any permissions' do
let(:members) { [member] }
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
- context 'with a global member without a permission' do
+ context 'with a global member without any permissions' do
let(:members) { [global_member] }
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
- context 'with a non member role without a permission' do
+ context 'with a non member role without any permissions' do
let(:members) { [non_member_role] }
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
- context 'with a global member with an action permission' do
+ context 'with a global member with a global permission' do
let(:global_permissions) { %i[manage_user] }
let(:members) { [global_member] }
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions', with: 'the actions of the global permission' do
let(:expected) do
[['users/create', user.id, nil],
['users/read', user.id, nil],
['users/update', user.id, nil]]
end
end
+
+ context 'with the user being locked' do
+ before do
+ user.locked!
+ end
+
+ include_examples 'is empty'
+ end
end
- context 'with a member with an action permission' do
+ context 'with a member with a project permission' do
let(:permissions) { %i[manage_members] }
let(:members) { [member] }
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions', with: 'the actions of the project permission' do
let(:expected) do
[['memberships/create', user.id, project.id],
['memberships/destroy', user.id, project.id],
['memberships/update', user.id, project.id]]
end
end
- end
-
- context 'with a global member with an action permission and the user being locked' do
- let(:permissions) { %i[manage_user] }
- let(:members) { [global_member] }
-
- before do
- user.locked!
- end
-
- it_behaves_like 'is empty'
- end
- context 'with a member with an action permission and the user being locked' do
- let(:permissions) { %i[manage_members] }
- let(:members) { [member] }
+ context 'with the user being locked' do
+ before do
+ user.locked!
+ end
- before do
- user.locked!
+ include_examples 'is empty'
end
-
- it_behaves_like 'is empty'
end
- context 'with the non member role with an action permission' do
+ context 'with the non member role with a project permission' do
let(:non_member_permissions) { %i[view_members] }
let(:members) { [non_member_role] }
context 'with the project being private' do
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
context 'with the project being public' do
@@ -179,17 +166,25 @@ describe Capabilities::Scopes::Default do
project.update(public: true)
end
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions', with: 'the actions of the project permission' do
let(:expected) do
[
['memberships/read', user.id, project.id]
]
end
end
+
+ context 'with the user being locked' do
+ before do
+ user.locked!
+ end
+
+ include_examples 'is empty'
+ end
end
end
- context 'with the anonymous role having the action permission in a public project' do
+ context 'with the anonymous role having a project permission in a public project' do
let(:anonymous_permissions) { %i[view_members] }
let(:members) { [anonymous_role] }
@@ -197,22 +192,22 @@ describe Capabilities::Scopes::Default do
project.update(public: true)
end
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
- context 'with the anonymous user with an action permission' do
+ context 'with the anonymous user with a project permission' do
let(:anonymous_permissions) { %i[view_members] }
let!(:user) { create(:anonymous) }
let(:members) { [anonymous_role] }
context 'with the project being private' do
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
- context 'with the anonymous role not having the permission' do
+ context 'with the anonymous role without any permissions' do
let(:anonymous_permissions) { %i[] }
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
context 'with the project being public' do
@@ -220,7 +215,7 @@ describe Capabilities::Scopes::Default do
project.update(public: true)
end
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions', with: 'the actions of the project permission' do
let(:expected) do
[
['memberships/read', user.id, project.id]
@@ -230,11 +225,11 @@ describe Capabilities::Scopes::Default do
end
end
- context 'with a member without a permission and with the non member having a permission' do
+ context 'with a member without any permissions and with the non member having a project permission' do
let(:non_member_permissions) { %i[view_members] }
let(:members) { [member, non_member_role] }
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions', with: 'the actions of the project permission' do
let(:expected) do
[
['memberships/read', user.id, project.id]
@@ -243,12 +238,12 @@ describe Capabilities::Scopes::Default do
end
end
- context 'with a member with a permission and with the non member having the same permission' do
+ context 'with a member with a project permission and with the non member having the same project permission' do
let(:non_member_permissions) { %i[view_members] }
let(:member_permissions) { %i[view_members] }
let(:members) { [member, non_member_role] }
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions', with: 'the actions of the project permission' do
let(:expected) do
[
['memberships/read', user.id, project.id]
@@ -257,81 +252,79 @@ describe Capabilities::Scopes::Default do
end
end
- context 'with the non member role with an action permission and the user being locked' do
- let(:non_member_permissions) { %i[view_members] }
- let(:members) { [non_member_role] }
-
- before do
- project.update(public: true)
- user.locked!
- end
-
- it_behaves_like 'is empty'
- end
-
context 'with an admin' do
before do
user.update(admin: true)
end
- it_behaves_like 'consists of contract actions' do
- let(:expected) do
- # This complicated and programmatic way is chosen so that the test can deal with additional actions being defined
- item = ->(namespace, action, global, module_name) {
- # We only expect contract actions for project modules that are enabled by default. In the
- # default edition the Bim module is not enabled by default for instance and thus it's contract
- # actions are not expected to be part of the default capabilities.
- return if module_name.present? && project.enabled_module_names.exclude?(module_name.to_s)
-
- ["#{API::Utilities::PropertyNameConverter.from_ar_name(namespace.to_s.singularize).pluralize.underscore}/#{action}",
- user.id,
- global ? nil : project.id]
- }
-
- OpenProject::AccessControl
- .contract_actions_map
- .select { |_, v| v[:grant_to_admin] }
- .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module_name]) } } }
- .flatten(2)
- .compact
+ context 'with modules activated' do
+ before do
+ project.enabled_module_names = OpenProject::AccessControl.available_project_modules
end
- end
- end
- context 'with an admin but with modules deactivated' do
- before do
- user.update(admin: true)
- project.enabled_modules = []
- end
+ include_examples 'consists of contract actions',
+ with: 'all actions of all permissions (project and global) grantable to admin' do
+ let(:expected) do
+ # This complicated and programmatic way is chosen so that the test can deal with additional actions being defined
+ item = ->(namespace, action, global, module_name) {
+ # We only expect contract actions for project modules that are enabled by default. In the
+ # default edition the Bim module is not enabled by default for instance and thus it's contract
+ # actions are not expected to be part of the default capabilities.
+ return if module_name.present? && project.enabled_module_names.exclude?(module_name.to_s)
+
+ ["#{API::Utilities::PropertyNameConverter.from_ar_name(namespace.to_s.singularize).pluralize.underscore}/#{action}",
+ user.id,
+ global ? nil : project.id]
+ }
+
+ OpenProject::AccessControl
+ .contract_actions_map
+ .select { |_, v| v[:grant_to_admin] }
+ .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module_name]) } } }
+ .flatten(2)
+ .compact
+ end
- it_behaves_like 'consists of contract actions' do
- let(:expected) do
- # This complicated and programmatic way is chosen so that the test can deal with additional actions being defined
- item = ->(namespace, action, global, module_name) {
- return if module_name.present?
-
- ["#{API::Utilities::PropertyNameConverter.from_ar_name(namespace.to_s.singularize).pluralize.underscore}/#{action}",
- user.id,
- global ? nil : project.id]
- }
-
- OpenProject::AccessControl
- .contract_actions_map
- .select { |_, v| v[:grant_to_admin] }
- .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module_name]) } } }
- .flatten(2)
- .compact
+ it 'does not include actions of permissions non-grantable to admin' do
+ expect(scope.pluck(:action)).not_to include('work_packages/assigned')
+ end
end
end
- end
- context 'with an admin but being locked' do
- before do
- user.update(admin: true)
- user.locked!
+ context 'with modules deactivated' do
+ before do
+ project.enabled_modules = []
+ end
+
+ include_examples 'consists of contract actions',
+ with: 'all actions of all core permissions without the ones from modules' do
+ let(:expected) do
+ # This complicated and programmatic way is chosen so that the test can deal with additional actions being defined
+ item = ->(namespace, action, global, module_name) {
+ return if module_name.present?
+
+ ["#{API::Utilities::PropertyNameConverter.from_ar_name(namespace.to_s.singularize).pluralize.underscore}/#{action}",
+ user.id,
+ global ? nil : project.id]
+ }
+
+ OpenProject::AccessControl
+ .contract_actions_map
+ .select { |_, v| v[:grant_to_admin] }
+ .map { |_, v| v[:actions].map { |vk, vv| vv.map { |vvv| item.call(vk, vvv, v[:global], v[:module_name]) } } }
+ .flatten(2)
+ .compact
+ end
+ end
end
- it_behaves_like 'is empty'
+ context 'with admin user being locked' do
+ before do
+ user.locked!
+ end
+
+ include_examples 'is empty'
+ end
end
context 'without the current user being member in a project' do
@@ -343,19 +336,26 @@ describe Capabilities::Scopes::Default do
current_user.update(admin: false)
end
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
context 'with the current user being member in a project' do
let(:permissions) { %i[manage_members] }
let(:global_permissions) { %i[manage_user] }
+ let(:own_role) { create(:role, permissions: []) }
+ let(:own_member) do
+ create(:member,
+ principal: current_user,
+ roles: [own_role],
+ project:)
+ end
let(:members) { [own_member, member, global_member] }
before do
current_user.update(admin: false)
end
- it_behaves_like 'consists of contract actions' do
+ include_examples 'consists of contract actions' do
let(:expected) do
[
['memberships/create', user.id, project.id],
@@ -373,7 +373,11 @@ describe Capabilities::Scopes::Default do
let(:permissions) { %i[work_package_assigned] }
let(:members) { [member] }
- it_behaves_like 'consists of contract actions' do
+ before do
+ project.enabled_module_names = ['work_package_tracking']
+ end
+
+ include_examples 'consists of contract actions', with: 'the actions of the permission' do
let(:expected) do
[
['work_packages/assigned', user.id, project.id]
@@ -382,7 +386,7 @@ describe Capabilities::Scopes::Default do
end
end
- context 'with a member with an action permission and the project being archived' do
+ context 'with a member with a project permission and the project being archived' do
let(:permissions) { %i[manage_members] }
let(:members) { [member] }
@@ -390,7 +394,7 @@ describe Capabilities::Scopes::Default do
project.update(active: false)
end
- it_behaves_like 'is empty'
+ include_examples 'is empty'
end
end
end