2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
2011-03-28 18:48:02 -04:00
|
|
|
|
|
|
|
class PostFlagTest < ActiveSupport::TestCase
|
2011-03-30 14:13:42 -04:00
|
|
|
context "In all cases" do
|
|
|
|
setup do
|
2018-06-15 20:11:06 -04:00
|
|
|
travel_to(2.weeks.ago) do
|
2022-02-19 11:34:14 -05:00
|
|
|
@bob = create(:user)
|
2019-06-29 11:45:58 -04:00
|
|
|
@alice = create(:privileged_user)
|
2018-06-15 20:11:06 -04:00
|
|
|
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
|
2018-06-15 20:11:06 -04:00
|
|
|
as(@bob) do
|
2022-02-19 11:34:14 -05:00
|
|
|
@post_flag = create(:post_flag, post: @post)
|
2018-06-15 20:11:06 -04:00
|
|
|
end
|
|
|
|
end
|
2022-02-19 11:34:14 -05:00
|
|
|
assert_match(/Post is deleted/, error.message)
|
|
|
|
end
|
2018-06-15 20:11:06 -04:00
|
|
|
|
2022-08-20 12:19:08 -04:00
|
|
|
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
|
2023-01-27 16:25:48 -05:00
|
|
|
assert_match(/not pending/, error.message)
|
2022-08-20 12:19:08 -04:00
|
|
|
|
|
|
|
@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)
|
2018-06-15 20:11:06 -04:00
|
|
|
|
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!
|
2017-05-19 15:37:38 -04:00
|
|
|
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)
|
2018-06-15 20:11:06 -04:00
|
|
|
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
|
2017-11-07 23:02:03 -05:00
|
|
|
|
2018-12-11 19:41:20 -05:00
|
|
|
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)
|
2018-12-11 19:41:20 -05:00
|
|
|
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) }
|
2018-12-11 19:41:20 -05:00
|
|
|
end
|
2022-02-19 11:34:14 -05:00
|
|
|
assert_match(/You cannot flag posts/, error.message)
|
2018-12-11 19:41:20 -05:00
|
|
|
end
|
|
|
|
end
|
2011-03-28 18:48:02 -04:00
|
|
|
end
|
|
|
|
end
|