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