media_proxy_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe 'Media Proxy' do
  4. describe 'GET /media_proxy/:id' do
  5. before { stub_attachment_request }
  6. context 'when attached to a status' do
  7. let(:status) { Fabricate(:status) }
  8. let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') }
  9. it 'redirects to correct original url' do
  10. get "/media_proxy/#{media_attachment.id}"
  11. expect(response)
  12. .to have_http_status(302)
  13. .and redirect_to media_attachment.file.url(:original)
  14. end
  15. it 'redirects to small style url' do
  16. get "/media_proxy/#{media_attachment.id}/small"
  17. expect(response)
  18. .to have_http_status(302)
  19. .and redirect_to media_attachment.file.url(:small)
  20. end
  21. end
  22. context 'when there is not an attached status' do
  23. let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') }
  24. it 'responds with missing' do
  25. get "/media_proxy/#{media_attachment.id}"
  26. expect(response)
  27. .to have_http_status(404)
  28. end
  29. end
  30. context 'when id cannot be found' do
  31. it 'responds with missing' do
  32. get '/media_proxy/missing'
  33. expect(response)
  34. .to have_http_status(404)
  35. end
  36. end
  37. context 'when not permitted to view' do
  38. let(:status) { Fabricate(:status, visibility: :direct) }
  39. let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') }
  40. it 'responds with missing' do
  41. get "/media_proxy/#{media_attachment.id}"
  42. expect(response)
  43. .to have_http_status(404)
  44. end
  45. end
  46. def stub_attachment_request
  47. stub_request(
  48. :get,
  49. 'http://example.com/attachment.png'
  50. )
  51. .to_return(
  52. request_fixture('avatar.txt')
  53. )
  54. end
  55. end
  56. end