1
0
mirror of https://github.com/django/django.git synced 2025-11-07 07:15:35 +00:00

Fixed #34305 -- Changed GeoIP2 tests to use MaxMind test databases.

GEOIP_SETTINGS is removed from the global scope as this prevents
modifications to the settings using @override_settings in tests.

Additional improvements now that we have stable test databases include:

- Made testing more comprehensive and improved coverage
- Patched socket.gethostbyname() for whole test case
- Added testing of non-free GeoIP2 databases

Co-authored-by: Tom Forbes <tom@tomforb.es>
This commit is contained in:
Nick Pope
2021-03-30 11:52:12 +01:00
committed by Mariusz Felisiak
parent ed4f83782d
commit a93375e8ab
9 changed files with 159 additions and 129 deletions

View File

@@ -25,19 +25,12 @@ __all__ = ["HAS_GEOIP2"]
try:
import geoip2.database
except ImportError:
except ImportError: # pragma: no cover
HAS_GEOIP2 = False
else:
HAS_GEOIP2 = True
__all__ += ["GeoIP2", "GeoIP2Exception"]
# Creating the settings dictionary with any settings, if needed.
GEOIP_SETTINGS = {
"GEOIP_PATH": getattr(settings, "GEOIP_PATH", None),
"GEOIP_CITY": getattr(settings, "GEOIP_CITY", "GeoLite2-City.mmdb"),
"GEOIP_COUNTRY": getattr(settings, "GEOIP_COUNTRY", "GeoLite2-Country.mmdb"),
}
class GeoIP2Exception(Exception):
pass
@@ -95,7 +88,7 @@ class GeoIP2:
raise GeoIP2Exception("Invalid GeoIP caching option: %s" % cache)
# Getting the GeoIP data path.
path = path or GEOIP_SETTINGS["GEOIP_PATH"]
path = path or getattr(settings, "GEOIP_PATH", None)
if not path:
raise GeoIP2Exception(
"GeoIP path must be provided via parameter or the GEOIP_PATH setting."
@@ -106,12 +99,16 @@ class GeoIP2:
# Constructing the GeoIP database filenames using the settings
# dictionary. If the database files for the GeoLite country
# and/or city datasets exist, then try to open them.
country_db = path / (country or GEOIP_SETTINGS["GEOIP_COUNTRY"])
country_db = path / (
country or getattr(settings, "GEOIP_COUNTRY", "GeoLite2-Country.mmdb")
)
if country_db.is_file():
self._country = geoip2.database.Reader(str(country_db), mode=cache)
self._country_file = country_db
city_db = path / (city or GEOIP_SETTINGS["GEOIP_CITY"])
city_db = path / (
city or getattr(settings, "GEOIP_CITY", "GeoLite2-City.mmdb")
)
if city_db.is_file():
self._city = geoip2.database.Reader(str(city_db), mode=cache)
self._city_file = city_db