gitlab-org--gitlab-foss/spec/uploaders/file_uploader_spec.rb

85 lines
2.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
describe FileUploader do
2018-01-29 12:57:34 -05:00
let(:group) { create(:group, name: 'awesome') }
let(:project) { create(:project, namespace: group, name: 'project') }
let(:uploader) { described_class.new(project) }
let(:upload) { double(model: project, path: 'secret/foo.jpg') }
2018-01-29 12:57:34 -05:00
subject { uploader }
2018-01-29 12:57:34 -05:00
shared_examples 'builds correct legacy storage paths' do
include_examples 'builds correct paths',
store_dir: %r{awesome/project/\h+},
absolute_path: %r{#{described_class.root}/awesome/project/secret/foo.jpg}
end
2018-01-29 12:57:34 -05:00
shared_examples 'uses hashed storage' do
context 'when rolled out attachments' do
2018-01-29 12:57:34 -05:00
before do
allow(project).to receive(:disk_path).and_return('ca/fe/fe/ed')
end
2018-01-29 12:57:34 -05:00
let(:project) { build_stubbed(:project, :hashed, namespace: group, name: 'project') }
2018-01-29 12:57:34 -05:00
it_behaves_like 'builds correct paths',
store_dir: %r{ca/fe/fe/ed/\h+},
absolute_path: %r{#{described_class.root}/ca/fe/fe/ed/secret/foo.jpg}
end
context 'when only repositories are rolled out' do
2018-01-29 12:57:34 -05:00
let(:project) { build_stubbed(:project, namespace: group, name: 'project', storage_version: Project::HASHED_STORAGE_FEATURES[:repository]) }
2018-01-29 12:57:34 -05:00
it_behaves_like 'builds correct legacy storage paths'
end
end
2018-01-29 12:57:34 -05:00
context 'legacy storage' do
it_behaves_like 'builds correct legacy storage paths'
include_examples 'uses hashed storage'
end
2016-08-18 10:31:44 -04:00
2018-01-29 12:57:34 -05:00
describe 'initialize' do
let(:uploader) { described_class.new(double, secret: 'secret') }
2016-08-18 10:31:44 -04:00
2018-01-29 12:57:34 -05:00
it 'accepts a secret parameter' do
expect(described_class).not_to receive(:generate_secret)
expect(uploader.secret).to eq('secret')
2016-08-18 10:31:44 -04:00
end
end
2018-01-29 12:57:34 -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')
end
end
2018-01-30 14:38:10 -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
context 'uploader_context is empty' do
it 'fallbacks to regex based extraction' do
expect(upload).to receive(:uploader_context).and_return({})
uploader.upload = upload
expect(uploader.secret).to eq(secret)
expect(uploader.instance_variable_get(:@identifier)).to eq('file.txt')
end
end
end
end