Skip to content

Commit

Permalink
Merge pull request #16506 from gtanzillo/tenant-name-unique-in-region
Browse files Browse the repository at this point in the history
Scope Tenant#name validation to the current region
  • Loading branch information
Fryguy authored Nov 27, 2017
2 parents 3e436cc + f2d4533 commit b3beb05
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/models/tenant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
9 changes: 9 additions & 0 deletions spec/models/tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit b3beb05

Please sign in to comment.