Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ip_address): properly handle private is false #374

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/validators/ip_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def _check_private_ip(value: str, is_private: Optional[bool]):
if is_private is None:
return True
if is_private and (
if (
any(
value.startswith(l_bit)
for l_bit in {
Expand All @@ -33,8 +33,9 @@ def _check_private_ip(value: str, is_private: Optional[bool]):
or re.match(r"^172\.(?:1[6-9]|2\d|3[0-1])\.", value) # private
or re.match(r"^(?:22[4-9]|23[0-9]|24[0-9]|25[0-5])\.", value) # broadcast
):
return True
return False
return bool(is_private)
else:
grleblanc marked this conversation as resolved.
Show resolved Hide resolved
return not bool(is_private)
yozachar marked this conversation as resolved.
Show resolved Hide resolved


@validator
Expand Down
56 changes: 56 additions & 0 deletions tests/test_ip_address.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,59 @@ def test_returns_failed_validation_on_invalid_ipv6_cidr_address(
):
"""Test returns failed validation on invalid ipv6 CIDR address."""
assert isinstance(ipv6(address, cidr=cidr, strict=strict, host_bit=host_bit), ValidationError)


@pytest.mark.parametrize(
("address", "private"),
[
("10.1.1.1", False),
("192.168.1.1", False),
("169.254.1.1", False),
("127.0.0.1", False),
("0.0.0.0", False),
],
)
def test_returns_failed_validation_on_private_ipv4_address(address: str, private: bool):
"""Test returns failed validation on invalid ipv4 CIDR address."""
assert isinstance(ipv4(address, private=private), ValidationError)
yozachar marked this conversation as resolved.
Show resolved Hide resolved


@pytest.mark.parametrize(
("address", "private"),
[
("10.1.1.1", True),
("192.168.1.1", True),
("169.254.1.1", True),
("127.0.0.1", True),
("0.0.0.0", True),
],
)
def test_returns_valid_on_private_ipv4_address(address: str, private: bool):
grleblanc marked this conversation as resolved.
Show resolved Hide resolved
"""Test returns failed validation on invalid ipv4 CIDR address."""
assert ipv4(address, private=private)


@pytest.mark.parametrize(
("address", "private"),
[
("1.1.1.1", True),
("192.169.1.1", True),
("7.53.12.1", True),
],
)
def test_returns_failed_validation_on_not_private_ipv4_address(address: str, private: bool):
"""Test returns failed validation on invalid ipv4 CIDR address."""
assert isinstance(ipv4(address, private=private), ValidationError)


@pytest.mark.parametrize(
("address", "private"),
[
("1.1.1.1", False),
("192.169.1.1", False),
("7.53.12.1", False),
],
)
def test_returns_valid_on_private_public_ipv4_address(address: str, private: bool):
"""Test returns failed validation on invalid ipv4 CIDR address."""
assert ipv4(address, private=private)
grleblanc marked this conversation as resolved.
Show resolved Hide resolved