Prevent commenting on nonexistent posts (#2704).

This commit is contained in:
evazion
2016-10-06 09:12:23 +00:00
parent 8c8f4a6a8f
commit cb1e1d3a94
3 changed files with 17 additions and 0 deletions

View File

@@ -65,6 +65,11 @@ class CommentsControllerTest < ActionController::TestCase
comment = Comment.last
assert_redirected_to post_path(comment.post)
end
should "not allow commenting on nonexistent posts" do
post :create, {:comment => FactoryGirl.attributes_for(:comment, :post_id => -1)}, {:user_id => @user.id}
assert_response :error
end
end
end
end

View File

@@ -101,6 +101,13 @@ class CommentTest < ActiveSupport::TestCase
assert(comment.errors.empty?, comment.errors.full_messages.join(", "))
end
should "not validate if the post does not exist" do
comment = FactoryGirl.build(:comment, :post_id => -1)
assert_not(comment.valid?)
assert_equal(["must exist"], comment.errors[:post])
end
should "not bump the parent post" do
post = FactoryGirl.create(:post)
comment = FactoryGirl.create(:comment, :do_not_bump_post => true, :post => post)