2012-11-09 22:16:06 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-08 00:08:47 +08:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
import os
|
2015-09-21 03:23:28 +08:00
|
|
|
import socket
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2015-07-31 01:24:08 +08:00
|
|
|
import warnings
|
2013-07-01 20:22:27 +08:00
|
|
|
from unittest import skipUnless
|
|
|
|
|
2011-09-11 02:04:27 +08:00
|
|
|
from django.conf import settings
|
2013-05-11 11:08:45 +08:00
|
|
|
from django.contrib.gis.geoip import HAS_GEOIP
|
2015-04-24 23:24:07 +08:00
|
|
|
from django.contrib.gis.geos import HAS_GEOS, GEOSGeometry
|
2015-07-31 01:24:08 +08:00
|
|
|
from django.test import ignore_warnings
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2015-07-31 01:24:08 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2015-07-30 21:16:46 +08:00
|
|
|
from django.utils.encoding import force_text
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
if HAS_GEOIP:
|
2015-02-10 23:07:44 +08:00
|
|
|
from django.contrib.gis.geoip import GeoIP, GeoIPException
|
2015-09-04 20:52:20 +08:00
|
|
|
from django.contrib.gis.geoip.prototypes import GeoIP_lib_version
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Note: Requires use of both the GeoIP country and city datasets.
|
|
|
|
# The GEOIP_DATA path should be the only setting set (the directory
|
2010-11-02 06:18:22 +08:00
|
|
|
# should contain links or the actual database files 'GeoIP.dat' and
|
2008-08-06 02:13:06 +08:00
|
|
|
# 'GeoLiteCity.dat'.
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
@skipUnless(
|
|
|
|
HAS_GEOIP and getattr(settings, "GEOIP_PATH", None),
|
|
|
|
"GeoIP is required along with the GEOIP_PATH setting."
|
|
|
|
)
|
2015-07-31 01:24:08 +08:00
|
|
|
@ignore_warnings(category=RemovedInDjango20Warning)
|
2008-08-06 02:13:06 +08:00
|
|
|
class GeoIPTest(unittest.TestCase):
|
2015-07-31 00:16:14 +08:00
|
|
|
addr = '128.249.1.1'
|
|
|
|
fqdn = 'tmc.edu'
|
2010-11-02 06:18:22 +08:00
|
|
|
|
2015-09-21 03:23:28 +08:00
|
|
|
def _is_dns_available(self, domain):
|
|
|
|
# Naive check to see if there is DNS available to use.
|
|
|
|
# Used to conditionally skip fqdn geoip checks.
|
|
|
|
# See #25407 for details.
|
|
|
|
ErrClass = socket.error if six.PY2 else OSError
|
|
|
|
try:
|
|
|
|
socket.gethostbyname(domain)
|
|
|
|
return True
|
|
|
|
except ErrClass:
|
|
|
|
return False
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def test01_init(self):
|
|
|
|
"Testing GeoIP initialization."
|
2013-11-03 05:02:56 +08:00
|
|
|
g1 = GeoIP() # Everything inferred from GeoIP path
|
2008-08-06 02:13:06 +08:00
|
|
|
path = settings.GEOIP_PATH
|
2013-11-03 05:02:56 +08:00
|
|
|
g2 = GeoIP(path, 0) # Passing in data path explicitly.
|
|
|
|
g3 = GeoIP.open(path, 0) # MaxMind Python API syntax.
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
for g in (g1, g2, g3):
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertTrue(g._country)
|
|
|
|
self.assertTrue(g._city)
|
2010-11-02 06:18:22 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# Only passing in the location of one database.
|
|
|
|
city = os.path.join(path, 'GeoLiteCity.dat')
|
|
|
|
cntry = os.path.join(path, 'GeoIP.dat')
|
|
|
|
g4 = GeoIP(city, country='')
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertIsNone(g4._country)
|
2008-08-06 02:13:06 +08:00
|
|
|
g5 = GeoIP(cntry, city='')
|
2015-04-27 22:59:16 +08:00
|
|
|
self.assertIsNone(g5._city)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Improper parameters.
|
|
|
|
bad_params = (23, 'foo', 15.23)
|
|
|
|
for bad in bad_params:
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GeoIPException):
|
|
|
|
GeoIP(cache=bad)
|
2012-07-20 20:22:00 +08:00
|
|
|
if isinstance(bad, six.string_types):
|
2008-08-06 02:13:06 +08:00
|
|
|
e = GeoIPException
|
|
|
|
else:
|
|
|
|
e = TypeError
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(e):
|
|
|
|
GeoIP(bad, 0)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def test02_bad_query(self):
|
|
|
|
"Testing GeoIP query parameter checking."
|
|
|
|
cntry_g = GeoIP(city='<foo>')
|
|
|
|
# No city database available, these calls should fail.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(GeoIPException):
|
|
|
|
cntry_g.city('google.com')
|
|
|
|
with self.assertRaises(GeoIPException):
|
|
|
|
cntry_g.coords('yahoo.com')
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
# Non-string query should raise TypeError
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
cntry_g.country_code(17)
|
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
cntry_g.country_name(GeoIP)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def test03_country(self):
|
|
|
|
"Testing GeoIP country querying methods."
|
|
|
|
g = GeoIP(city='<foo>')
|
2010-11-02 06:18:22 +08:00
|
|
|
|
2015-09-21 03:23:28 +08:00
|
|
|
queries = [self.addr]
|
|
|
|
if self._is_dns_available(self.fqdn):
|
|
|
|
queries.append(self.fqdn)
|
|
|
|
for query in queries:
|
2008-08-06 02:13:06 +08:00
|
|
|
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
|
2015-07-28 19:23:58 +08:00
|
|
|
self.assertEqual('US', func(query), 'Failed for func %s and query %s' % (func, query))
|
2008-08-06 02:13:06 +08:00
|
|
|
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
|
2015-07-28 19:23:58 +08:00
|
|
|
self.assertEqual('United States', func(query), 'Failed for func %s and query %s' % (func, query))
|
2013-10-27 09:27:42 +08:00
|
|
|
self.assertEqual({'country_code': 'US', 'country_name': 'United States'},
|
2008-08-06 02:13:06 +08:00
|
|
|
g.country(query))
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
@skipUnless(HAS_GEOS, "Geos is required")
|
2008-08-06 02:13:06 +08:00
|
|
|
def test04_city(self):
|
|
|
|
"Testing GeoIP city querying methods."
|
|
|
|
g = GeoIP(country='<foo>')
|
2010-11-02 06:18:22 +08:00
|
|
|
|
2015-09-21 03:23:28 +08:00
|
|
|
queries = [self.addr]
|
|
|
|
if self._is_dns_available(self.fqdn):
|
|
|
|
queries.append(self.fqdn)
|
|
|
|
for query in queries:
|
2008-08-06 02:13:06 +08:00
|
|
|
# Country queries should still work.
|
|
|
|
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
|
|
|
|
self.assertEqual('US', func(query))
|
|
|
|
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
|
|
|
|
self.assertEqual('United States', func(query))
|
2013-10-27 09:27:42 +08:00
|
|
|
self.assertEqual({'country_code': 'US', 'country_name': 'United States'},
|
2008-08-06 02:13:06 +08:00
|
|
|
g.country(query))
|
2010-11-02 06:18:22 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
# City information dictionary.
|
|
|
|
d = g.city(query)
|
|
|
|
self.assertEqual('USA', d['country_code3'])
|
|
|
|
self.assertEqual('Houston', d['city'])
|
|
|
|
self.assertEqual('TX', d['region'])
|
|
|
|
self.assertEqual(713, d['area_code'])
|
|
|
|
geom = g.geos(query)
|
2014-04-10 04:20:22 +08:00
|
|
|
self.assertIsInstance(geom, GEOSGeometry)
|
2011-09-11 02:04:27 +08:00
|
|
|
lon, lat = (-95.4010, 29.7079)
|
2008-08-06 02:13:06 +08:00
|
|
|
lat_lon = g.lat_lon(query)
|
|
|
|
lat_lon = (lat_lon[1], lat_lon[0])
|
|
|
|
for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
|
2016-07-19 19:56:15 +08:00
|
|
|
self.assertAlmostEqual(lon, tup[0], 0)
|
|
|
|
self.assertAlmostEqual(lat, tup[1], 0)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2011-10-21 04:08:33 +08:00
|
|
|
def test05_unicode_response(self):
|
2011-09-11 02:04:27 +08:00
|
|
|
"Testing that GeoIP strings are properly encoded, see #16553."
|
|
|
|
g = GeoIP()
|
2016-08-03 23:08:30 +08:00
|
|
|
fqdn = "hs-duesseldorf.de"
|
2015-09-21 03:23:28 +08:00
|
|
|
if self._is_dns_available(fqdn):
|
|
|
|
d = g.city(fqdn)
|
2016-08-03 23:08:30 +08:00
|
|
|
self.assertEqual('Düsseldorf', d['city'])
|
2015-06-26 03:47:54 +08:00
|
|
|
d = g.country('200.26.205.1')
|
|
|
|
# Some databases have only unaccented countries
|
|
|
|
self.assertIn(d['country_name'], ('Curaçao', 'Curacao'))
|
2015-07-31 01:24:08 +08:00
|
|
|
|
|
|
|
def test_deprecation_warning(self):
|
|
|
|
with warnings.catch_warnings(record=True) as warns:
|
|
|
|
warnings.simplefilter('always')
|
|
|
|
GeoIP()
|
|
|
|
|
|
|
|
self.assertEqual(len(warns), 1)
|
|
|
|
msg = str(warns[0].message)
|
|
|
|
self.assertIn('django.contrib.gis.geoip is deprecated', msg)
|
2015-07-30 21:16:46 +08:00
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
path = settings.GEOIP_PATH
|
|
|
|
g = GeoIP(path=path)
|
|
|
|
country_path = g._country_file
|
|
|
|
city_path = g._city_file
|
|
|
|
if GeoIP_lib_version:
|
|
|
|
expected = '<GeoIP [v%(version)s] _country_file="%(country)s", _city_file="%(city)s">' % {
|
|
|
|
'version': force_text(GeoIP_lib_version()),
|
|
|
|
'country': country_path,
|
|
|
|
'city': city_path,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
expected = '<GeoIP _country_file="%(country)s", _city_file="%(city)s">' % {
|
|
|
|
'country': country_path,
|
|
|
|
'city': city_path,
|
|
|
|
}
|
|
|
|
self.assertEqual(repr(g), expected)
|