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

Commit 2d5a1c8

Browse files
committed
DEV: Add back validator for Spam setting
1 parent 6827147 commit 2d5a1c8

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
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
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

0 commit comments

Comments
 (0)