Skip to content

Commit

Permalink
Improve spec coverage and clean up api/v1/media controller (mastodon#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mjankowski authored May 31, 2017
1 parent 83435c4 commit 8235623
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/controllers/api/v1/media_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@ 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

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
24 changes: 24 additions & 0 deletions spec/controllers/api/v1/media_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
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') }
Expand Down

0 comments on commit 8235623

Please sign in to comment.