Skip to content

Commit

Permalink
Add new cases to is_valid* (#1659)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ authored Aug 25, 2024
1 parent 950ea05 commit c588ea0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions hahomematic/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,10 @@ def get_ip_addr(host: str, port: int) -> str | None:
return local_ip


def is_valid_hostname(hostname: str) -> bool:
def is_valid_hostname(hostname: str | None) -> bool:
"""Return True if hostname is valid."""
if not hostname:
return False
if hostname[-1] == ".":
# strip exactly one dot from the right, if present
hostname = hostname[:-1]
Expand All @@ -265,8 +267,10 @@ def is_valid_hostname(hostname: str) -> bool:
return all(allowed.match(label) for label in labels)


def is_valid_ipv4_address(address: str) -> bool:
def is_valid_ipv4_address(address: str | None) -> bool:
"""Return True if ipv4_address is valid."""
if not address:
return False
try:
IPv4Address(address=address)
except ValueError:
Expand Down
3 changes: 3 additions & 0 deletions tests/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ def test_converter(

def test_is_valid_hostname() -> None:
"""Test is_valid_hostname."""
assert is_valid_hostname(None) is False
assert is_valid_hostname("") is False
assert is_valid_hostname(" ") is False
assert is_valid_hostname("123") is False
assert is_valid_hostname("ccu") is True
Expand All @@ -562,6 +564,7 @@ def test_is_valid_hostname() -> None:

def test_is_valid_ipv4_address() -> None:
"""Test is_valid_ipv4_address."""
assert is_valid_ipv4_address(None) is False
assert is_valid_ipv4_address("") is False
assert is_valid_ipv4_address(" ") is False
assert is_valid_ipv4_address("192.168.1782") is False
Expand Down

0 comments on commit c588ea0

Please sign in to comment.