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

tests[Op#54749]: add bulk create unit specs

Kabiru Mwenja42caf31b8414
diff --git a/app/services/project_custom_field_project_mappings/bulk_create_service.rb b/app/services/project_custom_field_project_mappings/bulk_create_service.rb
index 8bc811d85ba..410082f37aa 100644
--- a/app/services/project_custom_field_project_mappings/bulk_create_service.rb
+++ b/app/services/project_custom_field_project_mappings/bulk_create_service.rb
@@ -46,6 +46,8 @@ module ProjectCustomFieldProjectMappings
service_call
end
+ private
+
def validate_permissions
if @user.allowed_in_project?(:select_project_custom_fields, projects)
ServiceResult.success
diff --git a/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rb b/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rb
new file mode 100644
index 00000000000..968458780c1
--- /dev/null
+++ b/spec/services/project_custom_field_project_mappings/bulk_create_service_spec.rb
@@ -0,0 +1,110 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require "spec_helper"
+
+RSpec.describe ProjectCustomFieldProjectMappings::BulkCreateService do
+ shared_let(:project_custom_field) { create(:project_custom_field) }
+
+ context "with admin permissions" do
+ let(:user) { create(:admin) }
+
+ context "with a single project" do
+ let(:project) { create(:project) }
+ let(:instance) { described_class.new(user:, project:, project_custom_field:) }
+
+ it "creates the mappings" do
+ expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(1)
+
+ aggregate_failures "creates the mapping for the correct project and custom field" do
+ expect(ProjectCustomFieldProjectMapping.last.project).to eq(project)
+ expect(ProjectCustomFieldProjectMapping.last.project_custom_field).to eq(project_custom_field)
+ end
+ end
+ end
+
+ context "with subprojects" do
+ let(:project) { create(:project) }
+ let!(:subproject) { create(:project, parent: project) }
+ let(:instance) { described_class.new(user:, project:, project_custom_field:, include_sub_projects: true) }
+
+ it "creates the mappings for the project and sub-projects" do
+ expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(2)
+
+ aggregate_failures "creates the mapping for the correct project and custom field" do
+ expect(ProjectCustomFieldProjectMapping.last.project).to eq(subproject)
+ expect(ProjectCustomFieldProjectMapping.last.project_custom_field).to eq(project_custom_field)
+ end
+ end
+ end
+ end
+
+ context "with non-admin but sufficient permissions" do
+ let(:user) do
+ create(:user,
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ select_project_custom_fields
+ ]
+ })
+ end
+
+ let(:project) { create(:project) }
+ let(:instance) { described_class.new(user:, project:, project_custom_field:) }
+
+ it "creates the mappings" do
+ expect { instance.call }.to change(ProjectCustomFieldProjectMapping, :count).by(1)
+
+ aggregate_failures "creates the mapping for the correct project and custom field" do
+ expect(ProjectCustomFieldProjectMapping.last.project).to eq(project)
+ expect(ProjectCustomFieldProjectMapping.last.project_custom_field).to eq(project_custom_field)
+ end
+ end
+ end
+
+ context "without sufficient permissions" do
+ let(:user) do
+ create(:user,
+ member_with_permissions: {
+ project => %w[
+ view_work_packages
+ edit_project
+ ]
+ })
+ end
+ let(:project) { create(:project) }
+ let(:instance) { described_class.new(user:, project:, project_custom_field:) }
+
+ it "does not create the mappings" do
+ expect { instance.call }.not_to change(ProjectCustomFieldProjectMapping, :count)
+ expect(instance.call).to be_failure
+ end
+ end
+end