2013-02-05 14:16:51 -05:00
require 'spec_helper'
2013-03-18 17:52:29 -04:00
require_dependency 'post_destroyer'
2013-02-05 14:16:51 -05:00
describe PostAction do
it { should belong_to :user }
it { should belong_to :post }
it { should belong_to :post_action_type }
it { should rate_limit }
2013-03-18 17:52:29 -04:00
let ( :moderator ) { Fabricate ( :moderator ) }
2013-02-05 14:16:51 -05:00
let ( :codinghorror ) { Fabricate ( :coding_horror ) }
2014-08-19 16:14:17 +02:00
let ( :eviltrout ) { Fabricate ( :evil_trout ) }
2014-08-04 19:39:36 +02:00
let ( :admin ) { Fabricate ( :admin ) }
2013-02-05 14:16:51 -05:00
let ( :post ) { Fabricate ( :post ) }
2014-07-28 22:08:31 +02:00
let ( :second_post ) { Fabricate ( :post , topic_id : post . topic_id ) }
2013-03-01 15:07:44 +03:00
let ( :bookmark ) { PostAction . new ( user_id : post . user_id , post_action_type_id : PostActionType . types [ :bookmark ] , post_id : post . id ) }
2013-02-05 14:16:51 -05:00
2013-04-12 17:55:45 +10:00
describe " messaging " do
2013-04-22 17:45:03 +10:00
2013-05-04 02:52:45 +02:00
it " notify moderators integration test " do
2013-07-22 15:06:53 +10:00
post = create_post
2013-04-22 17:45:03 +10:00
mod = moderator
2014-06-24 12:31:36 -04:00
Group . refresh_automatic_groups!
2013-05-31 17:38:28 -04:00
action = PostAction . act ( codinghorror , post , PostActionType . types [ :notify_moderators ] , message : " this is my special long message " ) ;
2013-04-22 17:45:03 +10:00
2013-05-04 02:52:45 +02:00
posts = Post . joins ( :topic )
2013-05-13 11:48:01 +10:00
. select ( 'posts.id, topics.subtype, posts.topic_id' )
2013-05-04 02:52:45 +02:00
. where ( 'topics.archetype' = > Archetype . private_message )
. to_a
2013-04-22 17:45:03 +10:00
posts . count . should == 1
action . related_post_id . should == posts [ 0 ] . id . to_i
posts [ 0 ] . subtype . should == TopicSubtype . notify_moderators
2014-06-24 12:31:36 -04:00
topic = posts [ 0 ] . topic
2014-05-12 15:26:36 -04:00
# Moderators should be invited to the private topic, otherwise they're not permitted to see it
2014-06-24 12:31:36 -04:00
topic_user_ids = topic . topic_users ( true ) . map { | x | x . user_id }
2014-05-12 15:26:36 -04:00
topic_user_ids . should include ( codinghorror . id )
2014-06-24 12:31:36 -04:00
topic_user_ids . should include ( mod . id )
2014-05-13 17:44:23 +02:00
2014-06-24 12:31:36 -04:00
# Notification level should be "Watching" for everyone
topic . topic_users ( true ) . map ( & :notification_level ) . uniq . should == [ TopicUser . notification_levels [ :watching ] ]
2014-05-12 15:26:36 -04:00
2014-07-28 19:17:37 +02:00
# reply to PM should not clear flag
2014-08-18 17:00:14 +02:00
PostCreator . new ( mod , topic_id : posts [ 0 ] . topic_id , raw : " This is my test reply to the user, it should clear flags " ) . create
2013-05-13 11:48:01 +10:00
action . reload
2014-09-25 17:44:48 +02:00
action . deleted_at . should == nil
2014-08-04 19:39:36 +02:00
# Acting on the flag should post an automated status message
topic . posts . count . should == 2
PostAction . agree_flags! ( post , admin )
topic . reload
topic . posts . count . should == 3
topic . posts . last . post_type . should == Post . types [ :moderator_action ]
# Clearing the flags should not post another automated status message
PostAction . act ( mod , post , PostActionType . types [ :notify_moderators ] , message : " another special message " )
PostAction . clear_flags! ( post , admin )
topic . reload
topic . posts . count . should == 3
2013-04-22 17:45:03 +10:00
end
2013-04-16 16:56:18 -04:00
describe 'notify_moderators' do
before do
PostAction . stubs ( :create )
end
2013-09-06 18:03:30 +10:00
it " creates a pm if selected " do
2013-04-22 17:45:03 +10:00
post = build ( :post , id : 1000 )
PostCreator . any_instance . expects ( :create ) . returns ( post )
2013-05-31 17:38:28 -04:00
PostAction . act ( build ( :user ) , build ( :post ) , PostActionType . types [ :notify_moderators ] , message : " this is my special message " ) ;
2013-04-16 16:56:18 -04:00
end
2013-04-12 17:55:45 +10:00
end
2013-04-12 21:09:41 +10:00
2013-04-16 16:56:18 -04: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 17:45:03 +10:00
PostCreator . any_instance . expects ( :create ) . returns ( build ( :post ) )
2013-05-31 17:38:28 -04:00
PostAction . act ( build ( :user ) , post , PostActionType . types [ :notify_user ] , message : " this is my special message " ) ;
2013-04-16 16:56:18 -04:00
end
2013-04-12 18:14:36 +10:00
end
2013-04-12 17:55:45 +10:00
end
2013-02-25 19:42:20 +03:00
describe " flag counts " do
2013-02-05 14:16:51 -05:00
before do
PostAction . update_flagged_posts_count
2013-02-25 19:42:20 +03:00
end
2013-02-05 14:16:51 -05:00
it " increments the numbers correctly " do
2013-04-22 17:45:03 +10:00
PostAction . flagged_posts_count . should == 0
2013-03-01 15:07:44 +03:00
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
2013-02-05 14:16:51 -05:00
PostAction . flagged_posts_count . should == 1
2014-07-28 19:17:37 +02:00
PostAction . clear_flags! ( post , Discourse . system_user )
2013-02-05 14:16:51 -05:00
PostAction . flagged_posts_count . should == 0
end
2013-02-25 19:42:20 +03:00
it " should reset counts when a topic is deleted " do
2013-03-01 15:07:44 +03:00
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
2013-05-07 14:39:01 +10:00
post . topic . trash!
2013-02-06 12:13:41 +11:00
PostAction . flagged_posts_count . should == 0
end
2013-02-25 19:42:20 +03:00
2013-06-20 17:42:15 +10:00
it " should ignore validated flags " do
2013-07-22 15:06:53 +10:00
post = create_post
2014-08-19 16:14:17 +02:00
2013-06-20 17:42:15 +10:00
PostAction . act ( codinghorror , post , PostActionType . types [ :off_topic ] )
2014-09-25 17:44:48 +02:00
post . hidden . should == false
2014-06-20 15:03:02 -04:00
post . hidden_at . should be_blank
2014-07-28 19:17:37 +02:00
PostAction . defer_flags! ( post , admin )
2013-06-20 17:42:15 +10:00
PostAction . flagged_posts_count . should == 0
2014-08-19 16:14:17 +02:00
2013-06-20 17:42:15 +10:00
post . reload
2014-09-25 17:44:48 +02:00
post . hidden . should == false
2014-06-20 15:03:02 -04:00
post . hidden_at . should be_blank
2013-06-20 17:42:15 +10:00
2014-04-30 16:58:01 +02:00
PostAction . hide_post! ( post , PostActionType . types [ :off_topic ] )
2014-08-19 16:14:17 +02:00
2013-06-20 17:42:15 +10:00
post . reload
2014-09-25 17:44:48 +02:00
post . hidden . should == true
2014-06-20 15:03:02 -04:00
post . hidden_at . should be_present
2013-06-20 17:42:15 +10:00
end
2013-02-05 14:16:51 -05:00
end
2014-07-28 22:08:31 +02:00
describe " update_counters " do
it " properly updates topic counters " do
PostAction . act ( moderator , post , PostActionType . types [ :like ] )
PostAction . act ( codinghorror , second_post , PostActionType . types [ :like ] )
2014-08-19 16:14:17 +02:00
2014-07-28 22:08:31 +02:00
post . topic . reload
post . topic . like_count . should == 2
end
end
2013-05-04 02:52:45 +02:00
describe " when a user bookmarks something " do
it " increases the post's bookmark count when saved " do
lambda { bookmark . save ; post . reload } . should change ( post , :bookmark_count ) . by ( 1 )
end
2013-02-05 14:16:51 -05:00
2013-05-04 02:52:45 +02:00
it " increases the forum topic's bookmark count when saved " do
lambda { bookmark . save ; post . topic . reload } . should change ( post . topic , :bookmark_count ) . by ( 1 )
end
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-05 14:16:51 -05:00
2013-05-04 02:52:45 +02:00
it 'reduces the bookmark count of the post' do
lambda { post . reload } . should change ( post , :bookmark_count ) . by ( - 1 )
end
it 'reduces the bookmark count of the forum topic' do
lambda { @topic . reload } . should change ( post . topic , :bookmark_count ) . by ( - 1 )
end
end
end
2013-02-25 19:42:20 +03:00
describe 'when a user likes something' do
2013-05-27 12:45:10 -04:00
it 'should increase the `like_count` and `like_score` when a user likes something' do
PostAction . act ( codinghorror , post , PostActionType . types [ :like ] )
post . reload
post . like_count . should == 1
post . like_score . should == 1
post . topic . reload
post . topic . like_count . should == 1
# When a staff member likes it
PostAction . act ( moderator , post , PostActionType . types [ :like ] )
post . reload
post . like_count . should == 2
post . like_score . should == 4
# Removing likes
PostAction . remove_act ( codinghorror , post , PostActionType . types [ :like ] )
post . reload
post . like_count . should == 1
post . like_score . should == 3
PostAction . remove_act ( moderator , post , PostActionType . types [ :like ] )
post . reload
post . like_count . should == 0
post . like_score . should == 0
2013-02-05 14:16:51 -05:00
end
end
2014-07-18 16:14:47 -04: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 ] )
PostAction . where ( post : post ) . with_deleted . count . should == 1
PostAction . remove_act ( codinghorror , post , PostActionType . types [ :like ] )
# Check that we don't lose consistency into negatives
post . reload . like_count . should == 0
end
end
2013-02-25 19:42:20 +03:00
describe 'when a user votes for something' do
2013-05-04 02:52:45 +02:00
it 'should increase the vote counts when a user votes' do
2013-02-05 14:16:51 -05:00
lambda {
2013-03-01 15:07:44 +03:00
PostAction . act ( codinghorror , post , PostActionType . types [ :vote ] )
2013-02-05 14:16:51 -05:00
post . reload
} . should change ( post , :vote_count ) . by ( 1 )
end
2013-02-25 19:42:20 +03:00
it 'should increase the forum topic vote count when a user votes' do
2013-02-05 14:16:51 -05:00
lambda {
2013-03-01 15:07:44 +03:00
PostAction . act ( codinghorror , post , PostActionType . types [ :vote ] )
2013-02-05 14:16:51 -05:00
post . topic . reload
} . should change ( post . topic , :vote_count ) . by ( 1 )
end
end
describe 'flagging' do
2013-05-10 16:58:23 -04:00
context " flag_counts_for " do
it " returns the correct flag counts " do
2013-07-22 15:06:53 +10:00
post = create_post
2013-05-10 16:58:23 -04:00
SiteSetting . stubs ( :flags_required_to_hide_post ) . returns ( 7 )
# A post with no flags has 0 for flag counts
PostAction . flag_counts_for ( post . id ) . should == [ 0 , 0 ]
2014-08-19 16:14:17 +02:00
flag = PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
2013-05-10 16:58:23 -04:00
PostAction . flag_counts_for ( post . id ) . should == [ 0 , 1 ]
2013-05-31 17:38:28 -04:00
# If staff takes action, it is ranked higher
2014-07-28 19:17:37 +02:00
PostAction . act ( admin , post , PostActionType . types [ :spam ] , take_action : true )
2013-05-10 16:58:23 -04:00
PostAction . flag_counts_for ( post . id ) . should == [ 0 , 8 ]
# If a flag is dismissed
PostAction . clear_flags! ( post , admin )
PostAction . flag_counts_for ( post . id ) . should == [ 8 , 0 ]
end
end
2014-08-19 16:14:17 +02:00
it 'does not allow you to flag stuff with the same reason more than once' do
2013-02-07 10:45:58 +11:00
post = Fabricate ( :post )
2014-08-19 16:14:17 +02:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
lambda { PostAction . act ( eviltrout , post , PostActionType . types [ :off_topic ] ) } . should raise_error ( PostAction :: AlreadyActed )
2013-05-04 02:52:45 +02:00
end
it 'allows you to flag stuff with another reason' do
post = Fabricate ( :post )
2014-08-19 16:14:17 +02:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . remove_act ( eviltrout , post , PostActionType . types [ :spam ] )
lambda { PostAction . act ( eviltrout , post , PostActionType . types [ :off_topic ] ) } . should_not raise_error ( )
2013-02-07 10:45:58 +11:00
end
2013-02-25 19:42:20 +03:00
it 'should update counts when you clear flags' do
2013-02-05 14:16:51 -05:00
post = Fabricate ( :post )
2014-08-19 16:14:17 +02:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
2013-02-05 14:16:51 -05:00
post . reload
post . spam_count . should == 1
2014-07-28 19:17:37 +02:00
PostAction . clear_flags! ( post , Discourse . system_user )
2013-02-05 14:16:51 -05:00
2014-08-19 16:14:17 +02:00
post . reload
2013-02-05 14:16:51 -05:00
post . spam_count . should == 0
end
2013-02-25 19:42:20 +03:00
it 'should follow the rules for automatic hiding workflow' do
2013-07-22 15:06:53 +10:00
post = create_post
2014-08-19 16:14:17 +02:00
walterwhite = Fabricate ( :walter_white )
2013-02-05 14:16:51 -05:00
2013-06-20 17:42:15 +10:00
SiteSetting . stubs ( :flags_required_to_hide_post ) . returns ( 2 )
2014-08-19 16:14:17 +02:00
Discourse . stubs ( :site_contact_user ) . returns ( admin )
2013-02-05 14:16:51 -05:00
2014-08-19 16:14:17 +02:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . act ( walterwhite , post , PostActionType . types [ :spam ] )
2013-02-05 14:16:51 -05:00
post . reload
2014-09-25 17:44:48 +02:00
post . hidden . should == true
2014-06-20 15:03:02 -04:00
post . hidden_at . should be_present
2014-07-30 23:35:42 +02:00
post . hidden_reason_id . should == Post . hidden_reasons [ :flag_threshold_reached ]
2014-09-25 17:44:48 +02:00
post . topic . visible . should == false
2013-02-05 14:16:51 -05:00
2014-10-27 22:06:43 +01:00
post . revise ( post . user , { raw : post . raw + " ha I edited it " } )
2014-07-30 23:35:42 +02:00
2013-02-05 14:16:51 -05:00
post . reload
2014-09-25 17:44:48 +02:00
post . hidden . should == false
post . hidden_reason_id . should == nil
2014-06-20 15:03:02 -04:00
post . hidden_at . should be_blank
2014-09-25 17:44:48 +02:00
post . topic . visible . should == true
2013-02-05 14:16:51 -05:00
2014-08-19 16:14:17 +02:00
PostAction . act ( eviltrout , post , PostActionType . types [ :spam ] )
PostAction . act ( walterwhite , post , PostActionType . types [ :off_topic ] )
2013-02-05 14:16:51 -05:00
post . reload
2014-09-25 17:44:48 +02:00
post . hidden . should == true
2014-07-30 23:35:42 +02:00
post . hidden_at . should be_present
2013-03-18 14:59:34 -04:00
post . hidden_reason_id . should == Post . hidden_reasons [ :flag_threshold_reached_again ]
2014-09-25 17:44:48 +02:00
post . topic . visible . should == false
2013-02-05 14:16:51 -05:00
2014-10-27 22:06:43 +01:00
post . revise ( post . user , { raw : post . raw + " ha I edited it again " } )
2013-02-25 19:42:20 +03:00
2013-02-05 14:16:51 -05:00
post . reload
2014-09-25 17:44:48 +02:00
post . hidden . should == true
post . hidden_at . should be_present
2013-03-18 14:59:34 -04:00
post . hidden_reason_id . should == Post . hidden_reasons [ :flag_threshold_reached_again ]
2014-09-25 17:44:48 +02:00
post . topic . visible . should == false
2013-02-05 14:16:51 -05:00
end
2014-02-18 15:18:31 -05:00
2014-10-01 18:53:17 +02: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
post . hidden . should == true
post . hidden_at . should be_present
post . hidden_reason_id . should == Post . hidden_reasons [ :flagged_by_tl3_user ]
end
2014-02-18 15:18:31 -05:00
it " can flag the topic instead of a post " do
post1 = create_post
post2 = create_post ( topic : post1 . topic )
2014-08-19 16:14:17 +02:00
post_action = PostAction . act ( Fabricate ( :user ) , post1 , PostActionType . types [ :spam ] , { flag_topic : true } )
2014-02-18 15:18:31 -05:00
post_action . targets_topic . should == true
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
2014-08-19 16:14:17 +02:00
post_action = PostAction . act ( Fabricate ( :user ) , post , PostActionType . types [ :spam ] , { flag_topic : true } )
2014-02-18 15:18:31 -05:00
post_action . targets_topic . should == false
post_action . post_id . should == post . id
end
2014-08-19 16:14:17 +02: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
PostAction . act ( moderator , post , PostActionType . types [ :spam ] , { take_action : true } )
post . reload
post . hidden . should == true
PostAction . remove_act ( moderator , post , PostActionType . types [ :spam ] )
post . reload
post . hidden . should == false
end
2013-02-05 14:16:51 -05:00
end
2013-05-04 02:52:45 +02:00
it " prevents user to act twice at the same time " do
post = Fabricate ( :post )
# flags are already being tested
all_types_except_flags = PostActionType . types . except ( PostActionType . flag_types )
all_types_except_flags . values . each do | action |
lambda do
2014-08-19 16:14:17 +02:00
PostAction . act ( eviltrout , post , action )
PostAction . act ( eviltrout , post , action )
2013-05-04 02:52:45 +02:00
end . should raise_error ( PostAction :: AlreadyActed )
end
end
2013-02-05 14:16:51 -05:00
end