diff --git a/app/controllers/api/authentications_controller.rb b/app/controllers/api/authentications_controller.rb index 2be1ba026bb..6342961f103 100644 --- a/app/controllers/api/authentications_controller.rb +++ b/app/controllers/api/authentications_controller.rb @@ -1,5 +1,14 @@ module Api class AuthenticationsController < BaseController + def edit_resource(type, id, data) + auth = resource_search(id, type, collection_class(:authentications)) + raise "Update not supported for #{authentication_ident(auth)}" unless auth.respond_to?(:update_in_provider_queue) + task_id = auth.update_in_provider_queue(data) + action_result(true, "Updating #{authentication_ident(auth)}", :task_id => task_id) + rescue => err + action_result(false, err.to_s) + end + def delete_resource(type, id, _data = {}) auth = resource_search(id, type, collection_class(:authentications)) raise "Delete not supported for #{authentication_ident(auth)}" unless auth.respond_to?(:delete_in_provider_queue) diff --git a/config/api.yml b/config/api.yml index c8d6f6d389d..f71119f8945 100644 --- a/config/api.yml +++ b/config/api.yml @@ -242,6 +242,8 @@ :post: - :name: delete :identifier: embedded_automation_manager_credentials_delete + - :name: edit + :identifier: embedded_automation_manager_credentials_edit :resource_actions: :get: - :name: read @@ -249,6 +251,8 @@ :post: - :name: delete :identifier: embedded_automation_manager_credentials_delete + - :name: edit + :identifier: embedded_automation_manager_credentials_edit :delete: - :name: delete :identifier: embedded_automation_manager_credentials_delete diff --git a/spec/requests/api/authentications_spec.rb b/spec/requests/api/authentications_spec.rb index e0afcc4cfd9..eadeb984900 100644 --- a/spec/requests/api/authentications_spec.rb +++ b/spec/requests/api/authentications_spec.rb @@ -31,7 +31,6 @@ describe 'GET /api/authentications/:id' do it 'will show an authentication configuration script base' do - auth = FactoryGirl.create(:authentication) api_basic_authorize action_identifier(:authentications, :read, :resource_actions, :get) run_get(authentications_url(auth.id)) @@ -44,7 +43,6 @@ end it 'forbids access to an authentication configuration script base' do - auth = FactoryGirl.create(:authentication) api_basic_authorize run_get(authentications_url(auth.id)) @@ -54,6 +52,14 @@ end describe 'POST /api/authentications' do + let(:params) do + { + :id => auth.id, + :description => 'Description', + :name => 'Updated Credential' + } + end + it 'will delete an authentication' do api_basic_authorize collection_action_identifier(:authentications, :delete, :post) @@ -120,9 +126,66 @@ run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }]) expect(response).to have_http_status(:forbidden) end + + it 'can update an authentication with an appropriate role' do + api_basic_authorize collection_action_identifier(:authentications, :edit) + + run_post(authentications_url, :action => 'edit', :resources => [params]) + + expected = { + 'results' => [ + a_hash_including( + 'success' => true, + 'message' => a_string_including('Updating Authentication'), + 'task_id' => a_kind_of(Numeric) + ) + ] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'can update an authentication with an appropriate role' do + params2 = params.dup.merge(:id => auth_2.id) + api_basic_authorize collection_action_identifier(:authentications, :edit) + + run_post(authentications_url, :action => 'edit', :resources => [params, params2]) + + expected = { + 'results' => [ + a_hash_including( + 'success' => true, + 'message' => a_string_including('Updating Authentication'), + 'task_id' => a_kind_of(Numeric) + ), + a_hash_including( + 'success' => true, + 'message' => a_string_including('Updating Authentication'), + 'task_id' => a_kind_of(Numeric) + ) + ] + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'will forbid update to an authentication without appropriate role' do + api_basic_authorize + + run_post(authentications_url, :action => 'edit', :resources => [params]) + + expect(response).to have_http_status(:forbidden) + end end describe 'POST /api/authentications/:id' do + let(:params) do + { + :description => 'Description', + :name => 'Updated Credential' + } + end + it 'will delete an authentication' do api_basic_authorize action_identifier(:authentications, :delete, :resource_actions, :post) @@ -144,6 +207,42 @@ expect(response).to have_http_status(:forbidden) end + + it 'can update an authentication with an appropriate role' do + api_basic_authorize collection_action_identifier(:authentications, :edit) + + run_post(authentications_url(auth.id), :action => 'edit', :resource => params) + + expected = { + 'success' => true, + 'message' => a_string_including('Updating Authentication'), + 'task_id' => a_kind_of(Numeric) + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'requires that the type support update_in_provider_queue' do + api_basic_authorize collection_action_identifier(:authentications, :edit) + auth = FactoryGirl.create(:authentication) + + run_post(authentications_url(auth.id), :action => 'edit', :resource => params) + + expected = { + 'success' => false, + 'message' => "Update not supported for Authentication id:#{auth.id} name: '#{auth.name}'" + } + expect(response).to have_http_status(:ok) + expect(response.parsed_body).to include(expected) + end + + it 'will forbid update to an authentication without appropriate role' do + api_basic_authorize + + run_post(authentications_url(auth.id), :action => 'edit', :resource => params) + + expect(response).to have_http_status(:forbidden) + end end describe 'DELETE /api/authentications/:id' do