Fixed #28981 -- Added an exception if GeoIP database can't be loaded from the path.

This commit is contained in:
Alex Stovbur 2018-03-03 23:43:26 +02:00 committed by Tim Graham
parent 84398897a7
commit d171843f57
2 changed files with 11 additions and 2 deletions

View File

@ -92,6 +92,9 @@ class GeoIP2:
if os.path.isfile(city_db):
self._city = geoip2.database.Reader(city_db, mode=cache)
self._city_file = city_db
if not self._reader:
raise GeoIP2Exception('Could not load a database from %s.' % path)
elif os.path.isfile(path):
# Otherwise, some detective work will be needed to figure out
# whether the given database path is for the GeoIP country or city

View File

@ -1,10 +1,10 @@
import os
import unittest
from unittest import mock, skipUnless
from django.conf import settings
from django.contrib.gis.geoip2 import HAS_GEOIP2
from django.contrib.gis.geos import GEOSGeometry
from django.test import SimpleTestCase
if HAS_GEOIP2:
from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
@ -18,7 +18,7 @@ if HAS_GEOIP2:
HAS_GEOIP2 and getattr(settings, "GEOIP_PATH", None),
"GeoIP is required along with the GEOIP_PATH setting."
)
class GeoIPTest(unittest.TestCase):
class GeoIPTest(SimpleTestCase):
addr = '128.249.1.1'
fqdn = 'tmc.edu'
@ -53,6 +53,12 @@ class GeoIPTest(unittest.TestCase):
with self.assertRaises(e):
GeoIP2(bad, 0)
def test_no_database_file(self):
invalid_path = os.path.join(os.path.dirname(__file__), 'data')
msg = 'Could not load a database from %s.' % invalid_path
with self.assertRaisesMessage(GeoIP2Exception, msg):
GeoIP2(invalid_path)
def test02_bad_query(self):
"GeoIP query parameter checking."
cntry_g = GeoIP2(city='<foo>')