2013-05-31 10:41:40 -05:00
# encoding: UTF-8
2019-04-29 19:27:42 -05:00
# frozen_string_literal: true
2013-05-31 10:41:40 -05:00
2022-07-27 21:27:38 -05:00
RSpec . describe " spam rules for users " do
2013-05-31 10:41:40 -05:00
2019-02-07 12:46:05 -06:00
describe 'auto-silence users based on flagging' do
2019-05-06 22:12:20 -05:00
fab! ( :admin ) { Fabricate ( :admin ) } # needed to send a system message
fab! ( :moderator ) { Fabricate ( :moderator ) }
fab! ( :user1 ) { Fabricate ( :user ) }
fab! ( :user2 ) { Fabricate ( :user ) }
2013-10-11 12:33:23 -05:00
2017-01-27 20:55:49 -06:00
before do
2019-05-24 13:13:03 -05:00
SiteSetting . hide_post_sensitivity = Reviewable . sensitivity [ :disabled ]
Reviewable . set_priorities ( high : 4 . 0 )
SiteSetting . silence_new_user_sensitivity = Reviewable . sensitivity [ :low ]
2017-11-10 11:18:08 -06:00
SiteSetting . num_users_to_silence_new_user = 2
2017-01-27 20:55:49 -06:00
end
2013-10-11 12:33:23 -05:00
context 'spammer is a new user' do
2019-05-06 22:12:20 -05:00
fab! ( :spammer ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2013-10-11 12:33:23 -05:00
context 'spammer post is not flagged enough times' do
2016-12-12 19:59:38 -06:00
let! ( :spam_post ) { create_post ( user : spammer ) }
let! ( :spam_post2 ) { create_post ( user : spammer ) }
before do
2019-01-03 11:03:01 -06:00
PostActionCreator . create ( user1 , spam_post , :spam )
2016-12-12 19:59:38 -06:00
end
it 'should not hide the post' do
expect ( spam_post . reload ) . to_not be_hidden
end
2013-10-11 12:33:23 -05:00
context 'spam posts are flagged enough times, but not by enough users' do
2016-12-12 19:59:38 -06:00
it 'should not hide the post' do
2019-01-03 11:03:01 -06:00
PostActionCreator . create ( user1 , spam_post2 , :spam )
2016-12-12 19:59:38 -06:00
expect ( spam_post . reload ) . to_not be_hidden
expect ( spam_post2 . reload ) . to_not be_hidden
2017-11-10 11:18:08 -06:00
expect ( spammer . reload ) . to_not be_silenced
2016-12-12 19:59:38 -06:00
end
2013-10-11 12:33:23 -05:00
end
2013-05-31 10:41:40 -05:00
2013-10-11 12:33:23 -05:00
context 'one spam post is flagged enough times by enough users' do
2019-05-06 22:12:20 -05:00
fab! ( :another_topic ) { Fabricate ( :topic ) }
2016-12-12 19:59:38 -06:00
let! ( :private_messages_count ) { spammer . private_topics_count }
2019-01-03 11:03:01 -06:00
let! ( :mod_pm_count ) { moderator . private_topics_count }
let! ( :reviewable ) { PostActionCreator . spam ( user2 , spam_post ) . reviewable }
2013-05-31 10:41:40 -05:00
2019-01-03 11:03:01 -06:00
it 'should hide the posts' do
2016-12-12 19:59:38 -06:00
expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be ( false )
2017-07-27 20:20:09 -05:00
expect { PostCreator . create ( spammer , title : 'limited time offer for you' , raw : 'better buy this stuff ok' , archetype_id : 1 ) } . to raise_error ( Discourse :: InvalidAccess )
expect ( PostCreator . create ( spammer , topic_id : another_topic . id , raw : 'my reply is spam in your topic' , archetype_id : 1 ) ) . to eq ( nil )
2017-11-10 11:18:08 -06:00
expect ( spammer . reload ) . to be_silenced
2016-12-12 19:59:38 -06:00
expect ( spam_post . reload ) . to be_hidden
expect ( spam_post2 . reload ) . to be_hidden
expect ( spammer . reload . private_topics_count ) . to eq ( private_messages_count + 1 )
end
2013-05-31 10:41:40 -05:00
2013-10-11 12:33:23 -05:00
context " a post is deleted " do
2017-11-10 11:18:08 -06:00
it 'should silence the spammer' do
2016-12-12 19:59:38 -06:00
spam_post . trash! ( moderator ) ; spammer . reload
2017-11-10 11:18:08 -06:00
expect ( spammer . reload ) . to be_silenced
2016-12-12 19:59:38 -06:00
end
2013-10-11 12:33:23 -05:00
end
2013-05-31 10:41:40 -05:00
2014-09-04 15:32:00 -05:00
context " spammer becomes trust level 1 " do
2017-11-10 11:18:08 -06:00
it 'should silence the spammer' do
2016-12-12 19:59:38 -06:00
spammer . change_trust_level! ( TrustLevel [ 1 ] ) ; spammer . reload
2017-11-10 11:18:08 -06:00
expect ( spammer . reload ) . to be_silenced
2016-12-12 19:59:38 -06:00
end
2013-10-11 12:33:23 -05:00
end
2013-05-31 10:41:40 -05:00
end
2019-05-24 13:13:03 -05:00
context 'hide_post_sensitivity' do
2017-11-10 11:18:08 -06:00
it 'should silence the spammer' do
2019-05-24 13:13:03 -05:00
Reviewable . set_priorities ( high : 2 . 0 )
SiteSetting . hide_post_sensitivity = Reviewable . sensitivity [ :low ]
2019-01-03 11:03:01 -06:00
PostActionCreator . create ( user2 , spam_post , :spam )
2017-11-10 11:18:08 -06:00
expect ( spammer . reload ) . to be_silenced
2016-12-12 19:59:38 -06:00
expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be false
end
2013-05-31 10:41:40 -05:00
end
end
end
2013-10-11 12:33:23 -05:00
context " spammer has trust level basic " do
2016-12-12 19:59:38 -06:00
let ( :spammer ) { Fabricate ( :user , trust_level : TrustLevel [ 1 ] ) }
2013-10-11 12:33:23 -05:00
context 'one spam post is flagged enough times by enough users' do
2016-12-12 19:59:38 -06:00
let! ( :spam_post ) { Fabricate ( :post , user : spammer ) }
let! ( :private_messages_count ) { spammer . private_topics_count }
it 'should not allow spammer to create new posts' do
2019-01-03 11:03:01 -06:00
PostActionCreator . create ( user1 , spam_post , :spam )
PostActionCreator . create ( user2 , spam_post , :spam )
2016-12-12 19:59:38 -06:00
expect ( spam_post . reload ) . to_not be_hidden
expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be ( true )
2017-07-27 20:20:09 -05:00
expect { PostCreator . create ( spammer , title : 'limited time offer for you' , raw : 'better buy this stuff ok' , archetype_id : 1 ) } . to_not raise_error
2016-12-12 19:59:38 -06:00
expect ( spammer . reload . private_topics_count ) . to eq ( private_messages_count )
end
2013-10-11 12:33:23 -05:00
end
2013-05-31 10:41:40 -05:00
end
2014-09-05 00:20:39 -05:00
[ [ :user , trust_level : TrustLevel [ 2 ] ] , [ :admin ] , [ :moderator ] ] . each do | spammer_args |
2013-10-11 12:33:23 -05:00
context " spammer is trusted #{ spammer_args [ 0 ] } " do
2016-12-12 19:59:38 -06:00
let! ( :spammer ) { Fabricate ( * spammer_args ) }
let! ( :spam_post ) { Fabricate ( :post , user : spammer ) }
let! ( :private_messages_count ) { spammer . private_topics_count }
it 'should not hide the post' do
2019-01-03 11:03:01 -06:00
PostActionCreator . create ( user1 , spam_post , :spam )
PostActionCreator . create ( user2 , spam_post , :spam )
2016-12-12 19:59:38 -06:00
expect ( spam_post . reload ) . to_not be_hidden
end
2013-10-11 12:33:23 -05:00
end
2013-05-31 10:41:40 -05:00
end
end
end