Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentications Read and Delete api #13780

Merged
merged 2 commits into from
Feb 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/controllers/api/authentications_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module Api
class AuthenticationsController < BaseController
end
end
23 changes: 23 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,29 @@
:options:
- :primary
:verbs: *gd
:authentications:
:description: Authentications
:options:
- :collection
:verbs: *gpd
:klass: Authentication
:collection_actions:
:get:
- :name: read
:identifier: embedded_automation_manager_credentials_view
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abellotti how should we handle this identifier? We are using the Authentication model, but only have this identifier currently. Is it fine as is, and as needed we will add additional ones via #13827 ?

Copy link
Member

@abellotti abellotti Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thought behind #13827 is that higher level collections are authorizable if the user has at least one of the related/relevant roles, so if a user has embedded_automation_manager_credentials_view or later automation_manager_credentials_view, etc. he'd be able to access this collection.

Signature above is fine, once #13827 is merged, you'd also use the array equivalent:

  :get:
  - :name: read
     :identifier:
     - embedded_automation_manager_credentials_view
     - automation_manager_credentials_view  (adding later whatever additional ones materialize as )

:post:
- :name: delete
:identifier: embedded_automation_manager_credentials_delete
:resource_actions:
:get:
- :name: read
:identifier: embedded_automation_manager_credentials_view
:post:
- :name: delete
:identifier: embedded_automation_manager_credentials_delete
:delete:
- :name: delete
:identifier: embedded_automation_manager_credentials_delete
:automate:
:description: Automate
:options:
Expand Down
118 changes: 118 additions & 0 deletions spec/requests/api/authentications_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
RSpec.describe 'Authentications API' do
describe 'GET/api/authentications' do
it 'lists all the authentication configuration script bases with an appropriate role' do
auth = FactoryGirl.create(:authentication)
api_basic_authorize collection_action_identifier(:authentications, :read, :get)

run_get(authentications_url)

expected = {
'count' => 1,
'subcount' => 1,
'name' => 'authentications',
'resources' => [hash_including('href' => a_string_matching(authentications_url(auth.id)))]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'forbids access to authentication configuration script bases without an appropriate role' do
api_basic_authorize

run_get(authentications_url)

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

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))

expected = {
'href' => a_string_matching(authentications_url(auth.id))
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'forbids access to an authentication configuration script base' do
auth = FactoryGirl.create(:authentication)
api_basic_authorize

run_get(authentications_url(auth.id))

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

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

expected = {
'results' => [a_hash_including('success' => true)]
}
expect do
run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }])
end.to change(Authentication, :count).by(-1)
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

it 'will forbid deletion to an authentication without appropriate role' do
auth = FactoryGirl.create(:authentication)
api_basic_authorize

run_post(authentications_url, :action => 'delete', :resources => [{ 'id' => auth.id }])
expect(response).to have_http_status(:forbidden)
end
end

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)
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')

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

describe 'DELETE /api/authentications/:id' do
it 'will delete an authentication' do
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)
expect(response).to have_http_status(:no_content)
end

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

run_delete(authentications_url(auth.id))

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