comments: allow swapping votes.

Allow users to upvote a comment, then downvote it, without raising an
error or having to manually remove the upvote first. The upvote is
automatically removed and replaced by the downvote.

Changes to the /comment_votes API:

* `POST /comment_votes` and `DELETE /comment_votes` now return a comment
  vote instead of a comment.
* The `score` param in `POST /comment_votes` is now 1 or -1, not
  `up` or `down.`
This commit is contained in:
evazion
2021-01-21 01:02:22 -06:00
parent c31f2003d9
commit 9efb374ae5
12 changed files with 181 additions and 133 deletions

View File

@@ -143,55 +143,6 @@ class CommentTest < ActiveSupport::TestCase
end
end
should "not record the user id of the voter" do
user = FactoryBot.create(:user)
post = FactoryBot.create(:post)
c1 = FactoryBot.create(:comment, :post => post)
CurrentUser.scoped(user, "127.0.0.1") do
c1.vote!("up")
c1.reload
assert_not_equal(user.id, c1.updater_id)
end
end
should "not allow duplicate votes" do
user = FactoryBot.create(:user)
post = FactoryBot.create(:post)
c1 = FactoryBot.create(:comment, :post => post)
assert_nothing_raised { c1.vote!("down") }
exception = assert_raises(ActiveRecord::RecordInvalid) { c1.vote!("down") }
assert_equal("Validation failed: You have already voted for this comment", exception.message)
assert_equal(1, CommentVote.count)
assert_equal(-1, CommentVote.last.score)
c2 = FactoryBot.create(:comment, :post => post)
assert_nothing_raised { c2.vote!("down") }
assert_equal(2, CommentVote.count)
end
should "not allow upvotes by the creator" do
user = FactoryBot.create(:user)
post = FactoryBot.create(:post)
c1 = create(:comment, post: post, creator: CurrentUser.user)
exception = assert_raises(ActiveRecord::RecordInvalid) { c1.vote!("up") }
assert_equal("Validation failed: You cannot upvote your own comments", exception.message)
end
should "allow undoing of votes" do
user = FactoryBot.create(:user)
post = FactoryBot.create(:post)
comment = FactoryBot.create(:comment, :post => post)
CurrentUser.scoped(user, "127.0.0.1") do
comment.vote!("up")
comment.unvote!
comment.reload
assert_equal(0, comment.score)
assert_nothing_raised {comment.vote!("down")}
end
end
should "be searchable" do
c1 = FactoryBot.create(:comment, :body => "aaa bbb ccc")
c2 = FactoryBot.create(:comment, :body => "aaa ddd")

View File

@@ -0,0 +1,57 @@
require 'test_helper'
class CommentVoteTest < ActiveSupport::TestCase
context "A CommentVote" do
setup do
@user = create(:user)
@comment = as(@user) { create(:comment) }
end
context "during validation" do
subject { build(:comment_vote, comment: as(@user) { create(:comment) }) }
should validate_uniqueness_of(:user_id).scoped_to(:comment_id).with_message("have already voted for this comment")
should validate_inclusion_of(:score).in_array([-1, 1]).with_message("must be 1 or -1")
end
context "creating" do
context "an upvote" do
should "increment the comment's score" do
vote = create(:comment_vote, comment: @comment, score: 1)
assert_equal(1, @comment.reload.score)
end
end
context "a downvote" do
should "decrement the comment's score" do
vote = create(:comment_vote, comment: @comment, score: -1)
assert_equal(-1, @comment.reload.score)
end
end
end
context "destroying" do
context "an upvote" do
should "decrement the comment's score" do
vote = create(:comment_vote, comment: @comment, score: 1)
assert_equal(1, @comment.reload.score)
vote.destroy
assert_equal(0, @comment.reload.score)
end
end
context "a downvote" do
should "increment the comment's score" do
vote = create(:comment_vote, comment: @comment, score: -1)
assert_equal(-1, @comment.reload.score)
vote.destroy
assert_equal(0, @comment.reload.score)
end
end
end
end
end