Skip to content

Commit

Permalink
Specify a target and resource action when retrieving a service dialog
Browse files Browse the repository at this point in the history
Fetching the content from a resource sometimes requires both a target and resource_action, where we are currently passing in nil values. By accepting resource_action_id, target_type, and target_id, we are able to use resource search to pass in the applicable values.

https://bugzilla.redhat.com/show_bug.cgi?id=1518390
  • Loading branch information
Jillian Tullo committed Nov 29, 2017
1 parent 3c0d8dd commit b100aa7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
14 changes: 13 additions & 1 deletion app/controllers/api/service_dialogs_controller.rb
Expand Up @@ -2,6 +2,8 @@ module Api
class ServiceDialogsController < BaseController
before_action :set_additional_attributes, :only => [:index, :show]

CONTENT_PARAMS = %w[target_type target_id resource_action_id].freeze

def refresh_dialog_fields_resource(type, id = nil, data = nil)
raise BadRequestError, "Must specify an id for Reconfiguring a #{type} resource" unless id

Expand All @@ -14,7 +16,8 @@ def refresh_dialog_fields_resource(type, id = nil, data = nil)
end

def fetch_service_dialogs_content(resource)
resource.content(nil, nil, true)
target, resource_action = validate_dialog_content_params
resource.content(target, resource_action, true)
end

def create_resource(_type, _id, data)
Expand Down Expand Up @@ -46,6 +49,15 @@ def copy_resource(type, id, data)

private

def validate_dialog_content_params
return unless CONTENT_PARAMS.detect { |param| params.include?(param) }
raise BadRequestError, "Must specify all of #{CONTENT_PARAMS.join(',')}" unless (CONTENT_PARAMS - params.keys).count.zero?
target_type = params['target_type'].pluralize.to_sym
target = resource_search(params['target_id'], target_type, collection_class(target_type))
resource_action = resource_search(params['resource_action_id'], :resource_actions, ResourceAction)
[target, resource_action]
end

def set_additional_attributes
@additional_attributes = %w(content) if attribute_selection == "all"
end
Expand Down
25 changes: 25 additions & 0 deletions spec/requests/service_dialogs_spec.rb
Expand Up @@ -50,6 +50,31 @@
expect_result_to_have_keys(%w(content))
end

it "query single dialog to include content with target and resource action specified" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)
service_template = FactoryGirl.create(:service_template)
get(api_service_dialog_url(nil, dialog1), :params => { :resource_action_id => ra1.id, :target_id => service_template.id, :target_type => 'service_template' })

expect_single_resource_query(
"id" => dialog1.id.to_s,
"href" => api_service_dialog_url(nil, dialog1),
"label" => dialog1.label
)
expect_result_to_have_keys(%w(content))
end

it "requires both target_id, target_type, and resource_action" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)

get(api_service_dialog_url(nil, dialog1), :params => { :target_id => 'id' })

expected = {
'error' => a_hash_including('message' => a_string_including('Must specify all of'))
}
expect(response).to have_http_status(:bad_request)
expect(response.parsed_body).to include(expected)
end

it "query single dialog to exclude content when attributes are asked for" do
api_basic_authorize action_identifier(:service_dialogs, :read, :resource_actions, :get)

Expand Down

0 comments on commit b100aa7

Please sign in to comment.