2013-05-31 23:41:40 +08:00
# encoding: UTF-8
2015-10-11 17:41:23 +08:00
require 'rails_helper'
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
describe SpamRulesEnforcer do
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
describe 'auto-blocking users based on flagging' do
before do
SiteSetting . stubs ( :flags_required_to_hide_post ) . returns ( 0 ) # never
2016-06-16 01:18:54 +08:00
SiteSetting . stubs ( :num_spam_flags_to_block_new_user ) . returns ( 2 )
2013-10-12 01:33:23 +08:00
SiteSetting . stubs ( :num_users_to_block_new_user ) . returns ( 2 )
end
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
Given ! ( :admin ) { Fabricate ( :admin ) } # needed to send a system message
Given ! ( :moderator ) { Fabricate ( :moderator ) }
Given ( :user1 ) { Fabricate ( :user ) }
Given ( :user2 ) { Fabricate ( :user ) }
context 'spammer is a new user' do
2014-09-05 13:20:39 +08:00
Given ( :spammer ) { Fabricate ( :user , trust_level : TrustLevel [ 0 ] ) }
2013-10-12 01:33:23 +08:00
context 'spammer post is not flagged enough times' do
Given ! ( :spam_post ) { create_post ( user : spammer ) }
Given ! ( :spam_post2 ) { create_post ( user : spammer ) }
When { PostAction . act ( user1 , spam_post , PostActionType . types [ :spam ] ) }
Then { expect ( spam_post . reload ) . to_not be_hidden }
context 'spam posts are flagged enough times, but not by enough users' do
When { PostAction . act ( user1 , spam_post2 , PostActionType . types [ :spam ] ) }
Then { expect ( spam_post . reload ) . to_not be_hidden }
And { expect ( spam_post2 . reload ) . to_not be_hidden }
And { expect ( spammer . reload ) . to_not be_blocked }
end
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
context 'one spam post is flagged enough times by enough users' do
Given ! ( :another_topic ) { Fabricate ( :topic ) }
Given ! ( :private_messages_count ) { spammer . private_topics_count }
Given ! ( :mod_pm_count ) { moderator . private_topics_count }
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
When { PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] ) }
2013-05-31 23:41:40 +08:00
2014-11-07 22:05:44 +08:00
Invariant { expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be false }
2013-10-12 01:33:23 +08:00
Invariant { expect { PostCreator . create ( spammer , { title : 'limited time offer for you' , raw : 'better buy this stuff ok' , archetype_id : 1 } ) } . to raise_error ( Discourse :: InvalidAccess ) }
2015-04-25 23:18:35 +08:00
Invariant { expect ( PostCreator . create ( spammer , { topic_id : another_topic . id , raw : 'my reply is spam in your topic' , archetype_id : 1 } ) ) . to eq ( nil ) }
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
Then { expect ( spammer . reload ) . to be_blocked }
And { expect ( spam_post . reload ) . to be_hidden }
And { expect ( spam_post2 . reload ) . to be_hidden }
And { expect ( spammer . reload . private_topics_count ) . to eq ( private_messages_count + 1 ) }
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08: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 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
context " a post's flags are cleared " do
When { PostAction . clear_flags! ( spam_post , admin ) ; spammer . reload }
Then { expect ( spammer . reload ) . to be_blocked }
end
2013-05-31 23:41:40 +08:00
2013-10-12 01:33:23 +08:00
context " a post is deleted " do
When { spam_post . trash! ( moderator ) ; spammer . reload }
Then { expect ( spammer . reload ) . to be_blocked }
end
2013-05-31 23:41:40 +08:00
2014-09-05 04:32:00 +08:00
context " spammer becomes trust level 1 " do
2014-09-05 13:20:39 +08:00
When { spammer . change_trust_level! ( TrustLevel [ 1 ] ) ; spammer . reload }
2013-10-12 01:33:23 +08:00
Then { expect ( spammer . reload ) . to be_blocked }
end
2013-05-31 23:41:40 +08:00
end
2013-10-12 01:33:23 +08:00
context 'flags_required_to_hide_post takes effect too' do
Given { SiteSetting . stubs ( :flags_required_to_hide_post ) . returns ( 2 ) }
When { PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] ) }
Then { expect ( spammer . reload ) . to be_blocked }
2014-11-07 22:05:44 +08:00
And { expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be false }
2013-05-31 23:41:40 +08:00
end
end
end
2013-10-12 01:33:23 +08:00
context " spammer has trust level basic " do
2014-09-05 13:20:39 +08:00
Given ( :spammer ) { Fabricate ( :user , trust_level : TrustLevel [ 1 ] ) }
2013-10-12 01:33:23 +08:00
context 'one spam post is flagged enough times by enough users' do
Given ! ( :spam_post ) { Fabricate ( :post , user : spammer ) }
Given ! ( :private_messages_count ) { spammer . private_topics_count }
When { PostAction . act ( user1 , spam_post , PostActionType . types [ :spam ] ) }
When { PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] ) }
Then { expect ( spam_post . reload ) . to_not be_hidden }
2014-11-07 22:05:44 +08:00
And { expect ( Guardian . new ( spammer ) . can_create_topic? ( nil ) ) . to be true }
2013-10-12 01:33:23 +08:00
And { expect { PostCreator . create ( spammer , { title : 'limited time offer for you' , raw : 'better buy this stuff ok' , archetype_id : 1 } ) } . to_not raise_error }
And { expect ( spammer . reload . private_topics_count ) . to eq ( private_messages_count ) }
end
2013-05-31 23:41:40 +08:00
end
2014-09-05 13:20:39 +08:00
[ [ :user , trust_level : TrustLevel [ 2 ] ] , [ :admin ] , [ :moderator ] ] . each do | spammer_args |
2013-10-12 01:33:23 +08:00
context " spammer is trusted #{ spammer_args [ 0 ] } " do
Given ! ( :spammer ) { Fabricate ( * spammer_args ) }
Given ! ( :spam_post ) { Fabricate ( :post , user : spammer ) }
Given ! ( :private_messages_count ) { spammer . private_topics_count }
When { PostAction . act ( user1 , spam_post , PostActionType . types [ :spam ] ) }
When { PostAction . act ( user2 , spam_post , PostActionType . types [ :spam ] ) }
Then { expect ( spam_post . reload ) . to_not be_hidden }
end
2013-05-31 23:41:40 +08:00
end
end
end