Fix #4106: Allow moderators to IP ban subnets.

This commit is contained in:
evazion
2019-08-12 02:12:56 -05:00
parent 9729eeb829
commit 7316f41d1d
6 changed files with 62 additions and 38 deletions

View File

@@ -1,7 +1,7 @@
FactoryBot.define do
factory(:ip_ban) do
creator :factory => :user
reason {FFaker::Lorem.words.join(" ")}
ip_addr {"127.0.0.2"}
creator
reason { FFaker::Lorem.words.join(" ") }
ip_addr { FFaker::Internet.ip_v4_address }
end
end

View File

@@ -2,10 +2,8 @@ require 'test_helper'
class IpBanTest < ActiveSupport::TestCase
setup do
@user = FactoryBot.create(:user)
CurrentUser.user = FactoryBot.create(:mod_user)
CurrentUser.ip_addr = "127.0.0.1"
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now)
end
teardown do
@@ -13,24 +11,34 @@ class IpBanTest < ActiveSupport::TestCase
CurrentUser.ip_addr = nil
end
should "be able to count the number of comments an IP address is associated with" do
comment = FactoryBot.create(:comment)
counts = IpBan.count_by_ip_addr("comments", [comment.creator_id], "creator_id", "creator_ip_addr")
assert_equal([{"creator_ip_addr" => "127.0.0.1", "count" => 1}], counts)
end
should "be able to count any updates from a user, groupiny by IP address" do
CurrentUser.scoped(@user, "1.2.3.4") do
comment = FactoryBot.create(:comment, :body => "aaa")
counts = IpBan.query([comment.creator_id])
assert_equal([{"creator_ip_addr" => "1.2.3.4", "count" => 1}], counts["comments"])
end
end
should "be able to ban a user" do
ip_ban = IpBan.create(ip_addr: "1.2.3.4", reason: "test")
ip_ban = create(:ip_ban, ip_addr: "1.2.3.4")
assert_equal("1.2.3.4", ip_ban.ip_addr.to_s)
assert_equal("1.2.3.4", ip_ban.subnetted_ip)
assert(IpBan.is_banned?("1.2.3.4"))
end
should "be able to ban a subnet" do
ip_ban = create(:ip_ban, ip_addr: "1.2.3.4/24")
assert_equal("1.2.3.0/24", ip_ban.subnetted_ip)
assert(IpBan.is_banned?("1.2.3.0"))
assert(IpBan.is_banned?("1.2.3.255"))
end
context "validation" do
subject { build(:ip_ban) }
should allow_value("1.2.3.4").for(:ip_addr)
should allow_value("1.2.3.4/24").for(:ip_addr)
should allow_value("ABCD::1234").for(:ip_addr)
should allow_value("ABCD::1234/64").for(:ip_addr)
should_not allow_value("").for(:ip_addr)
should_not allow_value("foo").for(:ip_addr)
should_not allow_value("10.0.0.1").for(:ip_addr)
should_not allow_value("127.0.0.1").for(:ip_addr)
should_not allow_value("1.2.3.4/16").for(:ip_addr)
should_not allow_value("ABCD::1234/32").for(:ip_addr)
end
end