From cdefed2ca1a6fa09a54ea5179768f73ed0b83b66 Mon Sep 17 00:00:00 2001 From: Sam Perry Date: Mon, 7 Oct 2024 10:29:25 +0100 Subject: [PATCH] Add more scan_files specs --- .env.test | 1 - spec/models/concerns/scan_files_spec.rb | 60 ++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/.env.test b/.env.test index bbb8287a8..61b1dfcd5 100644 --- a/.env.test +++ b/.env.test @@ -3,7 +3,6 @@ DEVISE_SECRET_KEY=DEVISE_SECRET_KEY SENTRY_DSN=SENTRY_DSN AUTHY_API_KEY=AUTHY_API_KEY AUTHY_API_URL=https://api.authy.com -DISABLE_VIRUS_SCANNER=false PROFILE_MODE=false LOG_LEVEL=info ASSET_HOST= diff --git a/spec/models/concerns/scan_files_spec.rb b/spec/models/concerns/scan_files_spec.rb index cc4058585..42efda88b 100644 --- a/spec/models/concerns/scan_files_spec.rb +++ b/spec/models/concerns/scan_files_spec.rb @@ -1,9 +1,9 @@ require "rails_helper" RSpec.describe ScanFiles do - describe "callbacks" do - let(:record) { create(:form_answer_attachment) } + let(:record) { create(:form_answer_attachment) } + describe "callbacks" do describe "#set_scan_results_to_pending" do it "defines the method" do record.save @@ -43,4 +43,60 @@ end end end + + describe ".scan_for_viruses" do + it "defines methods for scanning and cleaning per given attribute" do + # assumes [scan_for_viruses :file] stated in model + expect(record.respond_to?(:scan_file!)).to be_truthy + expect(record.respond_to?(:on_scan_file)).to be_truthy + expect(record.respond_to?(:clean?)).to be_truthy + expect(record.respond_to?(:pending_or_scanning?)).to be_truthy + expect(record.respond_to?(:infected?)).to be_truthy + expect(record.respond_to?(:set_scan_results_to_pending)).to be_truthy + end + end + + describe ".scan_[attr_name]" do + context "when ENV['DISABLE_VIRUS_SCANNER'] == 'true'" do + before do + ENV["DISABLE_VIRUS_SCANNER"] = "true" + allow(record).to receive(:move_to_clean_bucket) + end + + after do + ENV["DISABLE_VIRUS_SCANNER"] = "false" + end + + it "updates [attr_name]_scan_results to clean" do + record.scan_file! + + expect(record.reload.file_scan_results).to eq "clean" + end + + it "calls the move_to_clean_bucket method" do + expect(record).to receive(:move_to_clean_bucket).with(:file) + + record.scan_file! + end + end + + context "when ENV['DISABLE_VIRUS_SCANNER'] == 'false'" do + it "enqueues a FileScanJob and updates [attr_name]_scan_results to scanning" do + expect(FileScanJob).to receive(:perform_later).with( + { + model: "FormAnswerAttachment", + column: :file, + id: record.id, + }.to_json, + "FormAnswerAttachment", + record.id, + "file", + ) + + record.scan_file! + + expect(record.reload.file_scan_results).to eq "scanning" + end + end + end end