33from django .test import TestCase
44from django .test .utils import override_settings
55
6+ from user_sessions .templatetags import user_sessions
67from user_sessions .templatetags .user_sessions import (
78 browser , city , country , device , location , platform ,
89)
1314 geoip = GeoIP2 ()
1415 geoip_msg = None
1516except Exception as error_geoip2 : # pragma: no cover
16- try :
17- from django .contrib .gis .geoip import GeoIP
18- geoip = GeoIP ()
19- geoip_msg = None
20- except Exception as error_geoip :
21- geoip = None
22- geoip_msg = str (error_geoip2 ) + " and " + str (error_geoip )
17+ geoip = None
18+ geoip_msg = str (error_geoip2 )
2319
2420
2521class LocationTemplateFilterTest (TestCase ):
26- @override_settings (GEOIP_PATH = None )
22+ def setUp (self ):
23+ # Remove the cached GeoIP object, since it caches the database type and
24+ # this fails when we switch between city and country MMDB files
25+ user_sessions ._geoip = None
26+
27+ @override_settings (GEOIP_CITY = "" )
2728 def test_no_location (self ):
28- self .assertIsNone (location ('127.0.0.1' ))
29+ with self .assertWarnsRegex (
30+ UserWarning ,
31+ r"The address 127\.0\.0\.1 is not in the database" ,
32+ ):
33+ self .assertIsNone (location ('127.0.0.1' ))
2934
3035 @skipUnless (geoip , geoip_msg )
3136 def test_city (self ):
@@ -41,6 +46,40 @@ def test_locations(self):
4146 self .assertEqual ('San Diego, United States' , location ('44.55.66.77' ))
4247
4348
49+ @override_settings (GEOIP_CITY = "doesnt_exist" )
50+ class CountryLocationTemplateFilterTest (TestCase ):
51+ def setUp (self ):
52+ # Remove the cached GeoIP object, since it caches the database type and
53+ # this fails when we switch between city and country MMDB files
54+ user_sessions ._geoip = None
55+
56+ def test_no_location (self ):
57+ with self .assertWarnsRegex (
58+ UserWarning ,
59+ r"The address 127\.0\.0\.1 is not in the database" ,
60+ ):
61+ self .assertIsNone (location ('127.0.0.1' ))
62+
63+ @skipUnless (geoip , geoip_msg )
64+ def test_city (self ):
65+ self .assertIsNone (city ('55.66.77.88' ))
66+ self .assertIsNone (city ('8.8.4.4' ))
67+ # Make sure it isn't somehow pulling any IP addresses from the city
68+ # database
69+ self .assertIsNone (city ('44.55.66.77' ))
70+ self .assertIsNone (city ('8.8.8.8' ))
71+
72+ @skipUnless (geoip , geoip_msg )
73+ def test_country (self ):
74+ self .assertEqual ('United States' , country ('55.66.77.88' ))
75+ self .assertEqual ('United States' , country ('8.8.4.4' ))
76+
77+ @skipUnless (geoip , geoip_msg )
78+ def test_locations (self ):
79+ self .assertEqual ('United States' , location ('55.66.77.88' ))
80+ self .assertEqual ('United States' , location ('8.8.4.4' ))
81+
82+
4483class PlatformTemplateFilterTest (TestCase ):
4584 def test_windows (self ):
4685 # Generic Windows
0 commit comments