diff --git a/modules/hlr_lookup.py b/modules/hlr_lookup.py index ae2b3ab..5b4e562 100644 --- a/modules/hlr_lookup.py +++ b/modules/hlr_lookup.py @@ -7,6 +7,7 @@ sys.path.append('..') from config import Colors, NUMVERIFY_API_KEY, USER_AGENT from modules import get_proxies +from modules.module_status import annotate, OK, ERROR class HLRLookup: @@ -85,11 +86,11 @@ def validate_phone(self, phone: str, country_code: str = None) -> Dict[str, Any] result.update(api_result) except phonenumbers.NumberParseException as e: - result["error"] = f"Parse error: {str(e)}" + return annotate(result, ERROR, f"Parse error: {str(e)}") except Exception as e: - result["error"] = str(e) + return annotate(result, ERROR, str(e)) - return result + return annotate(result, OK) @staticmethod def _http_fallback_allowed() -> bool: @@ -186,7 +187,6 @@ def print_result(self, result: Dict): print(f"{Colors.YELLOW}Timezones:{Colors.RESET} {', '.join(result['timezones'])}") def reverse_lookup(self, phone: str) -> Dict[str, Any]: - from modules.module_status import annotate, ERROR result = { "phone": phone, "names": [], diff --git a/tests/test_hlr_lookup_status.py b/tests/test_hlr_lookup_status.py new file mode 100644 index 0000000..92c8284 --- /dev/null +++ b/tests/test_hlr_lookup_status.py @@ -0,0 +1,41 @@ +from unittest.mock import patch + +import phonenumbers + +from modules.hlr_lookup import HLRLookup +from modules.module_status import ERROR, OK + + +def _hlr(): + hlr = HLRLookup() + hlr.api_key = None + return hlr + + +def test_valid_number_is_ok(): + result = _hlr().validate_phone("+14155552671") + assert result["status"] == OK + assert result["error"] is None + assert result["valid"] is True + + +def test_invalid_but_parseable_number_is_ok(): + result = _hlr().validate_phone("+1234") + assert result["status"] == OK + assert result["error"] is None + assert result["valid"] is False + + +def test_parse_failure_is_error(): + result = _hlr().validate_phone("not a number") + assert result["status"] == ERROR + assert result["error"].startswith("Parse error:") + assert result["status_reason"] == result["error"] + + +def test_unexpected_exception_is_error(): + hlr = _hlr() + with patch.object(phonenumbers, "is_valid_number", side_effect=RuntimeError("boom")): + result = hlr.validate_phone("+14155552671") + assert result["status"] == ERROR + assert result["error"] == "boom"