2024-04-29 00:17:15 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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-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
|
2021-03-11 14:42:57 +01:00
|
|
|
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 }
|
|
|
|
end
|
2021-04-06 14:59:12 +02:00
|
|
|
|
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-04-06 14:59:12 +02:00
|
|
|
|
2021-11-04 12:01:16 +01:00
|
|
|
assert_performed_jobs(5) do
|
|
|
|
VirusScannerJob.perform_later(blob)
|
2021-04-06 14:59:12 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-04 12:01:16 +01:00
|
|
|
it do
|
|
|
|
expect(blob.reload.virus_scanner.corrupt?).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
|
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
|
2018-05-11 14:59:20 +02:00
|
|
|
end
|
|
|
|
|
2021-11-04 12:01:16 +01:00
|
|
|
it { expect(blob.virus_scanner.safe?).to be_truthy }
|
|
|
|
end
|
2020-07-13 14:31:21 +02:00
|
|
|
|
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
|
2020-07-13 14:31:21 +02:00
|
|
|
end
|
|
|
|
|
2021-11-04 12:01:16 +01:00
|
|
|
it { expect(blob.virus_scanner.infected?).to be_truthy }
|
|
|
|
end
|
2021-03-11 14:42:57 +01:00
|
|
|
|
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
|
2020-07-13 14:31:21 +02:00
|
|
|
end
|
|
|
|
end
|
2018-05-11 14:59:20 +02:00
|
|
|
end
|