Prevent users from upvoting their own comments.

This commit is contained in:
evazion
2016-11-06 01:20:10 -06:00
parent 4e48e80e1f
commit 1047d7c96b
2 changed files with 17 additions and 5 deletions

View File

@@ -36,7 +36,10 @@ class CommentVote < ActiveRecord::Base
end
def validate_comment_can_be_down_voted
if is_negative? && comment.creator.is_admin?
if is_positive? && comment.creator == CurrentUser.user
errors.add :base, "You cannot upvote your own comments"
false
elsif is_negative? && comment.creator.is_admin?
errors.add :base, "You cannot downvote an admin comment"
false
else

View File

@@ -160,19 +160,28 @@ class CommentTest < ActiveSupport::TestCase
user = FactoryGirl.create(:user)
post = FactoryGirl.create(:post)
c1 = FactoryGirl.create(:comment, :post => post)
comment_vote = c1.vote!("up")
comment_vote = c1.vote!("down")
assert_equal([], comment_vote.errors.full_messages)
comment_vote = c1.vote!("up")
comment_vote = c1.vote!("down")
assert_equal(["You have already voted for this comment"], comment_vote.errors.full_messages)
assert_equal(1, CommentVote.count)
assert_equal(1, CommentVote.last.score)
assert_equal(-1, CommentVote.last.score)
c2 = FactoryGirl.create(:comment, :post => post)
comment_vote = c2.vote!("up")
comment_vote = c2.vote!("down")
assert_equal([], comment_vote.errors.full_messages)
assert_equal(2, CommentVote.count)
end
should "not allow upvotes by the creator" do
user = FactoryGirl.create(:user)
post = FactoryGirl.create(:post)
c1 = FactoryGirl.create(:comment, :post => post)
comment_vote = c1.vote!("up")
assert_equal(["You cannot upvote your own comments"], comment_vote.errors.full_messages)
end
should "allow undoing of votes" do
user = FactoryGirl.create(:user)
post = FactoryGirl.create(:post)