def set_geo_from_ip(self, ip): """ Sets this model's geo data to the same as the given ip address If `force` is True, this will overrite existing data Usage :: user.set_geo_from_ip(user_ip) """ geo_data = iplookup(ip) self.address = '' self.city = geo_data.get('city', '') self.region = geo_data.get('region_name', '') self.postal_code = geo_data.get('postal_code', '') country_code = geo_data.get('country_code', '') if country_code and country_code not in COUNTRY_CODES_DICT: country_code = '' self.country_code = country_code try: self.latitude = float(geo_data.get('latitude')) except (TypeError, ValueError): self.latitude = None try: self.longitude = float(geo_data.get('longitude')) except (TypeError, ValueError): self.longitude = None # The service returns 0, 0 for some invalid addresses, so we'll # treat those as None if (self.latitude, self.longitude) == (0.0, 0.0): self.latitude, self.longitude = None, None
def test_iplookup_malformed(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE mock_urlopen().read.return_value = """<!DOCTYPE html><html></html>""" self.assertEqual(iplookup(TEST_IP), {}) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip=TEST_IP))
def test_iplookup_valid(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE mock_urlopen().read.return_value = json.dumps(TEST_IP_RESPONSE) self.assertEqual(iplookup(TEST_IP)['city'], TEST_IP_RESPONSE['city']) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip=TEST_IP))
def test_iplookup_valid(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE mock_urlopen().read.return_value = json.dumps(TEST_IP_RESPONSE) self.assertEqual(iplookup(TEST_IP)["city"], TEST_IP_RESPONSE["city"]) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip=TEST_IP))
def test_iplookup_service_unavailable(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE from urllib2 import HTTPError mock_urlopen().read.side_effect = HTTPError("-1", 500, None, None, None) self.assertEqual(iplookup(TEST_IP), {}) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip=TEST_IP))
def test_iplookup_invalid(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE from urllib2 import HTTPError mock_urlopen().read.side_effect = HTTPError("-1", 404, None, None, None) self.assertEqual(iplookup("-1"), {}) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip="-1"))
def test_iplookup_service_unavailable(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE from urllib2 import HTTPError mock_urlopen().read.side_effect = HTTPError('-1', 500, None, None, None) self.assertEqual(iplookup(TEST_IP), {}) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip=TEST_IP))
def test_iplookup_invalid(self, mock_urlopen): from app.geo.lookup import iplookup, GEO_IP_SERVICE from urllib2 import HTTPError mock_urlopen().read.side_effect = HTTPError('-1', 404, None, None, None) self.assertEqual(iplookup('-1'), {}) mock_urlopen.assert_called_with(GEO_IP_SERVICE.format(ip='-1'))
def test_iplookup_missing(self, mock_urlopen): from app.geo.lookup import iplookup self.assertEqual(iplookup(''), {}) self.assertItemsEqual(mock_urlopen.call_args_list, [])
def test_iplookup_missing(self, mock_urlopen): from app.geo.lookup import iplookup self.assertEqual(iplookup(""), {}) self.assertItemsEqual(mock_urlopen.call_args_list, [])