| ... | @@ -23,63 +23,110 @@ describe FileMover do |
... | @@ -23,63 +23,110 @@ describe FileMover do |
|
|
subject { described_class.new(temp_file_path, from_model: user, to_model: snippet).execute }
|
|
subject { described_class.new(temp_file_path, from_model: user, to_model: snippet).execute }
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
describe '#execute' do
|
|
|
|
let(:tmp_upload) { tmp_uploader.upload }
|
|
|
|
|
|
|
before do
|
|
before do
|
|
|
tmp_uploader.store!(file)
|
|
tmp_uploader.store!(file)
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
|
|
context 'local storage' do
|
|
|
expect(FileUtils).to receive(:move).with(a_string_including(temp_file_path), a_string_including(file_path))
|
|
before do
|
|
|
allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:exists?).and_return(true)
|
|
allow(FileUtils).to receive(:mkdir_p).with(a_string_including(File.dirname(file_path)))
|
|
|
allow_any_instance_of(CarrierWave::SanitizedFile).to receive(:size).and_return(10)
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
it 'updates the description correctly' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
context 'when move and field update successful' do
|
|
expect(snippet.reload.description)
|
|
|
it 'updates the description correctly' do
|
|
.to eq("test  "\
|
|
|
subject
|
|
"same  ")
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(snippet.reload.description)
|
|
it 'updates existing upload record' do
|
|
|
.to eq(
|
|
expect { subject }
|
|
|
"test  "\
|
|
.to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
|
|
|
"same  "
|
|
.from([user.id, 'User']).to([snippet.id, 'Snippet'])
|
|
|
)
|
|
end
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it 'updates existing upload record' do
|
|
it 'schedules a background migration' do
|
|
|
expect { subject }
|
|
expect_any_instance_of(PersonalFileUploader).to receive(:schedule_background_upload).once
|
|
|
.to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
|
|
|
|
|
.from([user.id, 'User']).to([snippet.id, 'PersonalSnippet'])
|
|
subject
|
|
|
|
end
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
it 'schedules a background migration' do
|
|
context 'when update_markdown fails' do
|
|
|
expect_any_instance_of(PersonalFileUploader).to receive(:schedule_background_upload).once
|
|
before do
|
|
|
|
expect(FileUtils).to receive(:move).with(a_string_including(file_path), a_string_including(temp_file_path))
|
|
|
|
end
|
|
|
|
|
|
|
|
subject
|
|
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)
|
|
|
|
.to eq("test  "\
|
|
|
|
"same  ")
|
|
|
|
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
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
context 'when update_markdown fails' do
|
|
context 'when tmp uploader is not local storage' do
|
|
|
before do
|
|
before do
|
|
|
expect(FileUtils).to receive(:move).with(a_string_including(file_path), a_string_including(temp_file_path))
|
|
allow(PersonalFileUploader).to receive(:object_store_enabled?) { true }
|
|
|
|
tmp_uploader.object_store = ObjectStorage::Store::REMOTE
|
|
|
|
allow_any_instance_of(PersonalFileUploader).to receive(:file_storage?) { false }
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
subject { described_class.new(file_path, :non_existing_field, from_model: user, to_model: snippet).execute }
|
|
after do
|
|
|
|
FileUtils.rm_f(File.join('personal_snippet', snippet.id.to_s, secret, filename))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not update the description' do
|
|
context 'when move and field update successful' do
|
|
|
subject
|
|
it 'updates the description correctly' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.reload.description)
|
|
expect(snippet.reload.description)
|
|
|
.to eq(
|
|
.to eq("test  "\
|
|
|
"test  "\
|
|
"same  ")
|
|
|
"same  "
|
|
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
|
|
|
end
|
|
end
|
|
|
|
|
|
|
|
it 'does not change the upload record' do
|
|
context 'when update_markdown fails' do
|
|
|
expect { subject }
|
|
subject { described_class.new(file_path, :non_existing_field, from_model: user, to_model: snippet).execute }
|
|
|
.not_to change { tmp_upload.reload.attributes.values_at('model_id', 'model_type') }
|
|
|
|
|
|
it 'does not update the description' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(snippet.reload.description)
|
|
|
|
.to eq("test  "\
|
|
|
|
"same  ")
|
|
|
|
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
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
|
|
end
|
|
end
|
| ... | |
... | |
| ... | | ... | |