UT: Split up ip range validation tests more

This commit is contained in:
vakarisz 2022-07-28 11:23:56 +03:00
parent 96af1e0135
commit a1d9118433
1 changed files with 51 additions and 33 deletions

View File

@ -4,47 +4,65 @@ from marshmallow import ValidationError
from common.agent_configuration.validators.ip_ranges import validate_ip, validate_subnet_range
def test_validate_ip():
for good_input in ["192.168.56.1", "0.0.0.0"]:
validate_ip(good_input)
for bad_input in ["1.1.1", "257.256.255.255", "1.1.1.1.1"]:
with pytest.raises(ValidationError):
validate_ip(bad_input)
@pytest.mark.parametrize("ip", ["192.168.56.1", "0.0.0.0"])
def test_validate_ip_valid(ip):
validate_ip(ip)
def test_validate_subnet_range__ip():
_test_subent_range(
good_inputs=["192.168.56.1", "0.0.0.0"],
bad_inputs=["1.1.1", "257.256.255.255", "1.1.1.1.1"],
)
@pytest.mark.parametrize("ip", ["1.1.1", "257.256.255.255", "1.1.1.1.1"])
def test_validate_ip_invalid(ip):
with pytest.raises(ValidationError):
validate_ip(ip)
def test_validate_subnet_range__ip_range():
_test_subent_range(
good_inputs=["1.1.1.1 - 2.2.2.2", "1.1.1.255-1.1.1.1"],
bad_inputs=["1.1.1-2.2.2.2", "0-.1.1.1-2.2.2.2", "a..1.1.1-2.2.2.2", "257.1.1.1-2.2.2.2"],
)
@pytest.mark.parametrize("ip", ["192.168.56.1", "0.0.0.0"])
def test_validate_subnet_range__ip_valid(ip):
validate_subnet_range(ip)
def test_validate_subnet_range__hostname():
_test_subent_range(
good_inputs=["infection.monkey", "1nfection-Monkey"],
bad_inputs=["hy&!he.host", "čili-peppers.are-hot"],
)
@pytest.mark.parametrize("ip", ["1.1.1", "257.256.255.255", "1.1.1.1.1"])
def test_validate_subnet_range__ip_invalid(ip):
with pytest.raises(ValidationError):
validate_subnet_range(ip)
def test_validate_subnet_range__cidr():
_test_subent_range(
good_inputs=["1.1.1.1/24", "1.1.1.1/0"],
bad_inputs=["1.1.1/24", "1.1.1.1/-1", "1.1.1.1/33", "1.1.1.1/222"],
)
@pytest.mark.parametrize("ip_range", ["1.1.1.1 - 2.2.2.2", "1.1.1.255-1.1.1.1"])
def test_validate_subnet_range__ip_range_valid(ip_range):
validate_subnet_range(ip_range)
def _test_subent_range(good_inputs, bad_inputs):
for good_input in good_inputs:
validate_subnet_range(good_input)
@pytest.mark.parametrize(
"ip_range",
[
"1.1.1-2.2.2.2",
"0-.1.1.1-2.2.2.2",
"a..1.1.1-2.2.2.2",
"257.1.1.1-2.2.2.2",
"1.1.1.1-2.2.2.2-3.3.3.3",
],
)
def test_validate_subnet_range__ip_range_invalid(ip_range):
with pytest.raises(ValidationError):
validate_subnet_range(ip_range)
for bad_input in bad_inputs:
with pytest.raises(ValidationError):
validate_subnet_range(bad_input)
@pytest.mark.parametrize("hostname", ["infection.monkey", "1nfection-Monkey"])
def test_validate_subnet_range__hostname_valid(hostname):
validate_subnet_range(hostname)
@pytest.mark.parametrize("hostname", ["hy&!he.host", "čili-peppers.are-hot"])
def test_validate_subnet_range__hostname_invalid(hostname):
with pytest.raises(ValidationError):
validate_subnet_range(hostname)
@pytest.mark.parametrize("cidr_range", ["1.1.1.1/24", "1.1.1.1/0"])
def test_validate_subnet_range__cidr_valid(cidr_range):
validate_subnet_range(cidr_range)
@pytest.mark.parametrize("cidr_range", ["1.1.1/24", "1.1.1.1/-1", "1.1.1.1/33", "1.1.1.1/222"])
def test_validate_subnet_range__cidr_invalid(cidr_range):
with pytest.raises(ValidationError):
validate_subnet_range(cidr_range)