Add coverage for `api/v2/media` endpoint (#28027)

This commit is contained in:
Matt Jankowski 2023-11-22 10:39:34 -05:00 committed by GitHub
parent 183afc2465
commit 9742bccbe7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 88 additions and 6 deletions

View File

@ -2,12 +2,22 @@
class Api::V2::MediaController < Api::V1::MediaController class Api::V2::MediaController < Api::V1::MediaController
def create def create
@media_attachment = current_account.media_attachments.create!({ delay_processing: true }.merge(media_attachment_params)) @media_attachment = current_account.media_attachments.create!(media_and_delay_params)
render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: @media_attachment.not_processed? ? 202 : 200 render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: status_from_media_processing
rescue Paperclip::Errors::NotIdentifiedByImageMagickError rescue Paperclip::Errors::NotIdentifiedByImageMagickError
render json: file_type_error, status: 422 render json: file_type_error, status: 422
rescue Paperclip::Error => e rescue Paperclip::Error => e
Rails.logger.error "#{e.class}: #{e.message}" Rails.logger.error "#{e.class}: #{e.message}"
render json: processing_error, status: 500 render json: processing_error, status: 500
end end
private
def media_and_delay_params
{ delay_processing: true }.merge(media_attachment_params)
end
def status_from_media_processing
@media_attachment.not_processed? ? 202 : 200
end
end end

View File

@ -9,10 +9,82 @@ RSpec.describe 'Media API', :paperclip_processing do
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'POST /api/v2/media' do describe 'POST /api/v2/media' do
it 'returns http success' do context 'when small media format attachment is processed immediately' do
post '/api/v2/media', headers: headers, params: { file: fixture_file_upload('attachment-jpg.123456_abcd', 'image/jpeg') } let(:params) { { file: fixture_file_upload('attachment-jpg.123456_abcd', 'image/jpeg') } }
expect(File.exist?(user.account.media_attachments.first.file.path(:small))).to be true
expect(response).to have_http_status(200) it 'returns http success' do
post '/api/v2/media', headers: headers, params: params
expect(File.exist?(user.account.media_attachments.first.file.path(:small)))
.to be true
expect(response)
.to have_http_status(200)
expect(body_as_json)
.to be_a(Hash)
end
end
context 'when large format media attachment has not been processed' do
let(:params) { { file: fixture_file_upload('attachment.webm', 'video/webm') } }
it 'returns http accepted' do
post '/api/v2/media', headers: headers, params: params
expect(File.exist?(user.account.media_attachments.first.file.path(:small)))
.to be true
expect(response)
.to have_http_status(202)
expect(body_as_json)
.to be_a(Hash)
end
end
describe 'when paperclip errors occur' do
let(:media_attachments) { double }
let(:params) { { file: fixture_file_upload('attachment.jpg', 'image/jpeg') } }
before do
allow(User).to receive(:find).with(token.resource_owner_id).and_return(user)
allow(user.account).to receive(:media_attachments).and_return(media_attachments)
end
context 'when imagemagick cannot identify the file type' do
before do
allow(media_attachments).to receive(:create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError)
end
it 'returns http unprocessable entity' do
post '/api/v2/media', headers: headers, params: params
expect(response)
.to have_http_status(422)
expect(body_as_json)
.to be_a(Hash)
.and include(error: /File type/)
end
end
context 'when there is a generic error' do
before do
allow(media_attachments).to receive(:create!).and_raise(Paperclip::Error)
end
it 'returns http 500' do
post '/api/v2/media', headers: headers, params: params
expect(response)
.to have_http_status(500)
expect(body_as_json)
.to be_a(Hash)
.and include(error: /processing/)
end
end
end end
end end
end end