demarches-normaliennes/spec/jobs/virus_scanner_job_spec.rb

74 lines
1.8 KiB
Ruby
Raw Normal View History

describe VirusScannerJob, type: :job do
let(:blob) do
ActiveStorage::Blob.create_and_upload!(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
end
subject do
VirusScannerJob.perform_now(blob)
end
2021-11-04 12:01:16 +01:00
context "when the virus scan launch before rails analyze" do
before do
allow(ClamavService).to receive(:safe_file?).and_return(true)
subject
blob.analyze
end
2021-11-04 12:01:16 +01:00
it { expect(blob.virus_scanner.safe?).to be_truthy }
it { expect(blob.analyzed?).to be_truthy }
it { expect(blob.lock_version).to eq(2) }
end
2021-11-04 12:01:16 +01:00
context "should raise ActiveRecord::StaleObjectError" do
let(:blob_2) { ActiveStorage::Blob.find(blob.id) }
before do
2021-11-04 12:01:16 +01:00
blob_2.metadata[:virus_scan_result] = "infected"
blob.metadata[:virus_scan_result] = "safe"
blob.save
end
2021-11-04 12:01:16 +01:00
it { expect { blob_2.save }.to raise_error(ActiveRecord::StaleObjectError) }
end
2021-11-04 12:01:16 +01:00
context "when there is an integrity error" do
before do
blob.update_column('checksum', 'integrity error')
2021-11-04 12:01:16 +01:00
assert_performed_jobs(5) do
VirusScannerJob.perform_later(blob)
end
end
2021-11-04 12:01:16 +01:00
it do
expect(blob.reload.virus_scanner.corrupt?).to be_truthy
end
end
2021-11-04 12:01:16 +01:00
context "when no virus is found" do
before do
allow(ClamavService).to receive(:safe_file?).and_return(true)
subject
end
2021-11-04 12:01:16 +01:00
it { expect(blob.virus_scanner.safe?).to be_truthy }
end
2021-11-04 12:01:16 +01:00
context "when a virus is found" do
before do
allow(ClamavService).to receive(:safe_file?).and_return(false)
subject
end
2021-11-04 12:01:16 +01:00
it { expect(blob.virus_scanner.infected?).to be_truthy }
end
2021-11-04 12:01:16 +01:00
context "when the blob has been deleted" do
before do
ActiveStorage::Blob.find(blob.id).purge
end
it "ignores the error" do
expect { subject }.not_to raise_error
end
end
end