FIX: Raise right response when post_action does not exist.

This commit is contained in:
Guo Xiang Tan 2017-04-27 17:29:31 +08:00
parent e4b9f72f9e
commit 304ace926e
2 changed files with 8 additions and 2 deletions

View File

@ -383,8 +383,8 @@ class PostsController < ApplicationController
PostAction.act(current_user, post, PostActionType.types[:bookmark]) PostAction.act(current_user, post, PostActionType.types[:bookmark])
else else
post_action = PostAction.find_by(post_id: params[:post_id], user_id: current_user.id) post_action = PostAction.find_by(post_id: params[:post_id], user_id: current_user.id)
post = post_action.post post = post_action&.post
raise Discourse::InvalidParameters unless post_action raise Discourse::NotFound unless post_action
PostAction.remove_act(current_user, post, PostActionType.types[:bookmark]) PostAction.remove_act(current_user, post, PostActionType.types[:bookmark])
end end

View File

@ -448,6 +448,12 @@ describe PostsController do
let(:post_action) { PostAction.act(user, post, PostActionType.types[:bookmark]) } let(:post_action) { PostAction.act(user, post, PostActionType.types[:bookmark]) }
let(:admin) { Fabricate(:admin) } let(:admin) { Fabricate(:admin) }
it "returns the right response when post is not bookmarked" do
xhr :put, :bookmark, post_id: Fabricate(:post, user: user).id
expect(response.status).to eq(404)
end
it 'should be able to remove a bookmark' do it 'should be able to remove a bookmark' do
post_action post_action
xhr :put, :bookmark, post_id: post.id xhr :put, :bookmark, post_id: post.id