From c7f04c7c9dce3afd015c212057a6025bd2828973 Mon Sep 17 00:00:00 2001 From: Jason Frey Date: Mon, 27 Nov 2017 11:08:06 -0500 Subject: [PATCH] Merge pull request #16506 from gtanzillo/tenant-name-unique-in-region Scope Tenant#name validation to the current region (cherry picked from commit b3beb05a366986f8e6626f7971994cca49824a6c) https://bugzilla.redhat.com/show_bug.cgi?id=1517954 --- app/models/tenant.rb | 4 +++- spec/models/tenant_spec.rb | 9 +++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/tenant.rb b/app/models/tenant.rb index 7f9771f2c31..851cb12c25c 100644 --- a/app/models/tenant.rb +++ b/app/models/tenant.rb @@ -42,7 +42,9 @@ class Tenant < ApplicationRecord validate :validate_only_one_root validates :description, :presence => true validates :name, :presence => true, :unless => :use_config_for_attributes? - validates :name, :uniqueness => {:scope => :ancestry, :message => "should be unique per parent"} + validates :name, :uniqueness => {:scope => :ancestry, + :conditions => -> { in_my_region }, + :message => "should be unique per parent"} validate :validate_default_tenant, :on => :update, :if => :default_miq_group_id_changed? scope :all_tenants, -> { where(:divisible => true) } diff --git a/spec/models/tenant_spec.rb b/spec/models/tenant_spec.rb index 65b384e9018..9a6f23d19e1 100644 --- a/spec/models/tenant_spec.rb +++ b/spec/models/tenant_spec.rb @@ -227,6 +227,15 @@ end end + context "validate multi region" do + let(:other_region_id) { ApplicationRecord.id_in_region(1, ApplicationRecord.my_region_number + 1) } + + it "allows same name as tenant in a different region" do + described_class.create(:name => "GT", :description => "GT Tenant in other region", :id => other_region_id) + expect(described_class.new(:name => "GT", :description => "GT Tenant in this region").valid?).to be_truthy + end + end + context "#ensure_can_be_destroyed" do let(:tenant) { FactoryGirl.create(:tenant) } let(:cloud_tenant) { FactoryGirl.create(:cloud_tenant) }