2019-04-29 20:27:42 -04:00
# frozen_string_literal: true
2017-06-28 16:56:44 -04:00
require 'rails_helper'
describe WatchedWord do
2019-05-06 23:12:20 -04:00
fab! ( :tl2_user ) { Fabricate ( :user , trust_level : TrustLevel [ 2 ] ) }
fab! ( :admin ) { Fabricate ( :admin ) }
fab! ( :moderator ) { Fabricate ( :moderator ) }
2017-06-28 16:56:44 -04:00
2019-05-06 23:12:20 -04:00
fab! ( :topic ) { Fabricate ( :topic ) }
fab! ( :first_post ) { Fabricate ( :post , topic : topic ) }
2017-06-28 16:56:44 -04:00
let ( :require_approval_word ) { Fabricate ( :watched_word , action : WatchedWord . actions [ :require_approval ] ) }
let ( :flag_word ) { Fabricate ( :watched_word , action : WatchedWord . actions [ :flag ] ) }
let ( :block_word ) { Fabricate ( :watched_word , action : WatchedWord . actions [ :block ] ) }
2017-07-27 12:26:55 -04:00
after do
$redis . flushall
end
2017-06-28 16:56:44 -04:00
context " block " do
def should_block_post ( manager )
expect {
result = manager . perform
expect ( result ) . to_not be_success
2018-02-28 11:22:04 -05:00
expect ( result . errors [ :base ] & . first ) . to eq ( I18n . t ( 'contains_blocked_words' , word : block_word . word ) )
2017-06-28 16:56:44 -04:00
} . to_not change { Post . count }
end
it " should prevent the post from being created " do
manager = NewPostManager . new ( tl2_user , raw : " Want some #{ block_word . word } for cheap? " , topic_id : topic . id )
should_block_post ( manager )
end
it " look at title too " do
manager = NewPostManager . new ( tl2_user , title : " We sell #{ block_word . word } online " , raw : " Want some poutine for cheap? " , topic_id : topic . id )
should_block_post ( manager )
end
it " should not block the post from admin " do
manager = NewPostManager . new ( admin , raw : " Want some #{ block_word . word } for cheap? " , topic_id : topic . id )
result = manager . perform
expect ( result ) . to be_success
expect ( result . action ) . to eq ( :create_post )
end
it " should not block the post from moderator " do
manager = NewPostManager . new ( moderator , raw : " Want some #{ block_word . word } for cheap? " , topic_id : topic . id )
result = manager . perform
expect ( result ) . to be_success
expect ( result . action ) . to eq ( :create_post )
end
it " should block in a private message too " do
manager = NewPostManager . new (
tl2_user ,
raw : " Want some #{ block_word . word } for cheap? " ,
title : 'this is a new title' ,
archetype : Archetype . private_message ,
target_usernames : Fabricate ( :user , trust_level : TrustLevel [ 2 ] ) . username
)
should_block_post ( manager )
end
it " blocks on revisions " do
2019-05-08 20:15:38 -04:00
post = Fabricate ( :post_with_validation , topic : Fabricate ( :topic , user : tl2_user ) , user : tl2_user )
2017-06-28 16:56:44 -04:00
expect {
PostRevisor . new ( post ) . revise! ( post . user , { raw : " Want some #{ block_word . word } for cheap? " } , revised_at : post . updated_at + 10 . seconds )
expect ( post . errors ) . to be_present
post . reload
} . to_not change { post . raw }
end
end
context " require_approval " do
it " should queue the post for approval " do
manager = NewPostManager . new ( tl2_user , raw : " My dog's name is #{ require_approval_word . word } . " , topic_id : topic . id )
result = manager . perform
expect ( result . action ) . to eq ( :enqueued )
2019-04-10 17:42:49 -04:00
expect ( result . reason ) . to eq ( :watched_word )
2017-06-28 16:56:44 -04:00
end
it " looks at title too " do
manager = NewPostManager . new ( tl2_user , title : " You won't believe these #{ require_approval_word . word } dog names! " , raw : " My dog's name is Porkins. " , topic_id : topic . id )
result = manager . perform
expect ( result . action ) . to eq ( :enqueued )
end
it " should not queue posts from admin " do
manager = NewPostManager . new ( admin , raw : " My dog's name is #{ require_approval_word . word } . " , topic_id : topic . id )
result = manager . perform
expect ( result ) . to be_success
expect ( result . action ) . to eq ( :create_post )
end
it " should not queue posts from moderator " do
manager = NewPostManager . new ( moderator , raw : " My dog's name is #{ require_approval_word . word } . " , topic_id : topic . id )
result = manager . perform
expect ( result ) . to be_success
expect ( result . action ) . to eq ( :create_post )
end
it " doesn't need approval in a private message " do
manager = NewPostManager . new (
tl2_user ,
raw : " Want some #{ require_approval_word . word } for cheap? " ,
title : 'this is a new title' ,
archetype : Archetype . private_message ,
target_usernames : Fabricate ( :user , trust_level : TrustLevel [ 2 ] ) . username
)
result = manager . perform
expect ( result ) . to be_success
expect ( result . action ) . to eq ( :create_post )
end
end
context " flag " do
def should_flag_post ( author , raw , topic )
post = Fabricate ( :post , raw : raw , topic : topic , user : author )
expect {
Jobs :: ProcessPost . new . execute ( post_id : post . id )
} . to change { PostAction . count } . by ( 1 )
expect ( PostAction . where ( post_id : post . id , post_action_type_id : PostActionType . types [ :inappropriate ] ) . exists? ) . to eq ( true )
end
def should_not_flag_post ( author , raw , topic )
post = Fabricate ( :post , raw : raw , topic : topic , user : author )
expect {
Jobs :: ProcessPost . new . execute ( post_id : post . id )
} . to_not change { PostAction . count }
end
it " should flag the post as inappropriate " do
should_flag_post ( tl2_user , " I thought the #{ flag_word . word } was bad. " , Fabricate ( :topic , user : tl2_user ) )
end
it " should look at the title too " do
should_flag_post ( tl2_user , " I thought the movie was not bad actually. " , Fabricate ( :topic , user : tl2_user , title : " Read my #{ flag_word . word } review! " ) )
end
it " shouldn't flag posts by admin " do
should_not_flag_post ( admin , " I thought the #{ flag_word . word } was bad. " , Fabricate ( :topic , user : admin ) )
end
it " shouldn't flag posts by moderator " do
should_not_flag_post ( moderator , " I thought the #{ flag_word . word } was bad. " , Fabricate ( :topic , user : moderator ) )
end
it " is compatible with flag_sockpuppets " do
SiteSetting . flag_sockpuppets = true
ip_address = '182.189.119.174'
user1 = Fabricate ( :user , ip_address : ip_address , created_at : 2 . days . ago )
user2 = Fabricate ( :user , ip_address : ip_address )
first = create_post ( user : user1 , created_at : 2 . days . ago )
sockpuppet_post = create_post ( user : user2 , topic : first . topic , raw : " I thought the #{ flag_word . word } was bad. " )
expect ( PostAction . where ( post_id : sockpuppet_post . id ) . count ) . to eq ( 1 )
end
it " flags in private message too " do
post = Fabricate ( :private_message_post , raw : " Want some #{ flag_word . word } for cheap? " , user : tl2_user )
expect {
Jobs :: ProcessPost . new . execute ( post_id : post . id )
} . to change { PostAction . count } . by ( 1 )
expect ( PostAction . where ( post_id : post . id , post_action_type_id : PostActionType . types [ :inappropriate ] ) . exists? ) . to eq ( true )
end
it " flags on revisions " do
2019-03-14 10:47:38 -04:00
Jobs . run_immediately!
2017-06-28 16:56:44 -04:00
post = Fabricate ( :post , topic : Fabricate ( :topic , user : tl2_user ) , user : tl2_user )
expect {
PostRevisor . new ( post ) . revise! ( post . user , { raw : " Want some #{ flag_word . word } for cheap? " } , revised_at : post . updated_at + 10 . seconds )
} . to change { PostAction . count } . by ( 1 )
expect ( PostAction . where ( post_id : post . id , post_action_type_id : PostActionType . types [ :inappropriate ] ) . exists? ) . to eq ( true )
end
2017-10-12 15:34:22 -04:00
it " should not flag on rebake " do
post = Fabricate ( :post , topic : Fabricate ( :topic , user : tl2_user ) , user : tl2_user , raw : " I have coupon codes. Message me. " )
Fabricate ( :watched_word , action : WatchedWord . actions [ :flag ] , word : " coupon " )
expect {
post . rebake!
} . to_not change { PostAction . count }
end
2017-06-28 16:56:44 -04:00
end
2019-03-21 13:46:10 -04:00
describe 'upload' do
context 'logged in as admin' do
before do
sign_in ( admin )
end
it 'creates the words from the file' do
post '/admin/logs/watched_words/upload.json' , params : {
action_key : 'flag' ,
file : Rack :: Test :: UploadedFile . new ( file_from_fixtures ( " words.csv " , " csv " ) )
}
expect ( response . status ) . to eq ( 200 )
expect ( WatchedWord . count ) . to eq ( 6 )
expect ( WatchedWord . pluck ( :word ) ) . to contain_exactly (
'thread' , '线' , 'धागा' , '실' , 'tråd' , 'нить'
)
expect ( WatchedWord . pluck ( :action ) . uniq ) . to eq ( [ WatchedWord . actions [ :flag ] ] )
end
end
end
2017-06-28 16:56:44 -04:00
end