Prevent users from upvoting their own comments.
This commit is contained in:
@@ -36,7 +36,10 @@ class CommentVote < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def validate_comment_can_be_down_voted
|
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"
|
errors.add :base, "You cannot downvote an admin comment"
|
||||||
false
|
false
|
||||||
else
|
else
|
||||||
|
|||||||
@@ -160,19 +160,28 @@ class CommentTest < ActiveSupport::TestCase
|
|||||||
user = FactoryGirl.create(:user)
|
user = FactoryGirl.create(:user)
|
||||||
post = FactoryGirl.create(:post)
|
post = FactoryGirl.create(:post)
|
||||||
c1 = FactoryGirl.create(:comment, :post => post)
|
c1 = FactoryGirl.create(:comment, :post => post)
|
||||||
comment_vote = c1.vote!("up")
|
comment_vote = c1.vote!("down")
|
||||||
assert_equal([], comment_vote.errors.full_messages)
|
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(["You have already voted for this comment"], comment_vote.errors.full_messages)
|
||||||
assert_equal(1, CommentVote.count)
|
assert_equal(1, CommentVote.count)
|
||||||
assert_equal(1, CommentVote.last.score)
|
assert_equal(-1, CommentVote.last.score)
|
||||||
|
|
||||||
c2 = FactoryGirl.create(:comment, :post => post)
|
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([], comment_vote.errors.full_messages)
|
||||||
assert_equal(2, CommentVote.count)
|
assert_equal(2, CommentVote.count)
|
||||||
end
|
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
|
should "allow undoing of votes" do
|
||||||
user = FactoryGirl.create(:user)
|
user = FactoryGirl.create(:user)
|
||||||
post = FactoryGirl.create(:post)
|
post = FactoryGirl.create(:post)
|
||||||
|
|||||||
Reference in New Issue
Block a user