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