From 60938b1ba40d793c2367fc181fc641ea4dbc882c Mon Sep 17 00:00:00 2001 From: Harpreet Kataria Date: Thu, 23 Mar 2017 11:40:59 -0400 Subject: [PATCH] Addressed code climate error Added spec test to verify that `fetch_playook_details` sets data for tabs correctly. --- app/controllers/catalog_controller.rb | 19 +++---- spec/controllers/catalog_controller_spec.rb | 63 +++++++++++++++++++++ 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/app/controllers/catalog_controller.rb b/app/controllers/catalog_controller.rb index 7c5d83f5dc97..e42994f781c4 100644 --- a/app/controllers/catalog_controller.rb +++ b/app/controllers/catalog_controller.rb @@ -1797,11 +1797,7 @@ def fetch_playbook_details playbook_details[:provisioning][:machine_credential] = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential.find_by(:id => provision[:credential_id]).name playbook_details[:provisioning][:network_credential] = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::NetworkCredential.find_by(:id => provision[:network_credential_id]).name if provision[:network_credential_id] playbook_details[:provisioning][:cloud_credential] = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::CloudCredential.find_by(:id => provision[:cloud_credential_id]).name if provision[:cloud_credential_id] - if provision[:dialog_id] - dialog = Dialog.find_by(:id => provision[:dialog_id]) - playbook_details[:provisioning][:dialog] = dialog.name - playbook_details[:provisioning][:dialog_id] = dialog.id - end + fetch_dialog(playbook_details, provision[:dialog_id], :provisioning) if @record.config_info[:retirement] retirement = @record.config_info[:retirement] @@ -1813,16 +1809,19 @@ def fetch_playbook_details playbook_details[:retirement][:machine_credential] = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential.find_by(:id => retirement[:credential_id]).name playbook_details[:retirement][:network_credential] = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::NetworkCredential.find_by(:id => retirement[:network_credential_id]).name if retirement[:network_credential_id] playbook_details[:retirement][:cloud_credential] = ManageIQ::Providers::EmbeddedAnsible::AutomationManager::CloudCredential.find_by(:id => retirement[:cloud_credential_id]).name if retirement[:cloud_credential_id] - if retirement[:dialog_id] - dialog = Dialog.find_by(:id => retirement[:dialog_id]) - playbook_details[:retirement][:dialog] = dialog.name - playbook_details[:retirement][:dialog_id] = dialog.id - end + fetch_dialog(playbook_details, retirement[:dialog_id], :retirement) end end playbook_details end + def fetch_dialog(playbook_details, dialog_id, key) + return nil if dialog_id.nil? + dialog = Dialog.find_by(:id => dialog_id) + playbook_details[key][:dialog] = dialog.name + playbook_details[key][:dialog_id] = dialog.id + end + def open_parent_nodes(record) existing_node = nil # Init var diff --git a/spec/controllers/catalog_controller_spec.rb b/spec/controllers/catalog_controller_spec.rb index 8e6aa163dd3b..1953d7747618 100644 --- a/spec/controllers/catalog_controller_spec.rb +++ b/spec/controllers/catalog_controller_spec.rb @@ -739,4 +739,67 @@ expect(assigns(:edit)[:new][:available_resources].count).to eq(2) end end + + context "#fetch_playbook_details" do + let(:auth) { FactoryGirl.create(:authentication, :name => "machine_cred", :manager_ref => 6, :type => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::MachineCredential") } + let(:repository) { FactoryGirl.create(:configuration_script_source, :manager => ems, :type => "ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScriptSource") } + let(:inventory_root_group) { FactoryGirl.create(:inventory_root_group, :name => 'Demo Inventory') } + let(:ems) do + FactoryGirl.create(:automation_manager_ansible_tower, :inventory_root_groups => [inventory_root_group], :provider => FactoryGirl.create(:provider_embedded_ansible)) + end + let(:dialog) { FactoryGirl.create(:dialog, :label => "Some Label") } + let(:playbook) do + FactoryGirl.create(:embedded_playbook, + :configuration_script_source => repository, + :manager => ems, + :inventory_root_group => inventory_root_group) + end + + it "returns playbook service template details for provision & retirement tabs for summary screen" do + options = { + :name => 'test_ansible_catalog_item', + :description => 'test ansible', + :display => true, + :config_info => { + :provision => { + :new_dialog_name => 'test_dialog', + :hosts => 'many', + :credential_id => auth.id, + :repository_id => repository.id, + :playbook_id => playbook.id, + :dialog_id => dialog.id + }, + :retirement => { + :new_dialog_name => 'test_dialog', + :hosts => 'many', + :credential_id => auth.id, + :repository_id => repository.id, + :playbook_id => playbook.id, + :dialog_id => dialog.id + } + } + } + service_template = double("ServiceTemplateAnsiblePlaybook", options) + controller.instance_variable_set(:@record, service_template) + playbook_details = controller.send(:fetch_playbook_details) + st_details = { + :provisioning => { + :repository => repository.name, + :playbook => playbook.name, + :machine_credential => auth.name, + :dialog => "Some Label", + :dialog_id => dialog.id + }, + :retirement => { + :remove_resources => nil, + :repository => repository.name, + :playbook => playbook.name, + :machine_credential => auth.name, + :dialog => "Some Label", + :dialog_id => dialog.id + } + } + expect(playbook_details).to eq(st_details) + end + end end