2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe ScreenedIpAddress do
|
2013-10-21 14:49:51 -04:00
|
|
|
let(:ip_address) { "99.232.23.124" }
|
|
|
|
let(:valid_params) { { ip_address: ip_address } }
|
|
|
|
|
|
|
|
describe "new record" do
|
|
|
|
it "sets a default action_type" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.create(valid_params).action_type).to eq(
|
|
|
|
described_class.actions[:block],
|
|
|
|
)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
2013-10-22 16:30:30 -04:00
|
|
|
|
|
|
|
it "sets an error when ip_address is invalid" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(
|
|
|
|
described_class.create(valid_params.merge(ip_address: "99.99.99")).errors[:ip_address],
|
|
|
|
).to be_present
|
2013-10-22 16:30:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can set action_type using the action_name virtual attribute" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.new(valid_params.merge(action_name: :do_nothing)).action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
|
|
|
expect(described_class.new(valid_params.merge(action_name: :block)).action_type).to eq(
|
|
|
|
described_class.actions[:block],
|
|
|
|
)
|
|
|
|
expect(described_class.new(valid_params.merge(action_name: "do_nothing")).action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
|
|
|
expect(described_class.new(valid_params.merge(action_name: "block")).action_type).to eq(
|
|
|
|
described_class.actions[:block],
|
|
|
|
)
|
2013-10-22 16:30:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a useful exception when action is invalid" do
|
|
|
|
expect { described_class.new(valid_params.merge(action_name: "dance")) }.to raise_error(
|
|
|
|
ArgumentError,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises a useful exception when action is nil" do
|
|
|
|
expect { described_class.new(valid_params.merge(action_name: nil)) }.to raise_error(
|
|
|
|
ArgumentError,
|
|
|
|
)
|
|
|
|
end
|
2018-03-19 14:34:43 -04:00
|
|
|
|
|
|
|
it "returns a useful error if ip address matches an existing record" do
|
|
|
|
ScreenedIpAddress.create(ip_address: "2600:387:b:f::7a/128", action_name: :block)
|
|
|
|
r = ScreenedIpAddress.new(ip_address: "2600:387:b:f::7a", action_name: :block)
|
|
|
|
expect(r.save).to eq(false)
|
|
|
|
expect(r.errors[:ip_address]).to be_present
|
|
|
|
end
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
2014-02-18 10:33:08 -05:00
|
|
|
describe "ip_address_with_mask" do
|
|
|
|
it "returns nil when ip_address is nil" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.new.ip_address_with_mask).to eq(nil)
|
2014-02-18 10:33:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns ip_address without mask if there is no mask" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.new(ip_address: "123.123.23.22").ip_address_with_mask).to eq(
|
|
|
|
"123.123.23.22",
|
|
|
|
)
|
2014-02-18 10:33:08 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns ip_address with mask" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.new(ip_address: "123.12.0.0/16").ip_address_with_mask).to eq(
|
|
|
|
"123.12.0.0/16",
|
|
|
|
)
|
2014-02-18 10:33:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-02-18 13:00:46 -05:00
|
|
|
describe "ip_address=" do
|
|
|
|
let(:record) { described_class.new }
|
|
|
|
|
|
|
|
def test_good_value(arg, expected)
|
|
|
|
record.ip_address = arg
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(record.ip_address_with_mask).to eq(expected)
|
2014-02-18 13:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bad_value(arg)
|
|
|
|
r = described_class.new
|
|
|
|
r.ip_address = arg
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(r).not_to be_valid
|
|
|
|
expect(r.errors[:ip_address]).to be_present
|
2014-02-18 13:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles valid ip addresses" do
|
|
|
|
test_good_value("210.56.12.12", "210.56.12.12")
|
|
|
|
test_good_value("210.56.0.0/16", "210.56.0.0/16")
|
|
|
|
test_good_value("fc00::/7", "fc00::/7")
|
2014-02-21 14:14:30 -05:00
|
|
|
test_good_value(IPAddr.new("94.99.101.228"), "94.99.101.228")
|
|
|
|
test_good_value(IPAddr.new("94.99.0.0/16"), "94.99.0.0/16")
|
|
|
|
test_good_value(IPAddr.new("fc00::/7"), "fc00::/7")
|
2014-02-18 13:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "translates * characters" do
|
|
|
|
test_good_value("123.*.*.*", "123.0.0.0/8")
|
|
|
|
test_good_value("123.12.*.*", "123.12.0.0/16")
|
|
|
|
test_good_value("123.12.1.*", "123.12.1.0/24")
|
|
|
|
test_good_value("123.12.*.*/16", "123.12.0.0/16")
|
2014-09-24 12:05:29 -04:00
|
|
|
test_good_value("123.12.*", "123.12.0.0/16")
|
|
|
|
test_good_value("123.*", "123.0.0.0/8")
|
2014-02-18 13:00:46 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "handles bad input" do
|
|
|
|
test_bad_value(nil)
|
|
|
|
test_bad_value("123.123")
|
|
|
|
test_bad_value("my house")
|
|
|
|
test_bad_value("123.*.1.12")
|
|
|
|
test_bad_value("*.123.*.12")
|
|
|
|
test_bad_value("*.*.*.12")
|
|
|
|
test_bad_value("123.*.1.12/8")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-21 14:49:51 -04:00
|
|
|
describe "#watch" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when ip_address is not being watched" do
|
2013-10-21 14:49:51 -04:00
|
|
|
it "should create a new record" do
|
|
|
|
record = described_class.watch(ip_address)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(record).not_to be_new_record
|
|
|
|
expect(record.action_type).to eq(described_class.actions[:block])
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "lets action_type be overridden" do
|
|
|
|
record =
|
|
|
|
described_class.watch(ip_address, action_type: described_class.actions[:do_nothing])
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(record).not_to be_new_record
|
|
|
|
expect(record.action_type).to eq(described_class.actions[:do_nothing])
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "a record with subnet mask exists, but doesn't match" do
|
|
|
|
existing = Fabricate(:screened_ip_address, ip_address: "99.232.23.124/24")
|
|
|
|
expect { described_class.watch("99.232.55.124") }.to change { described_class.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "a record with exact matching exists, but doesn't match" do
|
|
|
|
existing = Fabricate(:screened_ip_address, ip_address: "99.232.23.124")
|
|
|
|
expect { described_class.watch("99.232.23.123") }.to change { described_class.count }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when ip_address is already being watched" do
|
2013-10-21 14:49:51 -04:00
|
|
|
shared_examples "exact match of ip address" do
|
|
|
|
it "should not create a new record" do
|
|
|
|
expect { described_class.watch(ip_address_arg) }.to_not change { described_class.count }
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the existing record" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.watch(ip_address_arg)).to eq(existing)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when using exact match" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:existing) { Fabricate(:screened_ip_address) }
|
2013-10-21 14:49:51 -04:00
|
|
|
let(:ip_address_arg) { existing.ip_address }
|
|
|
|
include_examples "exact match of ip address"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when using subnet mask 255.255.255.0" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:existing) { Fabricate(:screened_ip_address, ip_address: "99.232.23.124/24") }
|
2013-10-21 14:49:51 -04:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with exact address" do
|
2013-10-21 14:49:51 -04:00
|
|
|
let(:ip_address_arg) { "99.232.23.124" }
|
|
|
|
include_examples "exact match of ip address"
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with address in same subnet" do
|
2013-10-21 14:49:51 -04:00
|
|
|
let(:ip_address_arg) { "99.232.23.135" }
|
|
|
|
include_examples "exact match of ip address"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't block 10.0.0.0/8" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.watch("10.0.0.0").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
|
|
|
expect(described_class.watch("10.0.0.1").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
|
|
|
expect(described_class.watch("10.10.125.111").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't block 192.168.0.0/16" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.watch("192.168.0.5").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
|
|
|
expect(described_class.watch("192.168.10.1").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't block 127.0.0.0/8" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.watch("127.0.0.1").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't block fc00::/7 addresses (IPv6)" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.watch("fd12:db8::ff00:42:8329").action_type).to eq(
|
|
|
|
described_class.actions[:do_nothing],
|
|
|
|
)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#should_block?" do
|
|
|
|
it "returns false when record does not exist" do
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.should_block?(ip_address)).to eq(false)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when no record matches" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:block],
|
|
|
|
)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.should_block?("222.12.12.12")).to eq(false)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(0)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
2022-01-11 02:16:51 -05:00
|
|
|
it "returns false if a more specific recrord matches and action is :do_nothing" do
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.0/24",
|
|
|
|
action_type: described_class.actions[:block],
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2022-01-11 02:16:51 -05:00
|
|
|
expect(described_class.should_block?("111.234.23.11")).to eq(false)
|
|
|
|
expect(described_class.should_block?("111.234.23.12")).to eq(true)
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2022-01-11 02:16:51 -05:00
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "222.234.23.0/24",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "222.234.23.11",
|
|
|
|
action_type: described_class.actions[:block],
|
2023-01-09 06:18:21 -05:00
|
|
|
)
|
2022-01-11 02:16:51 -05:00
|
|
|
expect(described_class.should_block?("222.234.23.11")).to eq(true)
|
|
|
|
expect(described_class.should_block?("222.234.23.12")).to eq(false)
|
|
|
|
end
|
2023-01-09 06:18:21 -05:00
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with IPv4" do
|
2013-10-21 14:49:51 -04:00
|
|
|
it "returns false when when record matches and action is :do_nothing" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
|
|
|
)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.should_block?("111.234.23.11")).to eq(false)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(0)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when when record matches and action is :block" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:block],
|
|
|
|
)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.should_block?("111.234.23.11")).to eq(true)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(1)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with IPv6" do
|
2013-10-21 14:49:51 -04:00
|
|
|
it "returns false when when record matches and action is :do_nothing" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "2001:db8::ff00:42:8329",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
|
|
|
)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.should_block?("2001:db8::ff00:42:8329")).to eq(false)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(0)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when when record matches and action is :block" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "2001:db8::ff00:42:8329",
|
|
|
|
action_type: described_class.actions[:block],
|
|
|
|
)
|
2015-01-05 11:04:23 -05:00
|
|
|
expect(described_class.should_block?("2001:db8::ff00:42:8329")).to eq(true)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(1)
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2013-10-23 17:11:11 -04:00
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
describe "#is_allowed?" do
|
2013-10-23 17:11:11 -04:00
|
|
|
it "returns false when record does not exist" do
|
2020-07-26 20:23:54 -04:00
|
|
|
expect(described_class.is_allowed?(ip_address)).to eq(false)
|
2013-10-23 17:11:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when no record matches" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
|
|
|
)
|
2020-07-26 20:23:54 -04:00
|
|
|
expect(described_class.is_allowed?("222.12.12.12")).to eq(false)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(0)
|
2013-10-23 17:11:11 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with IPv4" do
|
2013-10-23 17:11:11 -04:00
|
|
|
it "returns true when when record matches and action is :do_nothing" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
|
|
|
)
|
2020-07-26 20:23:54 -04:00
|
|
|
expect(described_class.is_allowed?("111.234.23.11")).to eq(true)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(1)
|
2013-10-23 17:11:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when when record matches and action is :block" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "111.234.23.11",
|
|
|
|
action_type: described_class.actions[:block],
|
|
|
|
)
|
2020-07-26 20:23:54 -04:00
|
|
|
expect(described_class.is_allowed?("111.234.23.11")).to eq(false)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(0)
|
2013-10-23 17:11:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "with IPv6" do
|
2013-10-23 17:11:11 -04:00
|
|
|
it "returns true when when record matches and action is :do_nothing" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "2001:db8::ff00:42:8329",
|
|
|
|
action_type: described_class.actions[:do_nothing],
|
|
|
|
)
|
2020-07-26 20:23:54 -04:00
|
|
|
expect(described_class.is_allowed?("2001:db8::ff00:42:8329")).to eq(true)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(1)
|
2013-10-23 17:11:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false when when record matches and action is :block" do
|
2022-12-06 06:09:38 -05:00
|
|
|
screened_ip_address =
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: "2001:db8::ff00:42:8329",
|
|
|
|
action_type: described_class.actions[:block],
|
|
|
|
)
|
2020-07-26 20:23:54 -04:00
|
|
|
expect(described_class.is_allowed?("2001:db8::ff00:42:8329")).to eq(false)
|
2022-12-06 06:09:38 -05:00
|
|
|
expect(screened_ip_address.reload.match_count).to eq(0)
|
2013-10-23 17:11:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-09-04 18:50:27 -04:00
|
|
|
|
2015-03-02 12:13:10 -05:00
|
|
|
describe "#block_admin_login?" do
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when no allow_admin records exist" do
|
2020-07-26 20:23:54 -04:00
|
|
|
it "returns false when use_admin_ip_allowlist is false" do
|
2015-03-02 12:13:10 -05:00
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:user), "123.12.12.12")).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when use_admin_ip_allowlist is true" do
|
2020-07-26 20:23:54 -04:00
|
|
|
before { SiteSetting.use_admin_ip_allowlist = true }
|
2014-09-04 18:50:27 -04:00
|
|
|
|
2015-09-21 16:56:25 -04:00
|
|
|
it "returns false when user is nil" do
|
|
|
|
expect(described_class.block_admin_login?(nil, "123.12.12.12")).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for non-admin user" do
|
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:user), "123.12.12.12")).to eq(
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for admin user" do
|
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:admin), "123.12.12.12")).to eq(
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for admin user and ip_address arg is nil" do
|
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:admin), nil)).to eq(false)
|
|
|
|
end
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when allow_admin record exists" do
|
2014-09-04 18:50:27 -04:00
|
|
|
before do
|
|
|
|
@permitted_ip_address = "111.234.23.11"
|
|
|
|
Fabricate(
|
|
|
|
:screened_ip_address,
|
|
|
|
ip_address: @permitted_ip_address,
|
|
|
|
action_type: described_class.actions[:allow_admin],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2020-07-26 20:23:54 -04:00
|
|
|
it "returns false when use_admin_ip_allowlist is false" do
|
2015-09-21 16:56:25 -04:00
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:admin), "123.12.12.12")).to eq(
|
|
|
|
false,
|
|
|
|
)
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
|
2022-07-27 12:14:14 -04:00
|
|
|
context "when use_admin_ip_allowlist is true" do
|
2020-07-26 20:23:54 -04:00
|
|
|
before { SiteSetting.use_admin_ip_allowlist = true }
|
2014-09-04 18:50:27 -04:00
|
|
|
|
2015-09-21 16:56:25 -04:00
|
|
|
it "returns false when user is nil" do
|
|
|
|
expect(described_class.block_admin_login?(nil, @permitted_ip_address)).to eq(false)
|
|
|
|
end
|
2014-09-04 18:50:27 -04:00
|
|
|
|
2015-09-21 16:56:25 -04:00
|
|
|
it "returns false for an admin user at the allowed ip address" do
|
|
|
|
expect(
|
|
|
|
described_class.block_admin_login?(Fabricate.build(:admin), @permitted_ip_address),
|
|
|
|
).to eq(false)
|
|
|
|
end
|
2014-09-04 18:50:27 -04:00
|
|
|
|
2015-09-21 16:56:25 -04:00
|
|
|
it "returns true for an admin user at another ip address" do
|
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:admin), "123.12.12.12")).to eq(
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for regular user at allowed ip address" do
|
|
|
|
expect(
|
|
|
|
described_class.block_admin_login?(Fabricate.build(:user), @permitted_ip_address),
|
|
|
|
).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns false for regular user at another ip address" do
|
|
|
|
expect(described_class.block_admin_login?(Fabricate.build(:user), "123.12.12.12")).to eq(
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
end
|
2014-09-04 18:50:27 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2022-04-12 14:07:37 -04:00
|
|
|
|
|
|
|
describe "#roll_up" do
|
|
|
|
it "rolls up IPv4 addresses" do
|
|
|
|
SiteSetting.min_ban_entries_for_roll_up = 3
|
|
|
|
|
|
|
|
# this should not be touched
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "1.1.1.254/31")
|
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "1.1.1.1")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "1.1.1.2")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "1.1.1.3")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.to change { ScreenedIpAddress.count }.by(-2)
|
|
|
|
expect(ScreenedIpAddress.pluck(:ip_address)).to include("1.1.1.0/24", "1.1.1.254/31")
|
|
|
|
expect(ScreenedIpAddress.pluck(:ip_address)).not_to include("1.1.1.1", "1.1.1.2", "1.1.1.3")
|
|
|
|
|
|
|
|
# expect roll up to be stable
|
2022-07-19 10:03:03 -04:00
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
2022-04-12 14:07:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "rolls up IPv6 addresses" do
|
|
|
|
SiteSetting.min_ban_entries_for_roll_up = 3
|
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "2001:db8:3333:4441:5555:6666:7777:8888")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "2001:db8:3333:4441:5555:6666:7777:8889")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "2001:db8:3333:4441:5555:6666:7777:888a/96")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.to change { ScreenedIpAddress.count }.by(-2)
|
|
|
|
expect(ScreenedIpAddress.pluck(:ip_address)).to include("2001:db8:3333:4441::/64")
|
|
|
|
expect(ScreenedIpAddress.pluck(:ip_address)).not_to include(
|
|
|
|
"2001:db8:3333:4441:5555:6666:7777:8888",
|
|
|
|
"2001:db8:3333:4441:5555:6666:7777:8889",
|
|
|
|
"2001:db8:3333:4441:5555:6666:7777:888a",
|
|
|
|
)
|
|
|
|
|
|
|
|
# expect roll up to be stable
|
2022-07-19 10:03:03 -04:00
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
2022-04-12 14:07:37 -04:00
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "2001:db8:3333:4442::/64")
|
2022-07-19 10:03:03 -04:00
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
2022-04-12 14:07:37 -04:00
|
|
|
|
|
|
|
Fabricate(:screened_ip_address, ip_address: "2001:db8:3333:4443::/64")
|
|
|
|
expect { ScreenedIpAddress.roll_up }.to change { ScreenedIpAddress.count }.by(-2)
|
|
|
|
expect(ScreenedIpAddress.pluck(:ip_address)).to include("2001:db8:3333:4440::/60")
|
|
|
|
expect(ScreenedIpAddress.pluck(:ip_address)).not_to include(
|
|
|
|
"2001:db8:3333:4441::/64",
|
|
|
|
"2001:db8:3333:4442::/64",
|
|
|
|
"2001:db8:3333:4443::/64",
|
|
|
|
)
|
|
|
|
|
|
|
|
# expect roll up to be stable
|
2022-07-19 10:03:03 -04:00
|
|
|
expect { ScreenedIpAddress.roll_up }.not_to change { ScreenedIpAddress.count }
|
2022-04-12 14:07:37 -04:00
|
|
|
end
|
|
|
|
end
|
2013-10-21 14:49:51 -04:00
|
|
|
end
|