votes: fixup various minor issues.

* Add a gap between thumbnails on mobile.
* Adjust CSS for scores and vote buttons.
* Include "Private favorites" as an incentive on the user upgrade page.
* Fix vote buttons not being visible beneath thumbnails on mobile.
* Fix the "Show scores" link not preserving the current page number.
* Fix vote buttons being unintentionally enabled for all thumbnails by default.
* Fix banned and restricted users being able to favorite posts by
  tagging them with `fav:self`.
* Fix search engines being able to crawl /posts?view=score pages.
* Fix broken tests.
This commit is contained in:
evazion
2021-11-19 22:40:34 -06:00
parent 3ae62d08eb
commit eda23c719a
20 changed files with 108 additions and 94 deletions

View File

@@ -7,7 +7,7 @@ class PostNavbarComponentTest < ViewComponent::TestCase
setup do
@post = create(:post)
@user = create(:user)
@user = create(:gold_user)
end
context "The PostNavbarComponent" do
@@ -46,9 +46,8 @@ class PostNavbarComponentTest < ViewComponent::TestCase
context "for a post with favgroups" do
setup do
as(@user) do
@favgroup1 = create(:favorite_group, creator: @user, is_public: true)
@favgroup2 = create(:favorite_group, creator: @user, is_public: false)
@post.update(tag_string: "favgroup:#{@favgroup1.id} favgroup:#{@favgroup2.id}")
@favgroup1 = create(:favorite_group, creator: @user, post_ids: [@post.id])
@favgroup2 = create(:private_favorite_group, creator: @user, post_ids: [@post.id])
end
end

View File

@@ -11,12 +11,12 @@ class PostVotesComponentTest < ViewComponent::TestCase
end
context "for a user who can't vote" do
should "not show the vote buttons" do
should "show the vote buttons" do
render_post_votes(@post, current_user: User.anonymous)
assert_css(".post-score")
assert_no_css(".post-upvote-link")
assert_no_css(".post-downvote-link")
assert_css(".post-upvote-link.inactive-link")
assert_css(".post-downvote-link.inactive-link")
end
end
@@ -34,8 +34,8 @@ class PostVotesComponentTest < ViewComponent::TestCase
context "for a downvoted post" do
should "highlight the downvote button as active" do
@post.vote!(-1, @user)
render_post_votes(@post, current_user: @user)
create(:post_vote, post: @post, user: @user, score: -1)
as(@user) { render_post_votes(@post, current_user: @user) }
assert_css(".post-upvote-link.inactive-link")
assert_css(".post-downvote-link.active-link")
@@ -44,8 +44,8 @@ class PostVotesComponentTest < ViewComponent::TestCase
context "for an upvoted post" do
should "highlight the upvote button as active" do
@post.vote!(1, @user)
render_post_votes(@post, current_user: @user)
create(:post_vote, post: @post, user: @user, score: 1)
as(@user) { render_post_votes(@post, current_user: @user) }
assert_css(".post-upvote-link.active-link")
assert_css(".post-downvote-link.inactive-link")

View File

@@ -2,5 +2,9 @@ FactoryBot.define do
factory(:favorite) do
user
post
factory(:private_favorite) do
user factory: :gold_user, enable_private_favorites: true
end
end
end

View File

@@ -11,10 +11,13 @@ class FavoriteTest < ActiveSupport::TestCase
context "Favorites: " do
context "removing a favorite" do
should "update the post and user favorite counts" do
@user1 = create(:restricted_user)
fav = Favorite.create!(post: @p1, user: @user1)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(0, @p1.reload.score)
refute(PostVote.positive.exists?(post: @p1, user: @user))
Favorite.destroy_by(post: @p1, user: @user1)
@@ -42,14 +45,8 @@ class FavoriteTest < ActiveSupport::TestCase
end
context "adding a favorite" do
should "update the post and user favorite counts" do
Favorite.create!(post: @p1, user: @user1)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
end
should "not upvote the post if the user can't vote" do
@user1 = create(:restricted_user)
Favorite.create!(post: @p1, user: @user1)
assert_equal(1, @user1.reload.favorite_count)
@@ -89,7 +86,7 @@ class FavoriteTest < ActiveSupport::TestCase
assert_equal(["You have already favorited this post"], @f2.errors.full_messages)
assert_equal(1, @user1.reload.favorite_count)
assert_equal(1, @p1.reload.fav_count)
assert_equal(0, @p1.reload.score)
assert_equal(1, @p1.reload.score)
end
end
end

View File

@@ -390,7 +390,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
favgroup1 = create(:favorite_group, creator: CurrentUser.user, post_ids: [post1.id])
favgroup2 = create(:favorite_group, creator: CurrentUser.user, post_ids: [post2.id])
favgroup3 = create(:favorite_group, creator: create(:user), post_ids: [post3.id], is_public: false)
favgroup3 = create(:private_favorite_group, post_ids: [post3.id])
assert_tag_match([post1], "favgroup:#{favgroup1.id}")
assert_tag_match([post2], "favgroup:#{favgroup2.name}")
@@ -421,7 +421,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
post3 = create(:post)
favgroup1 = create(:favorite_group, creator: CurrentUser.user, post_ids: [post1.id, post2.id])
favgroup2 = create(:favorite_group, creator: create(:user), post_ids: [post2.id, post3.id], is_public: false)
favgroup2 = create(:private_favorite_group, post_ids: [post2.id, post3.id])
assert_tag_match([post1, post2], "ordfavgroup:#{favgroup1.id}")
assert_tag_match([post1, post2], "ordfavgroup:#{favgroup1.name}")
@@ -975,7 +975,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
context "for the upvote:<user> metatag" do
setup do
@user = create(:user)
@user = create(:gold_user)
@upvote = create(:post_vote, user: @user, score: 1)
@downvote = create(:post_vote, user: @user, score: -1)
end
@@ -1411,8 +1411,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
end
should "return the correct favorite count for a fav:<name> search for a user with private favorites" do
fav = create(:favorite)
fav.user.update!(favorite_count: 1, enable_private_favorites: true)
fav = create(:private_favorite)
assert_fast_count(0, "fav:#{fav.user.name}")
assert_fast_count(0, "ordfav:#{fav.user.name}")

View File

@@ -699,10 +699,34 @@ class PostTest < ActiveSupport::TestCase
context "for a fav" do
should "add/remove the current user to the post's favorite listing" do
@post.update(tag_string: "aaa fav:self")
assert_equal(1, @post.reload.score)
assert_equal(1, @post.favorites.where(user: @user).count)
assert_equal(1, @post.votes.positive.where(user: @user).count)
@post.update(tag_string: "aaa -fav:self")
assert_equal(0, @post.reload.score)
assert_equal(0, @post.favorites.count)
assert_equal(0, @post.votes.positive.where(user: @user).count)
end
should "not allow banned users to fav" do
assert_raises(User::PrivilegeError) do
as(create(:banned_user)) { @post.update(tag_string: "aaa fav:self") }
end
assert_raises(User::PrivilegeError) do
as(create(:banned_user)) { @post.update(tag_string: "aaa -fav:self") }
end
end
should "not allow restricted users to fav" do
assert_raises(User::PrivilegeError) do
as(create(:restricted_user)) { @post.update(tag_string: "aaa fav:self") }
end
assert_raises(User::PrivilegeError) do
as(create(:restricted_user)) { @post.update(tag_string: "aaa -fav:self") }
end
end
should "not fail when the fav: metatag is used twice" do
@@ -870,20 +894,20 @@ class PostTest < ActiveSupport::TestCase
context "upvote:self or downvote:self" do
context "by a member" do
should "not upvote the post" do
assert_no_difference("PostVote.count") do
should "upvote the post" do
assert_difference("PostVote.count") do
@post.update(tag_string: "upvote:self")
end
assert_equal(0, @post.reload.score)
assert_equal(1, @post.reload.score)
end
should "not downvote the post" do
assert_no_difference("PostVote.count") do
should "downvote the post" do
assert_difference("PostVote.count") do
@post.update(tag_string: "downvote:self")
end
assert_equal(0, @post.reload.score)
assert_equal(-1, @post.reload.score)
end
end
@@ -1476,7 +1500,8 @@ class PostTest < ActiveSupport::TestCase
should "create a vote for each user who can vote" do
assert(@parent.votes.where(user: @gold1).exists?)
assert_equal(1, @parent.score)
assert(@parent.votes.where(user: @user1).exists?)
assert_equal(2, @parent.score)
end
end
end
@@ -1523,13 +1548,13 @@ class PostTest < ActiveSupport::TestCase
end
context "Voting:" do
should "not allow members to vote" do
should "allow members to vote" do
user = create(:user)
post = create(:post)
assert_nothing_raised { post.vote!(1, user) }
assert_equal(0, post.votes.count)
assert_equal(0, post.reload.score)
assert_equal(1, post.votes.count)
assert_equal(1, post.reload.score)
end
should "not allow duplicate votes" do