[Tests] Fix vote tests

This commit is contained in:
Earlopain 2022-02-10 17:56:27 +01:00
parent c6dae231f0
commit 51e3d7d50d
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
3 changed files with 19 additions and 41 deletions

View File

@ -5,6 +5,8 @@ class ForumPostVotesControllerTest < ActionDispatch::IntegrationTest
setup do
@user1 = create(:user)
@user2 = create(:user)
CurrentUser.user = @user1
CurrentUser.ip_addr = "127.0.0.1"
as @user1 do
@forum_topic = create(:forum_topic, original_post_attributes: { body: "alias" })

View File

@ -3,9 +3,11 @@ require 'test_helper'
class CommentVotesControllerTest < ActionDispatch::IntegrationTest
context "A comment votes controller" do
setup do
CurrentUser.user = @user = create(:user)
@user = create(:user)
@post = create(:post, uploader: @user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@comment = create(:comment)
@comment = create(:comment, post: @post)
end
teardown do
@ -15,36 +17,17 @@ class CommentVotesControllerTest < ActionDispatch::IntegrationTest
context "#create.json" do
should "create a vote" do
assert_difference("CommentVote.count", 1) do
post_auth comment_votes_path(comment_id: @comment.id, format: "json"), @user
assert_response :success
assert_equal("{\"success\": true}", @response.body.strip)
end
end
should "fail silently on errors" do
create(:comment_vote, comment: @comment, score: -1)
assert_difference("CommentVote.count", 0) do
post_auth comment_votes_path(comment_id: @comment.id, score: "-1", format: "json"), @user
assert_response 422
assert_equal("{\"success\": false, \"errors\": \"Validation failed: You have already voted for this comment\"}", @response.body.strip)
end
end
end
context "#create.js" do
should "create a vote" do
assert_difference("CommentVote.count", 1) do
post_auth comment_votes_path(comment_id: @comment.id, format: "json", score: 1), @user
assert_difference(-> { CommentVote.count }, 1) do
post_auth comment_votes_path(@comment), @user, params: { score: -1, format: :json}
assert_response :success
end
end
should "fail on errors" do
create(:comment_vote, :comment => @comment, :score => -1)
assert_difference("CommentVote.count", 0) do
post_auth comment_votes_path(comment_id: @comment.id, :score => -1, format: "js"), @user
assert_response 422
should "unvote when the vote already exists" do
create(:comment_vote, comment: @comment, user: @user, score: -1)
assert_difference(-> { CommentVote.count }, -1) do
post_auth comment_votes_path(@comment), @user, params: { score: -1, format: :json }
assert_response :success
end
end
end

View File

@ -11,27 +11,20 @@ class PostVotesControllerTest < ActionDispatch::IntegrationTest
context "create action" do
should "not allow anonymous users to vote" do
post post_votes_path(post_id: @post.id), params: {:score => "up", :format => "js"}
post post_votes_path(post_id: @post.id), params: { score: 1, format: "json" }
assert_response 403
assert_equal(0, @post.reload.score)
end
should "not allow banned users to vote" do
@banned = create(:banned_user)
post_auth post_votes_path(post_id: @post.id), @banned, params: {:score => "up", :format => "js"}
assert_response 403
assert_equal(0, @post.reload.score)
end
should "not allow members to vote" do
@member = create(:member_user)
post_auth post_votes_path(post_id: @post.id), @member, params: {:score => "up", :format => "js"}
assert_response 403
post_auth post_votes_path(post_id: @post.id), @banned, params: { score: 1, format: "json" }
assert_response 401
assert_equal(0, @post.reload.score)
end
should "increment a post's score if the score is positive" do
post_auth post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
post_auth post_votes_path(post_id: @post.id), @user, params: { score: 1, format: "json" }
assert_response :success
@post.reload
assert_equal(1, @post.score)
@ -40,13 +33,13 @@ class PostVotesControllerTest < ActionDispatch::IntegrationTest
context "for a post that has already been voted on" do
setup do
@user.as_current do
@post.vote!("up")
post_auth post_votes_path(post_id: @post.id), @user, params: { score: 1, format: "json" }
end
end
should "fail silently on an error" do
assert_nothing_raised do
post_auth post_votes_path(post_id: @post.id), @user, params: {:score => "up", :format => "js"}
post_auth post_votes_path(post_id: @post.id), @user, params: { score: "up", format: "json" }
end
end
end