media_controller_spec.rb 4.0 KB

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