闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.0 KiB

  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) { double acceptable?: true, resource_owner_id: user.id }
  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(:error)
  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(:success)
  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
  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(:success)
  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
  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(:success)
  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
  79. end
  80. end
  81. end
  82. end