Skip to content

Commit

Permalink
Use named subjects in specs
Browse files Browse the repository at this point in the history
  • Loading branch information
Flink committed Nov 30, 2023
1 parent 58a1aab commit 311b080
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 42 deletions.
54 changes: 26 additions & 28 deletions spec/jobs/scheduled/flag_quarantined_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,49 @@

describe Jobs::FlagQuarantinedUploads do
describe "#execute" do
subject(:execute) { described_class.new.execute({}) }

let(:scan_message) { "Win.Test.EICAR_HDB-1 FOUND" }
let(:upload) { Fabricate(:upload) }

before do
SiteSetting.flag_malicious_uploads = true
@scanned_upload =
ScannedUpload.create!(upload: upload, quarantined: true, scan_result: scan_message)
let(:reviewable_upload) { ReviewableUpload.find_by(target: upload) }
let!(:scanned_upload) do
ScannedUpload.create!(upload: upload, quarantined: true, scan_result: scan_message)
end

it "flags the upload if it was previously quarantined" do
subject.execute({})

reviewable_upload = ReviewableUpload.find_by(target: upload)
before { SiteSetting.flag_malicious_uploads = true }

expect(reviewable_upload).to be_present
it "flags the upload if it was previously quarantined" do
execute
expect(reviewable_upload.payload["scan_message"]).to eq(scan_message)
end

it "does nothing if the flag_malicious_uploads flag is disabled" do
SiteSetting.flag_malicious_uploads = false

subject.execute({})
reviewable_upload = ReviewableUpload.find_by(target: upload)
context "when the flag_malicious_uploads flag is disabled" do
before { SiteSetting.flag_malicious_uploads = false }

expect(reviewable_upload).to be_nil
it "does nothing" do
execute
expect(reviewable_upload).to be_nil
end
end

it "does nothing if a reviewable already exists" do
@scanned_upload.flag_upload

subject.execute({})
context "when a reviewable already exists" do
before { scanned_upload.flag_upload }

reviewable_upload = ReviewableUpload.find_by(target: upload)
scores = reviewable_upload.reviewable_scores
it "does nothing" do
execute

expect(scores.size).to eq(1)
expect(reviewable_upload.reviewable_scores.size).to eq(1)
end
end

it "does nothing if the scanned upload is not quarantined" do
@scanned_upload.update!(quarantined: false)
context "when the scanned upload is not quarantined" do
before { scanned_upload.update!(quarantined: false) }

subject.execute({})
reviewable_upload = ReviewableUpload.find_by(target: upload)
it "does nothing" do
execute

expect(reviewable_upload).to be_nil
expect(reviewable_upload).to be_nil
end
end
end
end
27 changes: 13 additions & 14 deletions spec/jobs/scheduled/remove_orphaned_scanned_uploads_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
require "rails_helper"

describe Jobs::RemoveOrphanedScannedUploads do
let(:upload) { Fabricate(:upload) }
subject(:execute) { described_class.new.execute({}) }

before do
SiteSetting.discourse_antivirus_enabled = true
@scanned_upload = ScannedUpload.create_new!(upload)
end
let(:upload) { Fabricate(:upload) }
let!(:scanned_upload) { ScannedUpload.create_new!(upload) }

it "does nothing if the upload still exists" do
subject.execute({})
before { SiteSetting.discourse_antivirus_enabled = true }

expect(@scanned_upload.reload).to be_present
context "when the upload still exists" do
it "does nothing" do
expect { execute }.not_to change { scanned_upload.reload }
end
end

it "deletes the scanned upload if the upload is gone" do
upload.destroy!

subject.execute({})
orphaned_upload = ScannedUpload.find_by(id: @scanned_upload.id)
context "when the upload is gone" do
before { upload.destroy! }

expect(orphaned_upload).to be_nil
it "deletes the scanned upload" do
expect { execute }.to change { ScannedUpload.where(id: scanned_upload.id).count }.by(-1)
end
end
end

0 comments on commit 311b080

Please sign in to comment.