From 80edeaa410ec566b4b0d13bac42999c363833e1c Mon Sep 17 00:00:00 2001 From: Greg McCullough Date: Mon, 9 Jul 2018 16:09:31 -0400 Subject: [PATCH] Merge pull request #17677 from agrare/validate_name_is_unique_transformation_plan Validate name uniqueness for Transformation Plans (cherry picked from commit 6586413443d504a0a2ae5fa4a30072e0145d57b3) https://bugzilla.redhat.com/show_bug.cgi?id=1608768 --- app/models/service_template_transformation_plan.rb | 2 ++ spec/factories/service_template.rb | 4 +++- spec/models/service_template_transformation_plan_spec.rb | 7 +++++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/service_template_transformation_plan.rb b/app/models/service_template_transformation_plan.rb index fa2deb70584..84505f6aef6 100644 --- a/app/models/service_template_transformation_plan.rb +++ b/app/models/service_template_transformation_plan.rb @@ -32,6 +32,8 @@ def self.default_reconfiguration_entry_point nil end + validates :name, :presence => true, :uniqueness => {:scope => [:tenant_id]} + # create ServiceTemplate and supporting ServiceResources and ResourceActions # options # :name diff --git a/spec/factories/service_template.rb b/spec/factories/service_template.rb index 2594da7beec..0ad5e020aa6 100644 --- a/spec/factories/service_template.rb +++ b/spec/factories/service_template.rb @@ -8,7 +8,9 @@ factory :service_template_ansible_playbook, :class => 'ServiceTemplateAnsiblePlaybook', :parent => :service_template factory :service_template_container_template, :class => 'ServiceTemplateContainerTemplate', :parent => :service_template - factory :service_template_transformation_plan, :class => 'ServiceTemplateTransformationPlan', :parent => :service_template + factory :service_template_transformation_plan, :class => 'ServiceTemplateTransformationPlan', :parent => :service_template do + sequence(:name) { |n| "service_template_#{seq_padded_for_sorting(n)}" } + end trait :with_provision_resource_action_and_dialog do after(:create) do |x| diff --git a/spec/models/service_template_transformation_plan_spec.rb b/spec/models/service_template_transformation_plan_spec.rb index a072f9334f4..71ed306b426 100644 --- a/spec/models/service_template_transformation_plan_spec.rb +++ b/spec/models/service_template_transformation_plan_spec.rb @@ -71,5 +71,12 @@ described_class.create_catalog_item(catalog_item_options) end.to raise_error(StandardError, 'Must select a list of valid vms') end + + it 'validates unique name' do + described_class.create_catalog_item(catalog_item_options) + expect { described_class.create_catalog_item(catalog_item_options) }.to raise_error( + ActiveRecord::RecordInvalid, 'Validation failed: Name has already been taken' + ) + end end end