2017-05-03 11:26:49 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe FileMover do
|
2019-02-13 03:24:26 -05:00
|
|
|
include FileMoverHelpers
|
|
|
|
|
2017-05-03 11:26:49 -04:00
|
|
|
let(:filename) { 'banana_sample.gif' }
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:temp_file_path) { File.join('uploads/-/system/temp', 'secret55', filename) }
|
|
|
|
|
2017-05-29 03:54:35 -04:00
|
|
|
let(:temp_description) do
|
2018-02-02 08:59:43 -05:00
|
|
|
"test ![banana_sample](/#{temp_file_path}) "\
|
|
|
|
"same ![banana_sample](/#{temp_file_path}) "
|
2017-05-29 03:54:35 -04:00
|
|
|
end
|
2018-02-02 08:59:43 -05:00
|
|
|
let(:file_path) { File.join('uploads/-/system/personal_snippet', snippet.id.to_s, 'secret55', filename) }
|
2017-05-03 11:26:49 -04:00
|
|
|
let(:snippet) { create(:personal_snippet, description: temp_description) }
|
|
|
|
|
2019-02-13 03:23:58 -05:00
|
|
|
subject { described_class.new(temp_file_path, snippet).execute }
|
2017-05-03 11:26:49 -04:00
|
|
|
|
|
|
|
describe '#execute' do
|
2017-05-29 03:54:35 -04:00
|
|
|
before do
|
|
|
|
expect(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
|
2017-05-03 11:26:49 -04:00
|
|
|
expect(FileUtils).to receive(:move).with(a_string_including(temp_file_path), a_string_including(file_path))
|
2017-05-29 03:54:35 -04:00
|
|
|
allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:exists?).and_return(true)
|
|
|
|
allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:size).and_return(10)
|
2019-02-13 03:24:26 -05:00
|
|
|
|
|
|
|
stub_file_mover(temp_file_path)
|
2017-05-29 03:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when move and field update successful' do
|
|
|
|
it 'updates the description correctly' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.reload.description)
|
|
|
|
.to eq(
|
2018-02-02 08:59:43 -05:00
|
|
|
"test ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/secret55/banana_sample.gif) "\
|
|
|
|
"same ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/secret55/banana_sample.gif) "
|
2017-05-29 03:54:35 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new update record' do
|
|
|
|
expect { subject }.to change { Upload.count }.by(1)
|
|
|
|
end
|
2018-03-16 14:34:55 -04:00
|
|
|
|
|
|
|
it 'schedules a background migration' do
|
|
|
|
expect_any_instance_of(PersonalFileUploader).to receive(:schedule_background_upload).once
|
|
|
|
|
|
|
|
subject
|
|
|
|
end
|
2017-05-29 03:54:35 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when update_markdown fails' do
|
|
|
|
before do
|
|
|
|
expect(FileUtils).to receive(:move).with(a_string_including(file_path), a_string_including(temp_file_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(file_path, snippet, :non_existing_field).execute }
|
|
|
|
|
|
|
|
it 'does not update the description' do
|
|
|
|
subject
|
2017-05-03 11:26:49 -04:00
|
|
|
|
2017-05-29 03:54:35 -04:00
|
|
|
expect(snippet.reload.description)
|
|
|
|
.to eq(
|
2018-02-02 08:59:43 -05:00
|
|
|
"test ![banana_sample](/uploads/-/system/temp/secret55/banana_sample.gif) "\
|
|
|
|
"same ![banana_sample](/uploads/-/system/temp/secret55/banana_sample.gif) "
|
2017-05-29 03:54:35 -04:00
|
|
|
)
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
|
2017-05-29 03:54:35 -04:00
|
|
|
it 'does not create a new update record' do
|
|
|
|
expect { subject }.not_to change { Upload.count }
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|
|
|
|
end
|
2019-02-13 03:24:26 -05:00
|
|
|
|
|
|
|
context 'security' do
|
|
|
|
context 'when relative path is involved' do
|
|
|
|
let(:temp_file_path) { File.join('uploads/-/system/temp', '..', 'another_subdir_of_temp') }
|
|
|
|
|
|
|
|
it 'does not trigger move if path is outside designated directory' do
|
|
|
|
stub_file_mover('uploads/-/system/another_subdir_of_temp')
|
|
|
|
expect(FileUtils).not_to receive(:move)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.reload.description).to eq(temp_description)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when symlink is involved' do
|
|
|
|
it 'does not trigger move if path is outside designated directory' do
|
|
|
|
stub_file_mover(temp_file_path, stub_real_path: Pathname('/etc'))
|
|
|
|
expect(FileUtils).not_to receive(:move)
|
|
|
|
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.reload.description).to eq(temp_description)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-03 11:26:49 -04:00
|
|
|
end
|