2019-07-31 22:56:46 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2023-07-09 08:55:56 +05:30
|
|
|
RSpec.describe SendFileUpload, feature_category: :user_profile do
|
2018-05-09 12:01:36 +05:30
|
|
|
let(:uploader_class) do
|
|
|
|
Class.new(GitlabUploader) do
|
|
|
|
include ObjectStorage::Concern
|
|
|
|
|
2023-05-27 22:25:52 +05:30
|
|
|
storage_location :uploads
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# user/:id
|
|
|
|
def dynamic_segment
|
2019-09-04 21:01:54 +05:30
|
|
|
File.join(model.class.underscore, model.id.to_s)
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
let(:cdn_uploader_class) do
|
|
|
|
Class.new(uploader_class) do
|
|
|
|
include ObjectStorage::CDN::Concern
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
let(:controller_class) do
|
|
|
|
Class.new do
|
|
|
|
include SendFileUpload
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
def params
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_user; end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:object) { build_stubbed(:user) }
|
|
|
|
let(:uploader) { uploader_class.new(object, :file) }
|
|
|
|
|
|
|
|
describe '#send_upload' do
|
|
|
|
let(:controller) { controller_class.new }
|
|
|
|
let(:temp_file) { Tempfile.new('test') }
|
2018-12-13 13:39:08 +05:30
|
|
|
let(:params) { {} }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
subject { controller.send_upload(uploader, **params) }
|
2018-05-09 12:01:36 +05:30
|
|
|
|
|
|
|
before do
|
|
|
|
FileUtils.touch(temp_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
after do
|
|
|
|
FileUtils.rm_f(temp_file)
|
|
|
|
end
|
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
shared_examples 'handles image resize requests' do |mount|
|
2020-10-24 23:57:45 +05:30
|
|
|
let(:headers) { double }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:image_requester) { build(:user) }
|
|
|
|
let(:image_owner) { build(:user) }
|
2023-04-23 21:23:45 +05:30
|
|
|
let(:width) { mount == :pwa_icon ? 192 : 64 }
|
2020-11-24 15:15:51 +05:30
|
|
|
let(:params) do
|
|
|
|
{ attachment: 'avatar.png' }
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
before do
|
2020-11-24 15:15:51 +05:30
|
|
|
allow(uploader).to receive(:image_safe_for_scaling?).and_return(true)
|
2023-04-23 21:23:45 +05:30
|
|
|
allow(uploader).to receive(:mounted_as).and_return(mount)
|
2020-10-24 23:57:45 +05:30
|
|
|
|
|
|
|
allow(controller).to receive(:headers).and_return(headers)
|
|
|
|
# both of these are valid cases, depending on whether we are dealing with
|
|
|
|
# local or remote files
|
|
|
|
allow(controller).to receive(:send_file)
|
|
|
|
allow(controller).to receive(:redirect_to)
|
2020-11-24 15:15:51 +05:30
|
|
|
|
|
|
|
allow(controller).to receive(:current_user).and_return(image_requester)
|
|
|
|
allow(uploader).to receive(:model).and_return(image_owner)
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with valid width parameter' do
|
|
|
|
it 'renders OK with workhorse command header' do
|
|
|
|
expect(controller).not_to receive(:send_file)
|
2023-04-23 21:23:45 +05:30
|
|
|
expect(controller).to receive(:params).at_least(:once).and_return(width: width.to_s)
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(controller).to receive(:head).with(:ok)
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expect(Gitlab::Workhorse).to receive(:send_scaled_image)
|
2023-04-23 21:23:45 +05:30
|
|
|
.with(a_string_matching('^(/.+|https://.+)'), width, 'image/png')
|
2022-11-25 23:54:43 +05:30
|
|
|
.and_return([Gitlab::Workhorse::SEND_DATA_HEADER, "send-scaled-img:faux"])
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(headers).to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, "send-scaled-img:faux")
|
|
|
|
|
|
|
|
subject
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with missing width parameter' do
|
|
|
|
it 'does not write workhorse command header' do
|
|
|
|
expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/)
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
subject
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with invalid width parameter' do
|
|
|
|
it 'does not write workhorse command header' do
|
|
|
|
expect(controller).to receive(:params).at_least(:once).and_return(width: 'not a number')
|
|
|
|
expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/)
|
|
|
|
|
|
|
|
subject
|
2020-10-24 23:57:45 +05:30
|
|
|
end
|
2020-11-24 15:15:51 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2020-11-24 15:15:51 +05:30
|
|
|
context 'with width that is not allowed' do
|
2020-10-24 23:57:45 +05:30
|
|
|
it 'does not write workhorse command header' do
|
2020-11-24 15:15:51 +05:30
|
|
|
expect(controller).to receive(:params).at_least(:once).and_return(width: '63')
|
|
|
|
expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when image file is not an avatar' do
|
|
|
|
it 'does not write workhorse command header' do
|
|
|
|
expect(uploader).to receive(:mounted_as).and_return(nil) # FileUploader is not mounted
|
|
|
|
expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when image file type is not considered safe for scaling' do
|
|
|
|
it 'does not write workhorse command header' do
|
|
|
|
expect(uploader).to receive(:image_safe_for_scaling?).and_return(false)
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(headers).not_to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-scaled-img:/)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'when local file is used' do
|
|
|
|
before do
|
|
|
|
uploader.store!(temp_file)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends a file' do
|
|
|
|
expect(controller).to receive(:send_file).with(uploader.path, anything)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
it_behaves_like 'handles image resize requests', :avatar
|
|
|
|
it_behaves_like 'handles image resize requests', :pwa_icon
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
context 'with inline image' do
|
|
|
|
let(:filename) { 'test.png' }
|
|
|
|
let(:params) { { disposition: 'inline', attachment: filename } }
|
|
|
|
|
|
|
|
it 'sends a file with inline disposition' do
|
|
|
|
expected_params = {
|
|
|
|
filename: 'test.png',
|
2020-03-13 15:44:24 +05:30
|
|
|
disposition: 'inline'
|
2019-03-02 22:35:43 +05:30
|
|
|
}
|
|
|
|
expect(controller).to receive(:send_file).with(uploader.path, expected_params)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
context 'with attachment' do
|
2019-03-02 22:35:43 +05:30
|
|
|
let(:filename) { 'test.js' }
|
|
|
|
let(:params) { { attachment: filename } }
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'sends a file with content-type of text/plain' do
|
|
|
|
expected_params = {
|
|
|
|
content_type: 'text/plain',
|
|
|
|
filename: 'test.js',
|
2020-03-13 15:44:24 +05:30
|
|
|
disposition: 'attachment'
|
2018-11-08 19:23:39 +05:30
|
|
|
}
|
|
|
|
expect(controller).to receive(:send_file).with(uploader.path, expected_params)
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a proxied file in object storage' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(uploader: uploader_class)
|
|
|
|
uploader.object_store = ObjectStorage::Store::REMOTE
|
|
|
|
uploader.store!(temp_file)
|
|
|
|
allow(Gitlab.config.uploads.object_store).to receive(:proxy_download) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends a file with a custom type' do
|
|
|
|
headers = double
|
2019-09-04 21:01:54 +05:30
|
|
|
expected_headers = /response-content-disposition=attachment%3B%20filename%3D%22test.js%22%3B%20filename%2A%3DUTF-8%27%27test.js&response-content-type=application%2Fjavascript/
|
2018-11-20 20:47:30 +05:30
|
|
|
expect(Gitlab::Workhorse).to receive(:send_url).with(expected_headers).and_call_original
|
|
|
|
expect(headers).to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-url:/)
|
|
|
|
|
|
|
|
expect(controller).not_to receive(:send_file)
|
|
|
|
expect(controller).to receive(:headers) { headers }
|
|
|
|
expect(controller).to receive(:head).with(:ok)
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
subject
|
2018-11-20 20:47:30 +05:30
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'when remote file is used' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(uploader: uploader_class)
|
|
|
|
uploader.object_store = ObjectStorage::Store::REMOTE
|
|
|
|
uploader.store!(temp_file)
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
shared_examples 'proxied file' do
|
2018-05-09 12:01:36 +05:30
|
|
|
it 'sends a file' do
|
|
|
|
headers = double
|
2018-11-20 20:47:30 +05:30
|
|
|
expect(Gitlab::Workhorse).not_to receive(:send_url).with(/response-content-disposition/)
|
|
|
|
expect(Gitlab::Workhorse).not_to receive(:send_url).with(/response-content-type/)
|
|
|
|
expect(Gitlab::Workhorse).to receive(:send_url).and_call_original
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
expect(headers).to receive(:store).with(Gitlab::Workhorse::SEND_DATA_HEADER, /^send-url:/)
|
2018-11-20 20:47:30 +05:30
|
|
|
expect(controller).not_to receive(:send_file)
|
2018-05-09 12:01:36 +05:30
|
|
|
expect(controller).to receive(:headers) { headers }
|
|
|
|
expect(controller).to receive(:head).with(:ok)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-13 13:39:08 +05:30
|
|
|
context 'and proxying is enabled' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.uploads.object_store).to receive(:proxy_download) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'proxied file'
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
context 'and proxying is disabled' do
|
|
|
|
before do
|
|
|
|
allow(Gitlab.config.uploads.object_store).to receive(:proxy_download) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends a file' do
|
|
|
|
expect(controller).to receive(:redirect_to).with(/#{uploader.path}/)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2018-12-13 13:39:08 +05:30
|
|
|
|
|
|
|
context 'with proxy requested' do
|
|
|
|
let(:params) { { proxy: true } }
|
|
|
|
|
|
|
|
it_behaves_like 'proxied file'
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2020-10-24 23:57:45 +05:30
|
|
|
|
2023-04-23 21:23:45 +05:30
|
|
|
it_behaves_like 'handles image resize requests', :avatar
|
|
|
|
it_behaves_like 'handles image resize requests', :pwa_icon
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
context 'when CDN-enabled remote file is used' do
|
|
|
|
let(:uploader) { cdn_uploader_class.new(object, :file) }
|
|
|
|
let(:request) { instance_double('ActionDispatch::Request', remote_ip: '18.245.0.42') }
|
|
|
|
let(:signed_url) { 'https://cdn.example.org.test' }
|
|
|
|
let(:cdn_provider) { instance_double('ObjectStorage::CDN::GoogleCDN', signed_url: signed_url) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage(uploader: cdn_uploader_class)
|
|
|
|
uploader.object_store = ObjectStorage::Store::REMOTE
|
|
|
|
uploader.store!(temp_file)
|
|
|
|
allow(Gitlab.config.uploads.object_store).to receive(:proxy_download) { false }
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sends a file when CDN URL' do
|
|
|
|
expect(uploader).to receive(:use_cdn?).and_return(true)
|
|
|
|
expect(uploader).to receive(:cdn_provider).and_return(cdn_provider)
|
|
|
|
expect(controller).to receive(:request).and_return(request)
|
|
|
|
expect(controller).to receive(:redirect_to).with(signed_url)
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
|
|
|
end
|
2018-05-09 12:01:36 +05:30
|
|
|
end
|
|
|
|
end
|