2024-02-25 12:15:55 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "test_helper"
|
2010-03-18 17:55:57 -04:00
|
|
|
|
|
|
|
class IpBanTest < ActiveSupport::TestCase
|
2010-08-26 14:36:02 -04:00
|
|
|
setup do
|
2022-11-25 15:06:54 -05:00
|
|
|
CurrentUser.user = create(:mod_user)
|
2010-08-26 14:36:02 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-08-12 03:12:56 -04:00
|
|
|
should "be able to ban a user" do
|
|
|
|
ip_ban = create(:ip_ban, ip_addr: "1.2.3.4")
|
|
|
|
|
|
|
|
assert_equal("1.2.3.4", ip_ban.subnetted_ip)
|
|
|
|
assert(IpBan.is_banned?("1.2.3.4"))
|
2010-04-29 17:32:15 -04:00
|
|
|
end
|
2013-03-19 08:10:10 -04:00
|
|
|
|
2019-08-12 03:12:56 -04:00
|
|
|
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"))
|
2010-04-29 17:32:15 -04:00
|
|
|
end
|
2019-01-09 16:15:57 -05:00
|
|
|
|
2019-08-12 03:12:56 -04:00
|
|
|
context "validation" do
|
2022-11-25 15:06:54 -05:00
|
|
|
subject { build(:ip_ban) }
|
2019-01-09 16:15:57 -05:00
|
|
|
|
2019-08-12 03:12:56 -04:00
|
|
|
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)
|
2019-01-09 16:15:57 -05:00
|
|
|
end
|
2010-03-18 17:55:57 -04:00
|
|
|
end
|