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
[#61692] moved validation to base contract
Eric Schuberte52974c06561
diff --git a/app/contracts/types/base_contract.rb b/app/contracts/types/base_contract.rbindex 4f189e300fd..bdf4636c47e 100644--- a/app/contracts/types/base_contract.rb+++ b/app/contracts/types/base_contract.rb@@ -47,6 +47,7 @@ module Typesvalidate :validate_current_user_is_adminvalidate :validate_attribute_group_namesvalidate :validate_attribute_groups+ validate :validate_subject_generation_patterndef validate_current_user_is_adminunless user.admin?@@ -100,5 +101,23 @@ module Typesendendend++ def validate_subject_generation_pattern # rubocop:disable Metrics/AbcSize+ blueprint = model.patterns.subject&.blueprint+ return if blueprint.nil?++ valid_tokens = flat_valid_token_list+ invalid_tokens = blueprint.scan(PatternResolver::TOKEN_REGEX)+ .reduce([]) do |acc, match|+ token = Patterns::Token.build(match).key+ valid_tokens.include?(token) ? acc : acc << token+ end++ if invalid_tokens.any?+ errors.add(:patterns, :invalid_tokens, tokens: invalid_tokens.join(", "))+ end+ end++ def flat_valid_token_list = Patterns::TokenPropertyMapper.new.tokens_for_type(model).values.map(&:keys).flattenendenddiff --git a/app/services/update_type_service.rb b/app/services/update_type_service.rbindex e7bdc0f474c..d41b417f9ac 100644--- a/app/services/update_type_service.rb+++ b/app/services/update_type_service.rb@@ -46,7 +46,6 @@ class UpdateTypeService < BaseTypeServicepatterns = params[:patterns]if patterns.present?validate_enterprise_action(patterns)- validate_pattern_content(patterns)set_patterns(patterns)return [false, type.errors] if type.errors.any?end@@ -63,25 +62,6 @@ class UpdateTypeService < BaseTypeServiceendend- def validate_pattern_content(patterns) # rubocop:disable Metrics/AbcSize- blueprint = patterns.dig(:subject, :blueprint)- return if blueprint.nil?-- valid_tokens = flat_valid_token_list- invalid_tokens = blueprint.scan(Types::PatternResolver::TOKEN_REGEX)- .reduce([]) do |acc, match|- token = Types::Patterns::Token.build(match).key- valid_tokens.include?(token) ? acc : acc << token- end-- if invalid_tokens.any?- message = I18n.t("types.edit.subject_configuration.errors.invalid_tokens", tokens: invalid_tokens.join(", "))- type.errors.add(:patterns, message)- end- end-- def flat_valid_token_list = Types::Patterns::TokenPropertyMapper.new.tokens_for_type(type).values.map(&:keys).flatten-def set_patterns(patterns)Types::Patterns::Collection.build(patterns:)diff --git a/config/locales/en.yml b/config/locales/en.ymlindex d535248bb3e..694209a1133 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -677,8 +677,6 @@ en:pattern:label: "Subject pattern"caption: "Search for an attribute or add text. To create a space between elements add it as a text."- errors:- invalid_tokens: "The pattern contains invalid tokens: %{tokens}"export_configuration:tab: "Generate PDF"intro: "Select which templates from those that are available you would like to enable for this type. The template determines the design and attributes visible in the exported PDF of a work package using this type. The first template on the list is selected by default."@@ -1519,6 +1517,8 @@ en:duplicate_group: "The group name '%{group}' is used more than once. Group names must be unique."query_invalid: "The embedded query '%{group}' is invalid: %{details}"group_without_name: "Unnamed groups are not allowed."+ patterns:+ invalid_tokens: "The pattern contains invalid tokens: %{tokens}"user:attributes:base:diff --git a/spec/contracts/types/base_contract_spec.rb b/spec/contracts/types/base_contract_spec.rbnew file mode 100644index 00000000000..710370b6aee--- /dev/null+++ b/spec/contracts/types/base_contract_spec.rb@@ -0,0 +1,58 @@+# 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 Types::BaseContract do+ let(:current_user) { build_stubbed(:user) }+ let(:work_package_type) { Type.new }++ subject(:contract) { described_class.new(work_package_type, current_user) }++ describe "#validation" do+ context "if subject generation patterns contains invalid tokens" do+ let(:valid_tokens_hash) { { work_package: { assignee: "Assignee" } } }+ let(:work_package_type) do+ Type.new(patterns: { subject: { blueprint: "Vacation {{vaders_toy}}", enabled: true } })+ end++ before do+ token_mapper_double = instance_double(Types::Patterns::TokenPropertyMapper)+ allow(token_mapper_double).to receive(:tokens_for_type).and_return(valid_tokens_hash)+ allow(Types::Patterns::TokenPropertyMapper).to receive(:new).and_return(token_mapper_double)+ end++ it "is invalid" do+ contract.validate+ expect(subject.errors.symbols_for(:patterns)).to contain_exactly(:invalid_tokens)+ end+ end+ end+enddiff --git a/spec/services/update_type_service_spec.rb b/spec/services/update_type_service_spec.rbindex 0484aabf691..7b46002c164 100644--- a/spec/services/update_type_service_spec.rb+++ b/spec/services/update_type_service_spec.rb@@ -101,22 +101,6 @@ RSpec.describe UpdateTypeService doendend- context "if pattern contains invalid tokens", with_ee: %i[work_package_subject_generation] do- let(:params) { { patterns: { subject: { blueprint: "Vacation - {{vaders_toy}}", enabled: true } } } }- let(:valid_token_hash) { { work_package: { assignee: "Assignee" } } }-- before do- token_mapper_double = instance_double(Types::Patterns::TokenPropertyMapper)- allow(token_mapper_double).to receive(:tokens_for_type).and_return(valid_token_hash)- allow(Types::Patterns::TokenPropertyMapper).to receive(:new).and_return(token_mapper_double)- end-- it "is invalid" do- expect(service_call).to be_failure- expect(service_call.errors[:patterns].first).to include "contains invalid tokens"- end- end-context "if pattern build returns validation error", with_ee: %i[work_package_subject_generation] dolet(:params) { { patterns: { subject: { blueprint: "", enabled: true } } } }