diff --git a/app/controllers/api/authentications_controller.rb b/app/controllers/api/authentications_controller.rb index 86f2ff15f5..f1c0689c13 100644 --- a/app/controllers/api/authentications_controller.rb +++ b/app/controllers/api/authentications_controller.rb @@ -2,21 +2,25 @@ module Api class AuthenticationsController < BaseController def edit_resource(type, id, data) api_resource(type, id, "Updating") do |auth| - ensure_respond_to(type, auth, :update, :update_in_provider_queue) + ensure_supports(type, auth, :update) {:task_id => auth.update_in_provider_queue(data.deep_symbolize_keys)} end end - def create_resource(_type, _id, data) + def create_resource(type, _id, data) manager_resource, attrs = validate_auth_attrs(data) - task_id = AuthenticationService.create_authentication_task(manager_resource, attrs) + + klass = ::Authentication.descendant_get(attrs['type']) + ensure_supports(type, klass, :create) + + task_id = klass.create_in_provider_queue(manager_resource.id, attrs.deep_symbolize_keys) action_result(true, 'Creating Authentication', :task_id => task_id) rescue => err action_result(false, err.to_s) end def delete_resource_main_action(type, auth, _data) - ensure_respond_to(type, auth, :delete, :delete_in_provider_queue) + ensure_supports(type, auth, :delete) {:task_id => auth.delete_in_provider_queue} end diff --git a/app/controllers/api/subcollections/authentications.rb b/app/controllers/api/subcollections/authentications.rb index 1adfca23bd..932ef02ace 100644 --- a/app/controllers/api/subcollections/authentications.rb +++ b/app/controllers/api/subcollections/authentications.rb @@ -5,8 +5,11 @@ def authentications_query_resource(object) object.respond_to?(:authentications) ? object.authentications : [] end - def authentications_create_resource(parent, _type, _id, data) - task_id = AuthenticationService.create_authentication_task(parent.manager, data) + def authentications_create_resource(parent, type, _id, data) + klass = ::Authentication.descendant_get(data['type']) + ensure_supports(type, klass, :create) + + task_id = klass.create_in_provider_queue(parent.manager.id, data.deep_symbolize_keys) action_result(true, 'Creating Authentication', :task_id => task_id) rescue => err action_result(false, err.to_s) diff --git a/lib/services/api/authentication_service.rb b/lib/services/api/authentication_service.rb deleted file mode 100644 index a0d8f3b2ec..0000000000 --- a/lib/services/api/authentication_service.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Api - class AuthenticationService - def self.create_authentication_task(manager_resource, attrs) - klass = ::Authentication.descendant_get(attrs['type']) - raise 'type not currently supported' unless klass.respond_to?(:create_in_provider_queue) - klass.create_in_provider_queue(manager_resource.id, attrs.deep_symbolize_keys) - end - end -end diff --git a/spec/requests/authentications_spec.rb b/spec/requests/authentications_spec.rb index 144deb424e..c96c6ed2eb 100644 --- a/spec/requests/authentications_spec.rb +++ b/spec/requests/authentications_spec.rb @@ -1,4 +1,6 @@ RSpec.describe 'Authentications API' do + include Spec::Support::SupportsHelper + let(:manager) { FactoryBot.create(:automation_manager_ansible_tower) } let(:auth) { FactoryBot.create(:ansible_cloud_credential, :resource => manager) } let(:auth_2) { FactoryBot.create(:ansible_cloud_credential, :resource => manager) } @@ -69,6 +71,7 @@ it 'will delete an authentication' do api_basic_authorize collection_action_identifier(:authentications, :delete, :post) + stub_supports(auth, :delete) post(api_authentications_url, :params => { :action => 'delete', :resources => [{ 'id' => auth.id }] }) @@ -86,14 +89,17 @@ it 'verifies that the type is supported' do api_basic_authorize collection_action_identifier(:authentications, :delete, :post) auth = FactoryBot.create(:authentication) + stub_supports_not(auth, :delete) post(api_authentications_url, :params => { :action => 'delete', :resources => [{ 'id' => auth.id }] }) - expect_multiple_action_result(1, :success => false, :message => /Delete not supported/) + expect_multiple_action_result(1, :success => false, :message => /Feature not available\/supported/) end it 'will delete multiple authentications' do api_basic_authorize collection_action_identifier(:authentications, :delete, :post) + stub_supports(auth, :delete) + stub_supports(auth_2, :delete) post(api_authentications_url, :params => {:action => 'delete', :resources => [{'id' => auth.id}, {'id' => auth_2.id}]}) expect_multiple_action_result(2, :task => true, :success => true, :message => 'Deleting Authentication') @@ -109,6 +115,7 @@ it 'can update an authentication with an appropriate role' do api_basic_authorize collection_action_identifier(:authentications, :edit) + stub_supports(auth, :update) post(api_authentications_url, :params => { :action => 'edit', :resources => [params] }) @@ -128,6 +135,7 @@ 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) + stub_supports(auth_2, :update) post(api_authentications_url, :params => { :action => 'edit', :resources => [params, params2] }) @@ -189,7 +197,7 @@ expected = { 'results' => [ - { 'success' => false, 'message' => 'type not currently supported' } + { 'success' => false, 'message' => 'Create for Authentications: Feature not available/supported' } ] } expect(response).to have_http_status(:bad_request) @@ -199,6 +207,8 @@ it 'can create an authentication' do api_basic_authorize collection_action_identifier(:authentications, :create, :post) + stub_supports(create_params[:type].safe_constantize, :create) + expected = { 'results' => [a_hash_including( 'success' => true, @@ -298,6 +308,7 @@ it 'can update an authentication with an appropriate role' do api_basic_authorize collection_action_identifier(:authentications, :edit) + stub_supports(auth, :update) put(api_authentication_url(nil, auth), :params => { :resource => params }) @@ -322,6 +333,7 @@ it 'can update an authentication with an appropriate role' do api_basic_authorize collection_action_identifier(:authentications, :edit) + stub_supports(auth, :update) patch(api_authentication_url(nil, auth), :params => [params]) @@ -345,6 +357,7 @@ it 'will delete an authentication' do api_basic_authorize action_identifier(:authentications, :delete, :resource_actions, :post) + stub_supports(auth, :delete) post(api_authentication_url(nil, auth), :params => { :action => 'delete' }) @@ -361,6 +374,7 @@ it 'can update an authentication with an appropriate role' do api_basic_authorize collection_action_identifier(:authentications, :edit) + stub_supports(auth, :update) post(api_authentication_url(nil, auth), :params => { :action => 'edit', :resource => params }) @@ -376,10 +390,11 @@ it 'requires that the type support update_in_provider_queue' do api_basic_authorize collection_action_identifier(:authentications, :edit) auth = FactoryBot.create(:authentication) + stub_supports_not(auth, :update) post(api_authentication_url(nil, auth), :params => { :action => 'edit', :resource => params }) - expect_bad_request("Update not supported for Authentication id: #{auth.id} name: '#{auth.name}'") + expect_bad_request("Update for Authentication id: #{auth.id} name: '#{auth.name}': Feature not available/supported") end it 'will forbid update to an authentication without appropriate role' do @@ -418,6 +433,8 @@ describe 'DELETE /api/authentications/:id' do it 'will delete an authentication' do auth = FactoryBot.create(:embedded_ansible_openstack_credential) + stub_supports(auth, :delete) + api_basic_authorize action_identifier(:authentications, :delete, :resource_actions, :delete) delete(api_authentication_url(nil, auth)) diff --git a/spec/requests/configuration_script_payloads_spec.rb b/spec/requests/configuration_script_payloads_spec.rb index 2c7aa34f5e..4b442481e9 100644 --- a/spec/requests/configuration_script_payloads_spec.rb +++ b/spec/requests/configuration_script_payloads_spec.rb @@ -1,4 +1,6 @@ RSpec.describe 'Configuration Script Payloads API' do + include Spec::Support::SupportsHelper + describe 'GET /api/configuration_script_payloads' do it 'lists all the configuration script payloads with an appropriate role' do script_payload = FactoryBot.create(:configuration_script_payload) @@ -88,7 +90,7 @@ expected = { 'results' => [ - { 'success' => false, 'message' => 'type not currently supported' } + { 'success' => false, 'message' => 'Create for Authentications: Feature not available/supported' } ] } expect(response).to have_http_status(:bad_request) @@ -97,6 +99,7 @@ it 'creates a new authentication with an appropriate role' do api_basic_authorize subcollection_action_identifier(:configuration_script_payloads, :authentications, :create) + stub_supports(Authentication, :create) post(api_configuration_script_payload_authentications_url(nil, playbook), :params => params)