From 82356233621300b51b3e2a2c093e9c4107e12e81 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 30 May 2017 21:11:29 -0400 Subject: [PATCH] Improve spec coverage and clean up api/v1/media controller (#3467) --- app/controllers/api/v1/media_controller.rb | 14 ++++++++--- .../api/v1/media_controller_spec.rb | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/media_controller.rb b/app/controllers/api/v1/media_controller.rb index aed3578d7e8..3d7dcef420f 100644 --- a/app/controllers/api/v1/media_controller.rb +++ b/app/controllers/api/v1/media_controller.rb @@ -10,11 +10,11 @@ class Api::V1::MediaController < ApiController respond_to :json def create - @media = MediaAttachment.create!(account: current_user.account, file: media_params[:file]) + @media = current_account.media_attachments.create!(file: media_params[:file]) rescue Paperclip::Errors::NotIdentifiedByImageMagickError - render json: { error: 'File type of uploaded media could not be verified' }, status: 422 + render json: file_type_error, status: 422 rescue Paperclip::Error - render json: { error: 'Error processing thumbnail for uploaded media' }, status: 500 + render json: processing_error, status: 500 end private @@ -22,4 +22,12 @@ class Api::V1::MediaController < ApiController def media_params params.permit(:file) end + + def file_type_error + { error: 'File type of uploaded media could not be verified' } + end + + def processing_error + { error: 'Error processing thumbnail for uploaded media' } + end end diff --git a/spec/controllers/api/v1/media_controller_spec.rb b/spec/controllers/api/v1/media_controller_spec.rb index b1d9798eac2..00dcac95dcc 100644 --- a/spec/controllers/api/v1/media_controller_spec.rb +++ b/spec/controllers/api/v1/media_controller_spec.rb @@ -11,6 +11,30 @@ RSpec.describe Api::V1::MediaController, type: :controller do end describe 'POST #create' do + describe 'with paperclip errors' do + context 'when imagemagick cant identify the file type' do + before do + expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError) + post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') } + end + + it 'returns http 422' do + expect(response).to have_http_status(:unprocessable_entity) + end + end + + context 'when there is a generic error' do + before do + expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Error) + post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') } + end + + it 'returns http 422' do + expect(response).to have_http_status(:error) + end + end + end + context 'image/jpeg' do before do post :create, params: { file: fixture_file_upload('files/attachment.jpg', 'image/jpeg') }