| ... | @@ -3,20 +3,29 @@ require 'spec_helper' |
... | @@ -3,20 +3,29 @@ require 'spec_helper' |
|
|
describe FileMover do
|
|
describe FileMover do
|
|
|
include FileMoverHelpers
|
|
include FileMoverHelpers
|
|
|
|
|
|
|
|
|
let(:user) { create(:user) }
|
|
|
let(:filename) { 'banana_sample.gif' }
|
|
let(:filename) { 'banana_sample.gif' }
|
|
|
let(:temp_file_path) { File.join('uploads/-/system/temp', 'secret55', filename) }
|
|
let(:secret) { 'secret55' }
|
|
|
|
let(:temp_file_path) { File.join("uploads/-/system/user/#{user.id}", secret, filename) }
|
|
|
|
|
|
|
|
let(:temp_description) do
|
|
let(:temp_description) do
|
|
|
"test  "\
|
|
"test  "\
|
|
|
"same  "
|
|
"same  "
|
|
|
end
|
|
end
|
|
|
let(:file_path) { File.join('uploads/-/system/personal_snippet', snippet.id.to_s, 'secret55', filename) }
|
|
let(:file_path) { File.join('uploads/-/system/personal_snippet', snippet.id.to_s, secret, filename) }
|
|
|
let(:snippet) { create(:personal_snippet, description: temp_description) }
|
|
let(:snippet) { create(:personal_snippet, description: temp_description) }
|
|
|
|
|
|
|
|
subject { described_class.new(temp_file_path, snippet).execute }
|
|
let(:tmp_uploader) do
|
|
|
|
PersonalFileUploader.new(user, secret: secret)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:file) { fixture_file_upload('spec/fixtures/banana_sample.gif') }
|
|
|
|
subject { described_class.new(temp_file_path, from_model: user, to_model: snippet).execute }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
describe '#execute' do
|
|
|
before do
|
|
before do
|
|
|
|
tmp_uploader.store!(file)
|
|
|
|
|
|
|
expect(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
|
|
expect(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
|
|
|
expect(FileUtils).to receive(:move).with(a_string_including(temp_file_path), a_string_including(file_path))
|
|
expect(FileUtils).to receive(:move).with(a_string_including(temp_file_path), a_string_including(file_path))
|
|
|
allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:exists?).and_return(true)
|
|
allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:exists?).and_return(true)
|
| ... | @@ -25,6 +34,8 @@ describe FileMover do |
... | @@ -25,6 +34,8 @@ describe FileMover do |
|
|
stub_file_mover(temp_file_path)
|
|
stub_file_mover(temp_file_path)
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
|
let(:tmp_upload) { tmp_uploader.upload }
|
|
|
|
|
|
|
context 'when move and field update successful' do
|
|
context 'when move and field update successful' do
|
|
|
it 'updates the description correctly' do
|
|
it 'updates the description correctly' do
|
|
|
subject
|
|
subject
|
| ... | @@ -36,8 +47,10 @@ describe FileMover do |
... | @@ -36,8 +47,10 @@ describe FileMover do |
|
|
)
|
|
)
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
it 'creates a new update record' do
|
|
it 'updates existing upload record' do
|
|
|
expect { subject }.to change { Upload.count }.by(1)
|
|
expect { subject }
|
|
|
|
.to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
|
|
|
|
.from([user.id, 'User']).to([snippet.id, 'PersonalSnippet'])
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
it 'schedules a background migration' do
|
|
it 'schedules a background migration' do
|
| ... | @@ -52,30 +65,31 @@ describe FileMover do |
... | @@ -52,30 +65,31 @@ describe FileMover do |
|
|
expect(FileUtils).to receive(:move).with(a_string_including(file_path), a_string_including(temp_file_path))
|
|
expect(FileUtils).to receive(:move).with(a_string_including(file_path), a_string_including(temp_file_path))
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(file_path, snippet, :non_existing_field).execute }
|
|
subject { described_class.new(file_path, :non_existing_field, from_model: user, to_model: snippet).execute }
|
|
|
|
|
|
|
|
it 'does not update the description' do
|
|
it 'does not update the description' do
|
|
|
subject
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.reload.description)
|
|
expect(snippet.reload.description)
|
|
|
.to eq(
|
|
.to eq(
|
|
|
"test  "\
|
|
"test  "\
|
|
|
"same  "
|
|
"same  "
|
|
|
)
|
|
)
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
it 'does not create a new update record' do
|
|
it 'does not change the upload record' do
|
|
|
expect { subject }.not_to change { Upload.count }
|
|
expect { subject }
|
|
|
|
.not_to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
context 'security' do
|
|
context 'security' do
|
|
|
context 'when relative path is involved' do
|
|
context 'when relative path is involved' do
|
|
|
let(:temp_file_path) { File.join('uploads/-/system/temp', '..', 'another_subdir_of_temp') }
|
|
let(:temp_file_path) { File.join("uploads/-/system/user/#{user.id}", '..', 'another_subdir_of_temp') }
|
|
|
|
|
|
|
|
it 'does not trigger move if path is outside designated directory' do
|
|
it 'does not trigger move if path is outside designated directory' do
|
|
|
stub_file_mover('uploads/-/system/another_subdir_of_temp')
|
|
stub_file_mover("uploads/-/system/user/#{user.id}/another_subdir_of_temp")
|
|
|
expect(FileUtils).not_to receive(:move)
|
|
expect(FileUtils).not_to receive(:move)
|
|
|
|
|
|
|
|
subject
|
|
subject
|
| ... | |
... | |
| ... | | ... | |