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] add validation for invalid tokens
Eric Schubert45318613b063
diff --git a/app/models/types/pattern_resolver.rb b/app/models/types/pattern_resolver.rbindex 905b5484b4f..2d7524cb68f 100644--- a/app/models/types/pattern_resolver.rb+++ b/app/models/types/pattern_resolver.rb@@ -31,7 +31,6 @@module Typesclass PatternResolverTOKEN_REGEX = /{{[0-9A-Za-z_]+}}/- private_constant :TOKEN_REGEXdef initialize(pattern)@mapper = Patterns::TokenPropertyMapper.newdiff --git a/app/services/update_type_service.rb b/app/services/update_type_service.rbindex cfb28450dfe..e7bdc0f474c 100644--- a/app/services/update_type_service.rb+++ b/app/services/update_type_service.rb@@ -43,9 +43,11 @@ class UpdateTypeService < BaseTypeServicedef set_params_and_validate(params)# Set patterns includes a data validation before assigning the value to the attribute.# A validation failure should return a service call failure.- if params[:patterns].present?- validate_enterprise_action(params[:patterns])- set_patterns(params[:patterns])+ patterns = 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@@ -61,6 +63,25 @@ 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 c57c1d51c71..d535248bb3e 100644--- a/config/locales/en.yml+++ b/config/locales/en.yml@@ -677,6 +677,8 @@ 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."diff --git a/spec/services/update_type_service_spec.rb b/spec/services/update_type_service_spec.rbindex 7b46002c164..0484aabf691 100644--- a/spec/services/update_type_service_spec.rb+++ b/spec/services/update_type_service_spec.rb@@ -101,6 +101,22 @@ 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 } } } }