2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe SpamRule::AutoSilence do
|
2013-10-25 13:25:02 -04:00
|
|
|
before do
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.hide_post_sensitivity = Reviewable.sensitivities[:disabled]
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 4.0)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.silence_new_user_sensitivity = Reviewable.sensitivities[:low]
|
2017-11-10 12:18:08 -05:00
|
|
|
SiteSetting.num_users_to_silence_new_user = 2
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "perform" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:autosilence) { described_class.new(post.user) }
|
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
let(:user) { Fabricate.build(:newuser) }
|
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-10-25 13:25:02 -04:00
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
it "takes no action if user should not be silenced" do
|
2023-06-21 10:00:19 -04:00
|
|
|
autosilence.perform
|
2019-02-07 13:46:05 -05:00
|
|
|
expect(post.user.reload).not_to be_silenced
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
it "delivers punishment when user should be silenced" do
|
2019-05-24 14:13:03 -04:00
|
|
|
Reviewable.set_priorities(high: 2.0)
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.silence_new_user_sensitivity = Reviewable.sensitivities[:low]
|
2019-02-07 13:46:05 -05:00
|
|
|
SiteSetting.num_users_to_silence_new_user = 1
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(Discourse.system_user, post)
|
2023-06-21 10:00:19 -04:00
|
|
|
autosilence.perform
|
2019-02-07 13:46:05 -05:00
|
|
|
expect(post.user.reload).to be_silenced
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
describe "total_spam_score" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:score) { enforcer.user_spam_stats.total_spam_score }
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
let(:post) { Fabricate(:post) }
|
2013-10-25 13:25:02 -04:00
|
|
|
let(:enforcer) { described_class.new(post.user) }
|
2019-01-03 12:03:01 -05:00
|
|
|
let(:flagger) { Fabricate(:user) }
|
2013-10-25 13:25:02 -04:00
|
|
|
|
|
|
|
it "returns 0 when there are no flags" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(score).to eq(0)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 0 when there is one flag that has a reason other than spam" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.off_topic(flagger, post)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(score).to eq(0)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
it "returns the score when there are two flags with spam as the reason" do
|
2023-12-13 04:18:42 -05:00
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post)
|
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(score).to eq(4.0)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
it "returns the score when there are two spam flags, each on a different post" do
|
2023-12-13 04:18:42 -05:00
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post)
|
|
|
|
PostActionCreator.spam(
|
|
|
|
Fabricate(:user, refresh_auto_groups: true),
|
|
|
|
Fabricate(:post, user: post.user),
|
|
|
|
)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(score).to eq(4.0)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
describe "spam_user_count" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:count) { enforcer.user_spam_stats.spam_user_count }
|
|
|
|
|
2013-10-25 13:25:02 -04:00
|
|
|
let(:post) { Fabricate(:post) }
|
|
|
|
let(:enforcer) { described_class.new(post.user) }
|
|
|
|
|
|
|
|
it "returns 0 when there are no flags" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(count).to eq(0)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 0 when there is one flag that has a reason other than spam" do
|
2024-05-22 22:19:07 -04:00
|
|
|
Fabricate(
|
|
|
|
:flag_post_action,
|
|
|
|
post: post,
|
|
|
|
post_action_type_id: PostActionType.types[:off_topic],
|
|
|
|
)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(count).to eq(0)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 1 when there is one spam flag" do
|
2023-12-13 04:18:42 -05:00
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(count).to eq(1)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 2 when there are two spam flags from 2 users" do
|
2023-12-13 04:18:42 -05:00
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post)
|
|
|
|
PostActionCreator.spam(Fabricate(:user, refresh_auto_groups: true), post)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(count).to eq(2)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 1 when there are two spam flags on two different posts from 1 user" do
|
2023-12-13 04:18:42 -05:00
|
|
|
flagger = Fabricate(:user, refresh_auto_groups: true)
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
PostActionCreator.spam(flagger, Fabricate(:post, user: post.user))
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(count).to eq(1)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
describe "#silence_user" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:autosilence) { described_class.new(user) }
|
|
|
|
|
2013-10-25 13:25:02 -04:00
|
|
|
let!(:admin) { Fabricate(:admin) } # needed for SystemMessage
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let!(:post) { Fabricate(:post, user: user) }
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user is not silenced" do
|
2013-10-25 13:25:02 -04:00
|
|
|
it "prevents the user from making new posts" do
|
2023-06-21 10:00:19 -04:00
|
|
|
autosilence.silence_user
|
2019-02-07 13:46:05 -05:00
|
|
|
expect(user).to be_silenced
|
2014-09-25 11:44:48 -04:00
|
|
|
expect(Guardian.new(user).can_create_post?(nil)).to be_falsey
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
context "with a moderator" do
|
|
|
|
let!(:moderator) { Fabricate(:moderator) }
|
|
|
|
|
|
|
|
it "sends private message to moderators" do
|
|
|
|
SiteSetting.notify_mods_when_user_silenced = true
|
2023-06-21 10:00:19 -04:00
|
|
|
autosilence.silence_user
|
|
|
|
expect(autosilence.group_message).to be_present
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
it "doesn't send a pm to moderators if notify_mods_when_user_silenced is false" do
|
|
|
|
SiteSetting.notify_mods_when_user_silenced = false
|
2023-06-21 10:00:19 -04:00
|
|
|
autosilence.silence_user
|
|
|
|
expect(autosilence.group_message).to be_blank
|
2019-02-07 13:46:05 -05:00
|
|
|
end
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when user is already silenced" do
|
2019-02-07 13:46:05 -05:00
|
|
|
before { UserSilencer.silence(user) }
|
2013-10-25 13:25:02 -04:00
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
it "doesn't send a pm to moderators if the user is already silenced" do
|
2023-06-21 10:00:19 -04:00
|
|
|
autosilence.silence_user
|
|
|
|
expect(autosilence.group_message).to be_blank
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
describe "autosilenced?" do
|
|
|
|
let(:user) { Fabricate(:newuser) }
|
2023-12-13 04:18:42 -05:00
|
|
|
let(:flagger) { Fabricate(:user, refresh_auto_groups: true) }
|
|
|
|
let(:flagger2) { Fabricate(:user, refresh_auto_groups: true) }
|
2019-02-07 13:46:05 -05:00
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
|
|
|
let(:post2) { Fabricate(:post, user: user) }
|
2013-10-25 13:25:02 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with higher trust levels or staff" do
|
2019-02-07 13:46:05 -05:00
|
|
|
it "should not autosilence any of them" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
PostActionCreator.spam(flagger2, post)
|
2013-10-25 13:25:02 -04:00
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
enforcer = described_class.new(user)
|
|
|
|
expect(enforcer.should_autosilence?).to eq(true)
|
|
|
|
|
|
|
|
user.trust_level = 1
|
|
|
|
expect(enforcer.should_autosilence?).to eq(false)
|
|
|
|
|
|
|
|
user.trust_level = 2
|
|
|
|
expect(enforcer.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
|
2019-02-07 13:46:05 -05:00
|
|
|
user.trust_level = 3
|
|
|
|
expect(enforcer.should_autosilence?).to eq(false)
|
|
|
|
|
|
|
|
user.trust_level = 4
|
|
|
|
expect(enforcer.should_autosilence?).to eq(false)
|
|
|
|
|
|
|
|
user.trust_level = 0
|
|
|
|
user.moderator = true
|
|
|
|
expect(enforcer.should_autosilence?).to eq(false)
|
|
|
|
|
|
|
|
user.moderator = false
|
|
|
|
user.admin = true
|
|
|
|
expect(enforcer.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with new user" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:autosilence) { described_class.new(user) }
|
|
|
|
|
|
|
|
let(:stats) { autosilence.user_spam_stats }
|
2013-10-25 13:25:02 -04:00
|
|
|
|
|
|
|
it "returns false if there are no spam flags" do
|
2019-01-03 12:03:01 -05:00
|
|
|
expect(stats.total_spam_score).to eq(0)
|
|
|
|
expect(stats.spam_user_count).to eq(0)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if there are not received enough flags" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
expect(stats.total_spam_score).to eq(2.0)
|
|
|
|
expect(stats.spam_user_count).to eq(1)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false if there have not been enough users" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
PostActionCreator.spam(flagger, post2)
|
|
|
|
expect(stats.total_spam_score).to eq(4.0)
|
|
|
|
expect(stats.spam_user_count).to eq(1)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2019-05-24 14:13:03 -04:00
|
|
|
it "returns false if silence_new_user_sensitivity is disabled" do
|
2021-12-08 12:12:24 -05:00
|
|
|
SiteSetting.silence_new_user_sensitivity = Reviewable.sensitivities[:disabled]
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
PostActionCreator.spam(flagger2, post)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
it "returns false if num_users_to_silence_new_user is 0" do
|
|
|
|
SiteSetting.num_users_to_silence_new_user = 0
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
PostActionCreator.spam(flagger2, post)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when there are enough flags from enough users" do
|
2019-01-03 12:03:01 -05:00
|
|
|
PostActionCreator.spam(flagger, post)
|
|
|
|
PostActionCreator.spam(flagger2, post)
|
|
|
|
expect(stats.total_spam_score).to eq(4.0)
|
|
|
|
expect(stats.spam_user_count).to eq(2)
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(true)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when silenced, but has higher trust level now" do
|
2023-06-21 10:00:19 -04:00
|
|
|
subject(:autosilence) { described_class.new(user) }
|
|
|
|
|
2017-11-13 13:41:36 -05:00
|
|
|
let(:user) { Fabricate(:user, silenced_till: 1.year.from_now, trust_level: TrustLevel[1]) }
|
2013-10-25 13:25:02 -04:00
|
|
|
|
|
|
|
it "returns false" do
|
2023-06-21 10:00:19 -04:00
|
|
|
expect(autosilence.should_autosilence?).to eq(false)
|
2013-10-25 13:25:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|