carrierwave: when migrating, create an empty blob if file is missing

This commit is contained in:
Pierre de La Morinerie 2019-05-28 18:19:33 +02:00
parent 44c410d40d
commit 10df7b70ee
3 changed files with 66 additions and 7 deletions

View file

@ -8,7 +8,7 @@ describe CarrierwaveActiveStorageMigrationService do
end
describe '.make_blob' do
let(:pj) { create(:piece_justificative, :rib) }
let(:pj) { create(:piece_justificative, :rib, updated_at: Time.zone.local(2019, 01, 01, 12, 00)) }
let(:identify) { false }
before do
@ -17,6 +17,8 @@ describe CarrierwaveActiveStorageMigrationService do
subject(:blob) { service.make_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename, identify: identify) }
it { expect(blob.created_at).to eq pj.updated_at }
it 'marks the blob as already scanned by the antivirus' do
expect(blob.metadata[:virus_scan_result]).to eq(ActiveStorage::VirusScanner::SAFE)
end
@ -34,4 +36,37 @@ describe CarrierwaveActiveStorageMigrationService do
end
end
end
describe '.make_empty_blob' do
let(:pj) { create(:piece_justificative, :rib, updated_at: Time.zone.local(2019, 01, 01, 12, 00)) }
before 'set the underlying stored file as missing' do
allow(pj.content.file).to receive(:file).and_return(nil)
end
subject(:blob) { service.make_empty_blob(pj.content, pj.updated_at.iso8601, filename: pj.original_filename) }
it { expect(blob.created_at).to eq pj.updated_at }
it 'marks the blob as already scanned by the antivirus' do
expect(blob.metadata[:virus_scan_result]).to eq(ActiveStorage::VirusScanner::SAFE)
end
it 'sets the blob MIME type from the file' do
expect(blob.identified).to be true
expect(blob.content_type).to eq 'application/pdf'
end
context 'when the file metadata are also missing' do
before do
allow(pj).to receive(:original_filename).and_return(nil)
allow(pj.content).to receive(:content_type).and_return(nil)
end
it 'fallbacks on default values' do
expect(blob.filename).to eq pj.content.filename
expect(blob.content_type).to eq 'text/plain'
end
end
end
end