2021-03-11 14:42:57 +01:00
|
|
|
describe VirusScannerJob, type: :job do
|
|
|
|
let(:blob) do
|
|
|
|
ActiveStorage::Blob.create_and_upload!(io: StringIO.new("toto"), filename: "toto.txt", content_type: "text/plain")
|
2018-05-11 14:59:20 +02:00
|
|
|
end
|
|
|
|
|
2020-07-13 14:29:29 +02:00
|
|
|
subject do
|
2021-03-11 14:42:57 +01:00
|
|
|
VirusScannerJob.perform_now(blob)
|
2020-07-13 14:29:29 +02:00
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
context "when the blob is not analyzed yet" do
|
|
|
|
it "retries the job later" do
|
|
|
|
expect { subject }.to have_enqueued_job(VirusScannerJob)
|
|
|
|
end
|
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
context "when the blob has been analyzed" do
|
2018-05-11 14:59:20 +02:00
|
|
|
before do
|
2021-03-11 14:42:57 +01:00
|
|
|
blob.analyze
|
2018-05-11 14:59:20 +02:00
|
|
|
end
|
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
context "when no virus is found" do
|
|
|
|
before do
|
|
|
|
allow(ClamavService).to receive(:safe_file?).and_return(true)
|
|
|
|
subject
|
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
it { expect(blob.virus_scanner.safe?).to be_truthy }
|
2018-05-11 14:59:20 +02:00
|
|
|
end
|
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
context "when a virus is found" do
|
|
|
|
before do
|
|
|
|
allow(ClamavService).to receive(:safe_file?).and_return(false)
|
|
|
|
subject
|
|
|
|
end
|
2020-07-13 14:31:21 +02:00
|
|
|
|
2021-03-11 14:42:57 +01:00
|
|
|
it { expect(blob.virus_scanner.infected?).to be_truthy }
|
2020-07-13 14:31:21 +02:00
|
|
|
end
|
|
|
|
|
2021-03-11 14:42:57 +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
|
2020-07-13 14:31:21 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
end
|