From 04b4de4ec2e1365b916f1d07f55b16814401de20 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Tue, 27 Feb 2024 15:44:25 -0500 Subject: [PATCH] Remove tags from entitlement filters from the deleted tag's region The before_destroy callback wasn't previously scoped to the tag's region. From the global region, usually 99, you could delete a tag from region 1 with value "/managed/costcenter/004". We would then remove that value from filters with that tag from entitlements in region 99, even though we deleted a region 1 tag. The same named tag could still exist in region 99. This commit ensures that if you delete a region 99 tag, we'll only update filters in entitlements in region 99. --- app/models/tag.rb | 2 +- spec/models/tag_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/models/tag.rb b/app/models/tag.rb index ef8e6eb2adf..b6f2f476b54 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -160,7 +160,7 @@ def self.controlled_by_mapping private def remove_from_managed_filters - Entitlement.remove_tag_from_all_managed_filters(name) + Entitlement.with_region(self.class.id_to_region(id)) { Entitlement.remove_tag_from_all_managed_filters(name) } end def name_path diff --git a/spec/models/tag_spec.rb b/spec/models/tag_spec.rb index 3698501f306..79ed95b33ea 100644 --- a/spec/models/tag_spec.rb +++ b/spec/models/tag_spec.rb @@ -168,5 +168,24 @@ MiqGroup.all.each { |group| expect(group.get_managed_filters).to match_array(expected_filters) } expect(Tag.all).to be_empty end + + it "removing a tag will only remove it from entitlement filters in the tag's region" do + tag + other_region = ApplicationRecord.my_region_number + 1 + other_region_tag = FactoryBot.create(:tag, :name => "/managed/my_name/test", :id => ApplicationRecord.id_in_region(1, other_region)) + other_region_miq_group = FactoryBot.create(:miq_group, :entitlement => Entitlement.create!, :id => ApplicationRecord.id_in_region(1, other_region)) + other_region_miq_group.entitlement.set_managed_filters(filters) + other_region_miq_group.save + + other_region_tag.destroy + + # tag and filters from other region are deleted and updated + Tag.in_region(other_region) { expect(Tag.all).to be_empty } + MiqGroup.in_region(other_region) { MiqGroup.all.each { |group| expect(group.get_managed_filters).to be_empty } } + + # current region tag/filters are not changed + expect(Tag.pluck(:id)).to eq([tag.id]) + MiqGroup.all.each { |group| expect(group.get_managed_filters).to match_array(filters) } + end end end