discourse/spec/controllers/post_actions_controller_spec.rb

136 lines
3.7 KiB
Ruby
Raw Normal View History

require 'rails_helper'
2013-02-06 03:16:51 +08:00
describe PostActionsController do
describe 'create' do
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
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)) }
context 'logged in' do
let!(:user) { log_in }
it 'raises an error when the post_action_type_id is missing' do
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
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
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
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)
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
context 'defer_flags' do
let(:flagged_post) { Fabricate(:post, user: Fabricate(:coding_horror)) }
context "not logged in" do
it "should not allow them to clear flags" do
post :defer_flags, format: :json
expect(response.status).to eq(403)
2013-02-26 00:42:20 +08:00
end
end
context 'logged in' do
let!(:user) { log_in(:moderator) }
it "raises an error without a post_action_type_id" do
expect do
post :defer_flags, params: { id: flagged_post.id }, format: :json
end.to raise_error(ActionController::ParameterMissing)
end
it "raises an error when the user doesn't have access" do
Guardian.any_instance.expects(:can_defer_flags?).returns(false)
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
end
context "success" do
before do
Guardian.any_instance.expects(:can_defer_flags?).returns(true)
PostAction.expects(:defer_flags!).with(flagged_post, user)
end
it "delegates to defer_flags" do
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
it "works with a deleted post" do
2013-07-10 03:20:18 +08:00
flagged_post.trash!(user)
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
end
end
end
end
2013-02-06 03:16:51 +08:00
end