From c05d12cbd9e7f5f65c66feb90958109b3a1d27e0 Mon Sep 17 00:00:00 2001 From: d-m-u Date: Wed, 18 Sep 2019 09:31:45 -0400 Subject: [PATCH] Add check for nil group to system_context_requester --- app/models/mixins/retirement_mixin.rb | 4 ++++ spec/models/vm/retirement_management_spec.rb | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/app/models/mixins/retirement_mixin.rb b/app/models/mixins/retirement_mixin.rb index 9e847c78c7ed..39a9ab3d70be 100644 --- a/app/models/mixins/retirement_mixin.rb +++ b/app/models/mixins/retirement_mixin.rb @@ -257,6 +257,10 @@ def system_context_requester $log.info("System context defaulting to admin user because owner of #{name} (#{self.class}) not set or owner no longer found in database.") return User.super_admin end + if evm_owner.current_group.nil? + $log.info("#{name} has evm_owner but the user has no group.") + return User.super_admin + end $log.info("Setting retirement requester of #{name} to #{evm_owner_id}.") evm_owner end diff --git a/spec/models/vm/retirement_management_spec.rb b/spec/models/vm/retirement_management_spec.rb index dd83db5684a4..311a76ad9a3f 100644 --- a/spec/models/vm/retirement_management_spec.rb +++ b/spec/models/vm/retirement_management_spec.rb @@ -34,6 +34,26 @@ end end + + context "with user lacking group" do + let(:user1) { FactoryBot.create(:user) } + let(:vm_with_owner_no_group) { FactoryBot.create(:vm, :evm_owner => user1, :host => FactoryBot.create(:host)) } + + it "uses user as requester" do + vm_with_owner_no_group.update(:retires_on => 90.days.ago, :retirement_warn => 60, :retirement_last_warn => nil) + + expect(vm_with_owner.retirement_last_warn).to be_nil + allow(MiqAeEngine).to receive_messages(:deliver => ['ok', 'success', MiqAeEngine::MiqAeWorkspaceRuntime.new]) + vm_with_owner_no_group.retirement_check + status, message, result = MiqQueue.first.deliver + MiqQueue.first.delivered(status, message, result) + + expect(vm_with_owner_no_group.retirement_last_warn).not_to be_nil + # the next test is only nil because we're not creating a true super admin in these specs + expect(vm_with_owner_no_group.retirement_requester).to eq(nil) + end + end + context "without user" do before do user.destroy