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

Tags Subcollection on Generic Objects #64

Merged
merged 1 commit into from
Sep 21, 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
2 changes: 2 additions & 0 deletions app/controllers/api/generic_objects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module Api
class GenericObjectsController < BaseController
include Subcollections::Tags

ADDITIONAL_ATTRS = %w(generic_object_definition associations).freeze

before_action :set_additional_attributes
Expand Down
8 changes: 8 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,8 @@
- :subcollection
:verbs: *gpd
:klass: GenericObject
:subcollections:
- :tags
:collection_actions:
:get:
- :name: read
Expand All @@ -911,6 +913,12 @@
:delete:
- :name: delete
:identifier: generic_object_delete
:tags_subcollection_actions:
:post:
- :name: assign
:identifier: generic_object_edit
- :name: unassign
:identifier: generic_object_edit
:groups:
:description: Groups
:identifier: rbac_group
Expand Down
73 changes: 73 additions & 0 deletions spec/requests/tag_collections_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1108,4 +1108,77 @@ def expect_resource_has_tags(resource, tag_names)
expect(response.parsed_body).to include(expected)
end
end

context 'Generic Objects subcollection' do
let(:object) { FactoryGirl.create(:generic_object) }

describe 'POST /api/generic_objects/:id/tags' do
it 'cannot assign tags without an appropriate role' do
api_basic_authorize

post(api_generic_object_tags_url(nil, object), :params => { :action => 'assign' })

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

it 'can assign tags with an appropriate role' do
api_basic_authorize(subcollection_action_identifier(:generic_objects, :tags, :assign))

post(api_generic_object_tags_url(nil, object), :params => { :action => 'assign', :category => tag1[:category], :name => tag1[:name]})

expected = {
'results' => [
a_hash_including('success' => true, 'message' => a_string_including('Assigning Tag'))
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'cannot assign tags without an appropriate role' do
api_basic_authorize

post(api_generic_object_tags_url(nil, object), :params => { :action => 'unassign' })

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

it 'can unassign tags with an appropriate role' do
Classification.classify(object, tag1[:category], tag1[:name])
api_basic_authorize(subcollection_action_identifier(:generic_objects, :tags, :assign))

post(api_generic_object_tags_url(nil, object), :params => { :action => 'unassign', :category => tag1[:category], :name => tag1[:name]})

expected = {
'results' => [
a_hash_including('success' => true, 'message' => a_string_including('Unassigning Tag'))
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end

describe 'GET /api/generic_objects/:id/tags' do
before do
Classification.classify(object, tag1[:category], tag1[:name])
end

it 'returns tags for a generic object' do
api_basic_authorize

get(api_generic_object_tags_url(nil, object))

expected = {
'name' => 'tags',
'subcount' => 1,
'resources' => [
{ 'href' => a_string_including(api_generic_object_tags_url(nil, object)) }
]
}
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end
end
end
end