Skip to content

Commit

Permalink
Merge pull request #656 from fdupont-redhat/v2v_allow_error_messages_…
Browse files Browse the repository at this point in the history
…in_service_template_orderable

Expose error messages from ServiceTemplate.orderable?

(cherry picked from commit 2aa1e4c)

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1768520
  • Loading branch information
abellotti authored and simaishi committed Nov 4, 2019
1 parent b233df9 commit f852e17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
8 changes: 3 additions & 5 deletions app/controllers/api/mixins/service_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Mixins
module ServiceTemplates
def order_service_template(id, data, scheduled_time = nil)
service_template = resource_search(id, :service_templates, ServiceTemplate)
raise BadRequestError, "#{service_template_ident(service_template)} cannot be ordered" unless orderable?(service_template)
raise BadRequestError, "Service ordering via API is not allowed" unless api_request_allowed?
raise BadRequestError, "#{service_template_ident(service_template)} cannot be ordered - #{service_template.unsupported_reason(:order)}" unless service_template.supports_order?

request_result = service_template.order(User.current_user, (data || {}), order_request_options, scheduled_time)
errors = request_result[:errors]
if errors.present?
Expand All @@ -14,10 +16,6 @@ def order_service_template(id, data, scheduled_time = nil)

private

def orderable?(service_template)
api_request_allowed? && service_template.orderable?
end

def api_request_allowed?
return true if request_from_ui?
Settings.product.allow_api_service_ordering
Expand Down
9 changes: 5 additions & 4 deletions spec/requests/service_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,8 @@
end

describe "Service Templates order" do
let(:service_template) { FactoryBot.create(:service_template, :with_provision_resource_action_and_dialog, :orderable) }
let(:service_template_catalog) { FactoryBot.create(:service_template_catalog) }
let(:service_template) { FactoryBot.create(:service_template, :with_provision_resource_action_and_dialog, :service_template_catalog => service_template_catalog, :display => true) }
let(:allow_api_service_ordering) { true }

before do
Expand Down Expand Up @@ -557,7 +558,7 @@
end

it "can order multiple service templates" do
service_template2 = FactoryBot.create(:service_template, :with_provision_resource_action_and_dialog, :orderable)
service_template2 = FactoryBot.create(:service_template, :with_provision_resource_action_and_dialog, :service_template_catalog => service_template_catalog, :display => true)
api_basic_authorize action_identifier(:service_templates, :order, :resource_actions, :post)

post(api_service_templates_url, :params => { :action => "order", :resources =>
Expand Down Expand Up @@ -606,7 +607,7 @@
expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => /cannot be ordered/
"message" => /cannot be ordered - Service template is not configured to be displayed/
)
}
expect(response).to have_http_status(:bad_request)
Expand Down Expand Up @@ -640,7 +641,7 @@
expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => /cannot be ordered/
"message" => "Service ordering via API is not allowed"
)
}
expect(response).to have_http_status(:bad_request)
Expand Down

0 comments on commit f852e17

Please sign in to comment.