eBooru/test/unit/forum_post_test.rb

160 lines
4.4 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "test_helper"
2010-02-20 20:25:01 -05:00
class ForumPostTest < ActiveSupport::TestCase
context "A forum post" do
setup do
@user = create(:user)
CurrentUser.user = @user
@topic = create(:forum_topic)
end
2013-03-19 08:10:10 -04:00
context "#votable?" do
setup do
@post = build(:forum_post, topic_id: @topic.id, body: "[[aaa]] -> [[bbb]]")
@tag_alias = create(:tag_alias, forum_post: @post)
end
should "be true for a post associated with a tag alias" do
assert(@post.votable?)
end
end
2013-02-23 11:55:11 -05:00
context "that belongs to a topic with several pages of posts" do
setup do
Danbooru.config.stubs(:records_per_page).returns(3)
2013-02-23 11:55:11 -05:00
@posts = []
2013-05-07 20:20:42 -04:00
9.times do
@posts << create(:forum_post, topic_id: @topic.id)
2013-02-23 11:55:11 -05:00
end
travel_to(2.seconds.from_now) do
@posts << create(:forum_post, topic_id: @topic.id)
end
2013-02-23 11:55:11 -05:00
end
2013-03-19 08:10:10 -04:00
2014-03-19 17:21:53 -04:00
context "that is deleted" do
2015-07-15 17:56:56 -04:00
setup do
CurrentUser.user = create(:moderator_user)
2015-07-15 17:56:56 -04:00
end
2021-05-14 09:04:03 -04:00
2014-03-19 17:21:53 -04:00
should "update the topic's updated_at timestamp" do
@topic.reload
assert_in_delta(@posts[-1].updated_at.to_i, @topic.updated_at.to_i, 1)
2021-05-14 09:04:03 -04:00
@posts[-1].hide!
2014-03-19 17:21:53 -04:00
@topic.reload
assert_in_delta(@posts[-2].updated_at.to_i, @topic.updated_at.to_i, 1)
2014-03-19 17:21:53 -04:00
end
end
2013-02-23 11:55:11 -05:00
should "know which page it's on" do
assert_equal(2, @posts[3].forum_topic_page)
assert_equal(2, @posts[4].forum_topic_page)
2022-09-17 13:32:21 -04:00
assert_equal(3, @posts[5].forum_topic_page)
2013-02-23 11:55:11 -05:00
assert_equal(3, @posts[6].forum_topic_page)
end
2013-05-07 20:20:42 -04:00
2021-05-14 09:04:03 -04:00
should "update the topic's updated_at when destroyed" do
2013-05-07 20:20:42 -04:00
@posts.last.destroy
@topic.reload
2013-07-20 16:30:07 -04:00
assert_equal(@posts[8].updated_at.to_s, @topic.updated_at.to_s)
2013-05-07 20:20:42 -04:00
end
2013-02-23 11:55:11 -05:00
end
2013-03-19 08:10:10 -04:00
context "belonging to a locked topic" do
setup do
@post = create(:forum_post, topic_id: @topic.id, body: "zzz")
@topic.update_attribute(:is_locked, true)
@post.reload
end
2013-03-19 08:10:10 -04:00
should "not be updateable" do
@post.update(:body => "xxx")
@post.reload
assert_equal("zzz", @post.body)
end
2013-03-19 08:10:10 -04:00
should "not be deletable" 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
assert_difference("ForumPost.count", 0) do
@post.destroy
end
end
end
2013-03-19 08:10:10 -04:00
2013-05-31 18:08:57 -04:00
should "update the topic when created" do
@original_topic_updated_at = @topic.updated_at
travel_to(1.second.from_now) do
post = create(:forum_post, topic_id: @topic.id)
end
@topic.reload
2013-05-31 18:08:57 -04:00
assert_not_equal(@original_topic_updated_at.to_s, @topic.updated_at.to_s)
end
should "be searchable by body content" do
post = create(:forum_post, topic_id: @topic.id, body: "xxx")
assert_equal(1, ForumPost.search(body_matches: "xxx").count)
assert_equal(0, ForumPost.search(body_matches: "aaa").count)
end
2013-03-19 08:10:10 -04:00
should "initialize its creator" do
post = create(:forum_post, topic_id: @topic.id)
assert_equal(@user.id, post.creator_id)
end
2013-03-19 08:10:10 -04:00
context "that is edited by a moderator" do
setup do
@post = create(:forum_post, topic_id: @topic.id)
@mod = create(:moderator_user)
CurrentUser.user = @mod
end
2013-03-19 08:10:10 -04:00
should "create a mod action" do
assert_difference(-> { ModAction.count }, 1) do
@post.update(body: "nope")
end
end
should "credit the moderator as the updater" do
@post.update(body: "test")
assert_equal(@mod.id, @post.updater_id)
end
end
context "that is hidden by a moderator" do
setup do
@post = create(:forum_post, topic_id: @topic.id)
@mod = create(:moderator_user)
CurrentUser.user = @mod
end
should "create a mod action" do
assert_difference(-> { ModAction.count }, 1) do
@post.update(is_hidden: true)
end
end
should "credit the moderator as the updater" do
@post.update(is_hidden: true)
assert_equal(@mod.id, @post.updater_id)
end
end
context "that is deleted" do
setup do
@post = create(:forum_post, topic_id: @topic.id)
end
should "create a mod action" do
assert_difference(-> { ModAction.count }, 1) do
@post.destroy
end
end
end
context "during validation" do
subject { build(:forum_post) }
should_not allow_value(" ").for(:body)
2010-02-20 20:25:01 -05:00
end
end
end