diff --git a/app/models/resource_action_workflow.rb b/app/models/resource_action_workflow.rb index f385fdc4c10..e56b9e7ff6e 100644 --- a/app/models/resource_action_workflow.rb +++ b/app/models/resource_action_workflow.rb @@ -21,7 +21,8 @@ def initialize(values, requester, resource_action, options = {}) Vmdb::Deprecation.deprecate_methods(self, :dialogs => :dialog) - def submit_request + def submit_request(data = {}) + update_dialog_field_values(data) if data.present? process_request(ServiceOrder::STATE_ORDERED) end @@ -29,6 +30,10 @@ def add_request_to_cart process_request(ServiceOrder::STATE_CART) end + def update_dialog_field_values(data) + @dialog.load_values_into_fields(data.values.first) + end + def process_request(state) result = {:errors => validate_dialog} return result unless result[:errors].blank? diff --git a/spec/models/resource_action_workflow_spec.rb b/spec/models/resource_action_workflow_spec.rb index 7bbc3f298b9..45ca321869b 100644 --- a/spec/models/resource_action_workflow_spec.rb +++ b/spec/models/resource_action_workflow_spec.rb @@ -39,6 +39,7 @@ end context "with workflow" do + let(:data) { {"parameters" => {"field_1" => "new_value"}} } before do @wf = ResourceActionWorkflow.new({}, admin, @resource_action) end @@ -51,72 +52,154 @@ it "#validate" do expect { @wf.validate(nil) }.to_not raise_error end + + it "#update_dialog_field_values" do + @wf.update_dialog_field_values(data) + + expect(@dialog.dialog_fields.first.value).to eq("new_value") + end end context "#submit_request" do subject { ResourceActionWorkflow.new({}, admin, resource_action, :target => target) } let(:resource_action) { @resource_action } + context "with blank data" do + let(:data) { {} } + + context "with request class" do + let(:target) { FactoryGirl.create(:service) } + + it "creates requests" do + EvmSpecHelper.local_miq_server + expect(subject).to receive(:make_request).and_call_original + expect(AuditEvent).to receive(:success).with( + :event => "service_reconfigure_request_created", + :target_class => "Service", + :userid => admin.userid, + :message => "Service Reconfigure requested by <#{admin.userid}> for Service:[#{target.id}]" + ) + expect(subject.dialog.dialog_fields.first.value).to eq(nil) + response = subject.submit_request(data) + expect(response).to include(:errors => []) + end + end - context "with request class" do - let(:target) { FactoryGirl.create(:service) } - - it "creates requests" do - EvmSpecHelper.local_miq_server - expect(subject).to receive(:make_request).and_call_original - expect(AuditEvent).to receive(:success).with( - :event => "service_reconfigure_request_created", - :target_class => "Service", - :userid => admin.userid, - :message => "Service Reconfigure requested by <#{admin.userid}> for Service:[#{target.id}]" - ) - response = subject.submit_request - expect(response).to include(:errors => []) + context "without request class" do + subject { ResourceActionWorkflow.new({}, admin, resource_action, :target => target) } + let(:resource_action) { @resource_action } + + let(:target) { FactoryGirl.create(:vm_vmware) } + + it "calls automate" do + EvmSpecHelper.local_miq_server + expect(subject).not_to receive(:make_request) + expect_any_instance_of(ResourceAction).to receive(:deliver_to_automate_from_dialog).and_call_original + expect(MiqAeEngine).to receive(:deliver_queue) # calls into automate + expect(AuditEvent).not_to receive(:success) + expect(subject.dialog.dialog_fields.first.value).to eq(nil) + response = subject.submit_request(data) + expect(response).to eq(:errors => []) + end end - end - context "without request class" do - subject { ResourceActionWorkflow.new({}, admin, resource_action, :target => target) } - let(:resource_action) { @resource_action } + context "with custom button request" do + let(:target) { FactoryGirl.build(:service) } + let(:options) { {} } + let(:resource_action) do + @resource_action.tap do |ra| + ra.update_attributes(:resource => FactoryGirl.create(:custom_button, :applies_to_class => target.class.name, :options => options)) + end + end + + it "calls automate" do + expect(subject).not_to receive(:make_request) + expect_any_instance_of(ResourceAction).to receive(:deliver_to_automate_from_dialog) + + subject.submit_request(data) + expect(subject.dialog.dialog_fields.first.value).to eq(nil) + end - let(:target) { FactoryGirl.create(:vm_vmware) } + it "calls automate with miq_task" do + options[:open_url] = true + allow(resource_action).to(receive(:deliver_to_automate_from_dialog)) + allow(subject).to(receive(:load_resource_action)).and_return(resource_action) - it "calls automate" do - EvmSpecHelper.local_miq_server - expect(subject).not_to receive(:make_request) - expect_any_instance_of(ResourceAction).to receive(:deliver_to_automate_from_dialog).and_call_original - expect(MiqAeEngine).to receive(:deliver_queue) # calls into automate - expect(AuditEvent).not_to receive(:success) - response = subject.submit_request - expect(response).to eq(:errors => []) + result = subject.submit_request + miq_task = MiqTask.find(result[:task_id]) + expect(miq_task.state).to(eq(MiqTask::STATE_QUEUED)) + expect(miq_task.status).to(eq(MiqTask::STATUS_OK)) + expect(miq_task.message).to(eq('MiqTask has been queued.')) + end end end - context "with custom button request" do - let(:target) { FactoryGirl.build(:service) } - let(:options) { {} } - let(:resource_action) do - @resource_action.tap do |ra| - ra.update_attributes(:resource => FactoryGirl.create(:custom_button, :applies_to_class => target.class.name, :options => options)) + context "with non-blank data" do + let(:data) { {"parameters" => {"field_1" => "new_value"}} } + + context "with request class" do + let(:target) { FactoryGirl.create(:service) } + + it "creates requests" do + EvmSpecHelper.local_miq_server + expect(subject).to receive(:make_request).and_call_original + expect(AuditEvent).to receive(:success).with( + :event => "service_reconfigure_request_created", + :target_class => "Service", + :userid => admin.userid, + :message => "Service Reconfigure requested by <#{admin.userid}> for Service:[#{target.id}]" + ) + response = subject.submit_request(data) + expect(subject.dialog.dialog_fields.first.value).to eq("new_value") + expect(response).to include(:errors => []) end end - it "calls automate" do - expect(subject).not_to receive(:make_request) - expect_any_instance_of(ResourceAction).to receive(:deliver_to_automate_from_dialog) - - subject.submit_request + context "without request class" do + subject { ResourceActionWorkflow.new({}, admin, resource_action, :target => target) } + let(:resource_action) { @resource_action } + + let(:target) { FactoryGirl.create(:vm_vmware) } + + it "calls automate" do + EvmSpecHelper.local_miq_server + expect(subject).not_to receive(:make_request) + expect_any_instance_of(ResourceAction).to receive(:deliver_to_automate_from_dialog).and_call_original + expect(MiqAeEngine).to receive(:deliver_queue) # calls into automate + expect(AuditEvent).not_to receive(:success) + response = subject.submit_request(data) + expect(subject.dialog.dialog_fields.first.value).to eq("new_value") + expect(response).to eq(:errors => []) + end end - it "calls automate with miq_task" do - options[:open_url] = true - allow(resource_action).to(receive(:deliver_to_automate_from_dialog)) - allow(subject).to(receive(:load_resource_action)).and_return(resource_action) + context "with custom button request" do + let(:target) { FactoryGirl.build(:service) } + let(:options) { {} } + let(:resource_action) do + @resource_action.tap do |ra| + ra.update_attributes(:resource => FactoryGirl.create(:custom_button, :applies_to_class => target.class.name, :options => options)) + end + end + + it "calls automate" do + expect(subject).not_to receive(:make_request) + expect_any_instance_of(ResourceAction).to receive(:deliver_to_automate_from_dialog) + + subject.submit_request(data) + expect(subject.dialog.dialog_fields.first.value).to eq("new_value") + end - result = subject.submit_request - miq_task = MiqTask.find(result[:task_id]) - expect(miq_task.state).to(eq(MiqTask::STATE_QUEUED)) - expect(miq_task.status).to(eq(MiqTask::STATUS_OK)) - expect(miq_task.message).to(eq('MiqTask has been queued.')) + it "calls automate with miq_task" do + options[:open_url] = true + allow(resource_action).to(receive(:deliver_to_automate_from_dialog)) + allow(subject).to(receive(:load_resource_action)).and_return(resource_action) + + result = subject.submit_request + miq_task = MiqTask.find(result[:task_id]) + expect(miq_task.state).to(eq(MiqTask::STATE_QUEUED)) + expect(miq_task.status).to(eq(MiqTask::STATUS_OK)) + expect(miq_task.message).to(eq('MiqTask has been queued.')) + end end end end