test_geoip2.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import os
  2. import unittest
  3. from unittest import mock, skipUnless
  4. from django.conf import settings
  5. from django.contrib.gis.geoip2 import HAS_GEOIP2
  6. from django.contrib.gis.geos import GEOSGeometry
  7. if HAS_GEOIP2:
  8. from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
  9. # Note: Requires both the GeoIP country and city datasets.
  10. # The GEOIP_DATA path should be the only setting set (the directory
  11. # should contain links or the actual database files 'GeoLite2-City.mmdb' and
  12. # 'GeoLite2-City.mmdb'.
  13. @skipUnless(
  14. HAS_GEOIP2 and getattr(settings, "GEOIP_PATH", None),
  15. "GeoIP is required along with the GEOIP_PATH setting."
  16. )
  17. class GeoIPTest(unittest.TestCase):
  18. addr = '128.249.1.1'
  19. fqdn = 'tmc.edu'
  20. def test01_init(self):
  21. "GeoIP initialization."
  22. g1 = GeoIP2() # Everything inferred from GeoIP path
  23. path = settings.GEOIP_PATH
  24. g2 = GeoIP2(path, 0) # Passing in data path explicitly.
  25. g3 = GeoIP2.open(path, 0) # MaxMind Python API syntax.
  26. for g in (g1, g2, g3):
  27. self.assertTrue(g._country)
  28. self.assertTrue(g._city)
  29. # Only passing in the location of one database.
  30. city = os.path.join(path, 'GeoLite2-City.mmdb')
  31. cntry = os.path.join(path, 'GeoLite2-Country.mmdb')
  32. g4 = GeoIP2(city, country='')
  33. self.assertIsNone(g4._country)
  34. g5 = GeoIP2(cntry, city='')
  35. self.assertIsNone(g5._city)
  36. # Improper parameters.
  37. bad_params = (23, 'foo', 15.23)
  38. for bad in bad_params:
  39. with self.assertRaises(GeoIP2Exception):
  40. GeoIP2(cache=bad)
  41. if isinstance(bad, str):
  42. e = GeoIP2Exception
  43. else:
  44. e = TypeError
  45. with self.assertRaises(e):
  46. GeoIP2(bad, 0)
  47. def test02_bad_query(self):
  48. "GeoIP query parameter checking."
  49. cntry_g = GeoIP2(city='<foo>')
  50. # No city database available, these calls should fail.
  51. with self.assertRaises(GeoIP2Exception):
  52. cntry_g.city('tmc.edu')
  53. with self.assertRaises(GeoIP2Exception):
  54. cntry_g.coords('tmc.edu')
  55. # Non-string query should raise TypeError
  56. with self.assertRaises(TypeError):
  57. cntry_g.country_code(17)
  58. with self.assertRaises(TypeError):
  59. cntry_g.country_name(GeoIP2)
  60. @mock.patch('socket.gethostbyname')
  61. def test03_country(self, gethostbyname):
  62. "GeoIP country querying methods."
  63. gethostbyname.return_value = '128.249.1.1'
  64. g = GeoIP2(city='<foo>')
  65. for query in (self.fqdn, self.addr):
  66. self.assertEqual(
  67. 'US',
  68. g.country_code(query),
  69. 'Failed for func country_code and query %s' % query
  70. )
  71. self.assertEqual(
  72. 'United States',
  73. g.country_name(query),
  74. 'Failed for func country_name and query %s' % query
  75. )
  76. self.assertEqual(
  77. {'country_code': 'US', 'country_name': 'United States'},
  78. g.country(query)
  79. )
  80. @mock.patch('socket.gethostbyname')
  81. def test04_city(self, gethostbyname):
  82. "GeoIP city querying methods."
  83. gethostbyname.return_value = '128.249.1.1'
  84. g = GeoIP2(country='<foo>')
  85. for query in (self.fqdn, self.addr):
  86. # Country queries should still work.
  87. self.assertEqual(
  88. 'US',
  89. g.country_code(query),
  90. 'Failed for func country_code and query %s' % query
  91. )
  92. self.assertEqual(
  93. 'United States',
  94. g.country_name(query),
  95. 'Failed for func country_name and query %s' % query
  96. )
  97. self.assertEqual(
  98. {'country_code': 'US', 'country_name': 'United States'},
  99. g.country(query)
  100. )
  101. # City information dictionary.
  102. d = g.city(query)
  103. self.assertEqual('US', d['country_code'])
  104. self.assertEqual('Houston', d['city'])
  105. self.assertEqual('TX', d['region'])
  106. self.assertEqual('America/Chicago', d['time_zone'])
  107. geom = g.geos(query)
  108. self.assertIsInstance(geom, GEOSGeometry)
  109. lon, lat = (-95.4010, 29.7079)
  110. lat_lon = g.lat_lon(query)
  111. lat_lon = (lat_lon[1], lat_lon[0])
  112. for tup in (geom.tuple, g.coords(query), g.lon_lat(query), lat_lon):
  113. self.assertAlmostEqual(lon, tup[0], 4)
  114. self.assertAlmostEqual(lat, tup[1], 4)
  115. @mock.patch('socket.gethostbyname')
  116. def test05_unicode_response(self, gethostbyname):
  117. "GeoIP strings should be properly encoded (#16553)."
  118. gethostbyname.return_value = '194.27.42.76'
  119. g = GeoIP2()
  120. d = g.city("nigde.edu.tr")
  121. self.assertEqual('Niğde', d['city'])
  122. d = g.country('200.26.205.1')
  123. # Some databases have only unaccented countries
  124. self.assertIn(d['country_name'], ('Curaçao', 'Curacao'))
  125. def test06_ipv6_query(self):
  126. "GeoIP can lookup IPv6 addresses."
  127. g = GeoIP2()
  128. d = g.city('2002:81ed:c9a5::81ed:c9a5') # IPv6 address for www.nhm.ku.edu
  129. self.assertEqual('US', d['country_code'])
  130. self.assertEqual('Lawrence', d['city'])
  131. self.assertEqual('KS', d['region'])
  132. def test_repr(self):
  133. path = settings.GEOIP_PATH
  134. g = GeoIP2(path=path)
  135. meta = g._reader.metadata()
  136. version = '%s.%s' % (meta.binary_format_major_version, meta.binary_format_minor_version)
  137. country_path = g._country_file
  138. city_path = g._city_file
  139. expected = '<GeoIP2 [v%(version)s] _country_file="%(country)s", _city_file="%(city)s">' % {
  140. 'version': version,
  141. 'country': country_path,
  142. 'city': city_path,
  143. }
  144. self.assertEqual(repr(g), expected)
  145. @mock.patch('socket.gethostbyname', return_value='expected')
  146. def test_check_query(self, gethostbyname):
  147. g = GeoIP2()
  148. self.assertEqual(g._check_query('127.0.0.1'), '127.0.0.1')
  149. self.assertEqual(g._check_query('2002:81ed:c9a5::81ed:c9a5'), '2002:81ed:c9a5::81ed:c9a5')
  150. self.assertEqual(g._check_query('invalid-ip-address'), 'expected')