media_controller_spec.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. require 'rails_helper'
  2. RSpec.describe Api::V1::MediaController, type: :controller do
  3. render_views
  4. let(:user) { Fabricate(:user) }
  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('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('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('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('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('attachment.webm', 'video/webm') }
  67. end
  68. it do
  69. # returns http success
  70. expect(response).to have_http_status(200)
  71. # creates a media attachment
  72. expect(MediaAttachment.first).to_not be_nil
  73. # uploads a file
  74. expect(MediaAttachment.first).to have_attached_file(:file)
  75. # returns media ID in JSON
  76. expect(body_as_json[:id]).to eq MediaAttachment.first.id.to_s
  77. end
  78. end
  79. end
  80. describe 'PUT #update' do
  81. context 'when somebody else\'s' do
  82. let(:media) { Fabricate(:media_attachment, status: nil) }
  83. it 'returns http not found' do
  84. put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
  85. expect(response).to have_http_status(:not_found)
  86. end
  87. end
  88. context 'when the author \'s' do
  89. let(:status) { nil }
  90. let(:media) { Fabricate(:media_attachment, status: status, account: user.account) }
  91. before do
  92. put :update, params: { id: media.id, description: 'Lorem ipsum!!!' }
  93. end
  94. it 'updates the description' do
  95. expect(media.reload.description).to eq 'Lorem ipsum!!!'
  96. end
  97. context 'when already attached to a status' do
  98. let(:status) { Fabricate(:status, account: user.account) }
  99. it 'returns http not found' do
  100. expect(response).to have_http_status(:not_found)
  101. end
  102. end
  103. end
  104. end
  105. end