eBooru/test/unit/comment_test.rb

304 lines
9.4 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require "test_helper"
2010-02-15 13:59:58 -05:00
class CommentTest < ActiveSupport::TestCase
context "A comment" do
setup do
@user = create(:user)
CurrentUser.user = @user
2010-02-15 13:59:58 -05:00
end
2013-03-19 08:10:10 -04:00
2012-05-11 18:04:50 -04:00
context "created by a limited user" do
setup do
2022-03-28 15:17:34 -04:00
Danbooru.config.stubs(:disable_throttles?).returns(false)
2012-05-11 18:04:50 -04:00
end
2013-03-19 08:10:10 -04:00
2012-05-11 18:04:50 -04:00
should "fail creation" do
comment = build(:comment, post: create(:post))
2012-05-11 18:04:50 -04:00
comment.save
assert_equal(["Creator can not yet perform this action. Account is too new"], comment.errors.full_messages)
2012-05-11 18:04:50 -04:00
end
2010-02-15 13:59:58 -05:00
end
2013-03-19 08:10:10 -04:00
2012-05-11 18:04:50 -04:00
context "created by an unlimited user" do
setup do
Danbooru.config.stubs(:member_comment_limit).returns(100)
end
2013-03-19 08:10:10 -04:00
2013-02-24 17:54:42 -05:00
context "that is then deleted" do
setup do
@post = create(:post)
@comment = create(:comment, post_id: @post.id)
2022-03-28 15:17:34 -04:00
@comment.destroy
2013-02-24 17:54:42 -05:00
@post.reload
end
2013-03-19 08:10:10 -04:00
2013-02-24 17:54:42 -05:00
should "nullify the last_commented_at field" do
assert_nil(@post.last_commented_at)
end
end
2010-02-15 13:59:58 -05:00
2012-05-11 18:04:50 -04:00
should "be created" do
post = create(:post)
comment = build(:comment, post: post)
2012-05-11 18:04:50 -04:00
comment.save
assert(comment.errors.empty?, comment.errors.full_messages.join(", "))
end
should "not validate if the post does not exist" do
comment = build(:comment, post_id: -1)
assert_not(comment.valid?)
2022-03-28 15:17:34 -04:00
assert_match(/must exist/, comment.errors[:post].join(", "))
end
2012-05-11 18:04:50 -04:00
should "not bump the parent post" do
post = create(:post)
comment = create(:comment, do_not_bump_post: true, post: post)
2012-05-11 18:04:50 -04:00
post.reload
assert_nil(post.last_comment_bumped_at)
2012-05-11 18:04:50 -04:00
comment = create(:comment, post: post)
2012-05-11 18:04:50 -04:00
post.reload
assert_not_nil(post.last_comment_bumped_at)
2012-05-11 18:04:50 -04:00
end
should "not bump the post after exceeding the threshold" do
2012-05-11 18:04:50 -04:00
Danbooru.config.stubs(:comment_threshold).returns(1)
p = create(:post)
c1 = create(:comment, post: p)
travel_to(2.seconds.from_now) do
c2 = create(:comment, post: p)
end
2012-05-11 18:04:50 -04:00
p.reload
assert_equal(c1.created_at.to_s, p.last_comment_bumped_at.to_s)
end
should "always record the last_commented_at properly" do
post = create(:post)
Danbooru.config.stubs(:comment_threshold).returns(1)
c1 = create(:comment, do_not_bump_post: true, post: post)
post.reload
assert_equal(c1.created_at.to_s, post.last_commented_at.to_s)
c2 = create(:comment, post: post)
post.reload
assert_equal(c2.created_at.to_s, post.last_commented_at.to_s)
2012-05-11 18:04:50 -04:00
end
should "not record the user id of the voter" do
user = create(:user)
user2 = create(:user)
post = create(:post)
c1 = create(:comment, post: post)
2022-05-16 08:20:11 -04:00
as(user2) do
VoteManager.comment_vote!(user: user2, comment: c1, score: -1)
c1.reload
assert_not_equal(user2.id, c1.updater_id)
end
end
2012-05-11 18:04:50 -04:00
should "not allow duplicate votes" do
user = create(:user)
user2 = create(:user)
post = create(:post)
c1 = create(:comment, post: post)
c2 = create(:comment, post: post)
2022-05-16 08:20:11 -04:00
as(user2) do
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: c1, score: -1) }
assert_equal(:need_unvote, VoteManager.comment_vote!(user: user2, comment: c1, score: -1))
assert_equal(1, CommentVote.count)
assert_equal(-1, CommentVote.last.score)
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: c2, score: -1) }
assert_equal(2, CommentVote.count)
end
2012-05-11 18:04:50 -04:00
end
should "not allow upvotes by the creator" do
user = create(:user)
post = create(:post)
c1 = create(:comment, post: post)
2022-03-28 15:17:34 -04:00
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: 1) }
assert_equal("Validation failed: You cannot vote on your own comments", exception.message)
end
2022-05-16 08:20:11 -04:00
should "not allow downvotes by the creator" do
user = create(:user)
post = create(:post)
c1 = create(:comment, post: post)
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: -1) }
assert_equal("Validation failed: You cannot vote on your own comments", exception.message)
end
should "not allow votes on sticky comments" do
user = create(:user)
post = create(:post)
c1 = create(:comment, post: post, is_sticky: true)
exception = assert_raises(ActiveRecord::RecordInvalid) { VoteManager.comment_vote!(user: user, comment: c1, score: -1) }
assert_match(/You cannot vote on sticky comments/, exception.message)
end
2013-06-29 15:10:09 -04:00
should "allow undoing of votes" do
user = create(:user)
user2 = create(:user)
post = create(:post)
comment = create(:comment, post: post)
as(user2) do
2022-03-28 15:17:34 -04:00
VoteManager.comment_vote!(user: user2, comment: comment, score: 1)
comment.reload
assert_equal(1, comment.score)
VoteManager.comment_unvote!(user: user2, comment: comment)
2013-06-29 15:10:09 -04:00
comment.reload
assert_equal(0, comment.score)
2022-03-28 15:17:34 -04:00
assert_nothing_raised { VoteManager.comment_vote!(user: user2, comment: comment, score: -1) }
2013-06-29 15:10:09 -04:00
end
end
2012-05-11 18:04:50 -04:00
should "be searchable" do
c1 = create(:comment, body: "aaa bbb ccc")
c2 = create(:comment, body: "aaa ddd")
c3 = create(:comment, body: "eee")
2012-05-11 18:04:50 -04:00
matches = Comment.search(body_matches: "aaa")
2012-05-11 18:04:50 -04:00
assert_equal(2, matches.count)
assert_equal(c2.id, matches.all[0].id)
assert_equal(c1.id, matches.all[1].id)
end
2016-12-27 00:37:16 -05:00
should "default to id_desc order when searched with no options specified" do
comms = create_list(:comment, 3)
matches = Comment.search({})
assert_equal([comms[2].id, comms[1].id, comms[0].id], matches.map(&:id))
end
2017-01-03 19:02:36 -05:00
context "that is edited by a moderator" do
setup do
@post = create(:post)
@comment = create(:comment, post_id: @post.id)
@mod = create(:moderator_user)
2017-01-03 19:02:36 -05:00
CurrentUser.user = @mod
end
should "create a mod action" do
assert_difference("ModAction.count") do
@comment.update(body: "nope")
2017-01-03 19:02:36 -05:00
end
end
should "credit the moderator as the updater" do
Raise error on unpermitted params. Fail loudly if we forget to whitelist a param instead of silently ignoring it. misc models: convert to strong params. artist commentaries: convert to strong params. * Disallow changing or setting post_id to a nonexistent post. artists: convert to strong params. * Disallow setting `is_banned` in create/update actions. Changing it this way instead of with the ban/unban actions would leave the artist in a partially banned state. bans: convert to strong params. * Disallow changing the user_id after the ban has been created. comments: convert to strong params. favorite groups: convert to strong params. news updates: convert to strong params. post appeals: convert to strong params. post flags: convert to strong params. * Disallow users from setting the `is_deleted` / `is_resolved` flags. ip bans: convert to strong params. user feedbacks: convert to strong params. * Disallow users from setting `disable_dmail_notification` when creating feedbacks. * Disallow changing the user_id after the feedback has been created. notes: convert to strong params. wiki pages: convert to strong params. * Also fix non-Builders being able to delete wiki pages. saved searches: convert to strong params. pools: convert to strong params. * Disallow setting `post_count` or `is_deleted` in create/update actions. janitor trials: convert to strong params. post disapprovals: convert to strong params. * Factor out quick-mod bar to shared partial. * Fix quick-mod bar to use `Post#is_approvable?` to determine visibility of Approve button. dmail filters: convert to strong params. password resets: convert to strong params. user name change requests: convert to strong params. posts: convert to strong params. users: convert to strong params. * Disallow setting password_hash, last_logged_in_at, last_forum_read_at, has_mail, and dmail_filter_attributes[user_id]. * Remove initialize_default_image_size (dead code). uploads: convert to strong params. * Remove `initialize_status` because status already defaults to pending in the database. tag aliases/implications: convert to strong params. tags: convert to strong params. forum posts: convert to strong params. * Disallow changing the topic_id after creating the post. * Disallow setting is_deleted (destroy/undelete actions should be used instead). * Remove is_sticky / is_locked (nonexistent attributes). forum topics: convert to strong params. * merges https://github.com/evazion/danbooru/tree/wip-rails-5.1 * lock pg gem to 0.21 (1.0.0 is incompatible with rails 5.1.4) * switch to factorybot and change all references Co-authored-by: r888888888 <r888888888@gmail.com> Co-authored-by: evazion <noizave@gmail.com> add diffs
2018-04-02 13:51:26 -04:00
@comment.update(body: "test")
assert_equal(@mod.id, @comment.updater_id)
end
2017-01-03 19:02:36 -05:00
end
context "that is hidden by a moderator" do
setup do
@comment = create(:comment)
@mod = create(:moderator_user)
CurrentUser.user = @mod
end
should "create a mod action" do
assert_difference(-> { ModAction.count }, 1) do
@comment.update(is_hidden: true)
end
end
should "credit the moderator as the updater" do
@comment.update(is_hidden: true)
assert_equal(@mod.id, @comment.updater_id)
end
end
context "that is stickied by a moderator" do
setup do
@comment = create(:comment)
@mod = create(:moderator_user)
CurrentUser.user = @mod
end
should "create a mod action" do
assert_difference(-> { ModAction.count }, 1) do
@comment.update(is_sticky: true)
end
end
should "credit the moderator as the updater" do
@comment.update(is_sticky: true)
assert_equal(@mod.id, @comment.updater_id)
end
end
context "that is deleted" do
setup do
@comment = create(:comment)
end
should "create a mod action" do
assert_difference(-> { ModAction.count }, 1) do
@comment.destroy
end
end
end
2016-12-27 00:37:16 -05:00
context "that is below the score threshold" do
should "be hidden if not stickied" do
user = create(:user, comment_threshold: 0)
post = create(:post)
comment = create(:comment, post: post, score: -5)
2016-12-27 00:37:16 -05:00
assert_equal([comment], post.comments.hidden(user))
assert_equal([], post.comments.visible(user))
end
should "be visible if stickied" do
user = create(:user, comment_threshold: 0)
post = create(:post)
comment = create(:comment, post: post, score: -5, is_sticky: true)
2016-12-27 00:37:16 -05:00
assert_equal([], post.comments.hidden(user))
assert_equal([comment], post.comments.visible(user))
end
end
2022-05-16 08:20:11 -04:00
context "on a comment locked post" do
setup do
@post = create(:post, is_comment_locked: true)
2022-05-16 08:20:11 -04:00
end
should "prevent new comments" do
comment = build(:comment, post: @post)
2022-05-16 08:20:11 -04:00
comment.save
assert_equal(["Post has comments locked"], comment.errors.full_messages)
2022-05-16 08:20:11 -04:00
end
end
context "on a comment disabled post" do
setup do
@post = create(:post, is_comment_disabled: true)
end
should "prevent new comments" do
comment = build(:comment, post: @post)
comment.save
assert_equal(["Post has comments disabled"], comment.errors.full_messages)
end
end
2010-02-15 13:59:58 -05:00
end
context "during validation" do
subject { build(:comment) }
should_not allow_value(" ").for(:body)
end
2010-02-15 13:59:58 -05:00
end
end