-
Notifications
You must be signed in to change notification settings - Fork 898
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
Improve create picture validation #13697
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,9 @@ | ||
module Api | ||
class PicturesController < BaseController | ||
def create_resource(_type, _id, data) | ||
data['content'] = Base64.decode64(data['content']) | ||
picture = Picture.create(data) | ||
raise BadRequestError, | ||
"Failed to create Picture - #{picture.errors.full_messages.join(', ')}" unless picture.valid? | ||
picture | ||
Picture.create_from_base64(data) | ||
rescue => err | ||
raise BadRequestError, "Failed to create Picture - #{err}" | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
FactoryGirl.define do | ||
factory :picture do | ||
extension 'png' | ||
after(:build) do |x| | ||
x.content = 'foo' | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,20 +63,20 @@ def expect_result_to_include_picture_href(source_id) | |
end | ||
|
||
describe 'POST /api/pictures' do | ||
# one pixel png image encoded in Base64 | ||
# Valid base64 image | ||
let(:content) do | ||
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAABGdBTUEAALGP\n"\ | ||
"C/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3Cc\n"\ | ||
"ulE8AAAACXBIWXMAAAsTAAALEwEAmpwYAAABWWlUWHRYTUw6Y29tLmFkb2Jl\n"\ | ||
"LnhtcAAAAAAAPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIg\n"\ | ||
"eDp4bXB0az0iWE1QIENvcmUgNS40LjAiPgogICA8cmRmOlJERiB4bWxuczpy\n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DwvcmRmOkRlc2NyaXB0aW9uPgogICA8L3JkZjpS"\ | ||
"REY+CjwveDp4bXBtZXRhPgpMwidZAAAADUlEQVQIHWNgYGCwBQAAQgA+3N0+"\ | ||
"xQAAAABJRU5ErkJggg==" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the updates @jntullo, question about above change that is driven by using strict_decode64. does this mean we just broke compatibility with v2.3.0 or the old usage with \n would never really occur ? How does the current service-ui generate the content in these payloads ? Thanks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @abellotti the old usage with |
||
end | ||
|
||
it 'rejects create without an appropriate role' do | ||
|
@@ -117,5 +117,47 @@ def expect_result_to_include_picture_href(source_id) | |
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it 'requires an extension' do | ||
api_basic_authorize collection_action_identifier(:pictures, :create) | ||
|
||
run_post pictures_url, :content => content | ||
|
||
expected = { | ||
'error' => a_hash_including( | ||
'message' => a_string_including("Extension can't be blank") | ||
) | ||
} | ||
expect(response).to have_http_status(:bad_request) | ||
expect(response.parsed_body).to include(expected) | ||
end | ||
|
||
it 'requires content' do | ||
api_basic_authorize collection_action_identifier(:pictures, :create) | ||
|
||
run_post pictures_url, :extension => 'png' | ||
|
||
expected = { | ||
'error' => a_hash_including( | ||
'message' => a_string_including("Content can't be blank") | ||
) | ||
} | ||
expect(response).to have_http_status(:bad_request) | ||
expect(response.parsed_body).to include(expected) | ||
end | ||
|
||
it 'requires content with valid base64' do | ||
api_basic_authorize collection_action_identifier(:pictures, :create) | ||
|
||
run_post pictures_url, :content => 'not base64', :extension => 'png' | ||
|
||
expected = { | ||
'error' => a_hash_including( | ||
'message' => a_string_including('invalid base64') | ||
) | ||
} | ||
expect(response).to have_http_status(:bad_request) | ||
expect(response.parsed_body).to include(expected) | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though this looks like gibberish, I sort of liked this because it was a technically a valid image, giving anyone looking for documentation in these specs some idea of what to expect. The shorter, valid base 64 code above may suffice though. @abellotti any thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@imtayadeway
strict_decode64
had some issues with all of the newlines. Apparently it might not have been base64 😳There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that makes sense. So you'd need a really really really long line 😝
Or, you could use this pattern:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or, you could store the pixel as a fixture on the disk? I don't know.....could all be OTP