2
2
import os .path
3
3
4
4
from 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
7
9
from django .contrib .auth .models import User
8
10
from django .template import TemplateDoesNotExist
9
11
10
12
from django .test import TestCase
11
13
from django .test .client import Client
12
14
from django .test .utils import override_settings
13
15
14
- from django .conf .urls import url
15
-
16
16
from maintenancemode import utils
17
17
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
- ]
33
18
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' )
44
19
class MaintenanceModeMiddlewareTestCase (TestCase ):
45
20
46
21
def setUp (self ):
@@ -66,13 +41,13 @@ def test_disabled_middleware(self):
66
41
def test_enabled_middleware_without_template (self ):
67
42
# Enabling the middleware without a proper 503 template should
68
43
# raise a template error
69
- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = [], TEMPLATES = []):
44
+ with self .settings (MAINTENANCE_MODE = True , TEMPLATES = []):
70
45
self .assertRaises (TemplateDoesNotExist , self .client .get , '/' )
71
46
72
47
def test_enabled_middleware_with_template (self ):
73
48
# Enabling the middleware having a ``503.html`` in any of the
74
49
# 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 ):
76
51
response = self .client .get ('/' )
77
52
self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
78
53
self .assertContains (response , text = 'You requested: /' , count = 1 , status_code = 503 )
@@ -81,7 +56,7 @@ def test_middleware_with_non_staff_user(self):
81
56
# A logged in user that is not a staff user should see the 503 message
82
57
self .client .login (username = 'maintenance' , password = 'password' )
83
58
84
- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
59
+ with self .settings (MAINTENANCE_MODE = True ):
85
60
response = self .client .get ('/' )
86
61
self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
87
62
@@ -92,7 +67,7 @@ def test_middleware_with_staff_user(self):
92
67
93
68
self .client .login (username = 'maintenance' , password = 'password' )
94
69
95
- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
70
+ with self .settings (MAINTENANCE_MODE = True ):
96
71
response = self .client .get ('/' )
97
72
self .assertContains (response , text = 'Rendered response page' , count = 1 , status_code = 200 )
98
73
@@ -103,7 +78,7 @@ def test_middleware_with_staff_user_denied(self):
103
78
104
79
self .client .login (username = 'maintenance' , password = 'password' )
105
80
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 ):
107
82
response = self .client .get ('/' )
108
83
self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
109
84
@@ -114,7 +89,7 @@ def test_middleware_with_superuser_user_denied(self):
114
89
115
90
self .client .login (username = 'maintenance' , password = 'password' )
116
91
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 ):
118
93
response = self .client .get ('/' )
119
94
self .assertContains (response , text = 'Temporary unavailable' , count = 1 , status_code = 503 )
120
95
@@ -125,7 +100,7 @@ def test_middleware_with_superuser_user_allowed(self):
125
100
126
101
self .client .login (username = 'maintenance' , password = 'password' )
127
102
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 ):
129
104
response = self .client .get ('/' )
130
105
self .assertContains (response , text = 'Rendered response page' , count = 1 , status_code = 200 )
131
106
@@ -150,15 +125,15 @@ def test_middleware_with_internal_ips_range(self):
150
125
def test_ignored_path (self ):
151
126
# A path is ignored when applying the maintanance mode and
152
127
# should be reachable normally
153
- with self .settings (MAINTENANCE_MODE = True , TEMPLATE_DIRS = TEMPLATE_DIRS , TEMPLATES = TEMPLATES ):
128
+ with self .settings (MAINTENANCE_MODE = True ):
154
129
with self .settings (IGNORE_URLS = (re .compile (r'^/ignored.*' ),)):
155
130
response = self .client .get ('/ignored/' )
156
131
self .assertContains (response , text = 'Rendered response page' , count = 1 , status_code = 200 )
157
132
158
133
def test_management_command (self ):
159
134
out = StringIO ()
160
135
# 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 ):
162
137
management .call_command ('maintenance' , 'on' , stdout = out )
163
138
self .assertContains (self .client .get ('/' ), text = 'Temporary unavailable' , count = 1 , status_code = 503 )
164
139
0 commit comments