2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe PostActionsController do
|
|
|
|
|
|
|
|
describe 'create' do
|
|
|
|
|
2016-12-20 22:01:26 -06:00
|
|
|
context 'logged in as user' do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:private_message) { Fabricate(:private_message_post, user: Fabricate(:coding_horror)) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
log_in_user(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'fails when the user does not have permission to see the post' do
|
2017-08-30 23:06:56 -05:00
|
|
|
post :create, params: {
|
|
|
|
id: private_message.id,
|
|
|
|
post_action_type_id: PostActionType.types[:bookmark]
|
|
|
|
}, format: :json
|
2017-01-05 20:39:44 -06:00
|
|
|
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response).to be_forbidden
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'destroy' do
|
|
|
|
|
|
|
|
let(:post) { Fabricate(:post, user: Fabricate(:coding_horror)) }
|
|
|
|
|
|
|
|
context 'logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'raises an error when the post_action_type_id is missing' do
|
2017-08-30 23:06:56 -05:00
|
|
|
expect do
|
|
|
|
delete :destroy, params: { id: post.id }, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the post action type doesn't exist for that user" do
|
2017-08-30 23:06:56 -05:00
|
|
|
delete :destroy, params: { id: post.id, post_action_type_id: 1 }, format: :json
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response.code).to eq('404')
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a post_action record ' do
|
2017-07-27 20:20:09 -05:00
|
|
|
let!(:post_action) { PostAction.create(user_id: user.id, post_id: post.id, post_action_type_id: 1) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
it 'returns success' do
|
2017-08-30 23:06:56 -05:00
|
|
|
delete :destroy, params: { id: post.id, post_action_type_id: 1 }, format: :json
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response).to be_success
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the action' do
|
2017-08-30 23:06:56 -05:00
|
|
|
delete :destroy, params: {
|
|
|
|
id: post.id, post_action_type_id: 1
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 11:04:02 -06: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 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'ensures it can be deleted' do
|
|
|
|
Guardian.any_instance.expects(:can_delete?).with(post_action).returns(false)
|
2017-08-30 23:06:56 -05:00
|
|
|
|
|
|
|
delete :destroy, params: {
|
|
|
|
id: post.id, post_action_type_id: 1
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response).to be_forbidden
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-07-28 12:17:37 -05:00
|
|
|
context 'defer_flags' do
|
2013-02-08 18:04:14 -06:00
|
|
|
|
|
|
|
let(:flagged_post) { Fabricate(:post, user: Fabricate(:coding_horror)) }
|
|
|
|
|
|
|
|
context "not logged in" do
|
|
|
|
it "should not allow them to clear flags" do
|
2018-01-11 21:15:10 -06:00
|
|
|
post :defer_flags, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
2013-02-08 18:04:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'logged in' do
|
|
|
|
let!(:user) { log_in(:moderator) }
|
|
|
|
|
|
|
|
it "raises an error without a post_action_type_id" do
|
2017-08-30 23:06:56 -05:00
|
|
|
expect do
|
|
|
|
post :defer_flags, params: { id: flagged_post.id }, format: :json
|
|
|
|
end.to raise_error(ActionController::ParameterMissing)
|
2013-02-08 18:04:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises an error when the user doesn't have access" do
|
2014-07-28 12:17:37 -05:00
|
|
|
Guardian.any_instance.expects(:can_defer_flags?).returns(false)
|
2017-08-30 23:06:56 -05:00
|
|
|
|
|
|
|
post :defer_flags, params: {
|
|
|
|
id: flagged_post.id, post_action_type_id: PostActionType.types[:spam]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response).to be_forbidden
|
2013-02-08 18:04:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
context "success" do
|
|
|
|
before do
|
2014-07-28 12:17:37 -05:00
|
|
|
Guardian.any_instance.expects(:can_defer_flags?).returns(true)
|
|
|
|
PostAction.expects(:defer_flags!).with(flagged_post, user)
|
2013-02-08 18:04:14 -06:00
|
|
|
end
|
|
|
|
|
2014-07-28 12:17:37 -05:00
|
|
|
it "delegates to defer_flags" do
|
2017-08-30 23:06:56 -05:00
|
|
|
post :defer_flags, params: {
|
|
|
|
id: flagged_post.id, post_action_type_id: PostActionType.types[:spam]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response).to be_success
|
2013-02-25 10:42:20 -06:00
|
|
|
end
|
2013-02-08 18:04:14 -06:00
|
|
|
|
|
|
|
it "works with a deleted post" do
|
2013-07-09 14:20:18 -05:00
|
|
|
flagged_post.trash!(user)
|
2017-08-30 23:06:56 -05:00
|
|
|
|
|
|
|
post :defer_flags, params: {
|
|
|
|
id: flagged_post.id, post_action_type_id: PostActionType.types[:spam]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(response).to be_success
|
2013-02-08 18:04:14 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|