From bfb98554ff821897f176e7fc52ec4916dfbdda45 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 12 Feb 2024 16:30:08 +0100 Subject: [PATCH] fix(active-storage): don't fail on analyzing a blob derivated by watermark MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Quand l'analyse du job se produit après le watermark, elle doit comparer le checksum avec celui du fichier filigrané. --- app/jobs/titre_identite_watermark_job.rb | 5 +- .../jobs/titre_identite_watermark_job_spec.rb | 56 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 spec/jobs/titre_identite_watermark_job_spec.rb diff --git a/app/jobs/titre_identite_watermark_job.rb b/app/jobs/titre_identite_watermark_job.rb index 491530d05..1b261e291 100644 --- a/app/jobs/titre_identite_watermark_job.rb +++ b/app/jobs/titre_identite_watermark_job.rb @@ -19,8 +19,9 @@ class TitreIdentiteWatermarkJob < ApplicationJob processed = WatermarkService.new.process(file, output) return if processed.blank? - blob.upload(processed) - blob.touch(:watermarked_at) + blob.upload(processed) # also update checksum & byte_size accordingly + blob.watermarked_at = Time.current + blob.save! end end end diff --git a/spec/jobs/titre_identite_watermark_job_spec.rb b/spec/jobs/titre_identite_watermark_job_spec.rb new file mode 100644 index 000000000..ebb9497cc --- /dev/null +++ b/spec/jobs/titre_identite_watermark_job_spec.rb @@ -0,0 +1,56 @@ +describe TitreIdentiteWatermarkJob, type: :job do + let(:blob) do + ActiveStorage::Blob.create_and_upload!(io: StringIO.new("toto"), filename: "toto.png") + end + + let(:antivirus_pending) { false } + let(:watermark_service) { instance_double("WatermarkService") } + + before do + virus_scanner_mock = instance_double("ActiveStorage::VirusScanner", pending?: antivirus_pending) + allow(blob).to receive(:virus_scanner).and_return(virus_scanner_mock) + + allow(WatermarkService).to receive(:new).and_return(watermark_service) + allow(watermark_service).to receive(:process).and_return(true) + end + + context "when watermark is already done" do + before do + allow(blob).to receive(:watermark_done?).and_return(true) + end + + it "does not process the blob" do + expect(watermark_service).not_to receive(:process) + described_class.perform_now(blob) + end + end + + context "when the blob is not scanned yet" do + let(:antivirus_pending) { true } + + it "raises a FileNotScannedYetError" do + expect { described_class.perform_now(blob) }.to have_enqueued_job(described_class).with(blob) + end + end + + context "when the blob is ready to be watermarked" do + let(:watermarked_file) { Tempfile.new("watermarked.png") } + + before do + allow(watermarked_file).to receive(:size).and_return(100) + end + + it "processes the blob with watermark" do + expect(watermark_service).to receive(:process).and_return(watermarked_file) + + expect { + described_class.perform_now(blob) + }.to change { + blob.reload.checksum + } + + expect(blob.byte_size).to eq(100) + expect(blob.watermarked_at).to be_present + end + end +end