From fb695d03d79a00b74e4b0ff6d793d13161bbde71 Mon Sep 17 00:00:00 2001 From: Adam Grare Date: Mon, 9 Jul 2018 08:42:57 -0400 Subject: [PATCH] Validate name uniqueness for Transformation Plans Add a unique name validation, scoped to the tenant, for ServiceTemplateTransformationPlans. https://github.com/ManageIQ/miq_v2v_ui_plugin/issues/451 --- 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 1f8b630a5ca..1f9661f49b7 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 0006e014cd9..19e51fa3ff6 100644 --- a/spec/factories/service_template.rb +++ b/spec/factories/service_template.rb @@ -3,7 +3,9 @@ factory :service_template_orchestration, :class => 'ServiceTemplateOrchestration', :parent => :service_template 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 3ad62c39640..e75eabf564f 100644 --- a/spec/models/service_template_transformation_plan_spec.rb +++ b/spec/models/service_template_transformation_plan_spec.rb @@ -61,5 +61,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