def test_geolocation(self): """The GeoIP database is not available in CI.""" self.request.ip_address = "invalid-ip" geolocation = get_geolocation(self.request) self.assertIsNone(geolocation.country) with mock.patch("adserver.utils.geoip") as geoip: geoip.city.return_value = { "country_code": "FR", "region": None, "dma_code": None, } self.request.ip_address = "8.8.8.8" geolocation = get_geoipdb_geolocation(self.request) self.assertIsNotNone(geolocation) self.assertEqual(geolocation.country, "FR") with mock.patch("adserver.utils.geoip") as geoip: geoip.city.side_effect = AddressNotFoundError() geolocation = get_geoipdb_geolocation(self.request) self.assertIsNone(geolocation.country) with mock.patch("adserver.utils.geoip") as geoip: geoip.city.side_effect = GeoIP2Exception() geolocation = get_geoipdb_geolocation(self.request) self.assertIsNone(geolocation.country)
def test_init(self, mock_geo2): middleware = GeoIP2Middleware(get_response) self.assertEqual(middleware.get_response, get_response) # now force a known exception in the init mock_geo2.return_value._reader = mock.Mock(spec=Reader) mock_geo2.side_effect = GeoIP2Exception() self.assertRaises(MiddlewareNotUsed, GeoIP2Middleware, get_response)
def __init__(self, get_response): """Check settings to see if middleware is enabled, and try to init GeoIP2.""" try: self.geoip2 = GeoIP2() # See https://code.djangoproject.com/ticket/28981 if self.geoip2._reader is None: raise GeoIP2Exception( "MaxMind database not found at GEOIP_PATH") except GeoIP2Exception: raise MiddlewareNotUsed("Error loading GeoIP2 data") else: self.get_response = get_response
def __init__(self, get_response): """Check settings to see if middleware is enabled, and try to init GeoIP2.""" try: self.geoip2 = GeoIP2() # this addresses a bug in Django's GeoIP2 implementation whereby it expects # there to be a country or city file, and blows up if there isn't one. if self.geoip2._reader is None: raise GeoIP2Exception( "MaxMind database not found at GEOIP_PATH") except GeoIP2Exception: raise MiddlewareNotUsed("Error loading GeoIP2 data") else: self.get_response = get_response
def test_init(self, mock_geo2): middleware = GeoIP2Middleware(GeoIP2MiddlewareTests.get_response) self.assertEqual(middleware.get_response, GeoIP2MiddlewareTests.get_response) # mock out a GeoIP2 with no _reader set - mimics the case # when neither country nor city databases exist. mock_geo2.return_value._reader = None self.assertRaises(MiddlewareNotUsed, GeoIP2Middleware, GeoIP2MiddlewareTests.get_response) # now force a known exception in the init mock_geo2.return_value._reader = mock.Mock(spec=Reader) mock_geo2.side_effect = GeoIP2Exception() self.assertRaises(MiddlewareNotUsed, GeoIP2Middleware, GeoIP2MiddlewareTests.get_response)
def test__geoip2(self, mock_city): mock_city.return_value = self.test_city data = self.middleware._geoip2(self.test_ip, mock_city) self.assertEqual(data.ip_address, "8.8.8.8") self.assertEqual(data.city, "Los Angeles") self.assertEqual(data.country_code, "US") self.assertEqual(data.country_name, "United States") self.assertEqual(data.dma_code, None) self.assertEqual(data.latitude, 37.751) self.assertEqual(data.longitude, -97.822) self.assertEqual(data.postal_code, 90210) self.assertEqual(data.region, "CA") mock_city.side_effect = AddressNotFoundError() data = self.middleware.city(self.test_ip) self.assertTrue(data.is_unknown) mock_city.side_effect = GeoIP2Exception() self.assertIsNone(self.middleware.city(self.test_ip)) mock_city.side_effect = Exception() self.assertIsNone(self.middleware.city(self.test_ip))
def test_geo_data__geoip2_exception(self, mock_get, mock_city_or_country) -> None: middleware = GeoIP2Middleware(lambda r: HttpResponse()) mock_get.return_value = None mock_city_or_country.side_effect = GeoIP2Exception() assert middleware.geo_data("1.2.3.4") == None
def city_or_country(self, ip_address: str) -> dict: if self.geoip2._city: return self.geoip2.city(ip_address) if self.geoip2._country: return self.geoip2.country(ip_address) raise GeoIP2Exception("GeoIP2 has neither city nor country database")