2015-10-11 17:41:23 +08:00
require 'rails_helper'
2013-03-19 05:52:29 +08:00
require_dependency 'post_destroyer'
2013-02-06 03:16:51 +08:00
describe PostAction do
2014-12-31 22:55:03 +08:00
it { is_expected . to rate_limit }
2013-02-06 03:16:51 +08:00
2013-03-19 05:52:29 +08:00
let ( :moderator ) { Fabricate ( :moderator ) }
2013-02-06 03:16:51 +08:00
let ( :codinghorror ) { Fabricate ( :coding_horror ) }
2014-08-19 22:14:17 +08:00
let ( :eviltrout ) { Fabricate ( :evil_trout ) }
2014-08-05 01:39:36 +08:00
let ( :admin ) { Fabricate ( :admin ) }
2013-02-06 03:16:51 +08:00
let ( :post ) { Fabricate ( :post ) }
2018-06-05 15:29:17 +08:00
let ( :second_post ) { Fabricate ( :post , topic : post . topic ) }
2013-03-01 20:07:44 +08:00
let ( :bookmark ) { PostAction . new ( user_id : post . user_id , post_action_type_id : PostActionType . types [ :bookmark ] , post_id : post . id ) }
2013-02-06 03:16:51 +08:00
2016-03-18 23:17:51 +08:00
def value_for ( user_id , dt )
GivenDailyLike . find_for ( user_id , dt ) . pluck ( :likes_given ) [ 0 ] || 0
end
2016-03-06 06:51:30 +08:00
describe " rate limits " do
it " limits redo/undo " do
2017-12-04 18:23:11 +08:00
RateLimiter . enable
2016-03-06 06:51:30 +08:00
PostAction . act ( eviltrout , post , PostActionType . types [ :like ] )
PostAction . remove_act ( eviltrout , post , PostActionType . types [ :like ] )
PostAction . act ( eviltrout , post , PostActionType . types [ :like ] )
PostAction . remove_act ( eviltrout , post , PostActionType . types [ :like ] )
expect {
PostAction . act ( eviltrout , post , PostActionType . types [ :like ] )
2016-05-30 11:38:04 +08:00
} . to raise_error ( RateLimiter :: LimitExceeded )
2016-03-06 06:51:30 +08:00
end
end
2013-04-12 15:55:45 +08:00
describe " messaging " do
2013-04-22 15:45:03 +08:00
2014-12-12 02:34:52 +08:00
it " doesn't generate title longer than 255 characters " do
topic = create_topic ( title : " Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc sit amet rutrum neque. Pellentesque suscipit vehicula facilisis. Phasellus lacus sapien, aliquam nec convallis sit amet, vestibulum laoreet ante. Curabitur et pellentesque tortor. Donec non. " )
post = create_post ( topic : topic )
2014-12-31 22:55:03 +08:00
expect { PostAction . act ( admin , post , PostActionType . types [ :notify_user ] , message : " WAT " ) } . not_to raise_error
2014-12-12 02:34:52 +08:00
end
2017-10-24 05:19:30 +08:00
it " notify moderators integration test " do
post = create_post
mod = moderator
Group . refresh_automatic_groups!
action = PostAction . act ( codinghorror , post , PostActionType . types [ :notify_moderators ] , message : " this is my special long message " )
posts = Post . joins ( :topic )
. select ( 'posts.id, topics.subtype, posts.topic_id' )
. where ( 'topics.archetype' = > Archetype . private_message )
. to_a
expect ( posts . count ) . to eq ( 1 )
expect ( action . related_post_id ) . to eq ( posts [ 0 ] . id . to_i )
expect ( posts [ 0 ] . subtype ) . to eq ( TopicSubtype . notify_moderators )
topic = posts [ 0 ] . topic
# Moderators should be invited to the private topic, otherwise they're not permitted to see it
topic_user_ids = topic . reload . topic_users . map { | x | x . user_id }
expect ( topic_user_ids ) . to include ( codinghorror . id )
expect ( topic_user_ids ) . to include ( mod . id )
expect ( topic . topic_users . where ( user_id : mod . id )
. pluck ( :notification_level ) . first ) . to eq ( TopicUser . notification_levels [ :tracking ] )
expect ( topic . topic_users . where ( user_id : codinghorror . id )
. pluck ( :notification_level ) . first ) . to eq ( TopicUser . notification_levels [ :watching ] )
# reply to PM should not clear flag
PostCreator . new ( mod , topic_id : posts [ 0 ] . topic_id , raw : " This is my test reply to the user, it should clear flags " ) . create
action . reload
expect ( action . deleted_at ) . to eq ( nil )
# Acting on the flag should not post an automated status message (since a moderator already replied)
expect ( topic . posts . count ) . to eq ( 2 )
PostAction . agree_flags! ( post , admin )
topic . reload
expect ( topic . posts . count ) . to eq ( 2 )
# Clearing the flags should not post an automated status message
PostAction . act ( mod , post , PostActionType . types [ :notify_moderators ] , message : " another special message " )
PostAction . clear_flags! ( post , admin )
topic . reload
expect ( topic . posts . count ) . to eq ( 2 )
# Acting on the flag should post an automated status message
another_post = create_post
action = PostAction . act ( codinghorror , another_post , PostActionType . types [ :notify_moderators ] , message : " foobar " )
topic = action . related_post . topic
expect ( topic . posts . count ) . to eq ( 1 )
PostAction . agree_flags! ( another_post , admin )
topic . reload
expect ( topic . posts . count ) . to eq ( 2 )
expect ( topic . posts . last . post_type ) . to eq ( Post . types [ :moderator_action ] )
2013-04-22 15:45:03 +08:00
end
2013-04-17 04:56:18 +08:00
describe 'notify_moderators' do
before do
PostAction . stubs ( :create )
end
2013-09-06 16:03:30 +08:00
it " creates a pm if selected " do
2013-04-22 15:45:03 +08:00
post = build ( :post , id : 1000 )
PostCreator . any_instance . expects ( :create ) . returns ( post )
2015-03-12 02:07:17 +08:00
PostAction . act ( build ( :user ) , build ( :post ) , PostActionType . types [ :notify_moderators ] , message : " this is my special message " )
2013-04-17 04:56:18 +08:00
end
2013-04-12 15:55:45 +08:00
end
2013-04-12 19:09:41 +08:00
2013-04-17 04:56:18 +08:00
describe " notify_user " do
before do
PostAction . stubs ( :create )
post = build ( :post )
post . user = build ( :user )
end
it " sends an email to user if selected " do
2013-04-22 15:45:03 +08:00
PostCreator . any_instance . expects ( :create ) . returns ( build ( :post ) )
2015-03-12 02:07:17 +08:00
PostAction . act ( build ( :user ) , post , PostActionType . types [ :notify_user ] , message : " this is my special message " )
2013-04-17 04:56:18 +08:00
end
2013-04-12 16:14:36 +08:00
end
2013-04-12 15:55:45 +08:00
end
2013-02-26 00:42:20 +08:00
describe " flag counts " do
2013-02-06 03:16:51 +08:00
before do
PostAction . update_flagged_posts_count
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
it " increments the numbers correctly " do
2014-12-31 22:55:03 +08:00
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
2013-04-22 15:45:03 +08:00
2013-03-01 20:07:44 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
2014-12-31 22:55:03 +08:00
expect ( PostAction . flagged_posts_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
2014-07-29 01:17:37 +08:00
PostAction . clear_flags! ( post , Discourse . system_user )
2014-12-31 22:55:03 +08:00
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
2013-02-06 03:16:51 +08:00
end
2018-05-08 03:14:18 +08:00
it " respects min_flags_staff_visibility " do
SiteSetting . min_flags_staff_visibility = 2
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
PostAction . act ( eviltrout , post , PostActionType . types [ :off_topic ] )
expect ( PostAction . flagged_posts_count ) . to eq ( 1 )
end
2013-02-26 00:42:20 +08:00
it " should reset counts when a topic is deleted " do
2013-03-01 20:07:44 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
2013-05-07 12:39:01 +08:00
post . topic . trash!
2014-12-31 22:55:03 +08:00
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
2013-02-06 09:13:41 +08:00
end
2013-02-26 00:42:20 +08:00
2017-05-09 01:13:35 +08:00
it " should ignore flags on non-human users " do
post = create_post ( user : Discourse . system_user )
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
end
2013-06-20 15:42:15 +08:00
it " should ignore validated flags " do
2013-07-22 13:06:53 +08:00
post = create_post
2014-08-19 22:14:17 +08:00
2013-06-20 15:42:15 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( false )
expect ( post . hidden_at ) . to be_blank
2014-07-29 01:17:37 +08:00
PostAction . defer_flags! ( post , admin )
2014-12-31 22:55:03 +08:00
expect ( PostAction . flagged_posts_count ) . to eq ( 0 )
2014-08-19 22:14:17 +08:00
2013-06-20 15:42:15 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( false )
expect ( post . hidden_at ) . to be_blank
2013-06-20 15:42:15 +08:00
2014-04-30 22:58:01 +08:00
PostAction . hide_post! ( post , PostActionType . types [ :off_topic ] )
2014-08-19 22:14:17 +08:00
2013-06-20 15:42:15 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( true )
expect ( post . hidden_at ) . to be_present
2013-06-20 15:42:15 +08:00
end
2013-02-06 03:16:51 +08:00
end
2014-07-29 04:08:31 +08:00
describe " update_counters " do
it " properly updates topic counters " do
2017-07-24 21:17:42 +08:00
freeze_time Date . today
# we need this to test it
TopicUser . change ( codinghorror , post . topic , posted : true )
2015-01-08 11:35:56 +08:00
2017-07-24 21:17:42 +08:00
expect ( value_for ( moderator . id , Date . today ) ) . to eq ( 0 )
2014-08-19 22:14:17 +08:00
2017-07-24 21:17:42 +08:00
PostAction . act ( moderator , post , PostActionType . types [ :like ] )
PostAction . act ( codinghorror , second_post , PostActionType . types [ :like ] )
2015-01-08 11:35:56 +08:00
2017-07-24 21:17:42 +08:00
post . topic . reload
expect ( post . topic . like_count ) . to eq ( 2 )
2015-01-08 11:35:56 +08:00
2017-07-24 21:17:42 +08:00
expect ( value_for ( moderator . id , Date . today ) ) . to eq ( 1 )
2016-03-18 02:41:00 +08:00
2017-07-24 21:17:42 +08:00
tu = TopicUser . get ( post . topic , codinghorror )
expect ( tu . liked ) . to be true
expect ( tu . bookmarked ) . to be false
2014-07-29 04:08:31 +08:00
end
end
2013-05-04 08:52:45 +08:00
describe " when a user bookmarks something " do
it " increases the post's bookmark count when saved " do
2014-12-31 22:55:03 +08:00
expect { bookmark . save ; post . reload } . to change ( post , :bookmark_count ) . by ( 1 )
2013-05-04 08:52:45 +08:00
end
2013-02-06 03:16:51 +08:00
2013-05-04 08:52:45 +08:00
describe 'when deleted' do
before do
bookmark . save
post . reload
@topic = post . topic
@topic . reload
bookmark . deleted_at = DateTime . now
bookmark . save
end
2013-02-06 03:16:51 +08:00
2013-05-04 08:52:45 +08:00
it 'reduces the bookmark count of the post' do
2014-12-31 22:55:03 +08:00
expect { post . reload } . to change ( post , :bookmark_count ) . by ( - 1 )
2013-05-04 08:52:45 +08:00
end
end
end
2013-02-26 00:42:20 +08:00
2018-07-25 05:17:47 +08:00
describe " undo/redo repeatedly " do
it " doesn't create a second action for the same user/type " do
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
PostAction . remove_act ( codinghorror , post , PostActionType . types [ :like ] )
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
expect ( PostAction . where ( post : post ) . with_deleted . count ) . to eq ( 1 )
PostAction . remove_act ( codinghorror , post , PostActionType . types [ :like ] )
# Check that we don't lose consistency into negatives
expect ( post . reload . like_count ) . to eq ( 0 )
end
end
2013-02-26 00:42:20 +08:00
describe 'when a user likes something' do
2015-03-26 09:08:04 +08:00
it 'should generate notifications correctly' do
2016-12-22 13:46:22 +08:00
PostActionNotifier . enable
2015-03-26 09:08:04 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
2015-04-25 23:18:35 +08:00
expect ( Notification . count ) . to eq ( 1 )
2015-03-26 09:08:04 +08:00
mutee = Fabricate ( :user )
post = Fabricate ( :post )
MutedUser . create! ( user_id : post . user . id , muted_user_id : mutee . id )
PostAction . act ( mutee , post , PostActionType . types [ :like ] )
2015-04-25 23:18:35 +08:00
expect ( Notification . count ) . to eq ( 1 )
2015-03-26 09:08:04 +08:00
# you can not mute admin, sorry
MutedUser . create! ( user_id : post . user . id , muted_user_id : admin . id )
PostAction . act ( admin , post , PostActionType . types [ :like ] )
2015-04-25 23:18:35 +08:00
expect ( Notification . count ) . to eq ( 2 )
2015-03-26 09:08:04 +08:00
end
2013-05-28 00:45:10 +08:00
it 'should increase the `like_count` and `like_score` when a user likes something' do
2017-07-24 21:17:42 +08:00
freeze_time Date . today
2013-05-28 00:45:10 +08:00
2017-07-24 21:17:42 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
post . reload
expect ( post . like_count ) . to eq ( 1 )
expect ( post . like_score ) . to eq ( 1 )
post . topic . reload
expect ( post . topic . like_count ) . to eq ( 1 )
expect ( value_for ( codinghorror . id , Date . today ) ) . to eq ( 1 )
# When a staff member likes it
PostAction . act ( moderator , post , PostActionType . types [ :like ] )
post . reload
expect ( post . like_count ) . to eq ( 2 )
expect ( post . like_score ) . to eq ( 4 )
2013-05-28 00:45:10 +08:00
2017-07-24 21:17:42 +08:00
# Removing likes
PostAction . remove_act ( codinghorror , post , PostActionType . types [ :like ] )
post . reload
expect ( post . like_count ) . to eq ( 1 )
expect ( post . like_score ) . to eq ( 3 )
expect ( value_for ( codinghorror . id , Date . today ) ) . to eq ( 0 )
PostAction . remove_act ( moderator , post , PostActionType . types [ :like ] )
post . reload
expect ( post . like_count ) . to eq ( 0 )
expect ( post . like_score ) . to eq ( 0 )
2013-02-06 03:16:51 +08:00
end
2018-02-27 05:27:18 +08:00
it " shouldn't change given_likes unless likes are given or removed " do
freeze_time ( Time . zone . now )
PostAction . act ( codinghorror , Fabricate ( :post ) , PostActionType . types [ :like ] )
expect ( value_for ( codinghorror . id , Date . today ) ) . to eq ( 1 )
PostActionType . types . each do | type_name , type_id |
post = Fabricate ( :post )
PostAction . act ( codinghorror , post , type_id )
actual_count = value_for ( codinghorror . id , Date . today )
expected_count = type_name == :like ? 2 : 1
expect ( actual_count ) . to eq ( expected_count ) , " Expected likes_given to be #{ expected_count } when adding ' #{ type_name } ', but got #{ actual_count } "
PostAction . remove_act ( codinghorror , post , type_id )
actual_count = value_for ( codinghorror . id , Date . today )
expect ( actual_count ) . to eq ( 1 ) , " Expected likes_given to be 1 when removing ' #{ type_name } ', but got #{ actual_count } "
end
end
2014-07-19 04:14:47 +08:00
2016-12-02 14:03:31 +08:00
it 'should increase the like counts when a user votes' do
2014-12-31 22:55:03 +08:00
expect {
2016-12-02 14:03:31 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
2013-02-06 03:16:51 +08:00
post . reload
2016-12-02 14:03:31 +08:00
} . to change ( post , :like_count ) . by ( 1 )
2013-02-06 03:16:51 +08:00
end
2013-02-26 00:42:20 +08:00
it 'should increase the forum topic vote count when a user votes' do
2014-12-31 22:55:03 +08:00
expect {
2016-12-02 14:03:31 +08:00
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
post . topic . reload
} . to change ( post . topic , :like_count ) . by ( 1 )
expect {
PostAction . remove_act ( codinghorror , post , PostActionType . types [ :like ] )
2013-02-06 03:16:51 +08:00
post . topic . reload
2016-12-02 14:03:31 +08:00
} . to change ( post . topic , :like_count ) . by ( - 1 )
2013-02-06 03:16:51 +08:00
end
end
describe 'flagging' do
2013-05-11 04:58:23 +08:00
context " flag_counts_for " do
it " returns the correct flag counts " do
2013-07-22 13:06:53 +08:00
post = create_post
2013-05-11 04:58:23 +08:00
2017-07-07 14:09:14 +08:00
SiteSetting . flags_required_to_hide_post = 7
2013-05-11 04:58:23 +08:00
# A post with no flags has 0 for flag counts
2014-12-31 22:55:03 +08:00
expect ( PostAction . flag_counts_for ( post . id ) ) . to eq ( [ 0 , 0 ] )
2013-05-11 04:58:23 +08:00
2015-03-26 09:08:04 +08:00
_flag = PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
2014-12-31 22:55:03 +08:00
expect ( PostAction . flag_counts_for ( post . id ) ) . to eq ( [ 0 , 1 ] )
2013-05-11 04:58:23 +08:00
2013-06-01 05:38:28 +08:00
# If staff takes action, it is ranked higher
2014-07-29 01:17:37 +08:00
PostAction . act ( admin , post , PostActionType . types [ :spam ] , take_action : true )
2014-12-31 22:55:03 +08:00
expect ( PostAction . flag_counts_for ( post . id ) ) . to eq ( [ 0 , 8 ] )
2013-05-11 04:58:23 +08:00
# If a flag is dismissed
PostAction . clear_flags! ( post , admin )
2014-12-31 22:55:03 +08:00
expect ( PostAction . flag_counts_for ( post . id ) ) . to eq ( [ 8 , 0 ] )
2013-05-11 04:58:23 +08:00
end
end
2014-08-19 22:14:17 +08:00
it 'does not allow you to flag stuff with the same reason more than once' do
2013-02-07 07:45:58 +08:00
post = Fabricate ( :post )
2014-08-19 22:14:17 +08:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
2014-12-31 22:55:03 +08:00
expect { PostAction . act ( eviltrout , post , PostActionType . types [ :off_topic ] ) } . to raise_error ( PostAction :: AlreadyActed )
2013-05-04 08:52:45 +08:00
end
it 'allows you to flag stuff with another reason' do
post = Fabricate ( :post )
2014-08-19 22:14:17 +08:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . remove_act ( eviltrout , post , PostActionType . types [ :spam ] )
2014-12-31 22:55:03 +08:00
expect { PostAction . act ( eviltrout , post , PostActionType . types [ :off_topic ] ) } . not_to raise_error ( )
2013-02-07 07:45:58 +08:00
end
2013-02-26 00:42:20 +08:00
it 'should update counts when you clear flags' do
2013-02-06 03:16:51 +08:00
post = Fabricate ( :post )
2014-08-19 22:14:17 +08:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
2013-02-06 03:16:51 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . spam_count ) . to eq ( 1 )
2013-02-06 03:16:51 +08:00
2014-07-29 01:17:37 +08:00
PostAction . clear_flags! ( post , Discourse . system_user )
2013-02-06 03:16:51 +08:00
2014-08-19 22:14:17 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . spam_count ) . to eq ( 0 )
2013-02-06 03:16:51 +08:00
end
2018-03-20 21:38:23 +08:00
it " will not allow regular users to auto hide staff posts " do
2018-02-10 08:53:58 +08:00
mod = Fabricate ( :moderator )
post = Fabricate ( :post , user : mod )
SiteSetting . flags_required_to_hide_post = 2
Discourse . stubs ( :site_contact_user ) . returns ( admin )
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . act ( Fabricate ( :walter_white ) , post , PostActionType . types [ :spam ] )
post . reload
expect ( post . hidden ) . to eq ( false )
expect ( post . hidden_at ) . to be_blank
end
2018-03-20 21:38:23 +08:00
it " allows staff users to auto hide staff posts " do
mod = Fabricate ( :moderator )
post = Fabricate ( :post , user : mod )
SiteSetting . flags_required_to_hide_post = 2
Discourse . stubs ( :site_contact_user ) . returns ( admin )
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . act ( Fabricate ( :admin ) , post , PostActionType . types [ :spam ] )
post . reload
expect ( post . hidden ) . to eq ( true )
expect ( post . hidden_at ) . to be_present
end
2013-02-26 00:42:20 +08:00
it 'should follow the rules for automatic hiding workflow' do
2013-07-22 13:06:53 +08:00
post = create_post
2014-08-19 22:14:17 +08:00
walterwhite = Fabricate ( :walter_white )
2013-02-06 03:16:51 +08:00
2017-07-07 14:09:14 +08:00
SiteSetting . flags_required_to_hide_post = 2
2014-08-19 22:14:17 +08:00
Discourse . stubs ( :site_contact_user ) . returns ( admin )
2013-02-06 03:16:51 +08:00
2014-08-19 22:14:17 +08:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . act ( walterwhite , post , PostActionType . types [ :spam ] )
2013-02-06 03:16:51 +08:00
2018-04-05 23:27:16 +08:00
job_args = Jobs :: SendSystemMessage . jobs . last [ " args " ] . first
expect ( job_args [ " user_id " ] ) . to eq ( post . user . id )
expect ( job_args [ " message_type " ] ) . to eq ( " post_hidden " )
2013-02-06 03:16:51 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( true )
expect ( post . hidden_at ) . to be_present
expect ( post . hidden_reason_id ) . to eq ( Post . hidden_reasons [ :flag_threshold_reached ] )
expect ( post . topic . visible ) . to eq ( false )
2013-02-06 03:16:51 +08:00
2017-07-28 09:20:09 +08:00
post . revise ( post . user , raw : post . raw + " ha I edited it " )
2014-07-31 05:35:42 +08:00
2013-02-06 03:16:51 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( false )
2015-12-30 05:59:26 +08:00
expect ( post . hidden_reason_id ) . to eq ( Post . hidden_reasons [ :flag_threshold_reached ] ) # keep most recent reason
expect ( post . hidden_at ) . to be_present # keep the most recent hidden_at time
2014-12-31 22:55:03 +08:00
expect ( post . topic . visible ) . to eq ( true )
2013-02-06 03:16:51 +08:00
2014-08-19 22:14:17 +08:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . act ( walterwhite , post , PostActionType . types [ :off_topic ] )
2013-02-06 03:16:51 +08:00
2018-04-05 23:27:16 +08:00
job_args = Jobs :: SendSystemMessage . jobs . last [ " args " ] . first
expect ( job_args [ " user_id " ] ) . to eq ( post . user . id )
expect ( job_args [ " message_type " ] ) . to eq ( " post_hidden_again " )
2013-02-06 03:16:51 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( true )
expect ( post . hidden_at ) . to be_present
expect ( post . hidden_reason_id ) . to eq ( Post . hidden_reasons [ :flag_threshold_reached_again ] )
expect ( post . topic . visible ) . to eq ( false )
2013-02-06 03:16:51 +08:00
2017-07-28 09:20:09 +08:00
post . revise ( post . user , raw : post . raw + " ha I edited it again " )
2013-02-26 00:42:20 +08:00
2013-02-06 03:16:51 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( true )
expect ( post . hidden_at ) . to be_present
expect ( post . hidden_reason_id ) . to eq ( Post . hidden_reasons [ :flag_threshold_reached_again ] )
expect ( post . topic . visible ) . to eq ( false )
2013-02-06 03:16:51 +08:00
end
2014-02-19 04:18:31 +08:00
2018-08-30 08:03:32 +08:00
it " doesn't fail when post has nil user " do
post = create_post
post . update! ( user : nil )
PostAction . act ( codinghorror , post , PostActionType . types [ :spam ] , take_action : true )
post . reload
expect ( post . hidden ) . to eq ( true )
end
2014-10-02 00:53:17 +08:00
it " hide tl0 posts that are flagged as spam by a tl3 user " do
newuser = Fabricate ( :newuser )
post = create_post ( user : newuser )
Discourse . stubs ( :site_contact_user ) . returns ( admin )
PostAction . act ( Fabricate ( :leader ) , post , PostActionType . types [ :spam ] )
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( true )
expect ( post . hidden_at ) . to be_present
expect ( post . hidden_reason_id ) . to eq ( Post . hidden_reasons [ :flagged_by_tl3_user ] )
2014-10-02 00:53:17 +08:00
end
2014-02-19 04:18:31 +08:00
it " can flag the topic instead of a post " do
post1 = create_post
2015-03-26 09:08:04 +08:00
_post2 = create_post ( topic : post1 . topic )
2017-07-28 09:20:09 +08:00
post_action = PostAction . act ( Fabricate ( :user ) , post1 , PostActionType . types [ :spam ] , flag_topic : true )
2014-12-31 22:55:03 +08:00
expect ( post_action . targets_topic ) . to eq ( true )
2014-02-19 04:18:31 +08:00
end
it " will flag the first post if you flag a topic but there is only one post in the topic " do
post = create_post
2017-07-28 09:20:09 +08:00
post_action = PostAction . act ( Fabricate ( :user ) , post , PostActionType . types [ :spam ] , flag_topic : true )
2014-12-31 22:55:03 +08:00
expect ( post_action . targets_topic ) . to eq ( false )
expect ( post_action . post_id ) . to eq ( post . id )
2014-02-19 04:18:31 +08:00
end
2014-08-19 22:14:17 +08:00
it " will unhide the post when a moderator undos the flag on which s/he took action " do
Discourse . stubs ( :site_contact_user ) . returns ( admin )
post = create_post
2017-07-28 09:20:09 +08:00
PostAction . act ( moderator , post , PostActionType . types [ :spam ] , take_action : true )
2014-08-19 22:14:17 +08:00
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( true )
2014-08-19 22:14:17 +08:00
PostAction . remove_act ( moderator , post , PostActionType . types [ :spam ] )
post . reload
2014-12-31 22:55:03 +08:00
expect ( post . hidden ) . to eq ( false )
2014-08-19 22:14:17 +08:00
end
2017-03-31 14:35:05 +08:00
it " will automatically pause a topic due to large community flagging " do
SiteSetting . flags_required_to_hide_post = 0
SiteSetting . num_flags_to_close_topic = 3
SiteSetting . num_flaggers_to_close_topic = 2
SiteSetting . num_hours_to_close_topic = 1
2014-12-06 02:37:43 +08:00
topic = Fabricate ( :topic )
post1 = create_post ( topic : topic )
post2 = create_post ( topic : topic )
post3 = create_post ( topic : topic )
flagger1 = Fabricate ( :user )
flagger2 = Fabricate ( :user )
# reaching `num_flaggers_to_close_topic` isn't enough
2014-12-06 23:29:54 +08:00
[ flagger1 , flagger2 ] . each do | flagger |
2014-12-06 02:37:43 +08:00
PostAction . act ( flagger , post1 , PostActionType . types [ :inappropriate ] )
end
2014-12-31 22:55:03 +08:00
expect ( topic . reload . closed ) . to eq ( false )
2014-12-06 02:37:43 +08:00
# clean up
PostAction . where ( post : post1 ) . delete_all
# reaching `num_flags_to_close_topic` isn't enough
2014-12-06 23:29:54 +08:00
[ post1 , post2 , post3 ] . each do | post |
PostAction . act ( flagger1 , post , PostActionType . types [ :inappropriate ] )
2014-12-06 02:37:43 +08:00
end
2014-12-31 22:55:03 +08:00
expect ( topic . reload . closed ) . to eq ( false )
2014-12-06 02:37:43 +08:00
# clean up
2014-12-06 23:29:54 +08:00
PostAction . where ( post : [ post1 , post2 , post3 ] ) . delete_all
2014-12-06 02:37:43 +08:00
# reaching both should close the topic
2014-12-06 23:29:54 +08:00
[ flagger1 , flagger2 ] . each do | flagger |
[ post1 , post2 , post3 ] . each do | post |
2014-12-06 02:37:43 +08:00
PostAction . act ( flagger , post , PostActionType . types [ :inappropriate ] )
end
end
2014-12-31 22:55:03 +08:00
expect ( topic . reload . closed ) . to eq ( true )
2015-05-13 14:45:59 +08:00
2017-05-12 06:23:18 +08:00
topic_status_update = TopicTimer . last
2017-03-31 14:35:05 +08:00
expect ( topic_status_update . topic ) . to eq ( topic )
expect ( topic_status_update . execute_at ) . to be_within ( 1 . second ) . of ( 1 . hour . from_now )
2017-05-12 06:23:18 +08:00
expect ( topic_status_update . status_type ) . to eq ( TopicTimer . types [ :open ] )
2014-12-06 02:37:43 +08:00
end
2013-02-06 03:16:51 +08:00
end
2013-05-04 08:52:45 +08:00
it " prevents user to act twice at the same time " do
# flags are already being tested
2017-10-18 01:31:45 +08:00
all_types_except_flags = PostActionType . types . except ( PostActionType . flag_types_without_custom )
2013-05-04 08:52:45 +08:00
all_types_except_flags . values . each do | action |
2014-12-31 22:55:03 +08:00
expect do
2014-08-19 22:14:17 +08:00
PostAction . act ( eviltrout , post , action )
PostAction . act ( eviltrout , post , action )
2014-12-31 22:55:03 +08:00
end . to raise_error ( PostAction :: AlreadyActed )
2013-05-04 08:52:45 +08:00
end
end
2017-12-19 10:58:26 +08:00
describe " .create_message_for_post_action " do
2015-02-06 02:58:49 +08:00
it " does not create a message when there is no message " do
message_id = PostAction . create_message_for_post_action ( Discourse . system_user , post , PostActionType . types [ :spam ] , { } )
expect ( message_id ) . to be_nil
end
[ :notify_moderators , :notify_user , :spam ] . each do | post_action_type |
it " creates a message for #{ post_action_type } " do
message_id = PostAction . create_message_for_post_action ( Discourse . system_user , post , PostActionType . types [ post_action_type ] , message : " WAT " )
expect ( message_id ) . to be_present
end
end
2017-12-19 10:58:26 +08:00
it " should raise the right errors when it fails to create a post " do
begin
group = Group [ :moderators ]
messageable_level = group . messageable_level
group . update! ( messageable_level : Group :: ALIAS_LEVELS [ :nobody ] )
expect do
PostAction . create_message_for_post_action (
Fabricate ( :user ) ,
post ,
PostActionType . types [ :notify_moderators ] ,
message : 'testing' ,
)
end . to raise_error ( ActiveRecord :: RecordNotSaved )
ensure
group . update! ( messageable_level : messageable_level )
end
end
2018-05-30 00:21:47 +08:00
it " should succeed even with low max title length " do
SiteSetting . max_topic_title_length = 50
post . topic . title = 'This is a test topic ' * 2
post . topic . save!
message_id = PostAction . create_message_for_post_action ( Discourse . system_user , post , PostActionType . types [ :notify_moderators ] , message : " WAT " )
expect ( message_id ) . to be_present
end
2015-02-06 02:58:49 +08:00
end
2015-04-16 15:29:18 +08:00
describe " .lookup_for " do
it " returns the correct map " do
user = Fabricate ( :user )
post = Fabricate ( :post )
post_action = PostAction . create ( user_id : user . id , post_id : post . id , post_action_type_id : 1 )
map = PostAction . lookup_for ( user , [ post . topic ] , post_action . post_action_type_id )
2017-07-28 09:20:09 +08:00
expect ( map ) . to eq ( post . topic_id = > [ post . post_number ] )
2015-04-16 15:29:18 +08:00
end
end
2017-08-09 17:17:54 +08:00
describe " # add_moderator_post_if_needed " do
2015-03-12 02:07:17 +08:00
2015-03-12 02:29:09 +08:00
it " should not add a moderator post when it's disabled " do
2015-03-12 02:07:17 +08:00
post = create_post
2015-03-12 02:29:09 +08:00
action = PostAction . act ( moderator , post , PostActionType . types [ :spam ] , message : " WAT " )
action . reload
topic = action . related_post . topic
expect ( topic . posts . count ) . to eq ( 1 )
2017-08-09 17:17:54 +08:00
SiteSetting . auto_respond_to_flag_actions = false
2015-03-12 02:29:09 +08:00
PostAction . agree_flags! ( post , admin )
topic . reload
expect ( topic . posts . count ) . to eq ( 1 )
end
2015-10-15 08:56:10 +08:00
it " should create a notification in the related topic " do
2018-05-31 15:53:49 +08:00
SiteSetting . queue_jobs = false
2015-10-15 08:56:10 +08:00
post = Fabricate ( :post )
user = Fabricate ( :user )
action = PostAction . act ( user , post , PostActionType . types [ :spam ] , message : " WAT " )
topic = action . reload . related_post . topic
expect ( user . notifications . count ) . to eq ( 0 )
2017-08-09 17:17:54 +08:00
SiteSetting . auto_respond_to_flag_actions = true
2015-10-15 08:56:10 +08:00
PostAction . agree_flags! ( post , admin )
user_notifications = user . notifications
expect ( user_notifications . count ) . to eq ( 1 )
expect ( user_notifications . last . topic ) . to eq ( topic )
end
2018-07-19 05:18:14 +08:00
it " should not add a moderator post when post is flagged via private message " do
SiteSetting . queue_jobs = false
post = Fabricate ( :post )
user = Fabricate ( :user )
action = PostAction . act ( user , post , PostActionType . types [ :notify_user ] , message : " WAT " )
topic = action . reload . related_post . topic
expect ( user . notifications . count ) . to eq ( 0 )
SiteSetting . auto_respond_to_flag_actions = true
PostAction . agree_flags! ( post , admin )
user_notifications = user . notifications
expect ( user_notifications . count ) . to eq ( 0 )
end
2015-04-16 07:44:30 +08:00
end
describe " rate limiting " do
def limiter ( tl )
user = Fabricate . build ( :user )
user . trust_level = tl
action = PostAction . new ( user : user , post_action_type_id : PostActionType . types [ :like ] )
action . post_action_rate_limiter
end
it " should scale up like limits depending on liker " do
expect ( limiter ( 0 ) . max ) . to eq SiteSetting . max_likes_per_day
expect ( limiter ( 1 ) . max ) . to eq SiteSetting . max_likes_per_day
expect ( limiter ( 2 ) . max ) . to eq ( SiteSetting . max_likes_per_day * SiteSetting . tl2_additional_likes_per_day_multiplier ) . to_i
expect ( limiter ( 3 ) . max ) . to eq ( SiteSetting . max_likes_per_day * SiteSetting . tl3_additional_likes_per_day_multiplier ) . to_i
expect ( limiter ( 4 ) . max ) . to eq ( SiteSetting . max_likes_per_day * SiteSetting . tl4_additional_likes_per_day_multiplier ) . to_i
SiteSetting . tl2_additional_likes_per_day_multiplier = - 1
expect ( limiter ( 2 ) . max ) . to eq SiteSetting . max_likes_per_day
SiteSetting . tl2_additional_likes_per_day_multiplier = 0 . 8
expect ( limiter ( 2 ) . max ) . to eq SiteSetting . max_likes_per_day
SiteSetting . tl2_additional_likes_per_day_multiplier = " bob "
expect ( limiter ( 2 ) . max ) . to eq SiteSetting . max_likes_per_day
end
2015-03-12 02:29:09 +08:00
2015-03-12 02:07:17 +08:00
end
2018-02-28 11:22:51 +08:00
describe '#is_flag?' do
describe 'when post action is a flag' do
it 'should return true' do
PostActionType . notify_flag_types . each do | _type , id |
post_action = PostAction . new (
user : codinghorror ,
post_action_type_id : id
)
expect ( post_action . is_flag? ) . to eq ( true )
end
end
end
describe 'when post action is not a flag' do
it 'should return false' do
post_action = PostAction . new (
user : codinghorror ,
post_action_type_id : 99
)
expect ( post_action . is_flag? ) . to eq ( false )
end
end
end
2018-05-21 17:29:19 +08:00
describe " triggers Discourse events " do
2018-04-11 22:17:05 +08:00
let ( :post ) { Fabricate ( :post ) }
it 'flag created' do
event = DiscourseEvent . track_events { PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] ) } . last
expect ( event [ :event_name ] ) . to eq ( :flag_created )
end
context " resolving flags " do
before do
@flag = PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
end
it 'flag agreed' do
2018-08-17 00:11:29 +08:00
events = DiscourseEvent . track_events { PostAction . agree_flags! ( post , moderator ) } . last ( 2 )
expect ( events [ 0 ] [ :event_name ] ) . to eq ( :flag_reviewed )
expect ( events [ 1 ] [ :event_name ] ) . to eq ( :flag_agreed )
expect ( events [ 1 ] [ :params ] . first ) . to eq ( @flag )
2018-04-11 22:17:05 +08:00
end
it 'flag disagreed' do
2018-08-17 00:11:29 +08:00
events = DiscourseEvent . track_events { PostAction . clear_flags! ( post , moderator ) } . last ( 2 )
expect ( events [ 0 ] [ :event_name ] ) . to eq ( :flag_reviewed )
expect ( events [ 1 ] [ :event_name ] ) . to eq ( :flag_disagreed )
expect ( events [ 1 ] [ :params ] . first ) . to eq ( @flag )
2018-04-11 22:17:05 +08:00
end
it 'flag deferred' do
2018-08-17 00:11:29 +08:00
events = DiscourseEvent . track_events { PostAction . defer_flags! ( post , moderator ) } . last ( 2 )
expect ( events [ 0 ] [ :event_name ] ) . to eq ( :flag_reviewed )
expect ( events [ 1 ] [ :event_name ] ) . to eq ( :flag_deferred )
expect ( events [ 1 ] [ :params ] . first ) . to eq ( @flag )
2018-04-11 22:17:05 +08:00
end
end
end
2013-02-06 03:16:51 +08:00
end