Skip to content

Commit b7f1723

Browse files
committed
Don't include deleted groups in widget generation
If a group was previously subscribed to the widget, we shouldn't generate content for it. We should properly update widgets when groups are removed but for now, this allows the content generator to work with widgets having orphaned group references. Extracted from #23404 CP4AIOPS-15687
1 parent 271cce1 commit b7f1723

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

app/models/miq_widget.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,13 @@ def last_run_on_for_user(user)
397397

398398
def grouped_users_by_id
399399
id_groups = Hash.new { |h, k| h[k] = [] }
400+
group_ids = MiqGroup.in_my_region.pluck(:id)
400401
memberof.compact.each_with_object(id_groups) do |ws, h|
401-
h[ws.group_id] << ws.userid unless ws.userid.blank? || ws.group_id.blank?
402+
next if ws.userid.blank?
403+
next if ws.group_id.blank?
404+
next unless group_ids.include?(ws.group_id)
405+
406+
h[ws.group_id] << ws.userid
402407
end
403408
end
404409

spec/models/miq_widget_spec.rb

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,23 @@
177177
expect(result[@group1]).to match_array([@user1])
178178
end
179179

180+
it 'ignores a group that no longer exists' do
181+
@group1.delete
182+
result = @widget_report_vendor_and_guest_os.reload.grouped_subscribers
183+
expect(result.size).to eq(0)
184+
end
185+
180186
it 'ignores the group that has no members' do
181187
@user1.delete
182188
result = @widget_report_vendor_and_guest_os.grouped_subscribers
183189
expect(result.size).to eq(0)
184190
end
185191

186192
it 'only returns groups in the current region' do
187-
groups = MiqGroup.where(:id => [@group1, @group2])
188-
expect(MiqGroup).to receive(:in_my_region).and_return(groups)
189-
allow(groups).to receive(:where).and_return(groups)
193+
other_region = FactoryBot.create(:miq_region)
194+
other_region_id = ApplicationRecord.id_in_region(MiqGroup.count, other_region.region)
195+
FactoryBot.create(:miq_group, :id => other_region_id)
196+
allow(MiqGroup.in_my_region.all).to receive(:where).and_return(MiqGroup.in_my_region.all)
190197
@widget_report_vendor_and_guest_os.grouped_subscribers
191198
end
192199
end
@@ -300,8 +307,8 @@ def add_dashboard_for_user(db_name, userid, group)
300307
MiqReport.seed_report("Top CPU Consumers weekly")
301308

302309
role1 = FactoryBot.create(:miq_user_role, :name => 'EvmRole-support')
303-
group1 = FactoryBot.create(:miq_group, :description => "EvmGroup-support", :miq_user_role => role1)
304-
user1 = FactoryBot.create(:user, :miq_groups => [group1])
310+
@group1 = FactoryBot.create(:miq_group, :description => "EvmGroup-support", :miq_user_role => role1)
311+
user1 = FactoryBot.create(:user, :miq_groups => [@group1])
305312

306313
@user2 = FactoryBot.create(:user_admin)
307314
@group2 = @user2.current_group
@@ -332,7 +339,7 @@ def add_dashboard_for_user(db_name, userid, group)
332339
read_only: true
333340
')
334341

335-
ws1 = FactoryBot.create(:miq_widget_set, :name => "default", :userid => user1.userid, :owner => group1)
342+
ws1 = FactoryBot.create(:miq_widget_set, :name => "default", :userid => user1.userid, :owner => @group1)
336343
ws2 = FactoryBot.create(:miq_widget_set, :name => "default", :userid => @user2.userid, :owner => @group2)
337344

338345
@widget = MiqWidget.sync_from_hash(attrs)
@@ -407,6 +414,17 @@ def add_dashboard_for_user(db_name, userid, group)
407414
expect(task_id).to be_nil
408415
end
409416

417+
it "does not generate content for a deleted group" do
418+
@widget.visibility[:roles] = "_ALL_"
419+
@group2.delete
420+
421+
expect(@widget).to receive(:queue_generate_content_for_users_or_group).with("MiqGroup", @group1.description, any_args).once
422+
task_id = @widget.queue_generate_content
423+
424+
expect(MiqTask.count).to eq(1)
425+
expect(task_id).to eq(MiqTask.first.id)
426+
end
427+
410428
it "does not generate content if content_type of widget is 'menu'" do
411429
@widget.update(:content_type => "menu")
412430
expect(@widget).not_to receive(:queue_generate_content_for_users_or_group)

0 commit comments

Comments
 (0)