media_controller_spec.rb 3.8 KB

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