2016-07-12 13:28:39 -04:00
|
|
|
require 'spec_helper'
|
2016-04-03 01:00:06 -04:00
|
|
|
|
|
|
|
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') }
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:uploader) { described_class.new(project) }
|
|
|
|
let(:upload) { double(model: project, path: 'secret/foo.jpg') }
|
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>},
|
2018-02-02 08:59:43 -05:00
|
|
|
absolute_path: %r{#{described_class.root}/awesome/project/secret/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
|
2018-01-29 16:06:17 -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)
|
|
|
|
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-02-02 08:59:43 -05:00
|
|
|
describe '#secret' do
|
|
|
|
it 'generates a secret if none is provided' do
|
|
|
|
expect(described_class).to receive(:generate_secret).and_return('secret')
|
|
|
|
expect(uploader.secret).to eq('secret')
|
2016-08-18 10:31:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-02 08:59:43 -05:00
|
|
|
describe "#migrate!" do
|
|
|
|
before do
|
|
|
|
uploader.store!(fixture_file_upload(Rails.root.join('spec/fixtures/dk.png')))
|
|
|
|
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
|
2016-04-03 01:00:06 -04:00
|
|
|
end
|