22import os .path
33
44from django .core import management
5- from django .http import HttpResponse
6- from django .utils .six import StringIO
5+ try :
6+ from django .utils .six import StringIO
7+ except ImportError :
8+ from six import StringIO
79from django .contrib .auth .models import User
810from django .template import TemplateDoesNotExist
911
1012from django .test import TestCase
1113from django .test .client import Client
1214from django .test .utils import override_settings
1315
14- from django .conf .urls import url
15-
1616from maintenancemode import utils
1717
18- PROJECT_ROOT = os .path .abspath (os .path .dirname (__file__ ))
19-
20- TEMPLATE_DIRS = [
21- os .path .join (PROJECT_ROOT , 'test_templates' ),
22- ]
23-
24- TEMPLATES = [
25- {
26- 'BACKEND' : 'django.template.backends.django.DjangoTemplates' ,
27- 'DIRS' : [
28- os .path .join (PROJECT_ROOT , 'test_templates' ),
29- ],
30- 'APP_DIRS' : True ,
31- }
32- ]
3318
34- urlpatterns = [
35- url ('^$' , lambda r : HttpResponse ('Rendered response page' ), name = 'test' ),
36- url ('^ignored/$' , lambda r : HttpResponse ('Rendered response page' ), name = 'test' ),
37- ]
38-
39-
40- @override_settings (INTERNAL_IPS = [])
41- @override_settings (MAINTENANCE_MODE = False )
42- @override_settings (TEMPLATE_DIRS = [], TEMPLATES = [])
43- @override_settings (ROOT_URLCONF = 'maintenancemode.tests' )
4419class MaintenanceModeMiddlewareTestCase (TestCase ):
4520
4621 def setUp (self ):
@@ -66,13 +41,13 @@ def test_disabled_middleware(self):
6641 def test_enabled_middleware_without_template (self ):
6742 # Enabling the middleware without a proper 503 template should
6843 # raise a template error
69- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = [], TEMPLATES = []):
44+ with self .settings (MAINTENANCE_MODE = True , TEMPLATES = []):
7045 self .assertRaises (TemplateDoesNotExist , self .client .get , '/' )
7146
7247 def test_enabled_middleware_with_template (self ):
7348 # Enabling the middleware having a ``503.html`` in any of the
7449 # template locations should return the rendered template"
75- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
50+ with self .settings (MAINTENANCE_MODE = True ):
7651 response = self .client .get ('/' )
7752 self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
7853 self .assertContains (response , text = 'You requested: /' , count = 1 , status_code = 503 )
@@ -81,7 +56,7 @@ def test_middleware_with_non_staff_user(self):
8156 # A logged in user that is not a staff user should see the 503 message
8257 self .client .login (username = 'maintenance' , password = 'password' )
8358
84- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
59+ with self .settings (MAINTENANCE_MODE = True ):
8560 response = self .client .get ('/' )
8661 self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
8762
@@ -92,7 +67,7 @@ def test_middleware_with_staff_user(self):
9267
9368 self .client .login (username = 'maintenance' , password = 'password' )
9469
95- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
70+ with self .settings (MAINTENANCE_MODE = True ):
9671 response = self .client .get ('/' )
9772 self .assertContains (response , text = 'Rendered response page' , count = 1 , status_code = 200 )
9873
@@ -103,7 +78,7 @@ def test_middleware_with_staff_user_denied(self):
10378
10479 self .client .login (username = 'maintenance' , password = 'password' )
10580
106- with self .settings (MAINTENANCE_MODE = True , MAINTENANCE_ALLOW_STAFF = False , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
81+ with self .settings (MAINTENANCE_MODE = True , MAINTENANCE_ALLOW_STAFF = False ):
10782 response = self .client .get ('/' )
10883 self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
10984
@@ -114,7 +89,7 @@ def test_middleware_with_superuser_user_denied(self):
11489
11590 self .client .login (username = 'maintenance' , password = 'password' )
11691
117- with self .settings (MAINTENANCE_MODE = True , MAINTENANCE_ALLOW_SUPERUSER = False , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
92+ with self .settings (MAINTENANCE_MODE = True , MAINTENANCE_ALLOW_SUPERUSER = False ):
11893 response = self .client .get ('/' )
11994 self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
12095
@@ -125,7 +100,7 @@ def test_middleware_with_superuser_user_allowed(self):
125100
126101 self .client .login (username = 'maintenance' , password = 'password' )
127102
128- with self .settings (MAINTENANCE_MODE = True , MAINTENANCE_ALLOW_STAFF = False , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
103+ with self .settings (MAINTENANCE_MODE = True , MAINTENANCE_ALLOW_STAFF = False ):
129104 response = self .client .get ('/' )
130105 self .assertContains (response , text = 'Rendered response page' , count = 1 , status_code = 200 )
131106
@@ -150,15 +125,15 @@ def test_middleware_with_internal_ips_range(self):
150125 def test_ignored_path (self ):
151126 # A path is ignored when applying the maintanance mode and
152127 # should be reachable normally
153- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
128+ with self .settings (MAINTENANCE_MODE = True ):
154129 with self .settings (IGNORE_URLS = (re .compile (r'^/ignored.*' ),)):
155130 response = self .client .get ('/ignored/' )
156131 self .assertContains (response , text = 'Rendered response page' , count = 1 , status_code = 200 )
157132
158133 def test_management_command (self ):
159134 out = StringIO ()
160135 # Explicitly disabling the ``MAINTENANCE_MODE``
161- with self .settings (MAINTENANCE_MODE = False , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
136+ with self .settings (MAINTENANCE_MODE = False ):
162137 management .call_command ('maintenance' , 'on' , stdout = out )
163138 self .assertContains (self .client .get ('/' ), text = 'Temporary unavailable' , count = 1 , status_code = 503 )
164139
0 commit comments