2019-08-22 10:57:44 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-03 16:47:33 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 18:09:03 +00:00
|
|
|
RSpec.describe UploadedFile do
|
2018-07-18 14:21:32 +00:00
|
|
|
let(:temp_dir) { Dir.tmpdir }
|
2020-04-30 21:09:47 +00:00
|
|
|
let(:temp_file) { Tempfile.new(%w[test test], temp_dir) }
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2018-07-18 14:21:32 +00:00
|
|
|
before do
|
|
|
|
FileUtils.touch(temp_file)
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2018-07-18 14:21:32 +00:00
|
|
|
after do
|
|
|
|
FileUtils.rm_f(temp_file)
|
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'from_params functions' do
|
|
|
|
RSpec.shared_examples 'using the file path' do |filename:, content_type:, sha256:, path_suffix:|
|
|
|
|
it { is_expected.not_to be_nil }
|
|
|
|
|
|
|
|
it 'sets properly the attributes' do
|
|
|
|
expect(subject.original_filename).to eq(filename)
|
|
|
|
expect(subject.content_type).to eq(content_type)
|
|
|
|
expect(subject.sha256).to eq(sha256)
|
|
|
|
expect(subject.remote_id).to be_nil
|
|
|
|
expect(subject.path).to end_with(path_suffix)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles a blank path' do
|
|
|
|
params['file.path'] = ''
|
|
|
|
|
|
|
|
# Not a real file, so can't determine size itself
|
|
|
|
params['file.size'] = 1.byte
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
expect { described_class.from_params(params, :file, upload_path) }
|
|
|
|
.not_to raise_error
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
RSpec.shared_examples 'using the remote id' do |filename:, content_type:, sha256:, size:, remote_id:|
|
|
|
|
it { is_expected.not_to be_nil }
|
|
|
|
|
|
|
|
it 'sets properly the attributes' do
|
|
|
|
expect(subject.original_filename).to eq(filename)
|
|
|
|
expect(subject.content_type).to eq('application/octet-stream')
|
|
|
|
expect(subject.sha256).to eq('sha256')
|
|
|
|
expect(subject.path).to be_nil
|
|
|
|
expect(subject.size).to eq(123456)
|
|
|
|
expect(subject.remote_id).to eq('1234567890')
|
|
|
|
end
|
2018-07-18 14:21:32 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
describe '.from_params_without_field' do
|
|
|
|
let(:upload_path) { nil }
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
after do
|
|
|
|
FileUtils.rm_r(upload_path) if upload_path
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
subject do
|
|
|
|
described_class.from_params_without_field(params, [upload_path, Dir.tmpdir])
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when valid file is specified' do
|
|
|
|
context 'only local path is specified' do
|
|
|
|
let(:params) { { 'path' => temp_file.path } }
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it { is_expected.not_to be_nil }
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it 'generates filename from path' do
|
|
|
|
expect(subject.original_filename).to eq(::File.basename(temp_file.path))
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'all parameters are specified' do
|
|
|
|
context 'with a filepath' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'path' => temp_file.path,
|
|
|
|
'name' => 'dir/my file&.txt',
|
|
|
|
'type' => 'my/type',
|
|
|
|
'sha256' => 'sha256' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the file path',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'my/type',
|
|
|
|
sha256: 'sha256',
|
|
|
|
path_suffix: 'test'
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'with a remote id' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'name' => 'dir/my file&.txt',
|
|
|
|
'sha256' => 'sha256',
|
|
|
|
'remote_url' => 'http://localhost/file',
|
|
|
|
'remote_id' => '1234567890',
|
|
|
|
'etag' => 'etag1234567890',
|
|
|
|
'size' => '123456'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the remote id',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'application/octet-stream',
|
|
|
|
sha256: 'sha256',
|
|
|
|
size: 123456,
|
|
|
|
remote_id: '1234567890'
|
|
|
|
end
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'with a path and a remote id' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'path' => temp_file.path,
|
|
|
|
'name' => 'dir/my file&.txt',
|
|
|
|
'sha256' => 'sha256',
|
|
|
|
'remote_url' => 'http://localhost/file',
|
|
|
|
'remote_id' => '1234567890',
|
|
|
|
'etag' => 'etag1234567890',
|
|
|
|
'size' => '123456'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the remote id',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'application/octet-stream',
|
|
|
|
sha256: 'sha256',
|
|
|
|
size: 123456,
|
|
|
|
remote_id: '1234567890'
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
2020-09-18 12:09:50 +00:00
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when no params are specified' do
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
it 'does not return an object' do
|
|
|
|
is_expected.to be_nil
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
2020-09-18 12:09:50 +00:00
|
|
|
end
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when verifying allowed paths' do
|
|
|
|
let(:params) { { 'path' => temp_file.path } }
|
|
|
|
|
|
|
|
context 'when file is stored in system temporary folder' do
|
|
|
|
let(:temp_dir) { Dir.tmpdir }
|
2020-04-30 21:09:47 +00:00
|
|
|
|
|
|
|
it { is_expected.not_to be_nil }
|
2020-09-18 12:09:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when file is stored in user provided upload path' do
|
|
|
|
let(:upload_path) { Dir.mktmpdir }
|
|
|
|
let(:temp_dir) { upload_path }
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it { is_expected.not_to be_nil }
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when file is stored outside of user provided upload path' do
|
|
|
|
let!(:generated_dir) { Dir.mktmpdir }
|
|
|
|
let!(:temp_dir) { Dir.mktmpdir }
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
before do
|
|
|
|
# We overwrite default temporary path
|
|
|
|
allow(Dir).to receive(:tmpdir).and_return(generated_dir)
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it 'raises an error' do
|
|
|
|
expect { subject }.to raise_error(UploadedFile::InvalidPathError, /insecure path used/)
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
2020-09-18 12:09:50 +00:00
|
|
|
end
|
|
|
|
end
|
2020-03-25 18:08:10 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
describe '.from_params' do
|
|
|
|
let(:upload_path) { nil }
|
|
|
|
let(:file_path_override) { nil }
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
after do
|
|
|
|
FileUtils.rm_r(upload_path) if upload_path
|
|
|
|
end
|
|
|
|
|
|
|
|
subject do
|
|
|
|
described_class.from_params(params, :file, [upload_path, Dir.tmpdir], file_path_override)
|
|
|
|
end
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
RSpec.shared_context 'filepath override' do
|
|
|
|
let(:temp_file_override) { Tempfile.new(%w[override override], temp_dir) }
|
|
|
|
let(:file_path_override) { temp_file_override.path }
|
|
|
|
|
|
|
|
before do
|
|
|
|
FileUtils.touch(temp_file_override)
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
2020-03-25 18:08:10 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
after do
|
|
|
|
FileUtils.rm_f(temp_file_override)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when valid file is specified' do
|
|
|
|
context 'only local path is specified' do
|
2020-04-30 21:09:47 +00:00
|
|
|
let(:params) do
|
2020-09-18 12:09:50 +00:00
|
|
|
{ 'file.path' => temp_file.path }
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it { is_expected.not_to be_nil }
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it "generates filename from path" do
|
|
|
|
expect(subject.original_filename).to eq(::File.basename(temp_file.path))
|
|
|
|
end
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
2020-03-25 18:08:10 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'all parameters are specified' do
|
|
|
|
context 'with a filepath' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'file.path' => temp_file.path,
|
|
|
|
'file.name' => 'dir/my file&.txt',
|
|
|
|
'file.type' => 'my/type',
|
|
|
|
'file.sha256' => 'sha256' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the file path',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'my/type',
|
|
|
|
sha256: 'sha256',
|
|
|
|
path_suffix: 'test'
|
|
|
|
end
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'with a filepath override' do
|
|
|
|
include_context 'filepath override'
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{ 'file.path' => temp_file.path,
|
|
|
|
'file.name' => 'dir/my file&.txt',
|
|
|
|
'file.type' => 'my/type',
|
|
|
|
'file.sha256' => 'sha256' }
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the file path',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'my/type',
|
|
|
|
sha256: 'sha256',
|
|
|
|
path_suffix: 'override'
|
2020-04-30 21:09:47 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'with a remote id' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'file.name' => 'dir/my file&.txt',
|
|
|
|
'file.sha256' => 'sha256',
|
|
|
|
'file.remote_url' => 'http://localhost/file',
|
|
|
|
'file.remote_id' => '1234567890',
|
|
|
|
'file.etag' => 'etag1234567890',
|
|
|
|
'file.size' => '123456'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the remote id',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'application/octet-stream',
|
|
|
|
sha256: 'sha256',
|
|
|
|
size: 123456,
|
|
|
|
remote_id: '1234567890'
|
|
|
|
end
|
2020-04-30 21:09:47 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'with a path and a remote id' do
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'file.path' => temp_file.path,
|
|
|
|
'file.name' => 'dir/my file&.txt',
|
|
|
|
'file.sha256' => 'sha256',
|
|
|
|
'file.remote_url' => 'http://localhost/file',
|
|
|
|
'file.remote_id' => '1234567890',
|
|
|
|
'file.etag' => 'etag1234567890',
|
|
|
|
'file.size' => '123456'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the remote id',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'application/octet-stream',
|
|
|
|
sha256: 'sha256',
|
|
|
|
size: 123456,
|
|
|
|
remote_id: '1234567890'
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a path override and a remote id' do
|
|
|
|
include_context 'filepath override'
|
|
|
|
|
|
|
|
let(:params) do
|
|
|
|
{
|
|
|
|
'file.name' => 'dir/my file&.txt',
|
|
|
|
'file.sha256' => 'sha256',
|
|
|
|
'file.remote_url' => 'http://localhost/file',
|
|
|
|
'file.remote_id' => '1234567890',
|
|
|
|
'file.etag' => 'etag1234567890',
|
|
|
|
'file.size' => '123456'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'using the remote id',
|
|
|
|
filename: 'my_file_.txt',
|
|
|
|
content_type: 'application/octet-stream',
|
|
|
|
sha256: 'sha256',
|
|
|
|
size: 123456,
|
|
|
|
remote_id: '1234567890'
|
|
|
|
end
|
2020-03-25 18:08:10 +00:00
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when no params are specified' do
|
|
|
|
let(:params) do
|
|
|
|
{}
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it "does not return an object" do
|
|
|
|
is_expected.to be_nil
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when verifying allowed paths' do
|
|
|
|
let(:params) do
|
|
|
|
{ 'file.path' => temp_file.path }
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when file is stored in system temporary folder' do
|
|
|
|
let(:temp_dir) { Dir.tmpdir }
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it "succeeds" do
|
|
|
|
is_expected.not_to be_nil
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when file is stored in user provided upload path' do
|
|
|
|
let(:upload_path) { Dir.mktmpdir }
|
|
|
|
let(:temp_dir) { upload_path }
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it "succeeds" do
|
|
|
|
is_expected.not_to be_nil
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
context 'when file is stored outside of user provided upload path' do
|
|
|
|
let!(:generated_dir) { Dir.mktmpdir }
|
|
|
|
let!(:temp_dir) { Dir.mktmpdir }
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
before do
|
|
|
|
# We overwrite default temporary path
|
|
|
|
allow(Dir).to receive(:tmpdir).and_return(generated_dir)
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
|
2020-09-18 12:09:50 +00:00
|
|
|
it "raises an error" do
|
|
|
|
expect { subject }.to raise_error(UploadedFile::InvalidPathError, /insecure path used/)
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-07-18 14:21:32 +00:00
|
|
|
|
2019-10-18 11:11:44 +00:00
|
|
|
describe '.initialize' do
|
|
|
|
context 'when no size is provided' do
|
|
|
|
it 'determine size from local path' do
|
|
|
|
file = described_class.new(temp_file.path)
|
|
|
|
|
|
|
|
expect(file.size).to eq(temp_file.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if is a remote file' do
|
|
|
|
expect do
|
|
|
|
described_class.new(nil, remote_id: 'id')
|
|
|
|
end.to raise_error(UploadedFile::UnknownSizeError, 'Unable to determine file size')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when size is a number' do
|
|
|
|
let_it_be(:size) { 1.gigabyte }
|
|
|
|
|
|
|
|
it 'is overridden by the size of the local file' do
|
|
|
|
file = described_class.new(temp_file.path, size: size)
|
|
|
|
|
|
|
|
expect(file.size).to eq(temp_file.size)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is respected if is a remote file' do
|
|
|
|
file = described_class.new(nil, remote_id: 'id', size: size)
|
|
|
|
|
|
|
|
expect(file.size).to eq(size)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when size is a string' do
|
|
|
|
it 'is converted to a number' do
|
|
|
|
file = described_class.new(nil, remote_id: 'id', size: '1')
|
|
|
|
|
|
|
|
expect(file.size).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an exception if does not represent a number' do
|
|
|
|
expect do
|
|
|
|
described_class.new(nil, remote_id: 'id', size: 'not a number')
|
|
|
|
end.to raise_error(UploadedFile::UnknownSizeError, 'Unable to determine file size')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-18 14:21:32 +00:00
|
|
|
describe '#sanitize_filename' do
|
|
|
|
it { expect(described_class.new(temp_file.path).sanitize_filename('spaced name')).to eq('spaced_name') }
|
|
|
|
it { expect(described_class.new(temp_file.path).sanitize_filename('#$%^&')).to eq('_____') }
|
|
|
|
it { expect(described_class.new(temp_file.path).sanitize_filename('..')).to eq('_..') }
|
|
|
|
it { expect(described_class.new(temp_file.path).sanitize_filename('')).to eq('unnamed') }
|
|
|
|
end
|
2018-04-03 16:47:33 +00:00
|
|
|
end
|