Skip to content

Commit

Permalink
Refactor Quota Source method and spec
Browse files Browse the repository at this point in the history
  • Loading branch information
GregP committed Oct 15, 2019
1 parent 144be82 commit cf13086
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
# frozen_string_literal: true

#
# Description: Get quota source.
# Description: Get Quota Source
#
module ManageIQ
module Automate
module System
module CommonMethods
module QuotaMethods
class QuotaSource
def initialize(handle = $evm)
@handle = handle
end

def main
quota_source
end

@miq_request = $evm.root['miq_request']
$evm.root['quota_source_type'] = $evm.parent['quota_source_type'] || $evm.object['quota_source_type']
private

case $evm.root['quota_source_type'].downcase
when 'group'
$evm.root['quota_source'] = @miq_request.requester.current_group
when 'user'
$evm.root['quota_source'] = @miq_request.requester
else
$evm.root['quota_source'] = @miq_request.tenant
$evm.root['quota_source_type'] = 'tenant'
def miq_request
@handle.root['miq_request']
end

def quota_source
@handle.root['quota_source_type'] = @handle.object.parent['quota_source_type'] || @handle.object['quota_source_type']
case @handle.root['quota_source_type'].downcase
when 'group'
@handle.root['quota_source'] = miq_request.requester.current_group
when 'user'
@handle.root['quota_source'] = miq_request.requester
else
@handle.root['quota_source'] = miq_request.tenant
@handle.root['quota_source_type'] = 'tenant'
end
end
end
end
end
end
end
end

ManageIQ::Automate::System::CommonMethods::QuotaMethods::QuotaSource.new.main
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require_domain_file

describe ManageIQ::Automate::System::CommonMethods::QuotaMethods::QuotaSource do
include Spec::Support::QuotaHelper

let!(:model) { setup_model }
let(:root_hash) do
{
'miq_provision_request' => svc_miq_request,
'miq_request' => svc_miq_request,
'quota_source' => quota_source,
'quota_source_type' => quota_source_type
}
end

let(:svc_miq_request) { MiqAeMethodService::MiqAeServiceMiqRequest.find(@miq_request.id) }

let(:root_object) do
Spec::Support::MiqAeMockObject.new(root_hash)
end

let(:ae_service) do
Spec::Support::MiqAeMockService.new(root_object).tap do |service|
current_object = Spec::Support::MiqAeMockObject.new
current_object.parent = root_object
service.object = current_object
end
end

shared_examples_for "quota_source" do
it "request_info" do
described_class.new(ae_service).main

expect(ae_service.root['quota_source_type']).to eq(quota_source_type)
end
end

context "returns ok for 'group' as quota source" do
let(:quota_source) { MiqAeMethodService::MiqAeServiceMiqGroup.find(@miq_group.id) }
let(:quota_source_type) { 'group' }

it_behaves_like "quota_source"
end

context "returns ok for 'user' as quota source" do
let(:quota_source) { MiqAeMethodService::MiqAeServiceUser.find(@user.id) }
let(:quota_source_type) { 'user' }

it_behaves_like "quota_source"
end

context "returns ok for 'tenant' as quota source" do
let(:quota_source) { MiqAeMethodService::MiqAeServiceTenant.find(@tenant.id) }
let(:quota_source_type) { 'tenant' }

it_behaves_like "quota_source"
end
end

0 comments on commit cf13086

Please sign in to comment.