2019-10-14 23:06:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-07-12 13:28:39 -04:00
|
|
|
require 'spec_helper'
|
2016-04-03 01:00:06 -04:00
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe FileUploader do
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:group) { create(:group, name: 'awesome') }
|
2017-12-01 08:58:49 -05:00
|
|
|
let(:project) { create(:project, :legacy_storage, namespace: group, name: 'project') }
|
2019-10-02 20:05:59 -04:00
|
|
|
let(:uploader) { described_class.new(project, :avatar) }
|
2020-03-04 16:07:54 -05:00
|
|
|
let(:upload) { double(model: project, path: "#{secret}/foo.jpg") }
|
|
|
|
let(:secret) { "55dc16aa0edd05693fd98b5051e83321" } # this would be nicer as SecureRandom.hex, but the shared_examples breaks
|
2016-04-03 01:00:06 -04:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
subject { uploader }
|
2017-10-30 09:30:41 -04:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
shared_examples 'builds correct legacy storage paths' do
|
|
|
|
include_examples 'builds correct paths',
|
|
|
|
store_dir: %r{awesome/project/\h+},
|
2018-03-02 10:41:40 -05:00
|
|
|
upload_path: %r{\h+/<filename>},
|
2020-03-04 16:07:54 -05:00
|
|
|
absolute_path: %r{#{described_class.root}/awesome/project/55dc16aa0edd05693fd98b5051e83321/foo.jpg}
|
2017-02-28 13:34:43 -05:00
|
|
|
end
|
|
|
|
|
2018-03-02 10:41:40 -05:00
|
|
|
context 'legacy storage' do
|
|
|
|
it_behaves_like 'builds correct legacy storage paths'
|
2017-12-01 08:58:49 -05:00
|
|
|
|
2018-03-02 10:41:40 -05:00
|
|
|
context 'uses hashed storage' do
|
|
|
|
context 'when rolled out attachments' do
|
|
|
|
let(:project) { build_stubbed(:project, namespace: group, name: 'project') }
|
2017-11-21 12:34:00 -05:00
|
|
|
|
2018-03-02 10:41:40 -05:00
|
|
|
include_examples 'builds correct paths',
|
|
|
|
store_dir: %r{@hashed/\h{2}/\h{2}/\h+},
|
|
|
|
upload_path: %r{\h+/<filename>}
|
|
|
|
end
|
2017-10-30 09:30:41 -04:00
|
|
|
|
2018-03-02 10:41:40 -05:00
|
|
|
context 'when only repositories are rolled out' do
|
|
|
|
let(:project) { build_stubbed(:project, namespace: group, name: 'project', storage_version: Project::HASHED_STORAGE_FEATURES[:repository]) }
|
2017-06-07 23:32:38 -04:00
|
|
|
|
2018-03-02 10:41:40 -05:00
|
|
|
it_behaves_like 'builds correct legacy storage paths'
|
|
|
|
end
|
2018-02-02 08:59:43 -05:00
|
|
|
end
|
|
|
|
end
|
2017-11-21 12:34:00 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
context 'object store is remote' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage
|
|
|
|
end
|
2017-11-21 12:34:00 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
include_context 'with storage', described_class::Store::REMOTE
|
2017-11-21 12:34:00 -05:00
|
|
|
|
2018-03-02 10:41:40 -05:00
|
|
|
# always use hashed storage path for remote uploads
|
|
|
|
it_behaves_like 'builds correct paths',
|
|
|
|
store_dir: %r{@hashed/\h{2}/\h{2}/\h+},
|
|
|
|
upload_path: %r{@hashed/\h{2}/\h{2}/\h+/\h+/<filename>}
|
2017-06-07 23:32:38 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
describe 'initialize' do
|
2020-03-04 16:07:54 -05:00
|
|
|
let(:uploader) { described_class.new(double, secret: secret) }
|
2016-07-12 13:28:39 -04:00
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
it 'accepts a secret parameter' do
|
2018-02-02 08:59:43 -05:00
|
|
|
expect(described_class).not_to receive(:generate_secret)
|
2020-03-04 16:07:54 -05:00
|
|
|
expect(uploader.secret).to eq(secret)
|
2016-07-12 13:28:39 -04:00
|
|
|
end
|
2016-04-03 01:00:06 -04:00
|
|
|
end
|
2016-08-18 10:31:44 -04:00
|
|
|
|
2018-01-30 09:21:28 -05:00
|
|
|
describe 'callbacks' do
|
|
|
|
describe '#prune_store_dir after :remove' do
|
|
|
|
before do
|
|
|
|
uploader.store!(fixture_file_upload('spec/fixtures/doc_sample.txt'))
|
|
|
|
end
|
|
|
|
|
|
|
|
def store_dir
|
|
|
|
File.expand_path(uploader.store_dir, uploader.root)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is called' do
|
|
|
|
expect(uploader).to receive(:prune_store_dir).once
|
|
|
|
|
|
|
|
uploader.remove!
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'prune the store directory' do
|
|
|
|
expect { uploader.remove! }
|
2018-01-31 10:59:35 -05:00
|
|
|
.to change { File.exist?(store_dir) }.from(true).to(false)
|
2018-01-30 09:21:28 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-28 10:57:28 -04:00
|
|
|
describe 'copy_to' do
|
2018-10-29 05:05:47 -04:00
|
|
|
let(:new_project) { create(:project) }
|
|
|
|
let(:moved) { described_class.copy_to(subject, new_project) }
|
|
|
|
|
2018-06-28 10:57:28 -04:00
|
|
|
shared_examples 'returns a valid uploader' do
|
|
|
|
describe 'returned uploader' do
|
|
|
|
it 'generates a new secret' do
|
|
|
|
expect(subject).to be
|
|
|
|
expect(described_class).to receive(:generate_secret).once.and_call_original
|
|
|
|
expect(moved).to be
|
|
|
|
end
|
|
|
|
|
2018-10-29 05:05:47 -04:00
|
|
|
it 'creates new upload correctly' do
|
|
|
|
upload = moved.upload
|
|
|
|
|
|
|
|
expect(upload).not_to eq(subject.upload)
|
|
|
|
expect(upload.model).to eq(new_project)
|
|
|
|
expect(upload.uploader).to eq('FileUploader')
|
|
|
|
expect(upload.secret).not_to eq(subject.upload.secret)
|
2018-06-28 10:57:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'copies the file' do
|
|
|
|
expect(subject.file).to exist
|
|
|
|
expect(moved.file).to exist
|
|
|
|
expect(subject.file).not_to eq(moved.file)
|
|
|
|
expect(subject.object_store).to eq(moved.object_store)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-03 12:54:54 -04:00
|
|
|
context 'files are stored locally' do
|
2018-06-28 10:57:28 -04:00
|
|
|
before do
|
|
|
|
subject.store!(fixture_file_upload('spec/fixtures/dk.png'))
|
|
|
|
end
|
2018-07-03 12:54:54 -04:00
|
|
|
|
|
|
|
include_examples 'returns a valid uploader'
|
2018-10-29 05:05:47 -04:00
|
|
|
|
|
|
|
it 'copies the file to the correct location' do
|
|
|
|
expect(moved.upload.path).to eq("#{moved.upload.secret}/dk.png")
|
|
|
|
expect(moved.file.path).to end_with("public/uploads/#{new_project.disk_path}/#{moved.upload.secret}/dk.png")
|
|
|
|
expect(moved.filename).to eq('dk.png')
|
|
|
|
end
|
2018-06-28 10:57:28 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'files are stored remotely' do
|
|
|
|
before do
|
|
|
|
stub_uploads_object_storage
|
|
|
|
subject.store!(fixture_file_upload('spec/fixtures/dk.png'))
|
|
|
|
subject.migrate!(ObjectStorage::Store::REMOTE)
|
|
|
|
end
|
|
|
|
|
|
|
|
include_examples 'returns a valid uploader'
|
2018-10-29 05:05:47 -04:00
|
|
|
|
|
|
|
it 'copies the file to the correct location' do
|
|
|
|
expect(moved.upload.path).to eq("#{new_project.disk_path}/#{moved.upload.secret}/dk.png")
|
|
|
|
expect(moved.file.path).to eq("#{new_project.disk_path}/#{moved.upload.secret}/dk.png")
|
|
|
|
expect(moved.filename).to eq('dk.png')
|
|
|
|
end
|
2018-06-28 10:57:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-13 10:13:13 -04:00
|
|
|
describe '.extract_dynamic_path' do
|
2020-05-15 05:07:59 -04:00
|
|
|
shared_examples 'a valid secret' do |root_path|
|
|
|
|
context 'with a 32-byte hexadecimal secret' do
|
|
|
|
let(:secret) { SecureRandom.hex }
|
|
|
|
let(:path) { File.join(*[root_path, secret, 'dummy.txt'].compact) }
|
2018-07-13 10:13:13 -04:00
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
it 'extracts the secret' do
|
|
|
|
expect(described_class.extract_dynamic_path(path)[:secret]).to eq(secret)
|
|
|
|
end
|
2020-04-02 17:07:51 -04:00
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
it 'extracts the identifier' do
|
|
|
|
expect(described_class.extract_dynamic_path(path)[:identifier]).to eq('dummy.txt')
|
|
|
|
end
|
2020-04-02 17:07:51 -04:00
|
|
|
end
|
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
context 'with a 10-byte hexadecimal secret' do
|
|
|
|
let(:secret) { SecureRandom.hex[0, 10] }
|
|
|
|
let(:path) { File.join(*[root_path, secret, 'dummy.txt'].compact) }
|
|
|
|
|
|
|
|
it 'extracts the secret' do
|
|
|
|
expect(described_class.extract_dynamic_path(path)[:secret]).to eq(secret)
|
|
|
|
end
|
2020-04-02 17:07:51 -04:00
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
it 'extracts the identifier' do
|
|
|
|
expect(described_class.extract_dynamic_path(path)[:identifier]).to eq('dummy.txt')
|
|
|
|
end
|
2020-04-02 17:07:51 -04:00
|
|
|
end
|
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
context 'with an invalid secret' do
|
|
|
|
let(:secret) { 'foo' }
|
|
|
|
let(:path) { File.join(*[root_path, secret, 'dummy.txt'].compact) }
|
|
|
|
|
|
|
|
it 'returns nil' do
|
|
|
|
expect(described_class.extract_dynamic_path(path)).to be_nil
|
|
|
|
end
|
2020-04-02 17:07:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
context 'with an absolute path without a slash in the beginning' do
|
|
|
|
it_behaves_like 'a valid secret', 'export/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a/test/uploads'
|
|
|
|
end
|
2020-04-02 17:07:51 -04:00
|
|
|
|
2020-05-15 05:07:59 -04:00
|
|
|
context 'with an absolute path with a slash in the beginning' do
|
|
|
|
it_behaves_like 'a valid secret', '/export/4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a/test/uploads'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an relative path without a slash in the beginning' do
|
|
|
|
it_behaves_like 'a valid secret', nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an relative path with a slash in the beginning' do
|
|
|
|
it_behaves_like 'a valid secret', '/'
|
2018-07-13 10:13:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
describe '#secret' do
|
|
|
|
it 'generates a secret if none is provided' do
|
2020-03-04 16:07:54 -05:00
|
|
|
expect(described_class).to receive(:generate_secret).and_return(secret)
|
|
|
|
expect(uploader.secret).to eq(secret)
|
|
|
|
expect(uploader.secret.size).to eq(32)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "validation" do
|
|
|
|
before do
|
|
|
|
uploader.instance_variable_set(:@secret, secret)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "32-byte hexadecimal" do
|
|
|
|
let(:secret) { SecureRandom.hex }
|
|
|
|
|
|
|
|
it "returns the secret" do
|
|
|
|
expect(uploader.secret).to eq(secret)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "10-byte hexadecimal" do
|
2020-05-15 05:07:59 -04:00
|
|
|
let(:secret) { SecureRandom.hex[0, 10] }
|
2020-03-04 16:07:54 -05:00
|
|
|
|
|
|
|
it "returns the secret" do
|
|
|
|
expect(uploader.secret).to eq(secret)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "invalid secret supplied" do
|
|
|
|
let(:secret) { "%2E%2E%2F%2E%2E%2F%2E%2E%2F%2E%2E%2F%2E%2E%2F%2E%2E%2F%2E%2E%2Fgrafana%2Fconf%2F" }
|
|
|
|
|
|
|
|
it "raises an exception" do
|
|
|
|
expect { uploader.secret }.to raise_error(described_class::InvalidSecret)
|
|
|
|
end
|
|
|
|
end
|
2016-08-18 10:31:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
describe "#migrate!" do
|
|
|
|
before do
|
2018-06-05 17:18:06 -04:00
|
|
|
uploader.store!(fixture_file_upload('spec/fixtures/dk.png'))
|
2018-02-02 08:59:43 -05:00
|
|
|
stub_uploads_object_storage
|
2016-08-18 10:31:44 -04:00
|
|
|
end
|
2017-02-28 13:34:43 -05:00
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
it_behaves_like "migrates", to_store: described_class::Store::REMOTE
|
|
|
|
it_behaves_like "migrates", from_store: described_class::Store::REMOTE, to_store: described_class::Store::LOCAL
|
2017-02-28 13:34:43 -05:00
|
|
|
end
|
2018-02-21 11:43:21 -05:00
|
|
|
|
|
|
|
describe '#upload=' do
|
|
|
|
let(:secret) { SecureRandom.hex }
|
|
|
|
let(:upload) { create(:upload, :issuable_upload, secret: secret, filename: 'file.txt') }
|
|
|
|
|
|
|
|
it 'handles nil' do
|
|
|
|
expect(uploader).not_to receive(:apply_context!)
|
|
|
|
|
|
|
|
uploader.upload = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'extract the uploader context from it' do
|
|
|
|
expect(uploader).to receive(:apply_context!).with(a_hash_including(secret: secret, identifier: 'file.txt'))
|
|
|
|
|
|
|
|
uploader.upload = upload
|
|
|
|
end
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-10-02 20:05:59 -04:00
|
|
|
describe '#replace_file_without_saving!' do
|
|
|
|
let(:replacement) { Tempfile.create('replacement.jpg') }
|
|
|
|
|
|
|
|
it 'replaces an existing file without changing its metadata' do
|
|
|
|
expect { subject.replace_file_without_saving! CarrierWave::SanitizedFile.new(replacement) }.not_to change { subject.upload }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
context 'when remote file is used' do
|
|
|
|
let(:temp_file) { Tempfile.new("test") }
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
let!(:fog_connection) do
|
|
|
|
stub_uploads_object_storage(described_class)
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
let(:filename) { "my file.txt" }
|
|
|
|
let(:uploaded_file) do
|
|
|
|
UploadedFile.new(temp_file.path, filename: filename, remote_id: "test/123123")
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
let!(:fog_file) do
|
2020-10-09 11:09:18 -04:00
|
|
|
fog_connection.directories.new(key: 'uploads').files.create( # rubocop:disable Rails/SaveBang
|
2019-06-20 17:01:14 -04:00
|
|
|
key: 'tmp/uploads/test/123123',
|
|
|
|
body: 'content'
|
|
|
|
)
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
before do
|
|
|
|
FileUtils.touch(temp_file)
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
uploader.store!(uploaded_file)
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
after do
|
|
|
|
FileUtils.rm_f(temp_file)
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
|
2019-06-20 17:01:14 -04:00
|
|
|
describe '#cache!' do
|
2018-07-18 10:21:32 -04:00
|
|
|
it 'file is stored remotely in permament location with sanitized name' do
|
|
|
|
expect(uploader).to be_exists
|
|
|
|
expect(uploader).not_to be_cached
|
|
|
|
expect(uploader).not_to be_file_storage
|
|
|
|
expect(uploader.path).not_to be_nil
|
|
|
|
expect(uploader.path).not_to include('tmp/upload')
|
|
|
|
expect(uploader.path).not_to include('tmp/cache')
|
|
|
|
expect(uploader.url).to include('/my_file.txt')
|
|
|
|
expect(uploader.object_store).to eq(described_class::Store::REMOTE)
|
|
|
|
end
|
|
|
|
end
|
2019-06-20 17:01:14 -04:00
|
|
|
|
|
|
|
describe '#to_h' do
|
|
|
|
subject { uploader.to_h }
|
|
|
|
|
|
|
|
let(:filename) { 'my+file.txt' }
|
|
|
|
|
|
|
|
it 'generates URL using original file name instead of filename returned by object storage' do
|
|
|
|
# GCS returns a URL with a `+` instead of `%2B`
|
|
|
|
allow(uploader.file).to receive(:url).and_return('https://storage.googleapis.com/gitlab-test-uploads/@hashed/6b/86/6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b/64c5065e62100b1a12841644256a98be/my+file.txt')
|
|
|
|
|
|
|
|
expect(subject[:url]).to end_with(filename)
|
|
|
|
end
|
|
|
|
end
|
2018-07-18 10:21:32 -04:00
|
|
|
end
|
2016-04-03 01:00:06 -04:00
|
|
|
end
|