Example #1
0
    def testGetCityGeolocationYesCity(self):
        class Location:
            def __init__(self):
                self.city = 'city'
                self.country = 'country'
                self.latitude = 'latitude'
                self.longitude = 'longitude'

        location = Location()
        expected_geo_record = maxmind.GeoRecord()
        expected_geo_record.city = location.city
        expected_geo_record.country = location.country
        expected_geo_record.latitude = location.latitude
        expected_geo_record.longitude = location.longitude
        self.assertEqual(
            expected_geo_record,
            maxmind.get_city_geolocation(
                'unused_city',
                'unused_country',
                city_table=MaxmindTestClass.ModelMockup(
                    gql_obj=MaxmindTestClass.GqlMockup(result=location))))
Example #2
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
Example #3
0
    def testInitializeUsesMaxmindWhenAppEngineGeoDataIsMissing(self):
        # Remove all mock AppEngine headers
        self.mock_request.headers = {}

        maxmind_city = 'maxmind_city'
        maxmind_country = 'maxmind_country'
        maxmind_latitude = 55.5
        maxmind_longitude = 77.7

        maxmind.get_ip_geolocation.return_value = maxmind.GeoRecord(
            city=maxmind_city,
            country=maxmind_country,
            latitude=maxmind_latitude,
            longitude=maxmind_longitude)

        query = lookup_query.LookupQuery()
        query.initialize_from_http_request(self.mock_request)
        self.assertEqual(message.POLICY_GEO, query.policy)
        self.assertEqual(maxmind_city, query.city)
        self.assertEqual(maxmind_country, query.country)
        self.assertEqual(maxmind_latitude, query.latitude)
        self.assertEqual(maxmind_longitude, query.longitude)
Example #4
0
    def testGetGeolocationValidLocation(self):
        ip_addr = '1.2.3.4'
        address_family = message.ADDRESS_FAMILY_IPv4
        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, 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)
Example #5
0
    def testInitializeWithUserDefinedCountryGetsGeolocationForThatCountry(
            self):
        user_defined_country = 'user_defined_country'
        self.mock_query_params[message.COUNTRY] = user_defined_country

        maxmind_city = None
        maxmind_country = user_defined_country
        maxmind_latitude = 55.5
        maxmind_longitude = 77.7

        maxmind.get_country_geolocation.return_value = maxmind.GeoRecord(
            city=maxmind_city,
            country=maxmind_country,
            latitude=maxmind_latitude,
            longitude=maxmind_longitude)

        query = lookup_query.LookupQuery()
        query.initialize_from_http_request(self.mock_request)

        self.assertEqual(maxmind_latitude, query.latitude)
        self.assertEqual(maxmind_longitude, query.longitude)
        self.assertIsNone(query.city)
        self.assertEqual(maxmind_country, query.country)
    def testInitializeUsesMaxmindWhenUserDefinedIpv6Exists(self):
        user_defined_ip = '1:2:3::4'
        self.mock_query_params[message.REMOTE_ADDRESS] = user_defined_ip

        maxmind_city = 'maxmind_city'
        maxmind_country = 'maxmind_country'
        maxmind_latitude = 55.5
        maxmind_longitude = 77.7

        maxmind.get_ip_geolocation.return_value = maxmind.GeoRecord(
            city=maxmind_city, country=maxmind_country,
            latitude=maxmind_latitude, longitude=maxmind_longitude)

        query = lookup_query.LookupQuery()
        query.initialize_from_http_request(self.mock_request)

        # Make sure we looked up the user-defined IP, not the request IP.
        maxmind.get_ip_geolocation.assert_called_with(user_defined_ip)
        self.assertEqual(message.POLICY_GEO, query.policy)
        self.assertEqual(maxmind_city, query.city)
        self.assertEqual(maxmind_country, query.country)
        self.assertEqual(maxmind_latitude, query.latitude)
        self.assertEqual(maxmind_longitude, query.longitude)
Example #7
0
 def testDefaultConstructor(self):
     geo_record = maxmind.GeoRecord()
     self.assertIsNone(geo_record.city)
     self.assertIsNone(geo_record.country)
     self.assertIsNone(geo_record.latitude)
     self.assertIsNone(geo_record.longitude)