2016-07-12 13:28:39 -04:00
|
|
|
require 'spec_helper'
|
2016-04-03 01:00:06 -04:00
|
|
|
|
|
|
|
describe FileUploader do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:uploader) { described_class.new(build_stubbed(:project)) }
|
2016-04-03 01:00:06 -04:00
|
|
|
|
2017-10-30 09:30:41 -04:00
|
|
|
context 'legacy storage' do
|
|
|
|
let(:project) { build_stubbed(:project) }
|
2017-02-28 13:34:43 -05:00
|
|
|
|
2017-10-30 09:30:41 -04:00
|
|
|
describe '.absolute_path' do
|
|
|
|
it 'returns the correct absolute path by building it dynamically' do
|
|
|
|
upload = double(model: project, path: 'secret/foo.jpg')
|
2017-02-28 13:34:43 -05:00
|
|
|
|
2017-10-30 09:30:41 -04:00
|
|
|
dynamic_segment = project.full_path
|
|
|
|
|
|
|
|
expect(described_class.absolute_path(upload))
|
|
|
|
.to end_with("#{dynamic_segment}/secret/foo.jpg")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#store_dir" do
|
|
|
|
it "stores in the namespace path" do
|
|
|
|
uploader = described_class.new(project)
|
|
|
|
|
|
|
|
expect(uploader.store_dir).to include(project.full_path)
|
|
|
|
expect(uploader.store_dir).not_to include("system")
|
|
|
|
end
|
2017-02-28 13:34:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-30 09:30:41 -04:00
|
|
|
context 'hashed storage' do
|
2017-11-21 12:34:00 -05:00
|
|
|
context 'when rolled out attachments' do
|
|
|
|
let(:project) { build_stubbed(:project, :hashed) }
|
2017-10-30 09:30:41 -04:00
|
|
|
|
2017-11-21 12:34:00 -05:00
|
|
|
describe '.absolute_path' do
|
|
|
|
it 'returns the correct absolute path by building it dynamically' do
|
|
|
|
upload = double(model: project, path: 'secret/foo.jpg')
|
2017-10-30 09:30:41 -04:00
|
|
|
|
2017-11-21 12:34:00 -05:00
|
|
|
dynamic_segment = project.disk_path
|
2017-10-30 09:30:41 -04:00
|
|
|
|
2017-11-21 12:34:00 -05:00
|
|
|
expect(described_class.absolute_path(upload))
|
|
|
|
.to end_with("#{dynamic_segment}/secret/foo.jpg")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#store_dir" do
|
|
|
|
it "stores in the namespace path" do
|
|
|
|
uploader = described_class.new(project)
|
|
|
|
|
|
|
|
expect(uploader.store_dir).to include(project.disk_path)
|
|
|
|
expect(uploader.store_dir).not_to include("system")
|
|
|
|
end
|
2017-10-30 09:30:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-21 12:34:00 -05:00
|
|
|
context 'when only repositories are rolled out' do
|
|
|
|
let(:project) { build_stubbed(:project, storage_version: Project::HASHED_STORAGE_FEATURES[:repository]) }
|
2017-06-07 23:32:38 -04:00
|
|
|
|
2017-11-21 12:34:00 -05:00
|
|
|
describe '.absolute_path' do
|
|
|
|
it 'returns the correct absolute path by building it dynamically' do
|
|
|
|
upload = double(model: project, path: 'secret/foo.jpg')
|
|
|
|
|
|
|
|
dynamic_segment = project.full_path
|
|
|
|
|
|
|
|
expect(described_class.absolute_path(upload))
|
|
|
|
.to end_with("#{dynamic_segment}/secret/foo.jpg")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#store_dir" do
|
|
|
|
it "stores in the namespace path" do
|
|
|
|
uploader = described_class.new(project)
|
|
|
|
|
|
|
|
expect(uploader.store_dir).to include(project.full_path)
|
|
|
|
expect(uploader.store_dir).not_to include("system")
|
|
|
|
end
|
2017-10-30 09:30:41 -04:00
|
|
|
end
|
2017-06-07 23:32:38 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
describe 'initialize' do
|
|
|
|
it 'generates a secret if none is provided' do
|
|
|
|
expect(SecureRandom).to receive(:hex).and_return('secret')
|
2016-04-03 01:00:06 -04:00
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
uploader = described_class.new(double)
|
2016-07-12 13:28:39 -04:00
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
expect(uploader.secret).to eq 'secret'
|
2016-07-12 13:28:39 -04:00
|
|
|
end
|
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
it 'accepts a secret parameter' do
|
|
|
|
expect(SecureRandom).not_to receive(:hex)
|
2016-07-12 13:28:39 -04:00
|
|
|
|
2017-02-23 16:54:25 -05:00
|
|
|
uploader = described_class.new(double, 'secret')
|
2016-07-12 13:28:39 -04:00
|
|
|
|
2017-02-23 16:54:25 -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
|
|
|
|
|
|
|
describe '#move_to_cache' do
|
|
|
|
it 'is true' do
|
2017-02-23 16:54:25 -05:00
|
|
|
expect(uploader.move_to_cache).to eq(true)
|
2016-08-18 10:31:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#move_to_store' do
|
|
|
|
it 'is true' do
|
2017-02-23 16:54:25 -05:00
|
|
|
expect(uploader.move_to_store).to eq(true)
|
2016-08-18 10:31:44 -04:00
|
|
|
end
|
|
|
|
end
|
2017-02-28 13:34:43 -05:00
|
|
|
|
|
|
|
describe '#relative_path' do
|
|
|
|
it 'removes the leading dynamic path segment' do
|
|
|
|
fixture = Rails.root.join('spec', 'fixtures', 'rails_sample.jpg')
|
|
|
|
uploader.store!(fixture_file_upload(fixture))
|
|
|
|
|
|
|
|
expect(uploader.relative_path).to match(/\A\h{32}\/rails_sample.jpg\z/)
|
|
|
|
end
|
|
|
|
end
|
2016-04-03 01:00:06 -04:00
|
|
|
end
|