From 304ace926eaef81cbd57031c19f3915faee04ecb Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 27 Apr 2017 17:29:31 +0800 Subject: [PATCH] FIX: Raise right response when post_action does not exist. --- app/controllers/posts_controller.rb | 4 ++-- spec/controllers/posts_controller_spec.rb | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 9feab0fe827..6ed83c91ebb 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -383,8 +383,8 @@ class PostsController < ApplicationController PostAction.act(current_user, post, PostActionType.types[:bookmark]) else post_action = PostAction.find_by(post_id: params[:post_id], user_id: current_user.id) - post = post_action.post - raise Discourse::InvalidParameters unless post_action + post = post_action&.post + raise Discourse::NotFound unless post_action PostAction.remove_act(current_user, post, PostActionType.types[:bookmark]) end diff --git a/spec/controllers/posts_controller_spec.rb b/spec/controllers/posts_controller_spec.rb index 98b198550a7..ccee8bf1e2d 100644 --- a/spec/controllers/posts_controller_spec.rb +++ b/spec/controllers/posts_controller_spec.rb @@ -448,6 +448,12 @@ describe PostsController do let(:post_action) { PostAction.act(user, post, PostActionType.types[:bookmark]) } 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 post_action xhr :put, :bookmark, post_id: post.id