2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe PostActionsController do
|
|
|
|
|
|
|
|
describe 'create' do
|
|
|
|
|
2016-12-20 23:01:26 -05: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-31 00:06:56 -04:00
|
|
|
post :create, params: {
|
|
|
|
id: private_message.id,
|
|
|
|
post_action_type_id: PostActionType.types[:bookmark]
|
|
|
|
}, format: :json
|
2017-01-05 21:39:44 -05:00
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-02-05 14:16:51 -05: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-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
delete :destroy, params: { id: post.id }, format: :json
|
|
|
|
end.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
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: post.id, post_action_type_id: 1 }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response.code).to eq('404')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a post_action record ' do
|
2017-07-27 21:20:09 -04:00
|
|
|
let!(:post_action) { PostAction.create(user_id: user.id, post_id: post.id, post_action_type_id: 1) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
it 'returns success' do
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: { id: post.id, post_action_type_id: 1 }, format: :json
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'deletes the action' do
|
2017-08-31 00:06:56 -04:00
|
|
|
delete :destroy, params: {
|
|
|
|
id: post.id, post_action_type_id: 1
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05: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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
delete :destroy, params: {
|
|
|
|
id: post.id, post_action_type_id: 1
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2014-07-28 13:17:37 -04: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
|
2018-01-11 22:15:10 -05:00
|
|
|
post :defer_flags, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
2013-02-25 11:42:20 -05: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
|
2017-08-31 00:06:56 -04:00
|
|
|
expect do
|
|
|
|
post :defer_flags, params: { id: flagged_post.id }, format: :json
|
|
|
|
end.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 13:17:37 -04:00
|
|
|
Guardian.any_instance.expects(:can_defer_flags?).returns(false)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :defer_flags, params: {
|
|
|
|
id: flagged_post.id, post_action_type_id: PostActionType.types[:spam]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-02-08 19:04:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context "success" do
|
|
|
|
before do
|
2014-07-28 13:17:37 -04: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 13:17:37 -04:00
|
|
|
it "delegates to defer_flags" do
|
2017-08-31 00:06:56 -04:00
|
|
|
post :defer_flags, params: {
|
|
|
|
id: flagged_post.id, post_action_type_id: PostActionType.types[:spam]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-02-25 11:42:20 -05: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)
|
2017-08-31 00:06:56 -04:00
|
|
|
|
|
|
|
post :defer_flags, params: {
|
|
|
|
id: flagged_post.id, post_action_type_id: PostActionType.types[:spam]
|
|
|
|
}, format: :json
|
|
|
|
|
2015-01-09 12:04:02 -05: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
|