debian-mirror-gitlab/spec/uploaders/file_mover_spec.rb

159 lines
5.7 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-07-28 23:09:34 +05:30
RSpec.describe FileMover do
2019-03-13 22:55:13 +05:30
include FileMoverHelpers
2019-07-07 11:18:12 +05:30
let(:user) { create(:user) }
2017-09-10 17:25:29 +05:30
let(:filename) { 'banana_sample.gif' }
2020-03-07 23:17:34 +05:30
let(:secret) { SecureRandom.hex }
2019-07-07 11:18:12 +05:30
let(:temp_file_path) { File.join("uploads/-/system/user/#{user.id}", secret, filename) }
2018-03-17 18:26:18 +05:30
2017-09-10 17:25:29 +05:30
let(:temp_description) do
2018-03-17 18:26:18 +05:30
"test ![banana_sample](/#{temp_file_path}) "\
"same ![banana_sample](/#{temp_file_path}) "
2017-09-10 17:25:29 +05:30
end
2020-10-24 23:57:45 +05:30
2019-07-07 11:18:12 +05:30
let(:file_path) { File.join('uploads/-/system/personal_snippet', snippet.id.to_s, secret, filename) }
2017-09-10 17:25:29 +05:30
let(:snippet) { create(:personal_snippet, description: temp_description) }
2019-07-07 11:18:12 +05:30
let(:tmp_uploader) do
PersonalFileUploader.new(user, secret: secret)
end
let(:file) { fixture_file_upload('spec/fixtures/banana_sample.gif') }
2020-01-01 13:55:28 +05:30
2019-07-07 11:18:12 +05:30
subject { described_class.new(temp_file_path, from_model: user, to_model: snippet).execute }
2017-09-10 17:25:29 +05:30
describe '#execute' do
2019-07-07 11:18:12 +05:30
let(:tmp_upload) { tmp_uploader.upload }
2019-03-13 22:55:13 +05:30
2019-07-07 11:18:12 +05:30
before do
tmp_uploader.store!(file)
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
context 'local storage' do
before do
allow(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
allow(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(:size).and_return(10)
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
stub_file_mover(temp_file_path)
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
context 'when move and field update successful' do
it 'updates the description correctly' do
subject
expect(snippet.reload.description)
2020-03-07 23:17:34 +05:30
.to eq("test ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/#{secret}/banana_sample.gif) "\
"same ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/#{secret}/banana_sample.gif) ")
2019-07-07 11:18:12 +05:30
end
it 'updates existing upload record' do
expect { subject }
.to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
.from([user.id, 'User']).to([snippet.id, 'Snippet'])
end
it 'schedules a background migration' do
expect_any_instance_of(PersonalFileUploader).to receive(:schedule_background_upload).once
subject
end
2017-09-10 17:25:29 +05:30
end
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
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
2018-05-09 12:01:36 +05:30
2019-07-07 11:18:12 +05:30
subject { described_class.new(file_path, :non_existing_field, from_model: user, to_model: snippet).execute }
it 'does not update the description' do
subject
expect(snippet.reload.description)
2020-03-07 23:17:34 +05:30
.to eq("test ![banana_sample](/uploads/-/system/user/#{user.id}/#{secret}/banana_sample.gif) "\
"same ![banana_sample](/uploads/-/system/user/#{user.id}/#{secret}/banana_sample.gif) ")
2019-07-07 11:18:12 +05:30
end
it 'does not change the upload record' do
expect { subject }
.not_to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
end
2018-05-09 12:01:36 +05:30
end
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
context 'when tmp uploader is not local storage' do
2017-09-10 17:25:29 +05:30
before do
2019-07-31 22:56:46 +05:30
stub_uploads_object_storage(uploader: PersonalFileUploader)
2019-07-07 11:18:12 +05:30
allow_any_instance_of(PersonalFileUploader).to receive(:file_storage?) { false }
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
after do
FileUtils.rm_f(File.join('personal_snippet', snippet.id.to_s, secret, filename))
end
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
context 'when move and field update successful' do
it 'updates the description correctly' do
subject
2017-09-10 17:25:29 +05:30
2019-07-07 11:18:12 +05:30
expect(snippet.reload.description)
2020-03-07 23:17:34 +05:30
.to eq("test ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/#{secret}/banana_sample.gif) "\
"same ![banana_sample](/uploads/-/system/personal_snippet/#{snippet.id}/#{secret}/banana_sample.gif) ")
2019-07-07 11:18:12 +05:30
end
it 'creates new target upload record an delete the old upload' do
expect { subject }
.to change { Upload.last.attributes.values_at('model_id', 'model_type') }
.from([user.id, 'User']).to([snippet.id, 'Snippet'])
expect(Upload.count).to eq(1)
end
2017-09-10 17:25:29 +05:30
end
2019-07-07 11:18:12 +05:30
context 'when update_markdown fails' do
subject { described_class.new(file_path, :non_existing_field, from_model: user, to_model: snippet).execute }
it 'does not update the description' do
subject
expect(snippet.reload.description)
2020-03-07 23:17:34 +05:30
.to eq("test ![banana_sample](/uploads/-/system/user/#{user.id}/#{secret}/banana_sample.gif) "\
"same ![banana_sample](/uploads/-/system/user/#{user.id}/#{secret}/banana_sample.gif) ")
2019-07-07 11:18:12 +05:30
end
it 'does not change the upload record' do
expect { subject }
.to change { Upload.last.attributes.values_at('model_id', 'model_type') }.from([user.id, 'User'])
end
2017-09-10 17:25:29 +05:30
end
end
end
2019-03-13 22:55:13 +05:30
context 'security' do
context 'when relative path is involved' do
2019-07-07 11:18:12 +05:30
let(:temp_file_path) { File.join("uploads/-/system/user/#{user.id}", '..', 'another_subdir_of_temp') }
2019-03-13 22:55:13 +05:30
it 'does not trigger move if path is outside designated directory' do
expect(FileUtils).not_to receive(:move)
2020-03-07 23:17:34 +05:30
expect { subject }.to raise_error(FileUploader::InvalidSecret)
2019-03-13 22:55:13 +05:30
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-09-10 17:25:29 +05:30
end