From 0941c987e5d25eed5726f8e93af028f734e6c662 Mon Sep 17 00:00:00 2001 From: Mooli Tayer Date: Mon, 28 Nov 2016 14:31:06 +0200 Subject: [PATCH] Force unique endpoint hostname only for same type --- app/models/ext_management_system.rb | 5 +++-- spec/models/ext_management_system_spec.rb | 7 ++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/app/models/ext_management_system.rb b/app/models/ext_management_system.rb index 986697ba5b9..780247551d6 100644 --- a/app/models/ext_management_system.rb +++ b/app/models/ext_management_system.rb @@ -71,10 +71,11 @@ def self.supported_types_and_descriptions_hash def hostname_uniqueness_valid? return unless hostname_required? return unless hostname.present? # Presence is checked elsewhere + # check uniqueness per provider type - existing_hostnames = Endpoint.where.not(:resource_id => id).pluck(:hostname).compact.map(&:downcase) + existing_hostnames = (self.class.all - [self]).map(&:hostname).compact.map(&:downcase) - errors.add(:hostname, "has already been taken") if existing_hostnames.include?(hostname.downcase) + errors.add(:hostname, N_("has to be unique per provider type")) if existing_hostnames.include?(hostname.downcase) end include NewWithTypeStiMixin diff --git a/spec/models/ext_management_system_spec.rb b/spec/models/ext_management_system_spec.rb index f0ea25e3709..37294505794 100644 --- a/spec/models/ext_management_system_spec.rb +++ b/spec/models/ext_management_system_spec.rb @@ -355,11 +355,16 @@ end.to_not raise_error end - it "not allowing duplicate hostname" do + it "not allowing duplicate hostname for same type provider" do expect do FactoryGirl.create(:ems_vmware, :hostname => @ems.hostname, :tenant => @tenant2) end.to raise_error(ActiveRecord::RecordInvalid) end + + it "allowing duplicate hostname for different type providers" do + FactoryGirl.create(:ems_microsoft, :hostname => @ems.hostname, :tenant => @tenant2) + expect(ExtManagementSystem.count).to eq(2) + end end end