From 48f4e5444d1676414e7cf8e1344fab68fc61644b Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Tue, 27 Aug 2024 05:44:16 -0400 Subject: [PATCH] Convert `media_proxy` controller spec to request spec (#31600) --- .../media_proxy_controller_spec.rb | 42 ------------ spec/requests/media_proxy_spec.rb | 67 +++++++++++++++++++ 2 files changed, 67 insertions(+), 42 deletions(-) delete mode 100644 spec/controllers/media_proxy_controller_spec.rb create mode 100644 spec/requests/media_proxy_spec.rb diff --git a/spec/controllers/media_proxy_controller_spec.rb b/spec/controllers/media_proxy_controller_spec.rb deleted file mode 100644 index 32510cf43d..0000000000 --- a/spec/controllers/media_proxy_controller_spec.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -require 'rails_helper' - -describe MediaProxyController do - render_views - - before do - stub_request(:get, 'http://example.com/attachment.png').to_return(request_fixture('avatar.txt')) - end - - describe '#show' do - it 'redirects when attached to a status' do - status = Fabricate(:status) - media_attachment = Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') - get :show, params: { id: media_attachment.id } - - expect(response).to have_http_status(302) - end - - it 'responds with missing when there is not an attached status' do - media_attachment = Fabricate(:media_attachment, status: nil, remote_url: 'http://example.com/attachment.png') - get :show, params: { id: media_attachment.id } - - expect(response).to have_http_status(404) - end - - it 'raises when id cant be found' do - get :show, params: { id: 'missing' } - - expect(response).to have_http_status(404) - end - - it 'raises when not permitted to view' do - status = Fabricate(:status, visibility: :direct) - media_attachment = Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') - get :show, params: { id: media_attachment.id } - - expect(response).to have_http_status(404) - end - end -end diff --git a/spec/requests/media_proxy_spec.rb b/spec/requests/media_proxy_spec.rb new file mode 100644 index 0000000000..0524105d90 --- /dev/null +++ b/spec/requests/media_proxy_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require 'rails_helper' + +describe 'Media Proxy' do + describe 'GET /media_proxy/:id' do + before do + integration_session.https! # TODO: Move to global rails_helper for all request specs? + host! Rails.configuration.x.local_domain # TODO: Move to global rails_helper for all request specs? + + stub_request(:get, 'http://example.com/attachment.png').to_return(request_fixture('avatar.txt')) + end + + context 'when attached to a status' do + let(:status) { Fabricate(:status) } + let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') } + + it 'redirects to correct original url' do + get "/media_proxy/#{media_attachment.id}" + + expect(response) + .to have_http_status(302) + .and redirect_to media_attachment.file.url(:original) + end + + it 'redirects to small style url' do + get "/media_proxy/#{media_attachment.id}/small" + + expect(response) + .to have_http_status(302) + .and redirect_to media_attachment.file.url(:small) + end + end + + context 'when there is not an attached status' do + let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') } + + it 'responds with missing' do + get "/media_proxy/#{media_attachment.id}" + + expect(response) + .to have_http_status(404) + end + end + + context 'when id cannot be found' do + it 'responds with missing' do + get '/media_proxy/missing' + + expect(response) + .to have_http_status(404) + end + end + + context 'when not permitted to view' do + let(:status) { Fabricate(:status, visibility: :direct) } + let(:media_attachment) { Fabricate(:media_attachment, status: status, remote_url: 'http://example.com/attachment.png') } + + it 'responds with missing' do + get "/media_proxy/#{media_attachment.id}" + + expect(response) + .to have_http_status(404) + end + end + end +end