Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load dialog fields with updated values on workflow submit #17855

Merged
merged 2 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ 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?
Copy link
Member

@eclarizio eclarizio Aug 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Disregard. My mistake, when testing I set a variable of `test` to a valid hash and `test2` to an empty hash and when I went to test `.present?` I used `test.present?` which of course returned true. `test2.present?` does indeed return false.

process_request(ServiceOrder::STATE_ORDERED)
end

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?
Expand Down
177 changes: 130 additions & 47 deletions spec/models/resource_action_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down