Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 33fd680

Browse files
authored
DEV: Add back validator for Spam setting (#1415)
## 🔍 Overview This update re-introduces the validator used on the `ai_spam_detection_enabled` setting. It was initially added here: #1374 to prevent Spam from being enabled without creating an `AiModerationSetting` value in the database. However, due to issues with backups/migrations we temporarily removed it here: #1393. Now with some internal fixes, we can re-introduce it. We also update the validator so that it only validates when trying to turn on rather than when turning off too.
1 parent 6827147 commit 33fd680

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

config/settings.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ discourse_ai:
412412

413413
ai_spam_detection_enabled:
414414
default: false
415+
validator: "DiscourseAi::Configuration::SpamDetectionValidator"
415416
ai_spam_detection_user_id:
416417
default: ""
417418
hidden: true

lib/configuration/spam_detection_validator.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ def initialize(opts = {})
88
end
99

1010
def valid_value?(val)
11-
return true if Rails.env.test?
11+
# only validate when enabling spam detection
12+
return true if val == "f" || val == "false"
1213
return true if AiModerationSetting.spam
1314

1415
false

spec/configuration/llm_enumerator_spec.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,5 @@
6262
{ fake_model.id => [{ type: :automation, name: "some automation", id: automation.id }] },
6363
)
6464
end
65-
66-
it "doesn't error on spam when spam detection is enabled but moderation setting is missing" do
67-
SiteSetting.ai_spam_detection_enabled = true
68-
expect { described_class.global_usage }.not_to raise_error
69-
end
7065
end
7166
end
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe DiscourseAi::Configuration::SpamDetectionValidator do
4+
let(:validator) { described_class.new }
5+
6+
it "always returns true if setting the value to false" do
7+
expect(validator.valid_value?("f")).to eq(true)
8+
end
9+
10+
context "when a moderation setting exists" do
11+
fab!(:llm_model)
12+
before { AiModerationSetting.create!(setting_type: "spam", llm_model_id: llm_model.id) }
13+
14+
it "returns true if a moderation setting for spam exists" do
15+
expect(validator.valid_value?("t")).to eq(true)
16+
end
17+
end
18+
19+
context "when no moderation setting exists" do
20+
it "returns false if a moderation setting for spam does not exist" do
21+
expect(validator.valid_value?("t")).to eq(false)
22+
end
23+
24+
it "returns an error message when no moderation setting exists" do
25+
expect(validator.error_message).to eq(
26+
I18n.t("discourse_ai.spam_detection.configuration_missing"),
27+
)
28+
end
29+
end
30+
end

spec/requests/admin/ai_spam_controller_spec.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,10 @@
209209

210210
describe "#show" do
211211
context "when logged in as admin" do
212-
before { sign_in(admin) }
212+
before do
213+
sign_in(admin)
214+
AiModerationSetting.create!(setting_type: :spam, llm_model_id: llm_model.id)
215+
end
213216

214217
it "correctly filters seeded llms" do
215218
SiteSetting.ai_spam_detection_enabled = true
@@ -248,7 +251,7 @@
248251
it "return proper settings when spam detection is enabled" do
249252
SiteSetting.ai_spam_detection_enabled = true
250253

251-
AiModerationSetting.create(
254+
AiModerationSetting.update!(
252255
{
253256
setting_type: :spam,
254257
llm_model_id: llm_model.id,

0 commit comments

Comments
 (0)