Fixed #25407 -- Removed network dependency in GeoIP tests.
This commit is contained in:
parent
b1f6046066
commit
d0ed01cef0
|
@ -2,6 +2,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import os
|
||||
import socket
|
||||
import unittest
|
||||
import warnings
|
||||
from unittest import skipUnless
|
||||
|
@ -32,6 +33,17 @@ class GeoIPTest(unittest.TestCase):
|
|||
addr = '128.249.1.1'
|
||||
fqdn = 'tmc.edu'
|
||||
|
||||
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
|
||||
|
||||
def test01_init(self):
|
||||
"Testing GeoIP initialization."
|
||||
g1 = GeoIP() # Everything inferred from GeoIP path
|
||||
|
@ -76,7 +88,10 @@ class GeoIPTest(unittest.TestCase):
|
|||
"Testing GeoIP country querying methods."
|
||||
g = GeoIP(city='<foo>')
|
||||
|
||||
for query in (self.fqdn, self.addr):
|
||||
queries = [self.addr]
|
||||
if self._is_dns_available(self.fqdn):
|
||||
queries.append(self.fqdn)
|
||||
for query in queries:
|
||||
for func in (g.country_code, g.country_code_by_addr, g.country_code_by_name):
|
||||
self.assertEqual('US', func(query), 'Failed for func %s and query %s' % (func, query))
|
||||
for func in (g.country_name, g.country_name_by_addr, g.country_name_by_name):
|
||||
|
@ -89,7 +104,10 @@ class GeoIPTest(unittest.TestCase):
|
|||
"Testing GeoIP city querying methods."
|
||||
g = GeoIP(country='<foo>')
|
||||
|
||||
for query in (self.fqdn, self.addr):
|
||||
queries = [self.addr]
|
||||
if self._is_dns_available(self.fqdn):
|
||||
queries.append(self.fqdn)
|
||||
for query in queries:
|
||||
# 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))
|
||||
|
@ -116,8 +134,10 @@ class GeoIPTest(unittest.TestCase):
|
|||
def test05_unicode_response(self):
|
||||
"Testing that GeoIP strings are properly encoded, see #16553."
|
||||
g = GeoIP()
|
||||
d = g.city("duesseldorf.de")
|
||||
self.assertEqual('Düsseldorf', d['city'])
|
||||
fqdn = "duesseldorf.de"
|
||||
if self._is_dns_available(fqdn):
|
||||
d = g.city(fqdn)
|
||||
self.assertEqual('Düsseldorf', d['city'])
|
||||
d = g.country('200.26.205.1')
|
||||
# Some databases have only unaccented countries
|
||||
self.assertIn(d['country_name'], ('Curaçao', 'Curacao'))
|
||||
|
|
|
@ -8,6 +8,7 @@ from unittest import skipUnless
|
|||
from django.conf import settings
|
||||
from django.contrib.gis.geoip2 import HAS_GEOIP2
|
||||
from django.contrib.gis.geos import HAS_GEOS, GEOSGeometry
|
||||
from django.test import mock
|
||||
from django.utils import six
|
||||
|
||||
if HAS_GEOIP2:
|
||||
|
@ -64,8 +65,10 @@ class GeoIPTest(unittest.TestCase):
|
|||
self.assertRaises(TypeError, cntry_g.country_code, 17)
|
||||
self.assertRaises(TypeError, cntry_g.country_name, GeoIP2)
|
||||
|
||||
def test03_country(self):
|
||||
@mock.patch('socket.gethostbyname')
|
||||
def test03_country(self, gethostbyname):
|
||||
"GeoIP country querying methods."
|
||||
gethostbyname.return_value = '128.249.1.1'
|
||||
g = GeoIP2(city='<foo>')
|
||||
|
||||
for query in (self.fqdn, self.addr):
|
||||
|
@ -85,8 +88,10 @@ class GeoIPTest(unittest.TestCase):
|
|||
)
|
||||
|
||||
@skipUnless(HAS_GEOS, "Geos is required")
|
||||
def test04_city(self):
|
||||
@mock.patch('socket.gethostbyname')
|
||||
def test04_city(self, gethostbyname):
|
||||
"GeoIP city querying methods."
|
||||
gethostbyname.return_value = '128.249.1.1'
|
||||
g = GeoIP2(country='<foo>')
|
||||
|
||||
for query in (self.fqdn, self.addr):
|
||||
|
@ -121,8 +126,10 @@ class GeoIPTest(unittest.TestCase):
|
|||
self.assertAlmostEqual(lon, tup[0], 4)
|
||||
self.assertAlmostEqual(lat, tup[1], 4)
|
||||
|
||||
def test05_unicode_response(self):
|
||||
@mock.patch('socket.gethostbyname')
|
||||
def test05_unicode_response(self, gethostbyname):
|
||||
"GeoIP strings should be properly encoded (#16553)."
|
||||
gethostbyname.return_value = '194.27.42.76'
|
||||
g = GeoIP2()
|
||||
d = g.city("nigde.edu.tr")
|
||||
self.assertEqual('Niğde', d['city'])
|
||||
|
|
Loading…
Reference in New Issue