From 6c5610921bbc932f091d506ff4081b0900f9dfe2 Mon Sep 17 00:00:00 2001 From: William Fitzgerald Date: Thu, 7 Nov 2019 15:49:31 -0500 Subject: [PATCH] Refactor approve_request method for Infra VM Retirement Approval. This PR is based on the issue below. ManageIQ/manageiq#12038 @miq-bot add_label refactoring @miq-bot assign @tinaafitz --- .../__methods__/approve_request.rb | 41 +++++++++++++++---- .../__methods__/approve_request_spec.rb | 27 ++++++++++++ 2 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 spec/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request_spec.rb diff --git a/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request.rb b/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request.rb index 313b0bf56..c911adfb4 100644 --- a/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request.rb +++ b/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request.rb @@ -1,14 +1,37 @@ # # Description: This method is executed when the provisioning request is auto-approved # +module ManageIQ + module Automate + module Infrastructure + module VM + module Retirement + module StateMachines + module RetirementRequestApproval + class ApproveRequest + def initialize(handle = $evm) + @handle = handle + end -# Auto-Approve request -$evm.log("info", "Checking for auto_approval") -approval_type = $evm.object['approval_type'].downcase -if approval_type == 'auto' - $evm.log("info", "AUTO-APPROVING") - $evm.root["miq_request"].approve("admin", "Auto-Approved") -else - $evm.log("info", "Not Auto-Approved") - exit MIQ_ABORT + def main + # Auto-Approve request + @handle.log('info', 'Checking for auto_approval') + approval_type = @handle.object['approval_type'].try(:downcase) + if approval_type == 'auto' + @handle.log('info', 'AUTO-APPROVING') + @handle.root['miq_request'].approve('admin', 'Auto-Approved') + else + @handle.log('info', 'Not Auto-Approved') + raise 'Not Auto-Approved' + end + end + end + end + end + end + end + end + end end + +ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::RetirementRequestApproval::ApproveRequest.new.main diff --git a/spec/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request_spec.rb b/spec/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request_spec.rb new file mode 100644 index 000000000..41ca3b2f5 --- /dev/null +++ b/spec/content/automate/ManageIQ/Infrastructure/VM/Retirement/StateMachines/RetirementRequestApproval.class/__methods__/approve_request_spec.rb @@ -0,0 +1,27 @@ +require_domain_file + +describe ManageIQ::Automate::Infrastructure::VM::Retirement::StateMachines::RetirementRequestApproval::ApproveRequest do + let(:svc_request) { MiqAeMethodService::MiqAeServiceMiqProvisionRequest.find(request.id) } + let(:request) { FactoryBot.create(:miq_provision_request, :with_approval) } + let(:root_object) { Spec::Support::MiqAeMockObject.new(root_hash) } + let(:root_hash) { { 'miq_request' => svc_request } } + + let(:ae_service) do + Spec::Support::MiqAeMockService.new(root_object).tap do |service| + current_object = Spec::Support::MiqAeMockObject.new + current_object.parent = root_object + current_object['approval_type'] = 'auto' + service.object = current_object + end + end + + it 'approves request' do + expect(svc_request).to(receive(:approve).with('admin', 'Auto-Approved')) + described_class.new(ae_service).main + end + + it 'does not approve request' do + ae_service.object['approval_type'] = nil + expect { described_class.new(ae_service).main }.to raise_error('Not Auto-Approved') + end +end