🧪 test: Add test coverage for ValueError handling in ip_geo.py (#20)

* test: add tests for ValueError handling in ip_geo.py

This commit adds tests to ensure `ValueError` exception paths
in `ip_geo.py` are properly covered, specifically for `_ip_version`
and the ripestat HTTP JSON parsing fallback.

Coverage for `ip_geo.py` is now at 100%.

Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>

* drop redundant pytest-cov dep addition

---------

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
Horizontal42 2026-08-19 17:48:51 +03:00 • committed by GitHub
parent 0350abeea2
commit 7645802157
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
from __future__ import annotations
import httpx
from unittest.mock import patch
import pytest
from netsleuth.config import Providers
@ -321,3 +322,34 @@ async def test_gather_identity_for_a_target_never_reports_this_hosts_own_ip(http
assert merged.ip == "8.8.8.8"
assert merged.asn == "AS15169"
assert cf is not None and cf.ip == "203.0.113.9"
def test_ip_version_value_error_via_mock():
from netsleuth.ip_geo import _ip_version
with patch("netsleuth.ip_geo.ipaddress.ip_address", side_effect=ValueError("mock error")):
assert _ip_version("8.8.8.8") is None
async def test_gather_identity_tolerates_ripestat_value_error(httpx_mock):
httpx_mock.add_response(url="https://www.cloudflare.com/cdn-cgi/trace", text="")
httpx_mock.add_response(
url="http://ip-api.com/json/8.8.8.8",
json={
"status": "success",
"query": "8.8.8.8",
"as": "AS15169 GOOGLE",
"countryCode": "US",
},
)
httpx_mock.add_response(url="https://freeipapi.com/api/json/8.8.8.8", json={})
httpx_mock.add_response(url="https://ipinfo.io/8.8.8.8json", json={})
httpx_mock.add_response(url="https://ipwho.is/8.8.8.8", json={"success": False})
httpx_mock.add_response(
url="https://stat.ripe.net/data/network-info/data.json?resource=8.8.8.8",
text="this is not valid JSON and will cause ValueError",
)
async with httpx.AsyncClient() as client:
merged, _, _, _ = await gather_identity(client, Providers(), ip="8.8.8.8")
assert merged.ip == "8.8.8.8"
assert merged.asn == "AS15169"