mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-06-28 10:32:15 +00:00
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: https://github.com/discourse/discourse-ai/pull/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: https://github.com/discourse/discourse-ai/pull/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.
This commit is contained in:
parent
6827147362
commit
33fd6801e5
@ -412,6 +412,7 @@ discourse_ai:
|
|||||||
|
|
||||||
ai_spam_detection_enabled:
|
ai_spam_detection_enabled:
|
||||||
default: false
|
default: false
|
||||||
|
validator: "DiscourseAi::Configuration::SpamDetectionValidator"
|
||||||
ai_spam_detection_user_id:
|
ai_spam_detection_user_id:
|
||||||
default: ""
|
default: ""
|
||||||
hidden: true
|
hidden: true
|
||||||
|
@ -8,7 +8,8 @@ module DiscourseAi
|
|||||||
end
|
end
|
||||||
|
|
||||||
def valid_value?(val)
|
def valid_value?(val)
|
||||||
return true if Rails.env.test?
|
# only validate when enabling spam detection
|
||||||
|
return true if val == "f" || val == "false"
|
||||||
return true if AiModerationSetting.spam
|
return true if AiModerationSetting.spam
|
||||||
|
|
||||||
false
|
false
|
||||||
|
@ -62,10 +62,5 @@ RSpec.describe DiscourseAi::Configuration::LlmEnumerator do
|
|||||||
{ fake_model.id => [{ type: :automation, name: "some automation", id: automation.id }] },
|
{ fake_model.id => [{ type: :automation, name: "some automation", id: automation.id }] },
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't error on spam when spam detection is enabled but moderation setting is missing" do
|
|
||||||
SiteSetting.ai_spam_detection_enabled = true
|
|
||||||
expect { described_class.global_usage }.not_to raise_error
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
30
spec/configuration/spam_detection_validator_spec.rb
Normal file
30
spec/configuration/spam_detection_validator_spec.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
RSpec.describe DiscourseAi::Configuration::SpamDetectionValidator do
|
||||||
|
let(:validator) { described_class.new }
|
||||||
|
|
||||||
|
it "always returns true if setting the value to false" do
|
||||||
|
expect(validator.valid_value?("f")).to eq(true)
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when a moderation setting exists" do
|
||||||
|
fab!(:llm_model)
|
||||||
|
before { AiModerationSetting.create!(setting_type: "spam", llm_model_id: llm_model.id) }
|
||||||
|
|
||||||
|
it "returns true if a moderation setting for spam exists" do
|
||||||
|
expect(validator.valid_value?("t")).to eq(true)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when no moderation setting exists" do
|
||||||
|
it "returns false if a moderation setting for spam does not exist" do
|
||||||
|
expect(validator.valid_value?("t")).to eq(false)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns an error message when no moderation setting exists" do
|
||||||
|
expect(validator.error_message).to eq(
|
||||||
|
I18n.t("discourse_ai.spam_detection.configuration_missing"),
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -209,7 +209,10 @@ RSpec.describe DiscourseAi::Admin::AiSpamController do
|
|||||||
|
|
||||||
describe "#show" do
|
describe "#show" do
|
||||||
context "when logged in as admin" do
|
context "when logged in as admin" do
|
||||||
before { sign_in(admin) }
|
before do
|
||||||
|
sign_in(admin)
|
||||||
|
AiModerationSetting.create!(setting_type: :spam, llm_model_id: llm_model.id)
|
||||||
|
end
|
||||||
|
|
||||||
it "correctly filters seeded llms" do
|
it "correctly filters seeded llms" do
|
||||||
SiteSetting.ai_spam_detection_enabled = true
|
SiteSetting.ai_spam_detection_enabled = true
|
||||||
@ -248,7 +251,7 @@ RSpec.describe DiscourseAi::Admin::AiSpamController do
|
|||||||
it "return proper settings when spam detection is enabled" do
|
it "return proper settings when spam detection is enabled" do
|
||||||
SiteSetting.ai_spam_detection_enabled = true
|
SiteSetting.ai_spam_detection_enabled = true
|
||||||
|
|
||||||
AiModerationSetting.create(
|
AiModerationSetting.update!(
|
||||||
{
|
{
|
||||||
setting_type: :spam,
|
setting_type: :spam,
|
||||||
llm_model_id: llm_model.id,
|
llm_model_id: llm_model.id,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user