From 1027fa8dd3343e43bf0d579d1dd20ead7c1f2136 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Fri, 15 Nov 2024 15:40:51 +0100 Subject: [PATCH] Add richer set of errors caught --- apps/greencheck/domain_check.py | 17 +++++++++++++++-- apps/greencheck/tests/test_domain_checker.py | 19 ++++++++++++++++--- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/apps/greencheck/domain_check.py b/apps/greencheck/domain_check.py index 39933efc..88f1c711 100644 --- a/apps/greencheck/domain_check.py +++ b/apps/greencheck/domain_check.py @@ -23,7 +23,13 @@ import tld from django.utils import timezone from ipwhois.asn import IPASN -from ipwhois.exceptions import IPDefinedError, ASNParseError +from ipwhois.exceptions import ( + IPDefinedError, + ASNParseError, + ASNRegistryError, + ASNOriginLookupError, + ASNLookupError, +) from ipwhois.net import Net from .choices import GreenlistChoice @@ -309,7 +315,14 @@ def check_for_matching_asn(self, ip_address): try: asn_result = self.asn_from_ip(ip_address) - except (ASNParseError, IPDefinedError) as ex: + + except ( + ASNLookupError, + ASNParseError, + ASNRegistryError, + ASNOriginLookupError, + IPDefinedError, + ) as ex: logger.warning( f"Unable to parse ASN for IP: {ip_address} - error type: {type(ex).__name__} {ex}" ) diff --git a/apps/greencheck/tests/test_domain_checker.py b/apps/greencheck/tests/test_domain_checker.py index 838dea69..a24fcac1 100644 --- a/apps/greencheck/tests/test_domain_checker.py +++ b/apps/greencheck/tests/test_domain_checker.py @@ -104,19 +104,32 @@ def test_with_green_domain_by_asn_double(self, green_asn, checker): assert isinstance(res, legacy_workers.SiteCheck) assert res.hosting_provider_id == green_asn.hostingprovider.id - def test_asn_from_ip_fails_gracefully_with_bad_asn_lookup(self, checker, caplog): + @pytest.mark.parametrize( + "error_type", + ( + domain_check.ASNLookupError, + domain_check.ASNParseError, + domain_check.ASNRegistryError, + domain_check.ASNOriginLookupError, + domain_check.IPDefinedError, + ), + ) + def test_asn_from_ip_fails_gracefully_with_bad_asn_lookup( + self, checker, caplog, error_type + ): """ Sometimes calling lookup() on an IP address raises a ASNParseError. Do we catch this exception and log it? """ - checker.asn_from_ip = mock.MagicMock(side_effect=domain_check.ASNParseError) + checker.asn_from_ip = mock.MagicMock(side_effect=error_type) with caplog.at_level(logging.WARNING): res = checker.check_for_matching_asn("23.32.24.203") assert res is False logged_error = caplog.text - assert "ASNParseError" in logged_error + + assert error_type.__name__ in logged_error def test_with_green_domain_by_non_resolving_asn(self, green_asn, checker): """