test_geoip2.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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.geoip2 import HAS_GEOIP2
  8. from django.contrib.gis.geos import HAS_GEOS, GEOSGeometry
  9. from django.utils import six
  10. if HAS_GEOIP2:
  11. from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
  12. # Note: Requires both the GeoIP country and city datasets.
  13. # The GEOIP_DATA path should be the only setting set (the directory
  14. # should contain links or the actual database files 'GeoLite2-City.mmdb' and
  15. # 'GeoLite2-City.mmdb'.
  16. @skipUnless(HAS_GEOIP2 and getattr(settings, "GEOIP_PATH", None),
  17. "GeoIP is required along with the GEOIP_PATH setting.")
  18. class GeoIPTest(unittest.TestCase):
  19. addr = '128.249.1.1'
  20. fqdn = 'tmc.edu'
  21. def test01_init(self):
  22. "GeoIP initialization."
  23. g1 = GeoIP2() # Everything inferred from GeoIP path
  24. path = settings.GEOIP_PATH
  25. g2 = GeoIP2(path, 0) # Passing in data path explicitly.
  26. g3 = GeoIP2.open(path, 0) # MaxMind Python API syntax.
  27. for g in (g1, g2, g3):
  28. self.assertTrue(g._country)
  29. self.assertTrue(g._city)
  30. # Only passing in the location of one database.
  31. city = os.path.join(path, 'GeoLite2-City.mmdb')
  32. cntry = os.path.join(path, 'GeoLite2-Country.mmdb')
  33. g4 = GeoIP2(city, country='')
  34. self.assertIsNone(g4._country)
  35. g5 = GeoIP2(cntry, city='')
  36. self.assertIsNone(g5._city)
  37. # Improper parameters.
  38. bad_params = (23, 'foo', 15.23)
  39. for bad in bad_params:
  40. self.assertRaises(GeoIP2Exception, GeoIP2, cache=bad)
  41. if isinstance(bad, six.string_types):
  42. e = GeoIP2Exception
  43. else:
  44. e = TypeError
  45. self.assertRaises(e, GeoIP2, bad, 0)
  46. def test02_bad_query(self):
  47. "GeoIP query parameter checking."
  48. cntry_g = GeoIP2(city='<foo>')
  49. # No city database available, these calls should fail.
  50. self.assertRaises(GeoIP2Exception, cntry_g.city, 'tmc.edu')
  51. self.assertRaises(GeoIP2Exception, cntry_g.coords, 'tmc.edu')
  52. # Non-string query should raise TypeError
  53. self.assertRaises(TypeError, cntry_g.country_code, 17)
  54. self.assertRaises(TypeError, cntry_g.country_name, GeoIP2)
  55. def test03_country(self):
  56. "GeoIP country querying methods."
  57. g = GeoIP2(city='<foo>')
  58. for query in (self.fqdn, self.addr):
  59. self.assertEqual(
  60. 'US',
  61. g.country_code(query),
  62. 'Failed for func country_code and query %s' % query
  63. )
  64. self.assertEqual(
  65. 'United States',
  66. g.country_name(query),
  67. 'Failed for func country_name and query %s' % query
  68. )
  69. self.assertEqual(
  70. {'country_code': 'US', 'country_name': 'United States'},
  71. g.country(query)
  72. )
  73. @skipUnless(HAS_GEOS, "Geos is required")
  74. def test04_city(self):
  75. "GeoIP city querying methods."
  76. g = GeoIP2(country='<foo>')
  77. for query in (self.fqdn, self.addr):
  78. # Country queries should still work.
  79. self.assertEqual(
  80. 'US',
  81. g.country_code(query),
  82. 'Failed for func country_code and query %s' % query
  83. )
  84. self.assertEqual(
  85. 'United States',
  86. g.country_name(query),
  87. 'Failed for func country_name and query %s' % query
  88. )
  89. self.assertEqual(
  90. {'country_code': 'US', 'country_name': 'United States'},
  91. g.country(query)
  92. )
  93. # City information dictionary.
  94. d = g.city(query)
  95. self.assertEqual('US', d['country_code'])
  96. self.assertEqual('Houston', d['city'])
  97. self.assertEqual('TX', d['region'])
  98. geom = g.geos(query)
  99. self.assertIsInstance(geom, GEOSGeometry)
  100. lon, lat = (-95.4010, 29.7079)
  101. lat_lon = g.lat_lon(query)
  102. lat_lon = (lat_lon[1], lat_lon[0])
  103. for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
  104. self.assertAlmostEqual(lon, tup[0], 4)
  105. self.assertAlmostEqual(lat, tup[1], 4)
  106. def test05_unicode_response(self):
  107. "GeoIP strings should be properly encoded (#16553)."
  108. g = GeoIP2()
  109. d = g.city("duesseldorf.de")
  110. self.assertEqual('Düsseldorf', d['city'])
  111. d = g.country('200.26.205.1')
  112. # Some databases have only unaccented countries
  113. self.assertIn(d['country_name'], ('Curaçao', 'Curacao'))
  114. def test06_ipv6_query(self):
  115. "GeoIP can lookup IPv6 addresses."
  116. g = GeoIP2()
  117. d = g.city('2002:81ed:c9a5::81ed:c9a5') # IPv6 address for www.nhm.ku.edu
  118. self.assertEqual('US', d['country_code'])
  119. self.assertEqual('Lawrence', d['city'])
  120. self.assertEqual('KS', d['region'])