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

Fix specifying of additional attributes on pictures #327

Merged
merged 1 commit into from
Feb 27, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion app/controllers/api/mixins/pictures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ def fetch_picture(resource)

def format_picture_response(picture)
return unless picture
picture.attributes.except('content').merge('image_href' => picture.image_href)
picture_hash = picture.attributes.except('content').merge('image_href' => picture.image_href,
'href' => picture.href_slug)
normalize_hash(:picture, picture_hash)
end
end
end
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/api/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ def create_resource(_type, _id, data)
def set_additional_attributes
@additional_attributes = %w(image_href extension)
end

def attribute_selection
if !@req.attributes.empty? || @additional_attributes
@req.attributes | Array(@additional_attributes) | ID_ATTRS
else
Picture.attribute_names - %w(content)
end
end
end
end
42 changes: 42 additions & 0 deletions spec/requests/pictures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,29 @@ def expect_result_to_include_picture_href(source_id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(expected)
end

it 'allows specifying of additional attributes' do
api_basic_authorize

expected = {
'resources' => [
a_hash_including('href_slug' => @picture.href_slug,
'region_number' => @picture.region_number)
]
}
get(api_pictures_url, :params => { :expand => 'resources', :attributes => 'href_slug,region_number'})

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

it 'only allows specifying of valid attributes' do
api_basic_authorize

get(api_pictures_url, :params => { :expand => 'resources', :attributes => 'bad_attr'})

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

describe 'GET /api/pictures/:id' do
Expand All @@ -109,6 +132,25 @@ def expect_result_to_include_picture_href(source_id)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('image_href' => a_string_including(@picture.image_href), 'extension' => @picture.extension)
end

it 'allows specifying of additional attributes' do
api_basic_authorize

get(api_picture_url(nil, @picture), :params => { :attributes => 'href_slug,region_number' })

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include('href_slug' => @picture.href_slug,
'region_number' => @picture.region_number)
end

it 'will return only the requested physical attribute with the set additional attributes' do
api_basic_authorize

get(api_picture_url(nil, @picture), :params => { :attributes => 'md5' })

expect(response).to have_http_status(:ok)
expect(response.parsed_body.keys).to match_array(%w(md5 href id extension image_href))
end
end

describe 'POST /api/pictures' do
Expand Down