Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 4 additions & 4 deletions modules/hlr_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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": [],
Expand Down
41 changes: 41 additions & 0 deletions tests/test_hlr_lookup_status.py
Original file line number Diff line number Diff line change
@@ -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"
Loading