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

Prevent misuse of some PrincipalFactory attributes

Christophe Bliard7f47ed662934
diff --git a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
index 6c18823965b..ce10895ef73 100644
--- a/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
+++ b/modules/storages/spec/requests/api/v3/storages/storages_spec.rb
@@ -234,7 +234,7 @@ describe 'API v3 storages resource', content_type: :json, webmock: true do
context 'if user does not belong to a project using the given storage' do
let(:current_user) do
- create(:user, member_with_permissions: permissions)
+ create(:user)
end
it_behaves_like 'not found'
@@ -274,7 +274,7 @@ describe 'API v3 storages resource', content_type: :json, webmock: true do
context 'if user does not belong to a project using the given storage' do
let(:current_user) do
- create(:user, member_with_permissions: permissions)
+ create(:user)
end
it_behaves_like 'not found'
@@ -304,7 +304,7 @@ describe 'API v3 storages resource', content_type: :json, webmock: true do
context 'if user does not belong to a project using the given storage' do
let(:current_user) do
- create(:user, member_with_permissions: permissions)
+ create(:user)
end
it_behaves_like 'not found'
diff --git a/spec/factories/principal_factory.rb b/spec/factories/principal_factory.rb
index 0a5dfbcfbd4..66bcc016891 100644
--- a/spec/factories/principal_factory.rb
+++ b/spec/factories/principal_factory.rb
@@ -38,10 +38,17 @@ FactoryBot.define do
global_permission { nil }
end
- # necessary as we have created_on instead of created_at for which factory girl would
- # provide values automatically
- created_at { Time.now }
- updated_at { Time.now }
+ # prevent forgetting :member_in_project(s) when using :member_through_role
+ # or :member_with_permissions for a better DX
+ after(:build, :create, :stub) do |principal, evaluator|
+ next if %i[member_in_project member_in_projects].any? { |attr| evaluator.overrides?(attr) }
+
+ unusable_attribute = %i[member_through_role member_with_permissions].find { |attr| evaluator.send(attr).present? }
+ next unless unusable_attribute
+
+ raise ArgumentError, "Cannot use :#{unusable_attribute} without :member_in_project or :member_in_projects " \
+ "for #{principal.class} factory"
+ end
callback(:after_build) do |_principal, evaluator|
is_build_strategy = evaluator.instance_eval { @build_strategy.is_a? FactoryBot::Strategy::Build }