2013-05-31 11:41:40 -04:00
# encoding: UTF-8
2015-10-11 05:41:23 -04:00
require 'rails_helper'
2013-05-31 11:41:40 -04:00
2013-10-11 13:33:23 -04:00
describe SpamRulesEnforcer do
2013-05-31 11:41:40 -04:00
2013-10-11 13:33:23 -04:00
describe 'auto-blocking users based on flagging' do
2016-12-12 20:59:38 -05:00
site_setting ( :flags_required_to_hide_post , 0 )
site_setting ( :num_spam_flags_to_block_new_user , 2 )
site_setting ( :num_users_to_block_new_user , 2 )
2013-05-31 11:41:40 -04:00
2016-12-12 20:59:38 -05:00
let! ( :admin ) { Fabricate ( :admin ) } # needed to send a system message
let! ( :moderator ) { Fabricate ( :moderator ) }
let ( :user1 ) { Fabricate ( :user ) }
let ( :user2 ) { Fabricate ( :user ) }
2013-10-11 13:33:23 -04:00
context 'spammer is a new user' do
2016-12-12 20:59:38 -05:00
let ( :spammer ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2013-10-11 13:33:23 -04:00
context 'spammer post is not flagged enough times' do
2016-12-12 20:59:38 -05:00
let! ( :spam_post ) { create_post ( user : spammer ) }
let! ( :spam_post2 ) { create_post ( user : spammer ) }
before do
PostAction . act ( user1 , spam_post , PostActionType . types [ :spam ] )
end
it 'should not hide the post' do
expect ( spam_post . reload ) . to_not be_hidden
end
2013-10-11 13:33:23 -04:00
context 'spam posts are flagged enough times, but not by enough users' do
2016-12-12 20:59:38 -05:00
it 'should not hide the post' do
PostAction . act ( user1 , spam_post2 , PostActionType . types [ :spam ] )
expect ( spam_post . reload ) . to_not be_hidden
expect ( spam_post2 . reload ) . to_not be_hidden
expect ( spammer . reload ) . to_not be_blocked
end
2013-10-11 13:33:23 -04:00
end
2013-05-31 11:41:40 -04:00
2013-10-11 13:33:23 -04:00
context 'one spam post is flagged enough times by enough users' do
2016-12-12 20:59:38 -05:00
let! ( :another_topic ) { Fabricate ( :topic ) }
let! ( :private_messages_count ) { spammer . private_topics_count }
let! ( :mod_pm_count ) { moderator . private_topics_count }
2013-05-31 11:41:40 -04:00
2016-12-12 20:59:38 -05:00
before do
PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] )
2013-05-31 11:41:40 -04:00
2016-12-12 20:59:38 -05:00
expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be ( false )
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 )
end
2013-05-31 11:41:40 -04:00
2016-12-12 20:59:38 -05:00
it 'should hide the posts' do
expect ( spammer . reload ) . to be_blocked
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 11:41:40 -04:00
2013-10-11 13:33:23 -04:00
# The following cases describe when a staff user takes some action, but the user
# still won't be able to make posts.
# A staff user needs to clear the blocked flag from the user record.
2013-05-31 11:41:40 -04:00
2013-10-11 13:33:23 -04:00
context " a post's flags are cleared " do
2016-12-12 20:59:38 -05:00
it 'should block the spammer' do
PostAction . clear_flags! ( spam_post , admin ) ; spammer . reload
expect ( spammer . reload ) . to be_blocked
end
2013-10-11 13:33:23 -04:00
end
2013-05-31 11:41:40 -04:00
2013-10-11 13:33:23 -04:00
context " a post is deleted " do
2016-12-12 20:59:38 -05:00
it 'should block the spammer' do
spam_post . trash! ( moderator ) ; spammer . reload
expect ( spammer . reload ) . to be_blocked
end
2013-10-11 13:33:23 -04:00
end
2013-05-31 11:41:40 -04:00
2014-09-04 16:32:00 -04:00
context " spammer becomes trust level 1 " do
2016-12-12 20:59:38 -05:00
it 'should block the spammer' do
spammer . change_trust_level! ( TrustLevel [ 1 ] ) ; spammer . reload
expect ( spammer . reload ) . to be_blocked
end
2013-10-11 13:33:23 -04:00
end
2013-05-31 11:41:40 -04:00
end
2013-10-11 13:33:23 -04:00
context 'flags_required_to_hide_post takes effect too' do
2016-12-12 20:59:38 -05:00
site_setting ( :flags_required_to_hide_post , 2 )
it 'should block the spammer' do
PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] )
expect ( spammer . reload ) . to be_blocked
expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be false
end
2013-05-31 11:41:40 -04:00
end
end
end
2013-10-11 13:33:23 -04:00
context " spammer has trust level basic " do
2016-12-12 20:59:38 -05:00
let ( :spammer ) { Fabricate ( :user , trust_level : TrustLevel [ 1 ] ) }
2013-10-11 13:33:23 -04:00
context 'one spam post is flagged enough times by enough users' do
2016-12-12 20:59:38 -05: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
PostAction . act ( user1 , spam_post , PostActionType . types [ :spam ] )
PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] )
expect ( spam_post . reload ) . to_not be_hidden
expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be ( true )
expect { PostCreator . create ( spammer , { title : 'limited time offer for you' , raw : 'better buy this stuff ok' , archetype_id : 1 } ) } . to_not raise_error
expect ( spammer . reload . private_topics_count ) . to eq ( private_messages_count )
end
2013-10-11 13:33:23 -04:00
end
2013-05-31 11:41:40 -04:00
end
2014-09-05 01:20:39 -04:00
[ [ :user , trust_level : TrustLevel [ 2 ] ] , [ :admin ] , [ :moderator ] ] . each do | spammer_args |
2013-10-11 13:33:23 -04:00
context " spammer is trusted #{ spammer_args [ 0 ] } " do
2016-12-12 20:59:38 -05: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
PostAction . act ( user1 , spam_post , PostActionType . types [ :spam ] )
PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] )
expect ( spam_post . reload ) . to_not be_hidden
end
2013-10-11 13:33:23 -04:00
end
2013-05-31 11:41:40 -04:00
end
end
end