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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
[#60518] validate enterprise action in backend
Eric Schubert71f99d273893
diff --git a/app/controllers/work_packages/types/subject_configuration_controller.rb b/app/controllers/work_packages/types/subject_configuration_controller.rbindex 82fda439309..240686626aa 100644--- a/app/controllers/work_packages/types/subject_configuration_controller.rb+++ b/app/controllers/work_packages/types/subject_configuration_controller.rb@@ -63,7 +63,7 @@ module WorkPackagesdef tab_path = edit_tab_type_path(id: @type.id, tab: :subject_configuration)def pattern_collection_update(form_params)- patterns = @type.patterns.to_h+ patterns = @type.patterns.to_h.symbolize_keyssubject_pattern =case form_params@@ -82,10 +82,10 @@ module WorkPackagesendif subject_pattern.nil?- patterns.delete("subject")+ patterns.delete(:subject)patternselse- patterns.merge(subject_pattern.stringify_keys)+ patterns.merge(subject_pattern)endendenddiff --git a/app/services/update_type_service.rb b/app/services/update_type_service.rbindex 48ecddad286..cfb28450dfe 100644--- a/app/services/update_type_service.rb+++ b/app/services/update_type_service.rb@@ -42,19 +42,35 @@ class UpdateTypeService < BaseTypeServicedef set_params_and_validate(params)# Set patterns includes a data validation before assigning the value to the attribute.- # A failure should return a failure.- set_patterns(params) if params[:patterns].present?- return [false, type.errors] if type.errors.any?+ # A validation failure should return a service call failure.+ if params[:patterns].present?+ validate_enterprise_action(params[:patterns])+ set_patterns(params[:patterns])+ return [false, type.errors] if type.errors.any?+ endsuperend- def set_patterns(params)+ def validate_enterprise_action(patterns)+ change_from_manual_to_generated = !type.patterns.subject&.enabled? && patterns.dig(:subject, :enabled)+ action = :work_package_subject_generation++ if change_from_manual_to_generated && !EnterpriseToken.allows_to?(action)+ type.errors.add(:patterns, :error_enterprise_only, action: action.to_s.titleize)+ end+ end++ def set_patterns(patterns)Types::Patterns::Collection- .build(patterns: params[:patterns])+ .build(patterns:).either(->(collection) { type.patterns = collection },- ->(result) { type.errors.add(:patterns, result.errors(full: true).messages.join(", ")) }+ ->(result) do+ result.errors(full: true).messages.each do |message|+ type.errors.add(:patterns, message.text)+ end+ end)endenddiff --git a/spec/controllers/work_packages/types/subject_configuration_controller_spec.rb b/spec/controllers/work_packages/types/subject_configuration_controller_spec.rbindex d6356048534..d1fcb931f94 100644--- a/spec/controllers/work_packages/types/subject_configuration_controller_spec.rb+++ b/spec/controllers/work_packages/types/subject_configuration_controller_spec.rb@@ -73,7 +73,7 @@ RSpec.describe WorkPackages::Types::SubjectConfigurationController docontext "if form data is invalid" dolet(:form_data) { { subject_configuration: "generated", pattern: nil } }- let(:expected_pattern_data) { { "subject" => { blueprint: "", enabled: true } } }+ let(:expected_pattern_data) { { subject: { blueprint: "", enabled: true } } }let(:service_result) { ServiceResult.failure }it "renders the edit template" do@@ -85,7 +85,7 @@ RSpec.describe WorkPackages::Types::SubjectConfigurationController docontext "if form data is valid" docontext "with generated subject configuration" dolet(:form_data) { { subject_configuration: "generated", pattern: "Vacation - {{assignee}}" } }- let(:expected_pattern_data) { { "subject" => { blueprint: "Vacation - {{assignee}}", enabled: true } } }+ let(:expected_pattern_data) { { subject: { blueprint: "Vacation - {{assignee}}", enabled: true } } }let(:service_result) { ServiceResult.success }it "redirects to the current tab path" do@@ -95,7 +95,7 @@ RSpec.describe WorkPackages::Types::SubjectConfigurationController docontext "with manual subject configuration, but still persisted blueprint" dolet(:form_data) { { subject_configuration: "manual", pattern: "Vacation - {{assignee}}" } }- let(:expected_pattern_data) { { "subject" => { blueprint: "Vacation - {{assignee}}", enabled: false } } }+ let(:expected_pattern_data) { { subject: { blueprint: "Vacation - {{assignee}}", enabled: false } } }let(:service_result) { ServiceResult.success }it "redirects to the current tab path" dodiff --git a/spec/services/update_type_service_spec.rb b/spec/services/update_type_service_spec.rbindex 4ca14403ba4..7b46002c164 100644--- a/spec/services/update_type_service_spec.rb+++ b/spec/services/update_type_service_spec.rb@@ -1,3 +1,5 @@+# frozen_string_literal: true+#-- copyright# OpenProject is an open source project management software.# Copyright (C) the OpenProject GmbH@@ -46,25 +48,25 @@ RSpec.describe UpdateTypeService doit "raises an exception for invalid structure" do# Example for invalid structure:result = instance.call(attribute_groups: ["foo"])- expect(result.success?).to be_falsey+ expect(result).to be_failure# Example for invalid structure:result = instance.call(attribute_groups: [[]])- expect(result.success?).to be_falsey+ expect(result).to be_failure# Example for invalid group name:result = instance.call(attribute_groups: [["", ["date"]]])- expect(result.success?).to be_falsey+ expect(result).to be_failureendit "fails for duplicate group names" doresult = instance.call(attribute_groups: [valid_group, valid_group])- expect(result.success?).to be_falsey+ expect(result).to be_failureexpect(result.errors[:attribute_groups].first).to include "used more than once."endit "passes validations for known attributes" doexpect(type).to receive(:save).and_return(true)result = instance.call(attribute_groups: [valid_group])- expect(result.success?).to be_truthy+ expect(result).to be_successendit "passes validation for defaults" do@@ -75,7 +77,7 @@ RSpec.describe UpdateTypeService do# A reset is to save an empty Arrayexpect(type).to receive(:save).and_return(true)result = instance.call(attribute_groups: [])- expect(result.success?).to be_truthy+ expect(result).to be_successexpect(type).to be_validend@@ -83,7 +85,29 @@ RSpec.describe UpdateTypeService dolet(:params) { { attribute_groups: [{ "type" => "query", name: "some name", query: "wat" }] } }it "is invalid" do- expect(service_call.success?).to be_falsey+ expect(service_call).to be_failure+ end+ end+ end++ describe "#validate_work_package_subject_generation" do+ context "if enterprise token does not allow subject generation", with_ee: %i[] do+ let(:params) { { patterns: { subject: { blueprint: "Vacation - {{assignee}}", enabled: true } } } }++ it "is invalid" do+ expect(service_call).to be_failure+ expect(service_call.errors[:patterns].first)+ .to include "is only available in the OpenProject Enterprise edition"+ end+ end++ context "if pattern build returns validation error", with_ee: %i[work_package_subject_generation] do+ let(:params) { { patterns: { subject: { blueprint: "", enabled: true } } } }++ it "is invalid" do+ expect(service_call).to be_failure+ expect(service_call.errors[:patterns].first)+ .to include "Pattern blueprint must be filled"endendend