Skip to content

Commit

Permalink
Merge pull request #179 from jntullo/bz/put_patch_on_edit
Browse files Browse the repository at this point in the history
Return put and patch edit actions for resources
(cherry picked from commit 3931588)

https://bugzilla.redhat.com/show_bug.cgi?id=1511545
  • Loading branch information
abellotti authored and simaishi committed Nov 14, 2017
1 parent 8d77e60 commit 5fd0240
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
14 changes: 11 additions & 3 deletions app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,13 +441,21 @@ def gen_action_spec_for_resources(cspec, is_subcollection, href, resource)
next unless render_actions_for_method(cspec[:verbs], method)
typed_action_definitions = action_definitions || fetch_typed_subcollection_actions(method, is_subcollection)
typed_action_definitions.each.collect do |action|
if !action[:disabled] && api_user_role_allows?(action[:identifier]) && action_validated?(resource, action)
{"name" => action[:name], "method" => method, "href" => href}
end
next unless !action[:disabled] && api_user_role_allows?(action[:identifier]) && action_validated?(resource, action)
build_resource_actions(action, method, href, cspec[:verbs])
end
end.flatten.compact
end

def build_resource_actions(action, method, href, verbs)
actions = [{"name" => action[:name], "method" => method, "href" => href}]
if action[:name] == "edit"
actions << { 'name' => 'edit', 'method' => :patch, 'href' => href } if verbs.include?(:patch)
actions << { 'name' => 'edit', 'method' => :put, 'href' => href } if verbs.include?(:put)
end
actions
end

def render_actions_for_method(methods, method)
method != :get && methods.include?(method)
end
Expand Down
13 changes: 10 additions & 3 deletions spec/requests/authentications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@

describe 'GET /api/authentications/:id' do
it 'will show an authentication configuration script base' do
api_basic_authorize action_identifier(:authentications, :read, :resource_actions, :get)
api_basic_authorize action_identifier(:authentications, :read, :resource_actions, :get),
action_identifier(:authentications, :edit)
href = api_authentication_url(nil, auth)

get(api_authentication_url(nil, auth))
get(href)

expected = {
'href' => api_authentication_url(nil, auth)
'href' => href,
'actions' => [
{ 'name' => 'edit', 'method' => 'post', 'href' => href },
{ 'name' => 'edit', 'method' => 'patch', 'href' => href },
{ 'name' => 'edit', 'method' => 'put', 'href' => href }
]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
Expand Down
6 changes: 3 additions & 3 deletions spec/requests/custom_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def expect_result_to_have_custom_actions_hash
get api_service_url(nil, svc1)

expect_result_to_have_keys(%w(id href actions))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit add_resource remove_resource remove_all_resources add_provider_vms))
expect(response.parsed_body["actions"].select { |a| a["method"] == "post" }.pluck("name")).to match_array(%w(edit add_resource remove_resource remove_all_resources add_provider_vms))
end
end

Expand All @@ -91,7 +91,7 @@ def expect_result_to_have_custom_actions_hash
get api_service_url(nil, svc1)

expect_result_to_have_keys(%w(id href actions))
expect(response.parsed_body["actions"].collect { |a| a["name"] }).to match_array(%w(edit button1 button2 button3 add_resource remove_resource remove_all_resources add_provider_vms))
expect(response.parsed_body["actions"].select { |a| a["method"] == "post" }.pluck("name")).to match_array(%w(edit button1 button2 button3 add_resource remove_resource remove_all_resources add_provider_vms))
end

it "supports the custom_actions attribute" do
Expand Down Expand Up @@ -128,7 +128,7 @@ def expect_result_to_have_custom_actions_hash

expect_result_to_have_keys(%w(id href actions))
action_specs = response.parsed_body["actions"]
expect(action_specs.size).to eq(1)
expect(action_specs.size).to eq(3)
expect(action_specs.first["name"]).to eq("edit")
end

Expand Down

0 comments on commit 5fd0240

Please sign in to comment.