Skip to content

Commit

Permalink
Validate service action.
Browse files Browse the repository at this point in the history
  • Loading branch information
tinaafitz committed Feb 14, 2017
1 parent 9132b11 commit c442b44
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def main
dump_root
options = {}
# user can insert options to override options from dialog
service.preprocess(@handle.root["service_action"], options)
service.preprocess(service_action, options)
@handle.root['ae_result'] = 'ok'
@handle.log(:info, "Ending preprocess")
rescue => err
Expand Down Expand Up @@ -55,6 +55,15 @@ def service
end
end
end

def service_action
@handle.root["service_action"].tap do |action|
unless %w(Provision Retirement Reconfigure).include?(action)
@handle.log(:error, "Invalid service action: #{action}")
raise "Invalid service_action"
end
end
end
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
let(:task) { FactoryGirl.create(:service_template_provision_task, :destination => service_ansible_tower, :miq_request => request) }
let(:svc_task) { MiqAeMethodService::MiqAeServiceServiceTemplateProvisionTask.find(task.id) }
let(:svc_service) { MiqAeMethodService::MiqAeServiceServiceAnsibleTower.find(service_ansible_tower.id) }
let(:root_object) { Spec::Support::MiqAeMockObject.new('service_template_provision_task' => svc_task) }
let(:root_object) { Spec::Support::MiqAeMockObject.new('service_template_provision_task' => svc_task, 'service_action' => 'Provision') }
let(:ae_service) { Spec::Support::MiqAeMockService.new(root_object) }
let(:errormsg) { "error" }
let(:status_and_message) { [true, ""] }

context "PreProcess" do
it "failed scenario" do
Expand All @@ -25,14 +24,42 @@
expect(ae_service.root['ae_reason']).to eq(errormsg)
expect(svc_task.miq_request[:options][:user_message]).to eq(errormsg)
end
end

it "successful scenario" do
shared_examples_for "preprocess_error" do
it "preprocess_error" do
allow(svc_task).to receive(:destination).and_return(svc_service)
allow(svc_service).to receive(:preprocess).and_return(nil)
root_object['service_action'] = service_action

described_class.new(ae_service).main

expect(ae_service.root['ae_result']).to eq("ok")
expect(ae_service.root['ae_result']).to eq(ae_result)
expect(ae_service.root['ae_reason']).to eq(errormsg)
expect(svc_task.miq_request[:options][:user_message]).to eq(errormsg)
end
end

shared_examples_for "preprocess" do
it "preprocess" do
allow(svc_task).to receive(:destination).and_return(svc_service)
allow(svc_service).to receive(:preprocess).and_return(nil)

described_class.new(ae_service).main

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

context "successful scenario" do
let(:ae_result) { 'ok' }
it_behaves_like "preprocess"
end

context "invalid service_action failure" do
let(:service_action) { 'fred' }
let(:ae_result) { 'error' }
let(:errormsg) { 'Invalid service_action' }
it_behaves_like "preprocess_error"
end
end

0 comments on commit c442b44

Please sign in to comment.