Skip to content

Commit

Permalink
Merge pull request #14307 from jntullo/enhancement/delete_authentication
Browse files Browse the repository at this point in the history
delete authentication in provider
  • Loading branch information
abellotti committed Mar 16, 2017
2 parents e646755 + 88ec961 commit ef2219d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
14 changes: 14 additions & 0 deletions app/controllers/api/authentications_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
module Api
class AuthenticationsController < BaseController
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)
task_id = auth.delete_in_provider_queue
action_result(true, "Deleting #{authentication_ident(auth)}", :task_id => task_id)
rescue => err
action_result(false, err.to_s)
end

private

def authentication_ident(auth)
"Authentication id:#{auth.id} name: '#{auth.name}'"
end
end
end
76 changes: 63 additions & 13 deletions spec/requests/api/authentications_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
RSpec.describe 'Authentications API' do
let(:provider) { FactoryGirl.create(:provider_ansible_tower) }
let(:auth) { FactoryGirl.create(:ansible_cloud_credential, :resource => provider) }
let(:auth_2) { FactoryGirl.create(:ansible_cloud_credential, :resource => provider) }

describe 'GET/api/authentications' do
it 'lists all the authentication configuration script bases with an appropriate role' do
auth = FactoryGirl.create(:authentication)
Expand Down Expand Up @@ -51,15 +55,60 @@

describe 'POST /api/authentications' do
it 'will delete an authentication' do
api_basic_authorize collection_action_identifier(:authentications, :delete, :post)

expected = {
'results' => [
a_hash_including(
'success' => true,
'message' => a_string_including('Deleting Authentication'),
'task_id' => a_kind_of(Numeric)
)
]
}
run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }])

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'verifies that the type is supported' do
api_basic_authorize collection_action_identifier(:authentications, :delete, :post)
auth = FactoryGirl.create(:authentication)

run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }])

expected = {
'results' => [
{
'success' => false,
'message' => "Delete 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 delete multiple authentications' do
api_basic_authorize collection_action_identifier(:authentications, :delete, :post)

expected = {
'results' => [a_hash_including('success' => true)]
'results' => [
a_hash_including(
'success' => true,
'message' => a_string_including('Deleting Authentication'),
'task_id' => a_kind_of(Numeric)
),
a_hash_including(
'success' => true,
'message' => a_string_including('Deleting Authentication'),
'task_id' => a_kind_of(Numeric)
)
]
}
expect do
run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }])
end.to change(Authentication, :count).by(-1)
run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }, { 'id' => auth_2.id }])

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end
Expand All @@ -75,18 +124,20 @@

describe 'POST /api/authentications/:id' do
it 'will delete an authentication' do
auth = FactoryGirl.create(:authentication)
api_basic_authorize action_identifier(:authentications, :delete, :resource_actions, :post)

expect do
run_post(authentications_url(auth.id), :action => 'delete')
end.to change(Authentication, :count).by(-1)
expect(response.parsed_body).to include('success' => true)
run_post(authentications_url(auth.id), :action => 'delete')

expected = {
'success' => true,
'message' => a_string_including('Deleting Authentication'),
'task_id' => a_kind_of(Numeric)
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'will not delete an authentication without an appropriate role' do
auth = FactoryGirl.create(:authentication)
api_basic_authorize

run_post(authentications_url(auth.id), :action => 'delete')
Expand All @@ -100,9 +151,8 @@
auth = FactoryGirl.create(:authentication)
api_basic_authorize action_identifier(:authentications, :delete, :resource_actions, :delete)

expect do
run_delete(authentications_url(auth.id))
end.to change(Authentication, :count).by(-1)
run_delete(authentications_url(auth.id))

expect(response).to have_http_status(:no_content)
end

Expand Down

0 comments on commit ef2219d

Please sign in to comment.