From 9742bccbe76fbd32cc3db8ad889ea7b5b77b8733 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 22 Nov 2023 10:39:34 -0500 Subject: [PATCH] Add coverage for `api/v2/media` endpoint (#28027) --- app/controllers/api/v2/media_controller.rb | 14 +++- spec/requests/api/v2/media_spec.rb | 80 ++++++++++++++++++++-- 2 files changed, 88 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v2/media_controller.rb b/app/controllers/api/v2/media_controller.rb index 72bc69442..36c15165d 100644 --- a/app/controllers/api/v2/media_controller.rb +++ b/app/controllers/api/v2/media_controller.rb @@ -2,12 +2,22 @@ class Api::V2::MediaController < Api::V1::MediaController def create - @media_attachment = current_account.media_attachments.create!({ delay_processing: true }.merge(media_attachment_params)) - render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: @media_attachment.not_processed? ? 202 : 200 + @media_attachment = current_account.media_attachments.create!(media_and_delay_params) + render json: @media_attachment, serializer: REST::MediaAttachmentSerializer, status: status_from_media_processing rescue Paperclip::Errors::NotIdentifiedByImageMagickError render json: file_type_error, status: 422 rescue Paperclip::Error => e Rails.logger.error "#{e.class}: #{e.message}" render json: processing_error, status: 500 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 diff --git a/spec/requests/api/v2/media_spec.rb b/spec/requests/api/v2/media_spec.rb index fc6946be5..990fa5d0b 100644 --- a/spec/requests/api/v2/media_spec.rb +++ b/spec/requests/api/v2/media_spec.rb @@ -9,10 +9,82 @@ RSpec.describe 'Media API', :paperclip_processing do let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } describe 'POST /api/v2/media' do - it 'returns http success' do - post '/api/v2/media', headers: headers, 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) + context 'when small media format attachment is processed immediately' do + let(:params) { { file: fixture_file_upload('attachment-jpg.123456_abcd', 'image/jpeg') } } + + 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