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

[#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.rb
index 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 WorkPackages
def 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_keys
subject_pattern =
case form_params
@@ -82,10 +82,10 @@ module WorkPackages
end
if subject_pattern.nil?
- patterns.delete("subject")
+ patterns.delete(:subject)
patterns
else
- patterns.merge(subject_pattern.stringify_keys)
+ patterns.merge(subject_pattern)
end
end
end
diff --git a/app/services/update_type_service.rb b/app/services/update_type_service.rb
index 48ecddad286..cfb28450dfe 100644
--- a/app/services/update_type_service.rb
+++ b/app/services/update_type_service.rb
@@ -42,19 +42,35 @@ class UpdateTypeService < BaseTypeService
def 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?
+ end
super
end
- 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
)
end
end
diff --git a/spec/controllers/work_packages/types/subject_configuration_controller_spec.rb b/spec/controllers/work_packages/types/subject_configuration_controller_spec.rb
index 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 do
context "if form data is invalid" do
let(: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 do
context "if form data is valid" do
context "with generated subject configuration" do
let(: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 do
context "with manual subject configuration, but still persisted blueprint" do
let(: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" do
diff --git a/spec/services/update_type_service_spec.rb b/spec/services/update_type_service_spec.rb
index 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 do
it "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_failure
end
it "fails for duplicate group names" do
result = instance.call(attribute_groups: [valid_group, valid_group])
- expect(result.success?).to be_falsey
+ expect(result).to be_failure
expect(result.errors[:attribute_groups].first).to include "used more than once."
end
it "passes validations for known attributes" do
expect(type).to receive(:save).and_return(true)
result = instance.call(attribute_groups: [valid_group])
- expect(result.success?).to be_truthy
+ expect(result).to be_success
end
it "passes validation for defaults" do
@@ -75,7 +77,7 @@ RSpec.describe UpdateTypeService do
# A reset is to save an empty Array
expect(type).to receive(:save).and_return(true)
result = instance.call(attribute_groups: [])
- expect(result.success?).to be_truthy
+ expect(result).to be_success
expect(type).to be_valid
end
@@ -83,7 +85,29 @@ RSpec.describe UpdateTypeService do
let(: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"
end
end
end