2015-10-11 10:41:23 +01:00
require 'rails_helper'
2013-02-05 14:16:51 -05:00
describe PostActionsController do
describe 'create' do
it 'requires you to be logged in' do
2015-01-09 14:04:02 -03:00
expect { xhr :post , :create } . to raise_error ( Discourse :: NotLoggedIn )
2013-02-05 14:16:51 -05:00
end
2016-03-15 16:06:50 -04:00
describe 'logged in as moderator' do
2013-02-05 14:16:51 -05:00
before do
2013-05-31 17:38:28 -04:00
@user = log_in ( :moderator )
2013-02-05 14:16:51 -05:00
@post = Fabricate ( :post , user : Fabricate ( :coding_horror ) )
end
it 'raises an error when the id is missing' do
2015-01-09 14:04:02 -03:00
expect { xhr :post , :create , post_action_type_id : PostActionType . types [ :like ] } . to raise_error ( ActionController :: ParameterMissing )
2013-02-05 14:16:51 -05:00
end
it 'raises an error when the post_action_type_id index is missing' do
2015-01-09 14:04:02 -03:00
expect { xhr :post , :create , id : @post . id } . to raise_error ( ActionController :: ParameterMissing )
2013-02-05 14:16:51 -05:00
end
2013-02-25 19:42:20 +03:00
2013-02-05 14:16:51 -05:00
it " fails when the user doesn't have permission to see the post " do
Guardian . any_instance . expects ( :can_see? ) . with ( @post ) . returns ( false )
2013-03-01 15:07:44 +03:00
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ]
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_forbidden
2013-02-05 14:16:51 -05:00
end
it " fails when the user doesn't have permission to perform that action " do
2014-09-03 14:43:07 -04:00
Guardian . any_instance . expects ( :post_can_act? ) . with ( @post , :like , taken_actions : nil ) . returns ( false )
2013-03-01 15:07:44 +03:00
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ]
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_forbidden
2013-02-05 14:16:51 -05:00
end
it 'allows us to create an post action on a post' do
2013-05-31 17:38:28 -04:00
PostAction . expects ( :act ) . once . with ( @user , @post , PostActionType . types [ :like ] , { } )
2013-03-01 15:07:44 +03:00
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ]
2013-02-05 14:16:51 -05:00
end
2013-05-31 17:38:28 -04:00
2014-09-03 14:43:07 -04:00
it " passes a list of taken actions through " do
PostAction . create ( post_id : @post . id , user_id : @user . id , post_action_type_id : PostActionType . types [ :inappropriate ] )
Guardian . any_instance . expects ( :post_can_act? ) . with ( @post , :off_topic , has_entry ( { :taken_actions = > has_key ( PostActionType . types [ :inappropriate ] ) } ) )
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :off_topic ]
end
2013-05-31 17:38:28 -04:00
it 'passes the message through' do
PostAction . expects ( :act ) . once . with ( @user , @post , PostActionType . types [ :like ] , { message : 'action message goes here' } )
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ] , message : 'action message goes here'
end
2016-04-03 19:44:14 -04:00
it 'passes the message through as warning' do
PostAction . expects ( :act ) . once . with ( @user , @post , PostActionType . types [ :like ] , { message : 'action message goes here' , is_warning : true } )
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ] , message : 'action message goes here' , is_warning : true
end
it " doesn't create message as a warning if the user isn't staff " do
Guardian . any_instance . stubs ( :is_staff? ) . returns ( false )
PostAction . expects ( :act ) . once . with ( @user , @post , PostActionType . types [ :like ] , { message : 'action message goes here' } )
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ] , message : 'action message goes here' , is_warning : true
end
2013-05-31 17:38:28 -04:00
it 'passes take_action through' do
PostAction . expects ( :act ) . once . with ( @user , @post , PostActionType . types [ :like ] , { take_action : true } )
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ] , take_action : 'true'
end
it " doesn't pass take_action through if the user isn't staff " do
Guardian . any_instance . stubs ( :is_staff? ) . returns ( false )
PostAction . expects ( :act ) . once . with ( @user , @post , PostActionType . types [ :like ] , { } )
xhr :post , :create , id : @post . id , post_action_type_id : PostActionType . types [ :like ] , take_action : 'true'
end
2013-02-05 14:16:51 -05:00
end
end
context 'destroy' do
let ( :post ) { Fabricate ( :post , user : Fabricate ( :coding_horror ) ) }
it 'requires you to be logged in' do
2015-01-09 14:04:02 -03:00
expect { xhr :delete , :destroy , id : post . id } . to raise_error ( Discourse :: NotLoggedIn )
2013-02-05 14:16:51 -05:00
end
context 'logged in' do
let! ( :user ) { log_in }
it 'raises an error when the post_action_type_id is missing' do
2015-01-09 14:04:02 -03:00
expect { xhr :delete , :destroy , id : post . id } . to raise_error ( ActionController :: ParameterMissing )
2013-02-05 14:16:51 -05:00
end
it " returns 404 when the post action type doesn't exist for that user " do
xhr :delete , :destroy , id : post . id , post_action_type_id : 1
2015-01-09 14:04:02 -03:00
expect ( response . code ) . to eq ( '404' )
2013-02-05 14:16:51 -05:00
end
context 'with a post_action record ' do
let! ( :post_action ) { PostAction . create ( user_id : user . id , post_id : post . id , post_action_type_id : 1 ) }
it 'returns success' do
xhr :delete , :destroy , id : post . id , post_action_type_id : 1
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_success
2013-02-05 14:16:51 -05:00
end
it 'deletes the action' do
xhr :delete , :destroy , id : post . id , post_action_type_id : 1
2015-01-09 14:04:02 -03:00
expect ( PostAction . exists? ( user_id : user . id , post_id : post . id , post_action_type_id : 1 , deleted_at : nil ) ) . to eq ( false )
2013-02-05 14:16:51 -05:00
end
it 'ensures it can be deleted' do
Guardian . any_instance . expects ( :can_delete? ) . with ( post_action ) . returns ( false )
xhr :delete , :destroy , id : post . id , post_action_type_id : 1
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_forbidden
2013-02-05 14:16:51 -05:00
end
end
end
end
2014-07-28 19:17:37 +02:00
context 'defer_flags' do
2013-02-08 19:04:14 -05:00
let ( :flagged_post ) { Fabricate ( :post , user : Fabricate ( :coding_horror ) ) }
context " not logged in " do
it " should not allow them to clear flags " do
2015-01-09 14:04:02 -03:00
expect { xhr :post , :defer_flags } . to raise_error ( Discourse :: NotLoggedIn )
2013-02-25 19:42:20 +03:00
end
2013-02-08 19:04:14 -05:00
end
context 'logged in' do
let! ( :user ) { log_in ( :moderator ) }
it " raises an error without a post_action_type_id " do
2015-01-09 14:04:02 -03:00
expect { xhr :post , :defer_flags , id : flagged_post . id } . to raise_error ( ActionController :: ParameterMissing )
2013-02-08 19:04:14 -05:00
end
it " raises an error when the user doesn't have access " do
2014-07-28 19:17:37 +02:00
Guardian . any_instance . expects ( :can_defer_flags? ) . returns ( false )
xhr :post , :defer_flags , id : flagged_post . id , post_action_type_id : PostActionType . types [ :spam ]
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_forbidden
2013-02-08 19:04:14 -05:00
end
context " success " do
before do
2014-07-28 19:17:37 +02:00
Guardian . any_instance . expects ( :can_defer_flags? ) . returns ( true )
PostAction . expects ( :defer_flags! ) . with ( flagged_post , user )
2013-02-08 19:04:14 -05:00
end
2014-07-28 19:17:37 +02:00
it " delegates to defer_flags " do
xhr :post , :defer_flags , id : flagged_post . id , post_action_type_id : PostActionType . types [ :spam ]
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_success
2013-02-25 19:42:20 +03:00
end
2013-02-08 19:04:14 -05:00
it " works with a deleted post " do
2013-07-09 15:20:18 -04:00
flagged_post . trash! ( user )
2014-07-28 19:17:37 +02:00
xhr :post , :defer_flags , id : flagged_post . id , post_action_type_id : PostActionType . types [ :spam ]
2015-01-09 14:04:02 -03:00
expect ( response ) . to be_success
2013-02-08 19:04:14 -05:00
end
end
end
end
2013-02-05 14:16:51 -05:00
end