media_spec.rb 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Media API', :attachment_processing do
  4. let(:user) { Fabricate(:user) }
  5. let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
  6. let(:scopes) { 'write' }
  7. let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
  8. describe 'POST /api/v2/media' do
  9. context 'when small media format attachment is processed immediately' do
  10. let(:params) { { file: fixture_file_upload('attachment-jpg.123456_abcd', 'image/jpeg') } }
  11. it 'returns http success' do
  12. post '/api/v2/media', headers: headers, params: params
  13. expect(File.exist?(user.account.media_attachments.first.file.path(:small)))
  14. .to be true
  15. expect(response)
  16. .to have_http_status(200)
  17. expect(response.content_type)
  18. .to start_with('application/json')
  19. expect(response.parsed_body)
  20. .to be_a(Hash)
  21. end
  22. end
  23. context 'when media description is too long' do
  24. let(:params) do
  25. {
  26. file: fixture_file_upload('attachment-jpg.123456_abcd', 'image/jpeg'),
  27. description: 'aa' * MediaAttachment::MAX_DESCRIPTION_LENGTH,
  28. }
  29. end
  30. it 'returns http error' do
  31. post '/api/v2/media', headers: headers, params: params
  32. expect(response).to have_http_status(422)
  33. expect(response.body).to include 'Description is too long'
  34. end
  35. end
  36. context 'when large format media attachment has not been processed' do
  37. let(:params) { { file: fixture_file_upload('attachment.webm', 'video/webm') } }
  38. it 'returns http accepted' do
  39. post '/api/v2/media', headers: headers, params: params
  40. expect(File.exist?(user.account.media_attachments.first.file.path(:small)))
  41. .to be true
  42. expect(response)
  43. .to have_http_status(202)
  44. expect(response.content_type)
  45. .to start_with('application/json')
  46. expect(response.parsed_body)
  47. .to be_a(Hash)
  48. end
  49. end
  50. describe 'when paperclip errors occur' do
  51. let(:media_attachments) { double }
  52. let(:params) { { file: fixture_file_upload('attachment.jpg', 'image/jpeg') } }
  53. before do
  54. allow(User).to receive(:find).with(token.resource_owner_id).and_return(user)
  55. allow(user.account).to receive(:media_attachments).and_return(media_attachments)
  56. end
  57. context 'when imagemagick cannot identify the file type' do
  58. before do
  59. allow(media_attachments).to receive(:create!).and_raise(Paperclip::Errors::NotIdentifiedByImageMagickError)
  60. end
  61. it 'returns http unprocessable entity' do
  62. post '/api/v2/media', headers: headers, params: params
  63. expect(response)
  64. .to have_http_status(422)
  65. expect(response.content_type)
  66. .to start_with('application/json')
  67. expect(response.parsed_body)
  68. .to be_a(Hash)
  69. .and include(error: /File type/)
  70. end
  71. end
  72. context 'when there is a generic error' do
  73. before do
  74. allow(media_attachments).to receive(:create!).and_raise(Paperclip::Error)
  75. end
  76. it 'returns http 500' do
  77. post '/api/v2/media', headers: headers, params: params
  78. expect(response)
  79. .to have_http_status(500)
  80. expect(response.content_type)
  81. .to start_with('application/json')
  82. expect(response.parsed_body)
  83. .to be_a(Hash)
  84. .and include(error: /processing/)
  85. end
  86. end
  87. end
  88. end
  89. end