2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
2010-03-10 18:21:43 -05:00
|
|
|
|
2018-04-02 13:51:26 -04:00
|
|
|
class PostVotesControllerTest < ActionDispatch::IntegrationTest
|
2011-01-26 18:10:49 -05:00
|
|
|
context "The post vote controller" do
|
|
|
|
setup do
|
2019-06-29 11:45:58 -04:00
|
|
|
@user = create(:privileged_user)
|
2022-11-26 09:25:27 -05:00
|
|
|
as(@user) do
|
2018-04-02 13:51:26 -04:00
|
|
|
@post = create(:post)
|
|
|
|
end
|
2011-01-26 18:10:49 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-01-26 18:10:49 -05:00
|
|
|
context "create action" do
|
2016-10-14 00:38:41 -04:00
|
|
|
should "not allow anonymous users to vote" do
|
2022-02-10 11:56:27 -05:00
|
|
|
post post_votes_path(post_id: @post.id), params: { score: 1, format: "json" }
|
2016-10-14 00:38:41 -04:00
|
|
|
assert_response 403
|
2018-04-02 13:51:26 -04:00
|
|
|
assert_equal(0, @post.reload.score)
|
2016-10-14 00:38:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
should "not allow banned users to vote" do
|
2018-04-02 13:51:26 -04:00
|
|
|
@banned = create(:banned_user)
|
2022-02-10 11:56:27 -05:00
|
|
|
post_auth post_votes_path(post_id: @post.id), @banned, params: { score: 1, format: "json" }
|
|
|
|
assert_response 401
|
2018-04-02 13:51:26 -04:00
|
|
|
assert_equal(0, @post.reload.score)
|
2016-10-14 00:38:41 -04:00
|
|
|
end
|
|
|
|
|
2011-01-26 18:10:49 -05:00
|
|
|
should "increment a post's score if the score is positive" do
|
2022-02-10 11:56:27 -05:00
|
|
|
post_auth post_votes_path(post_id: @post.id), @user, params: { score: 1, format: "json" }
|
2012-01-15 17:04:22 -05:00
|
|
|
assert_response :success
|
2011-01-26 18:10:49 -05:00
|
|
|
@post.reload
|
|
|
|
assert_equal(1, @post.score)
|
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-01-26 18:10:49 -05:00
|
|
|
context "for a post that has already been voted on" do
|
|
|
|
setup do
|
2022-11-26 09:25:27 -05:00
|
|
|
as(@user) do
|
2022-02-10 11:56:27 -05:00
|
|
|
post_auth post_votes_path(post_id: @post.id), @user, params: { score: 1, format: "json" }
|
2018-04-02 13:51:26 -04:00
|
|
|
end
|
2011-01-26 18:10:49 -05:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2011-01-26 18:10:49 -05:00
|
|
|
should "fail silently on an error" do
|
|
|
|
assert_nothing_raised do
|
2022-02-10 11:56:27 -05:00
|
|
|
post_auth post_votes_path(post_id: @post.id), @user, params: { score: "up", format: "json" }
|
2011-01-26 18:10:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-03-10 18:21:43 -05:00
|
|
|
end
|
|
|
|
end
|