Exemple #1
0
    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)
Exemple #2
0
 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)
Exemple #3
0
 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)
Exemple #4
0
 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 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 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)
Exemple #7
0
 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)
Exemple #8
0
 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
Exemple #9
0
 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
Exemple #10
0
    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)
Exemple #12
0
    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)
Exemple #13
0
    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
Exemple #14
0
 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))
Exemple #15
0
 def testGetGeolocationNotValidAddress(self):
     self.assertNoneGeoRecord(maxmind.get_ip_geolocation('non_valid_ip'))
Exemple #16
0
 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))
Exemple #17
0
 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))
Exemple #18
0
 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))
 def testGetGeolocationNotValidAddress(self):
     self.assertNoneGeoRecord(maxmind.get_ip_geolocation('non_valid_ip'))