test_geoip.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import os
  4. import unittest
  5. from unittest import skipUnless
  6. from django.conf import settings
  7. from django.contrib.gis.geoip import HAS_GEOIP
  8. from django.contrib.gis.geos import HAS_GEOS
  9. from django.utils import six
  10. if HAS_GEOIP:
  11. from django.contrib.gis.geoip import GeoIP, GeoIPException
  12. if HAS_GEOS:
  13. from django.contrib.gis.geos import GEOSGeometry
  14. # Note: Requires use of both the GeoIP country and city datasets.
  15. # The GEOIP_DATA path should be the only setting set (the directory
  16. # should contain links or the actual database files 'GeoIP.dat' and
  17. # 'GeoLiteCity.dat'.
  18. @skipUnless(HAS_GEOIP and getattr(settings, "GEOIP_PATH", None),
  19. "GeoIP is required along with the GEOIP_PATH setting.")
  20. class GeoIPTest(unittest.TestCase):
  21. def test01_init(self):
  22. "Testing GeoIP initialization."
  23. g1 = GeoIP() # Everything inferred from GeoIP path
  24. path = settings.GEOIP_PATH
  25. g2 = GeoIP(path, 0) # Passing in data path explicitly.
  26. g3 = GeoIP.open(path, 0) # MaxMind Python API syntax.
  27. for g in (g1, g2, g3):
  28. self.assertEqual(True, bool(g._country))
  29. self.assertEqual(True, bool(g._city))
  30. # Only passing in the location of one database.
  31. city = os.path.join(path, 'GeoLiteCity.dat')
  32. cntry = os.path.join(path, 'GeoIP.dat')
  33. g4 = GeoIP(city, country='')
  34. self.assertEqual(None, g4._country)
  35. g5 = GeoIP(cntry, city='')
  36. self.assertEqual(None, g5._city)
  37. # Improper parameters.
  38. bad_params = (23, 'foo', 15.23)
  39. for bad in bad_params:
  40. self.assertRaises(GeoIPException, GeoIP, cache=bad)
  41. if isinstance(bad, six.string_types):
  42. e = GeoIPException
  43. else:
  44. e = TypeError
  45. self.assertRaises(e, GeoIP, bad, 0)
  46. def test02_bad_query(self):
  47. "Testing GeoIP query parameter checking."
  48. cntry_g = GeoIP(city='<foo>')
  49. # No city database available, these calls should fail.
  50. self.assertRaises(GeoIPException, cntry_g.city, 'google.com')
  51. self.assertRaises(GeoIPException, cntry_g.coords, 'yahoo.com')
  52. # Non-string query should raise TypeError
  53. self.assertRaises(TypeError, cntry_g.country_code, 17)
  54. self.assertRaises(TypeError, cntry_g.country_name, GeoIP)
  55. def test03_country(self):
  56. "Testing GeoIP country querying methods."
  57. g = GeoIP(city='<foo>')
  58. fqdn = 'www.google.com'
  59. addr = '12.215.42.19'
  60. for query in (fqdn, addr):
  61. for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
  62. self.assertEqual('US', func(query))
  63. for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
  64. self.assertEqual('United States', func(query))
  65. self.assertEqual({'country_code': 'US', 'country_name': 'United States'},
  66. g.country(query))
  67. @skipUnless(HAS_GEOS, "Geos is required")
  68. def test04_city(self):
  69. "Testing GeoIP city querying methods."
  70. g = GeoIP(country='<foo>')
  71. addr = '128.249.1.1'
  72. fqdn = 'tmc.edu'
  73. for query in (fqdn, addr):
  74. # Country queries should still work.
  75. for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
  76. self.assertEqual('US', func(query))
  77. for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
  78. self.assertEqual('United States', func(query))
  79. self.assertEqual({'country_code': 'US', 'country_name': 'United States'},
  80. g.country(query))
  81. # City information dictionary.
  82. d = g.city(query)
  83. self.assertEqual('USA', d['country_code3'])
  84. self.assertEqual('Houston', d['city'])
  85. self.assertEqual('TX', d['region'])
  86. self.assertEqual(713, d['area_code'])
  87. geom = g.geos(query)
  88. self.assertIsInstance(geom, GEOSGeometry)
  89. lon, lat = (-95.4010, 29.7079)
  90. lat_lon = g.lat_lon(query)
  91. lat_lon = (lat_lon[1], lat_lon[0])
  92. for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
  93. self.assertAlmostEqual(lon, tup[0], 4)
  94. self.assertAlmostEqual(lat, tup[1], 4)
  95. def test05_unicode_response(self):
  96. "Testing that GeoIP strings are properly encoded, see #16553."
  97. g = GeoIP()
  98. d = g.city("www.osnabrueck.de")
  99. self.assertEqual('Osnabrück', d['city'])
  100. d = g.country('200.7.49.81')
  101. self.assertEqual('Curaçao', d['country_name'])