From 3868a636076689d6675acad81e6edb8b7dfa0a43 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 6 Feb 2024 10:35:27 +0100 Subject: [PATCH] Fix already-invalid reports failing to resolve (#29027) --- app/models/report.rb | 6 ++---- spec/models/report_spec.rb | 13 +++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/models/report.rb b/app/models/report.rb index 3ae5c10dd0..470c6d1dd1 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -43,9 +43,9 @@ class Report < ApplicationRecord delegate :local?, to: :account validates :comment, length: { maximum: 1_000 }, if: :local? - validates :rule_ids, absence: true, unless: :violation? + validates :rule_ids, absence: true, if: -> { (category_changed? || rule_ids_changed?) && !violation? } - validate :validate_rule_ids + validate :validate_rule_ids, if: -> { (category_changed? || rule_ids_changed?) && violation? } enum category: { other: 0, @@ -147,8 +147,6 @@ class Report < ApplicationRecord end def validate_rule_ids - return unless violation? - errors.add(:rule_ids, I18n.t('reports.errors.invalid_rules')) unless rules.size == rule_ids&.size end diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb index c485a4a3c9..dbfdde9ed4 100644 --- a/spec/models/report_spec.rb +++ b/spec/models/report_spec.rb @@ -137,5 +137,18 @@ describe Report do report = Fabricate.build(:report, account: remote_account, comment: Faker::Lorem.characters(number: 1001)) expect(report.valid?).to be true end + + it 'is invalid if it references invalid rules' do + report = Fabricate.build(:report, category: :violation, rule_ids: [-1]) + expect(report.valid?).to be false + expect(report).to model_have_error_on_field(:rule_ids) + end + + it 'is invalid if it references rules but category is not "violation"' do + rule = Fabricate(:rule) + report = Fabricate.build(:report, category: :spam, rule_ids: rule.id) + expect(report.valid?).to be false + expect(report).to model_have_error_on_field(:rule_ids) + end end end