mod reports: autoreport spam and autoban spammers.

* Automatically generate a mod report when a comment, forum post, or
  dmail is detected as spam.
* Automatically ban users that receive too many automatic spam reports
  within a short window of time.
* Automatically mark spam dmails as deleted.
* Change ban threshold from 10 spam reports in 24 hours to 10 reports in 1 hour.
* Change ban length from 3 days to forever.
This commit is contained in:
evazion
2020-02-03 04:12:03 -06:00
parent 170a0e8a48
commit bb2022abed
7 changed files with 89 additions and 78 deletions

View File

@@ -15,31 +15,16 @@ class DmailTest < ActiveSupport::TestCase
CurrentUser.user = nil
end
context "spam" do
setup do
Dmail.any_instance.stubs(:spam?).returns(true)
@spammer = create(:user)
context "that is spam" do
should "be automatically reported and deleted" do
@recipient = create(:user)
end
@spammer = create(:user, created_at: 2.weeks.ago, email: "akismet-guaranteed-spam@example.com")
should "not validate" do
assert_difference("Dmail.count", 2) do
Dmail.create_split(from: @spammer, to: @recipient, title: "spam", body: "wonderful spam")
assert(@recipient.dmails.last.is_spam?)
end
end
SpamDetector.stubs(:enabled?).returns(true)
dmail = create(:dmail, owner: @recipient, from: @spammer, to: @recipient, creator_ip_addr: "127.0.0.1")
should "autoban spammers after sending spam to N distinct users" do
users = create_list(:user, Dmail::AUTOBAN_THRESHOLD)
users.each do |user|
Dmail.create_split(from: @spammer, to: user, title: "spam", body: "wonderful spam")
end
assert_equal(true, Dmail.is_spammer?(@spammer))
assert_equal(true, @spammer.reload.is_banned)
assert_equal(1, @spammer.bans.count)
assert_match(/Spambot./, @spammer.bans.last.reason)
assert_match(/Spambot./, @spammer.feedback.last.body)
assert_equal(1, dmail.moderation_reports.count)
assert_equal(true, dmail.reload.is_deleted?)
end
end

View File

@@ -16,7 +16,6 @@ class SpamDetectorTest < ActiveSupport::TestCase
dmail = @user.dmails.last
assert(SpamDetector.new(dmail).spam?)
assert(dmail.is_spam?)
end
should "not detect gold users as spammers" do
@@ -24,7 +23,6 @@ class SpamDetectorTest < ActiveSupport::TestCase
dmail = @spammer.dmails.last
refute(SpamDetector.new(dmail).spam?)
refute(dmail.is_spam?)
end
should "not detect old users as spammers" do
@@ -33,20 +31,27 @@ class SpamDetectorTest < ActiveSupport::TestCase
dmail = @spammer.dmails.last
refute(SpamDetector.new(dmail).spam?)
refute(dmail.is_spam?)
end
should "log a message when spam is detected" do
Rails.logger.expects(:info)
should "generate a moderation report when spam is detected" do
Dmail.create_split(from: @spammer, to: @user, title: "spam", body: "wonderful spam", creator_ip_addr: "127.0.0.1")
assert_equal(1, @user.dmails.last.moderation_reports.count)
end
should "pass messages through if akismet is down" do
Rakismet.expects(:akismet_call).raises(StandardError)
Rakismet.stubs(:akismet_call).raises(StandardError)
dmail = create(:dmail, from: @spammer, to: @user, owner: @user, title: "spam", body: "wonderful spam", creator_ip_addr: "127.0.0.1")
refute(SpamDetector.new(dmail).spam?)
end
should "autoban the user if they send too many spam dmails" do
count = SpamDetector::AUTOBAN_THRESHOLD
dmails = create_list(:dmail, count, from: @spammer, to: @user, owner: @user, creator_ip_addr: "127.0.0.1")
assert_equal(count, ModerationReport.where(model: Dmail.sent_by(@spammer)).count)
assert_equal(true, @spammer.reload.is_banned?)
end
end
context "for forum posts" do
@@ -54,45 +59,41 @@ class SpamDetectorTest < ActiveSupport::TestCase
@forum_topic = as(@user) { create(:forum_topic) }
end
should "detect spam" do
should "generate a moderation report when spam is detected" do
as(@spammer) do
forum_post = build(:forum_post, topic: @forum_topic)
forum_post.validate
forum_post = create(:forum_post, creator: @spammer, topic: @forum_topic)
assert(SpamDetector.new(forum_post, user_ip: "127.0.0.1").spam?)
assert(forum_post.invalid?)
assert_equal(["Failed to create forum post"], forum_post.errors.full_messages)
assert_equal(1, forum_post.moderation_reports.count)
end
end
should "not detect gold users as spammers" do
as(@user) do
forum_post = create(:forum_post, topic: @forum_topic)
forum_post = create(:forum_post, creator: @user, topic: @forum_topic)
refute(SpamDetector.new(forum_post).spam?)
assert(forum_post.valid?)
assert_equal(0, forum_post.moderation_reports.count)
end
end
end
context "for comments" do
should "detect spam" do
should "generate a moderation report when spam is detected" do
as(@spammer) do
comment = build(:comment)
comment.validate
comment = create(:comment, creator: @spammer)
assert(SpamDetector.new(comment).spam?)
assert(comment.invalid?)
assert_equal(["Failed to create comment"], comment.errors.full_messages)
assert_equal(1, comment.moderation_reports.count)
end
end
should "not detect gold users as spammers" do
as(@user) do
comment = create(:comment)
comment = create(:comment, creator: @user)
refute(SpamDetector.new(comment).spam?)
assert(comment.valid?)
assert_equal(0, comment.moderation_reports.count)
end
end
end