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

Expose error messages from ServiceTemplate.orderable? #656

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