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
[#64615] add params validation for work package type updates
Eric Schuberta7e0dd3574b9
diff --git a/app/contracts/work_package_types/copy_workflow_attribute_contract.rb b/app/contracts/work_package_types/copy_workflow_attribute_contract.rbnew file mode 100644index 00000000000..515d7e1fa04--- /dev/null+++ b/app/contracts/work_package_types/copy_workflow_attribute_contract.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++module WorkPackageTypes+ class CopyWorkflowAttributeContract < Dry::Validation::Contract+ config.messages.backend = :i18n++ params do+ optional(:copy_workflow_from).filled(:string)+ end++ rule(:copy_workflow_from) do+ next unless key?++ type = Type.find_by(id: value)+ if type.nil?+ key.failure(:not_found)+ next+ end++ key.failure(:workflow_missing) if type.workflows.empty?+ end+ end+enddiff --git a/app/contracts/work_package_types/update_projects_contract.rb b/app/contracts/work_package_types/update_projects_contract.rbnew file mode 100644index 00000000000..8af223f7ecb--- /dev/null+++ b/app/contracts/work_package_types/update_projects_contract.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++module WorkPackageTypes+ class UpdateProjectsContract < BaseContract+ attribute :projects+ end+enddiff --git a/app/forms/work_packages/types/settings_form.rb b/app/forms/work_packages/types/settings_form.rbindex c7ab04ee73f..a6ab9bba160 100644--- a/app/forms/work_packages/types/settings_form.rb+++ b/app/forms/work_packages/types/settings_form.rb@@ -52,7 +52,8 @@ module WorkPackagesname: :copy_workflow_from,input_width: :medium,label: I18n.t(:label_copy_workflow_from),- include_blank: true+ include_blank: true,+ validation_message: validation_message_for(:copy_workflow_from)) do |other_types|work_package_types.each do |type|other_types.option(@@ -106,6 +107,10 @@ module WorkPackagesType.allend+ def validation_message_for(attribute)+ model.errors.messages_for(attribute).to_sentence.presence+ end+def prefilled_copy_workflow_from@builder.options[:copy_workflow_from]enddiff --git a/app/services/work_package_types/set_attributes_service.rb b/app/services/work_package_types/set_attributes_service.rbindex b8fc3e8077a..af0265ffb0f 100644--- a/app/services/work_package_types/set_attributes_service.rb+++ b/app/services/work_package_types/set_attributes_service.rb@@ -30,42 +30,89 @@module WorkPackageTypesclass SetAttributesService < ::BaseServices::SetAttributes+ include Dry::Monads[:result]+def initialize(user:, model:, contract_class:, contract_options: nil)super- @valid_pattern = true+ @param_validations = {}endprivatedef set_attributes(params)permitted = params.except(:copy_workflow_from)- @valid_pattern = check_patterns(permitted)- if @valid_pattern- super(permitted)- else- super(permitted.except(:patterns))- end+ check_patterns(permitted).or { |failures| @param_validations.update(failures) }+ check_copy_workflow(params).or { |failure| @param_validations.update(failure) }+ check_projects(permitted).or { |failure| @param_validations.update(failure) }++ super(permitted.except(*@param_validations.keys))enddef validate_and_resultsuccess, errors = validate(model, user, options: {})- if @valid_pattern+ if @param_validations.empty?ServiceResult.new(success:, errors:, result: model)else- errors.add(:patterns, :is_invalid)+ @param_validations.each_pair do |key, error|+ errors.add(key, error)+ end+ServiceResult.failure(errors:, result: model)endenddef check_patterns(params)- return true unless params.key?(:patterns)- return true if params.key?(:patterns) && params[:patterns].blank?+ return Success() unless params.key?(:patterns)+ return Success() if params.key?(:patterns) && params[:patterns].blank?- Types::Patterns::CollectionContract.new.call(params[:patterns]).success?+ Types::Patterns::CollectionContract+ .new+ .call(params[:patterns])+ .to_monad+ .or { |result| Failure({ patterns: validation_failure_to_message(result).join(", ") }) }rescue ArgumentError- false+ Failure({ patterns: :is_invalid })+ end++ def check_copy_workflow(params)+ return Success() unless params.key?(:copy_workflow_from)++ CopyWorkflowAttributeContract+ .new+ .call(params.slice(:copy_workflow_from))+ .to_monad+ .or { |result| Failure({ copy_workflow_from: validation_failure_to_message(result).join(", ") }) }+ end++ def check_projects(params)+ return Success() unless params.key?(:project_ids)++ invalid_project_ids = params[:project_ids].reject { |id| Project.exists?(id) }++ if invalid_project_ids.empty?+ Success()+ else+ Failure({ project_ids: "Projects with ids #{invalid_project_ids.join(', ')} do not exist." })+ end+ end++ def validation_failure_to_message(result)+ flatten_error_messages result.errors(full: true).to_h+ end++ def flatten_error_messages(errors)+ case errors+ when String+ [errors]+ when Array+ errors.flat_map { |e| flatten_error_messages(e) }+ when Hash+ errors.values.flat_map { |e| flatten_error_messages(e) }+ else+ []+ endendendenddiff --git a/config/locales/en.yml b/config/locales/en.ymlindex b4af5f34bc8..4b7eb34f944 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -318,6 +318,8 @@ en:greater_or_equal_zero: "must be greater or equal to 0."not_found: "not found."rules:+ copy_workflow_from:+ workflow_missing: "has no own workflow."item:root_item: "cannot be a root item."not_persisted: "must be an already existing item."@@ -328,6 +330,8 @@ en:parent:not_descendant: "must be a descendant of the hierarchy root."rules:+ copy_workflow_from: "Type for workflow copy"+ enabled: "Enabled"depth: "Depth"item: "Item"label: "Label"diff --git a/spec/services/work_package_types/copy_workflow_attribute_contract_spec.rb b/spec/services/work_package_types/copy_workflow_attribute_contract_spec.rbnew file mode 100644index 00000000000..6e7d2934fce--- /dev/null+++ b/spec/services/work_package_types/copy_workflow_attribute_contract_spec.rb@@ -0,0 +1,65 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackageTypes::CopyWorkflowAttributeContract do+ subject { described_class.new }++ context "if the given work package type id exists and has a workflow" do+ let(:wp_type) { create(:type_with_workflow) }+ let(:params) { { copy_workflow_from: wp_type.id.to_s } }++ it "is valid" do+ expect(subject.call(params)).to be_success+ end+ end++ context "if the given work package type id does not exists" do+ let(:params) { { copy_workflow_from: "NO" } }++ it "is invalid" do+ result = subject.call(params)+ expect(result).to be_failure+ expect(result.errors(full: true)[:copy_workflow_from]).to match_array("Type for workflow copy not found.")+ end+ end++ context "if the given work package type id does not have a workflow" do+ let(:wp_type) { create(:type) }+ let(:params) { { copy_workflow_from: wp_type.id.to_s } }++ it "is invalid" do+ result = subject.call(params)+ expect(result).to be_failure+ expect(result.errors(full: true)[:copy_workflow_from]).to match_array("Type for workflow copy has no own workflow.")+ end+ end+enddiff --git a/spec/services/work_package_types/set_attributes_service_spec.rb b/spec/services/work_package_types/set_attributes_service_spec.rbindex 3335b9cdf79..08c535061df 100644--- a/spec/services/work_package_types/set_attributes_service_spec.rb+++ b/spec/services/work_package_types/set_attributes_service_spec.rb@@ -68,7 +68,7 @@ module WorkPackageTypesit "adds an error on the patterns attribute" doresult = service.perform(params)- expect(result.errors.details).to eq(patterns: [{ error: :is_invalid }])+ expect(result.errors.details).to eq(patterns: [{ error: "Enabled is missing" }])endit "does not override the already existing value on the model" do@@ -89,5 +89,64 @@ module WorkPackageTypesexpect(model.patterns).to eq(Types::Patterns::Collection.empty)endend++ context "if copy workflow source type does not exist" do+ let(:params) { { copy_workflow_from: "1337" } }++ it "fails" do+ result = service.perform(params)+ expect(result).to be_failure+ end++ it "adds an error on the copy_workflow_from attribute" do+ result = service.perform(params)+ expect(result.errors.details).to eq(copy_workflow_from: [{ error: "Type for workflow copy not found." }])+ end++ it "does not override the already existing value on the model" do+ service.perform(params)+ expect(model).not_to be_changed+ end+ end++ context "if copy workflow source type does not have a workflow" do+ let(:wp_type) { create(:type_bug) }+ let(:params) { { copy_workflow_from: wp_type.id.to_s } }++ it "fails" do+ result = service.perform(params)+ expect(result).to be_failure+ end++ it "adds an error on the copy_workflow_from attribute" do+ result = service.perform(params)+ expect(result.errors.details).to eq(copy_workflow_from: [{ error: "Type for workflow copy has no own workflow." }])+ end++ it "does not override the already existing value on the model" do+ service.perform(params)+ expect(model).not_to be_changed+ end+ end++ context "if project ids contain a not existent id" do+ let(:project) { create(:project) }+ let(:params) { { project_ids: [project.id.to_s, "1337"] } }++ it "fails" do+ result = service.perform(params)+ expect(result).to be_failure+ end++ it "adds an error on the project_ids attribute" do+ result = service.perform(params)+ expect(result.errors.details).to eq(project_ids: [{ error: "Projects with ids 1337 do not exist." }])+ end++ it "does not override the already existing value on the model" do+ service.perform(params)+ expect(model).not_to be_changed+ end+ endendend