From 76458021570c3b86d444d7d19fed31b933f04ea8 Mon Sep 17 00:00:00 2001 From: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> Date: Wed, 19 Aug 2026 17:48:51 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=AA=20test:=20Add=20test=20coverage=20?= =?UTF-8?q?for=20ValueError=20handling=20in=20ip=5Fgeo.py=20(#20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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> --- tests/test_ip_geo_normalize.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/test_ip_geo_normalize.py b/tests/test_ip_geo_normalize.py index 7fda6a9..cd8ade9 100644 --- a/tests/test_ip_geo_normalize.py +++ b/tests/test_ip_geo_normalize.py @@ -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"