def _set_maxmind_geolocation(self, ip_address, country, city): geo_record = maxmind.GeoRecord() address_family = None if _is_valid_ipv6(ip_address): address_family = message.ADDRESS_FAMILY_IPv6 elif _is_valid_ipv4(ip_address): address_family = message.ADDRESS_FAMILY_IPv4 if ip_address is not None: logging.debug('Getting maxmind info for ip %s in family %s', ip_address, address_family) geo_record = maxmind.get_ip_geolocation(ip_address) elif city is not None and country is not None: geo_record = maxmind.get_city_geolocation(city, country) elif country is not None: geo_record = maxmind.get_country_geolocation(country) self._maxmind_city = geo_record.city self._maxmind_country = geo_record.country self._maxmind_latitude = geo_record.latitude self._maxmind_longitude = geo_record.longitude return (geo_record.latitude is not None and geo_record.longitude is not None)
def testGetGeolocationNoneAddress(self): ip_addr = None self.mock_record_by_addr.side_effect = TypeError self.assertNoneGeoRecord(maxmind.get_ip_geolocation(ip_addr)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with( constants.GEOLOCATION_MAXMIND_CITY_FILE, flags=pygeoip.const.STANDARD)
def testGetGeolocationNoneAddress(self): ip_addr = None address_family = None self.mock_record_by_addr.side_effect = TypeError self.assertNoneGeoRecord( maxmind.get_ip_geolocation(ip_addr, address_family)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with(None, flags=pygeoip.const.STANDARD)
def testGetGeolocationNoRecordForIp(self): ip_addr = '1.2.3.4' self.mock_record_by_addr.return_value = None self.assertNoneGeoRecord(maxmind.get_ip_geolocation(ip_addr)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with( constants.GEOLOCATION_MAXMIND_CITY_FILE, flags=pygeoip.const.STANDARD)
def testGetGeolocationNoRecordForIp(self): ip_addr = '1.2.3.4' address_family = message.ADDRESS_FAMILY_IPv4 self.mock_record_by_addr.return_value = None self.assertNoneGeoRecord( maxmind.get_ip_geolocation(ip_addr, address_family)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with( constants.GEOLOCATION_MAXMIND_CITY_FILE_IPv4, flags=pygeoip.const.STANDARD)
def _set_maxmind_geolocation(self, ip_address, country, city): geo_record = maxmind.GeoRecord() if ip_address is not None: geo_record = maxmind.get_ip_geolocation(ip_address) elif city is not None and country is not None: geo_record = maxmind.get_city_geolocation(city, country) elif country is not None: geo_record = maxmind.get_country_geolocation(country) self._maxmind_city = geo_record.city self._maxmind_country = geo_record.country self._maxmind_latitude = geo_record.latitude self._maxmind_longitude = geo_record.longitude
def testGetGeolocationValidLocation(self): ip_addr = "1.2.3.4" mock_record = {"city": "Greenwich, London", "country_code": "UK", "latitude": "51.4800", "longitude": "0.0000"} expected_record = maxmind.GeoRecord( city=mock_record["city"], country=mock_record["country_code"], latitude=mock_record["latitude"], longitude=mock_record["longitude"], ) self.mock_record_by_addr.return_value = mock_record self.assertEqual(expected_record, maxmind.get_ip_geolocation(ip_addr)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with(constants.GEOLOCATION_MAXMIND_CITY_FILE, flags=pygeoip.const.STANDARD)
def testGetGeolocationValidLocation(self): ip_addr = '1.2.3.4' mock_record = { 'city': 'Greenwich, London', 'country_code': 'UK', 'latitude': '51.4800', 'longitude': '0.0000' } expected_record = maxmind.GeoRecord(city=mock_record['city'], country=mock_record['country_code'], latitude=mock_record['latitude'], longitude=mock_record['longitude']) self.mock_record_by_addr.return_value = mock_record self.assertEqual(expected_record, maxmind.get_ip_geolocation(ip_addr)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with( constants.GEOLOCATION_MAXMIND_CITY_FILE, flags=pygeoip.const.STANDARD)
def testGetGeolocationValidLocation(self): ip_addr = '1.2.3.4' mock_record = { 'city': 'Greenwich, London', 'country_code': 'UK', 'latitude': '51.4800', 'longitude': '0.0000' } expected_record = maxmind.GeoRecord( city=mock_record['city'], country=mock_record['country_code'], latitude=mock_record['latitude'], longitude=mock_record['longitude']) self.mock_record_by_addr.return_value = mock_record self.assertEqual(expected_record, maxmind.get_ip_geolocation(ip_addr)) self.mock_record_by_addr.assert_called_with(ip_addr) pygeoip.GeoIP.assert_called_with( constants.GEOLOCATION_MAXMIND_CITY_FILE, flags=pygeoip.const.STANDARD)
def _set_maxmind_geolocation(self, ip_address, country, city): geo_record = maxmind.GeoRecord() address_family = None if _is_valid_ipv6(ip_address): address_family = message.ADDRESS_FAMILY_IPv6 elif _is_valid_ipv4(ip_address): address_family = message.ADDRESS_FAMILY_IPv4 if ip_address is not None: logging.debug('Getting maxmind info for ip %s in family %s', ip_address, address_family) geo_record = maxmind.get_ip_geolocation(ip_address) elif city is not None and country is not None: geo_record = maxmind.get_city_geolocation(city, country) elif country is not None: geo_record = maxmind.get_country_geolocation(country) self._maxmind_city = geo_record.city self._maxmind_country = geo_record.country self._maxmind_latitude = geo_record.latitude self._maxmind_longitude = geo_record.longitude
def testGetGeolocationNotValidAddress(self, mock_database_file): mock_database_file.side_effect = self.get_database_file ip_addr = 'abc' self.assertNoneGeoRecord(maxmind.get_ip_geolocation(ip_addr))
def testGetGeolocationNotValidAddress(self): self.assertNoneGeoRecord(maxmind.get_ip_geolocation('non_valid_ip'))
def testGetGeolocationNoRecordForIp(self, mock_database_file): mock_database_file.side_effect = self.get_database_file # ip_addr can be any invalid IP that looks like an IP. ip_addr = '0.1.2.3' self.assertNoneGeoRecord(maxmind.get_ip_geolocation(ip_addr))