eBooru/test/unit/post_flag_test.rb

108 lines
3.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "test_helper"
class PostFlagTest < ActiveSupport::TestCase
2011-03-30 14:13:42 -04:00
context "In all cases" do
setup do
travel_to(2.weeks.ago) do
2022-02-19 11:34:14 -05:00
@bob = create(:user)
@alice = create(:privileged_user)
end
as(@alice) do
@post = create(:post, tag_string: "aaa", uploader: @alice)
2013-08-02 19:55:30 -04:00
end
2011-03-30 14:13:42 -04:00
end
2013-03-19 08:10:10 -04:00
2022-02-19 11:34:14 -05:00
should "respect the throttle limit" do
as(@bob) do
Danbooru.config.stubs(:disable_throttles?).returns(false)
Danbooru.config.stubs(:post_flag_limit).returns(0)
2013-03-19 08:10:10 -04:00
2022-02-19 11:34:14 -05:00
error = assert_raises(ActiveRecord::RecordInvalid) do
@post_flag = create(:post_flag, post: @post)
2013-08-02 19:55:30 -04:00
end
2022-02-19 11:34:14 -05:00
assert_match(/You have reached the hourly limit for this action/, error.message)
2013-08-02 19:55:30 -04:00
end
end
2022-02-19 11:34:14 -05:00
should "not be able to flag a deleted post" do
as(@alice) do
@post.update(is_deleted: true)
2011-03-30 14:13:42 -04:00
end
2013-03-19 08:10:10 -04:00
2022-02-19 11:34:14 -05:00
error = assert_raises(ActiveRecord::RecordInvalid) do
as(@bob) do
2022-02-19 11:34:14 -05:00
@post_flag = create(:post_flag, post: @post)
end
end
2022-02-19 11:34:14 -05:00
assert_match(/Post is deleted/, error.message)
end
should "not be able to flag a non-pending post with the uploading_guidelines reason" do
error = assert_raises(ActiveRecord::RecordInvalid) do
as(@bob) do
@post_flag = create(:post_flag, post: @post, reason_name: "uploading_guidelines")
end
end
assert_match(/not pending/, error.message)
@post = create(:post, is_pending: true)
as(@bob) do
@post_flag = create(:post_flag, post: @post, reason_name: "uploading_guidelines")
end
end
2022-02-19 11:34:14 -05:00
should "not be able to flag a post in the cooldown period" do
@mod = create(:moderator_user)
2022-11-26 07:52:37 -05:00
@users = create_list(:user, 2, created_at: 2.weeks.ago)
2013-03-19 08:10:10 -04:00
2022-02-19 11:34:14 -05:00
as(@users.first) do
@flag1 = create(:post_flag, post: @post)
2011-03-30 14:13:42 -04:00
end
2013-03-19 08:10:10 -04:00
2022-02-19 11:34:14 -05:00
as(@mod) do
@post.approve!
end
2022-02-19 11:34:14 -05:00
travel_to(PostFlag::COOLDOWN_PERIOD.from_now - 1.minute) do
as(@users.second) do
error = assert_raises(ActiveRecord::RecordInvalid) do
@flag2 = create(:post_flag, post: @post)
end
2022-02-19 11:34:14 -05:00
assert_match(/cannot be flagged more than once/, error.message)
2017-05-04 17:39:47 -04:00
end
2022-02-19 11:34:14 -05:00
end
2017-05-04 17:39:47 -04:00
2022-02-19 11:34:14 -05:00
travel_to(PostFlag::COOLDOWN_PERIOD.from_now + 1.minute) do
as(@users.second) do
@flag3 = create(:post_flag, post: @post)
2017-05-04 17:39:47 -04:00
end
2022-02-19 11:34:14 -05:00
assert(@flag3.errors.empty?)
2017-05-04 17:39:47 -04:00
end
2022-02-19 11:34:14 -05:00
end
2017-05-04 17:39:47 -04:00
2022-02-19 11:34:14 -05:00
should "initialize its creator" do
@post_flag = as(@alice) do
create(:post_flag, post: @post)
2011-03-30 14:13:42 -04:00
end
2022-02-19 11:34:14 -05:00
assert_equal(@alice.id, @post_flag.creator_id)
assert_equal(IPAddr.new("127.0.0.1"), @post_flag.creator_ip_addr)
2011-03-30 14:13:42 -04:00
end
context "a user with no_flag=true" do
setup do
2022-11-26 07:52:37 -05:00
@bob = create(:user, no_flagging: true, created_at: 2.weeks.ago)
end
2022-02-19 11:34:14 -05:00
should "not be able to flag" do
error = assert_raises(ActiveRecord::RecordInvalid) do
as(@bob) { create(:post_flag, post: @post) }
end
2022-02-19 11:34:14 -05:00
assert_match(/You cannot flag posts/, error.message)
end
end
end
end