]> cat aescling's git repositories - mastodon.git/blob - spec/controllers/api/v1/media_controller_spec.rb
Update Mastodon to Rails 6.1 (#15910)
[mastodon.git] / spec / controllers / api / v1 / media_controller_spec.rb
1 require 'rails_helper'
2
3 RSpec.describe Api::V1::MediaController, type: :controller do
4 render_views
5
6 let(:user) { Fabricate(:user, account: Fabricate(:account, username: 'alice')) }
7 let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: 'write:media') }
8
9 before do
10 allow(controller).to receive(:doorkeeper_token) { token }
11 end
12
13 describe 'POST #create' do
14 describe 'with paperclip errors' do
15 context 'when imagemagick cant identify the file type' do
16 before do
17 expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError)
18 post :create, params: { file: fixture_file_upload('attachment.jpg', 'image/jpeg') }
19 end
20
21 it 'returns http 422' do
22 expect(response).to have_http_status(:unprocessable_entity)
23 end
24 end
25
26 context 'when there is a generic error' do
27 before do
28 expect_any_instance_of(Account).to receive_message_chain(:media_attachments, :create!).and_raise(Paperclip::Error)
29 post :create, params: { file: fixture_file_upload('attachment.jpg', 'image/jpeg') }
30 end
31
32 it 'returns http 422' do
33 expect(response).to have_http_status(500)
34 end
35 end
36 end
37
38 context 'image/jpeg' do
39 before do
40 post :create, params: { file: fixture_file_upload('attachment.jpg', 'image/jpeg') }
41 end
42
43 it 'returns http success' do
44 expect(response).to have_http_status(200)
45 end
46
47 it 'creates a media attachment' do
48 expect(MediaAttachment.first).to_not be_nil
49 end
50
51 it 'uploads a file' do
52 expect(MediaAttachment.first).to have_attached_file(:file)
53 end
54
55 it 'returns media ID in JSON' do
56 expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
57 end
58 end
59
60 context 'image/gif' do
61 before do
62 post :create, params: { file: fixture_file_upload('attachment.gif', 'image/gif') }
63 end
64
65 it 'returns http success' do
66 expect(response).to have_http_status(200)
67 end
68
69 it 'creates a media attachment' do
70 expect(MediaAttachment.first).to_not be_nil
71 end
72
73 it 'uploads a file' do
74 expect(MediaAttachment.first).to have_attached_file(:file)
75 end
76
77 it 'returns media ID in JSON' do
78 expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
79 end
80 end
81
82 context 'video/webm' do
83 before do
84 post :create, params: { file: fixture_file_upload('attachment.webm', 'video/webm') }
85 end
86
87 it do
88 # returns http success
89 expect(response).to have_http_status(200)
90
91 # creates a media attachment
92 expect(MediaAttachment.first).to_not be_nil
93
94 # uploads a file
95 expect(MediaAttachment.first).to have_attached_file(:file)
96
97 # returns media ID in JSON
98 expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
99 end
100 end
101 end
102
103 describe 'PUT #update' do
104 context 'when somebody else\'s' do
105 let(:media) { Fabricate(:media_attachment, status: nil) }
106
107 it 'returns http not found' do
108 put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
109 expect(response).to have_http_status(:not_found)
110 end
111 end
112
113 context 'when not attached to a status' do
114 let(:media) { Fabricate(:media_attachment, status: nil, account: user.account) }
115
116 it 'updates the description' do
117 put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
118 expect(media.reload.description).to eq 'Lorem ipsum!!!'
119 end
120 end
121
122 context 'when attached to a status' do
123 let(:media) { Fabricate(:media_attachment, status: Fabricate(:status), account: user.account) }
124
125 it 'returns http not found' do
126 put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
127 expect(response).to have_http_status(:not_found)
128 end
129 end
130 end
131 end
This page took 0.131323 seconds and 4 git commands to generate.