75a1046315
Follow-up of #5953. Refactor the concerns with two goals: - Getting closer from the way ActiveStorage adds its own hooks. Usually ActiveStorage does this using an `Attachment#after_create` hook, which then delegates to the blob to enqueue the job. - Enqueuing each job only once. By hooking on `Attachment#after_create`, we guarantee each job will be added only once. We then let the jobs themselves check if they are relevant or not, and retry or discard themselves if necessary. We also need to update the tests a bit, because Rails' `perform_enqueued_jobs(&block)` test helper doesn't honor the `retry_on` clause of jobs. Instead it forwards the exception to the caller – which makes the test fail. Instead we use the inline version of `perform_enqueued_jobs()`, without a block, which properly ignores errors catched by retry_on.
49 lines
1.1 KiB
Ruby
49 lines
1.1 KiB
Ruby
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
|
|
|
|
context "when the blob is not analyzed yet" do
|
|
it "retries the job later" do
|
|
expect { subject }.to have_enqueued_job(VirusScannerJob)
|
|
end
|
|
end
|
|
|
|
context "when the blob has been analyzed" do
|
|
before do
|
|
blob.analyze
|
|
end
|
|
|
|
context "when no virus is found" do
|
|
before do
|
|
allow(ClamavService).to receive(:safe_file?).and_return(true)
|
|
subject
|
|
end
|
|
|
|
it { expect(blob.virus_scanner.safe?).to be_truthy }
|
|
end
|
|
|
|
context "when a virus is found" do
|
|
before do
|
|
allow(ClamavService).to receive(:safe_file?).and_return(false)
|
|
subject
|
|
end
|
|
|
|
it { expect(blob.virus_scanner.infected?).to be_truthy }
|
|
end
|
|
|
|
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
|
|
end
|