-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Quota Source method and spec
- Loading branch information
Showing
2 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
51 changes: 40 additions & 11 deletions
51
...ent/automate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/quota_source.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
58 changes: 58 additions & 0 deletions
58
...utomate/ManageIQ/System/CommonMethods/QuotaMethods.class/__methods__/quota_source_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |