diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py
index 0337ef1..941fa26 100644
a
|
b
|
from django.conf import settings
|
2 | 2 | from django.contrib.auth.models import User, Group, Permission, AnonymousUser |
3 | 3 | from django.contrib.contenttypes.models import ContentType |
4 | 4 | from django.core.exceptions import ImproperlyConfigured |
5 | | from django.test import TestCase |
| 5 | from django.test import DefaultSettingsTestCase |
| 6 | from django.test.utils import override_settings |
6 | 7 | |
7 | 8 | |
8 | | class BackendTest(TestCase): |
| 9 | class CustomBackendTestCase(DefaultSettingsTestCase): |
| 10 | def setUp(self): |
| 11 | self.settings_override = override_settings( |
| 12 | AUTHENTICATION_BACKENDS=(self.backend,) |
| 13 | ) |
| 14 | self.settings_override.enable() |
9 | 15 | |
10 | | backend = 'django.contrib.auth.backends.ModelBackend' |
| 16 | def tearDown(self): |
| 17 | self.settings_override.disable() |
| 18 | # Some tests (e.g. custom_perms) mess with ContentTypes, which will |
| 19 | # be cached; flush the cache to ensure there are no side effects |
| 20 | # Refs #14975, #14925 |
| 21 | ContentType.objects.clear_cache() |
11 | 22 | |
| 23 | |
| 24 | class AdditionalBackendTestCase(DefaultSettingsTestCase): |
12 | 25 | def setUp(self): |
13 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
14 | | settings.AUTHENTICATION_BACKENDS = (self.backend,) |
15 | | User.objects.create_user('test', 'test@example.com', 'test') |
16 | | User.objects.create_superuser('test2', 'test2@example.com', 'test') |
| 26 | self.settings_override = override_settings( |
| 27 | AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS + (self.backend,) |
| 28 | ) |
| 29 | self.settings_override.enable() |
17 | 30 | |
18 | 31 | def tearDown(self): |
19 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
20 | | # The custom_perms test messes with ContentTypes, which will |
| 32 | self.settings_override.disable() |
| 33 | # Some tests (e.g. custom_perms) mess with ContentTypes, which will |
21 | 34 | # be cached; flush the cache to ensure there are no side effects |
22 | 35 | # Refs #14975, #14925 |
23 | 36 | ContentType.objects.clear_cache() |
24 | 37 | |
| 38 | |
| 39 | class BackendTest(CustomBackendTestCase): |
| 40 | |
| 41 | backend = 'django.contrib.auth.backends.ModelBackend' |
| 42 | |
| 43 | def setUp(self): |
| 44 | super(BackendTest, self).setUp() |
| 45 | User.objects.create_user('test', 'test@example.com', 'test') |
| 46 | User.objects.create_superuser('test2', 'test2@example.com', 'test') |
| 47 | |
25 | 48 | def test_has_perm(self): |
26 | 49 | user = User.objects.get(username='test') |
27 | 50 | self.assertEqual(user.has_perm('auth.test'), False) |
… |
… |
class SimpleRowlevelBackend(object):
|
154 | 177 | return ['none'] |
155 | 178 | |
156 | 179 | |
157 | | class RowlevelBackendTest(TestCase): |
| 180 | class RowlevelBackendTest(AdditionalBackendTestCase): |
158 | 181 | """ |
159 | 182 | Tests for auth backend that supports object level permissions |
160 | 183 | """ |
161 | 184 | backend = 'django.contrib.auth.tests.auth_backends.SimpleRowlevelBackend' |
162 | 185 | |
163 | 186 | def setUp(self): |
164 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
165 | | settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,) |
| 187 | super(RowlevelBackendTest, self).setUp() |
166 | 188 | self.user1 = User.objects.create_user('test', 'test@example.com', 'test') |
167 | 189 | self.user2 = User.objects.create_user('test2', 'test2@example.com', 'test') |
168 | 190 | self.user3 = User.objects.create_user('test3', 'test3@example.com', 'test') |
169 | 191 | |
170 | | def tearDown(self): |
171 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
172 | | # The get_group_permissions test messes with ContentTypes, which will |
173 | | # be cached; flush the cache to ensure there are no side effects |
174 | | # Refs #14975, #14925 |
175 | | ContentType.objects.clear_cache() |
176 | | |
177 | 192 | def test_has_perm(self): |
178 | 193 | self.assertEqual(self.user1.has_perm('perm', TestObj()), False) |
179 | 194 | self.assertEqual(self.user2.has_perm('perm', TestObj()), True) |
… |
… |
class RowlevelBackendTest(TestCase):
|
189 | 204 | self.assertEqual(self.user2.get_all_permissions(), set([])) |
190 | 205 | |
191 | 206 | def test_get_group_permissions(self): |
192 | | content_type=ContentType.objects.get_for_model(Group) |
193 | 207 | group = Group.objects.create(name='test_group') |
194 | 208 | self.user3.groups.add(group) |
195 | 209 | self.assertEqual(self.user3.get_group_permissions(TestObj()), set(['group_perm'])) |
… |
… |
class AnonymousUserBackend(SimpleRowlevelBackend):
|
199 | 213 | supports_inactive_user = False |
200 | 214 | |
201 | 215 | |
202 | | class AnonymousUserBackendTest(TestCase): |
| 216 | class AnonymousUserBackendTest(CustomBackendTestCase): |
203 | 217 | """ |
204 | 218 | Tests for AnonymousUser delegating to backend. |
205 | 219 | """ |
… |
… |
class AnonymousUserBackendTest(TestCase):
|
207 | 221 | backend = 'django.contrib.auth.tests.auth_backends.AnonymousUserBackend' |
208 | 222 | |
209 | 223 | def setUp(self): |
210 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
211 | | settings.AUTHENTICATION_BACKENDS = (self.backend,) |
| 224 | super(AnonymousUserBackendTest, self).setUp() |
212 | 225 | self.user1 = AnonymousUser() |
213 | 226 | |
214 | | def tearDown(self): |
215 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
216 | | |
217 | 227 | def test_has_perm(self): |
218 | 228 | self.assertEqual(self.user1.has_perm('perm', TestObj()), False) |
219 | 229 | self.assertEqual(self.user1.has_perm('anon', TestObj()), True) |
… |
… |
class AnonymousUserBackendTest(TestCase):
|
232 | 242 | |
233 | 243 | |
234 | 244 | |
235 | | class NoBackendsTest(TestCase): |
| 245 | class NoBackendsTest(DefaultSettingsTestCase): |
236 | 246 | """ |
237 | 247 | Tests that an appropriate error is raised if no auth backends are provided. |
238 | 248 | """ |
239 | 249 | def setUp(self): |
240 | | self.old_AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS |
241 | | settings.AUTHENTICATION_BACKENDS = [] |
242 | 250 | self.user = User.objects.create_user('test', 'test@example.com', 'test') |
243 | 251 | |
244 | | def tearDown(self): |
245 | | settings.AUTHENTICATION_BACKENDS = self.old_AUTHENTICATION_BACKENDS |
246 | | |
| 252 | @override_settings(AUTHENTICATION_BACKENDS=[]) |
247 | 253 | def test_raises_exception(self): |
248 | 254 | self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),)) |
249 | 255 | |
… |
… |
class NoInActiveUserBackend(SimpleRowlevelBackend):
|
256 | 262 | supports_inactive_user = False |
257 | 263 | |
258 | 264 | |
259 | | class InActiveUserBackendTest(TestCase): |
| 265 | class InActiveUserBackendTest(CustomBackendTestCase): |
260 | 266 | """ |
261 | 267 | Tests for a inactive user delegating to backend if it has 'supports_inactive_user' = True |
262 | 268 | """ |
… |
… |
class InActiveUserBackendTest(TestCase):
|
264 | 270 | backend = 'django.contrib.auth.tests.auth_backends.InActiveUserBackend' |
265 | 271 | |
266 | 272 | def setUp(self): |
267 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
268 | | settings.AUTHENTICATION_BACKENDS = (self.backend,) |
| 273 | super(InActiveUserBackendTest, self).setUp() |
269 | 274 | self.user1 = User.objects.create_user('test', 'test@example.com', 'test') |
270 | 275 | self.user1.is_active = False |
271 | 276 | self.user1.save() |
272 | 277 | |
273 | | def tearDown(self): |
274 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
275 | | |
276 | 278 | def test_has_perm(self): |
277 | 279 | self.assertEqual(self.user1.has_perm('perm', TestObj()), False) |
278 | 280 | self.assertEqual(self.user1.has_perm('inactive', TestObj()), True) |
… |
… |
class InActiveUserBackendTest(TestCase):
|
282 | 284 | self.assertEqual(self.user1.has_module_perms("app2"), False) |
283 | 285 | |
284 | 286 | |
285 | | class NoInActiveUserBackendTest(TestCase): |
| 287 | class NoInActiveUserBackendTest(AdditionalBackendTestCase): |
286 | 288 | """ |
287 | 289 | Tests that an inactive user does not delegate to backend if it has 'supports_inactive_user' = False |
288 | 290 | """ |
289 | 291 | backend = 'django.contrib.auth.tests.auth_backends.NoInActiveUserBackend' |
290 | 292 | |
291 | 293 | def setUp(self): |
292 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
293 | | settings.AUTHENTICATION_BACKENDS = tuple(self.curr_auth) + (self.backend,) |
| 294 | super(NoInActiveUserBackendTest, self).setUp() |
294 | 295 | self.user1 = User.objects.create_user('test', 'test@example.com', 'test') |
295 | 296 | self.user1.is_active = False |
296 | 297 | self.user1.save() |
297 | 298 | |
298 | | def tearDown(self): |
299 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
300 | | |
301 | 299 | def test_has_perm(self): |
302 | 300 | self.assertEqual(self.user1.has_perm('perm', TestObj()), False) |
303 | 301 | self.assertEqual(self.user1.has_perm('inactive', TestObj()), False) |
… |
… |
class NoInActiveUserBackendTest(TestCase):
|
305 | 303 | def test_has_module_perms(self): |
306 | 304 | self.assertEqual(self.user1.has_module_perms("app1"), False) |
307 | 305 | self.assertEqual(self.user1.has_module_perms("app2"), False) |
308 | | |
diff --git a/django/contrib/auth/tests/basic.py b/django/contrib/auth/tests/basic.py
index 512de16..31eb873 100644
a
|
b
|
|
1 | | from django.test import TestCase |
2 | | from django.utils.unittest import skipUnless |
| 1 | from StringIO import StringIO |
| 2 | |
3 | 3 | from django.contrib.auth.models import User, AnonymousUser |
4 | 4 | from django.core.management import call_command |
5 | | from StringIO import StringIO |
| 5 | from django.test import DefaultSettingsTestCase |
| 6 | from django.utils.unittest import skipUnless |
6 | 7 | |
7 | 8 | try: |
8 | 9 | import crypt as crypt_module |
… |
… |
except ImportError:
|
10 | 11 | crypt_module = None |
11 | 12 | |
12 | 13 | |
13 | | class BasicTestCase(TestCase): |
| 14 | class BasicTestCase(DefaultSettingsTestCase): |
14 | 15 | def test_user(self): |
15 | 16 | "Check that users can be created and can set their password" |
16 | 17 | u = User.objects.create_user('testuser', 'test@example.com', 'testpw') |
diff --git a/django/contrib/auth/tests/context_processors.py b/django/contrib/auth/tests/context_processors.py
index 49172c6..0e1f5ac 100644
a
|
b
|
|
1 | | import os |
2 | | |
3 | | from django.conf import settings |
4 | 1 | from django.contrib.auth import authenticate |
| 2 | from django.contrib.auth.tests.views import AuthViewsTestCase |
5 | 3 | from django.db.models import Q |
6 | | from django.test import TestCase |
7 | 4 | |
8 | 5 | |
9 | | class AuthContextProcessorTests(TestCase): |
| 6 | class AuthContextProcessorTests(AuthViewsTestCase): |
10 | 7 | """ |
11 | 8 | Tests for the ``django.contrib.auth.context_processors.auth`` processor |
12 | 9 | """ |
13 | | urls = 'django.contrib.auth.tests.urls' |
14 | 10 | fixtures = ['context-processors-users.xml'] |
15 | 11 | |
16 | | def setUp(self): |
17 | | self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS |
18 | | settings.TEMPLATE_DIRS = ( |
19 | | os.path.join(os.path.dirname(__file__), 'templates'), |
20 | | ) |
21 | | |
22 | | def tearDown(self): |
23 | | settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS |
24 | | |
25 | 12 | def test_session_not_accessed(self): |
26 | 13 | """ |
27 | 14 | Tests that the session is not accessed simply by including |
diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py
index 2bacc8e..239aa6f 100644
a
|
b
|
from django.core import mail
|
4 | 4 | from django.forms.fields import Field, EmailField |
5 | 5 | from django.contrib.auth.models import User |
6 | 6 | from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm |
7 | | from django.test import TestCase |
| 7 | from django.test import DefaultSettingsTestCase |
8 | 8 | from django.utils.encoding import force_unicode |
9 | 9 | from django.utils import translation |
10 | 10 | |
11 | 11 | |
12 | | class UserCreationFormTest(TestCase): |
| 12 | class UserCreationFormTest(DefaultSettingsTestCase): |
13 | 13 | |
14 | 14 | fixtures = ['authtestdata.json'] |
15 | 15 | |
… |
… |
class UserCreationFormTest(TestCase):
|
75 | 75 | self.assertEqual(repr(u), '<User: jsmith@example.com>') |
76 | 76 | |
77 | 77 | |
78 | | class AuthenticationFormTest(TestCase): |
| 78 | class AuthenticationFormTest(DefaultSettingsTestCase): |
79 | 79 | |
80 | 80 | fixtures = ['authtestdata.json'] |
81 | 81 | |
… |
… |
class AuthenticationFormTest(TestCase):
|
126 | 126 | self.assertEqual(form.non_field_errors(), []) |
127 | 127 | |
128 | 128 | |
129 | | class SetPasswordFormTest(TestCase): |
| 129 | class SetPasswordFormTest(DefaultSettingsTestCase): |
130 | 130 | |
131 | 131 | fixtures = ['authtestdata.json'] |
132 | 132 | |
… |
… |
class SetPasswordFormTest(TestCase):
|
152 | 152 | self.assertTrue(form.is_valid()) |
153 | 153 | |
154 | 154 | |
155 | | class PasswordChangeFormTest(TestCase): |
| 155 | class PasswordChangeFormTest(DefaultSettingsTestCase): |
156 | 156 | |
157 | 157 | fixtures = ['authtestdata.json'] |
158 | 158 | |
… |
… |
class PasswordChangeFormTest(TestCase):
|
199 | 199 | ['old_password', 'new_password1', 'new_password2']) |
200 | 200 | |
201 | 201 | |
202 | | class UserChangeFormTest(TestCase): |
| 202 | class UserChangeFormTest(DefaultSettingsTestCase): |
203 | 203 | |
204 | 204 | fixtures = ['authtestdata.json'] |
205 | 205 | |
… |
… |
class UserChangeFormTest(TestCase):
|
227 | 227 | form = MyUserForm({}) |
228 | 228 | |
229 | 229 | |
230 | | class PasswordResetFormTest(TestCase): |
| 230 | class PasswordResetFormTest(DefaultSettingsTestCase): |
231 | 231 | |
232 | 232 | fixtures = ['authtestdata.json'] |
233 | 233 | |
diff --git a/django/contrib/auth/tests/management.py b/django/contrib/auth/tests/management.py
index 0af6873..bec15b4 100644
a
|
b
|
|
1 | | from django.test import TestCase |
2 | 1 | from django.contrib.auth import models, management |
| 2 | from django.test import DefaultSettingsTestCase |
3 | 3 | |
4 | 4 | |
5 | | class GetDefaultUsernameTestCase(TestCase): |
| 5 | class GetDefaultUsernameTestCase(DefaultSettingsTestCase): |
6 | 6 | |
7 | 7 | def setUp(self): |
8 | 8 | self._getpass_getuser = management.get_system_username |
diff --git a/django/contrib/auth/tests/models.py b/django/contrib/auth/tests/models.py
index 754c6db..8a25224 100644
a
|
b
|
|
1 | | from django.conf import settings |
2 | | from django.test import TestCase |
3 | 1 | from django.contrib.auth.models import User, SiteProfileNotAvailable |
| 2 | from django.test import DefaultSettingsTestCase |
| 3 | from django.test.utils import override_settings |
4 | 4 | |
5 | | class ProfileTestCase(TestCase): |
6 | | fixtures = ['authtestdata.json'] |
7 | | def setUp(self): |
8 | | """Backs up the AUTH_PROFILE_MODULE""" |
9 | | self.old_AUTH_PROFILE_MODULE = getattr(settings, |
10 | | 'AUTH_PROFILE_MODULE', None) |
11 | | |
12 | | def tearDown(self): |
13 | | """Restores the AUTH_PROFILE_MODULE -- if it was not set it is deleted, |
14 | | otherwise the old value is restored""" |
15 | | if self.old_AUTH_PROFILE_MODULE is None and \ |
16 | | hasattr(settings, 'AUTH_PROFILE_MODULE'): |
17 | | del settings.AUTH_PROFILE_MODULE |
18 | 5 | |
19 | | if self.old_AUTH_PROFILE_MODULE is not None: |
20 | | settings.AUTH_PROFILE_MODULE = self.old_AUTH_PROFILE_MODULE |
| 6 | class ProfileTestCase(DefaultSettingsTestCase): |
| 7 | fixtures = ['authtestdata.json'] |
21 | 8 | |
| 9 | @override_settings(AUTH_PROFILE_MODULE='') |
22 | 10 | def test_site_profile_not_available(self): |
23 | 11 | # calling get_profile without AUTH_PROFILE_MODULE set |
24 | | if hasattr(settings, 'AUTH_PROFILE_MODULE'): |
25 | | del settings.AUTH_PROFILE_MODULE |
26 | 12 | user = User.objects.get(username='testclient') |
27 | 13 | self.assertRaises(SiteProfileNotAvailable, user.get_profile) |
28 | 14 | |
| 15 | @override_settings(AUTH_PROFILE_MODULE='foobar') |
| 16 | def test_site_profile_bad_syntax(self): |
29 | 17 | # Bad syntax in AUTH_PROFILE_MODULE: |
30 | | settings.AUTH_PROFILE_MODULE = 'foobar' |
| 18 | user = User.objects.get(username='testclient') |
31 | 19 | self.assertRaises(SiteProfileNotAvailable, user.get_profile) |
32 | 20 | |
| 21 | @override_settings(AUTH_PROFILE_MODULE='foo.bar') |
| 22 | def test_site_profile_missing_module(self): |
33 | 23 | # module that doesn't exist |
34 | | settings.AUTH_PROFILE_MODULE = 'foo.bar' |
| 24 | user = User.objects.get(username='testclient') |
35 | 25 | self.assertRaises(SiteProfileNotAvailable, user.get_profile) |
diff --git a/django/contrib/auth/tests/remote_user.py b/django/contrib/auth/tests/remote_user.py
index fa32478..e821711 100644
a
|
b
|
from datetime import datetime
|
3 | 3 | from django.conf import settings |
4 | 4 | from django.contrib.auth.backends import RemoteUserBackend |
5 | 5 | from django.contrib.auth.models import User |
6 | | from django.test import TestCase |
| 6 | from django.test import DefaultSettingsTestCase |
| 7 | from django.test.utils import override_settings |
7 | 8 | from django.utils import timezone |
8 | 9 | |
9 | 10 | |
10 | | class RemoteUserTest(TestCase): |
| 11 | class RemoteUserTest(DefaultSettingsTestCase): |
11 | 12 | |
12 | 13 | urls = 'django.contrib.auth.tests.urls' |
13 | 14 | middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware' |
… |
… |
class RemoteUserTest(TestCase):
|
18 | 19 | known_user2 = 'knownuser2' |
19 | 20 | |
20 | 21 | def setUp(self): |
21 | | self.curr_middleware = settings.MIDDLEWARE_CLASSES |
22 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
23 | | settings.MIDDLEWARE_CLASSES += (self.middleware,) |
24 | | settings.AUTHENTICATION_BACKENDS = (self.backend,) |
| 22 | self.settings_override = override_settings( |
| 23 | MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES + (self.middleware,), |
| 24 | AUTHENTICATION_BACKENDS = (self.backend,), |
| 25 | ) |
| 26 | self.settings_override.enable() |
| 27 | |
| 28 | def tearDown(self): |
| 29 | self.settings_override.disable() |
25 | 30 | |
26 | 31 | def test_no_remote_user(self): |
27 | 32 | """ |
… |
… |
class RemoteUserTest(TestCase):
|
95 | 100 | response = self.client.get('/remote_user/', REMOTE_USER=self.known_user) |
96 | 101 | self.assertEqual(default_login, response.context['user'].last_login) |
97 | 102 | |
98 | | def tearDown(self): |
99 | | """Restores settings to avoid breaking other tests.""" |
100 | | settings.MIDDLEWARE_CLASSES = self.curr_middleware |
101 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
102 | | |
103 | 103 | |
104 | 104 | class RemoteUserNoCreateBackend(RemoteUserBackend): |
105 | 105 | """Backend that doesn't create unknown users.""" |
diff --git a/django/contrib/auth/tests/signals.py b/django/contrib/auth/tests/signals.py
index 3806021..4b507a1 100644
a
|
b
|
|
1 | | from django.test import TestCase |
2 | 1 | from django.contrib.auth import signals |
| 2 | from django.test import DefaultSettingsTestCase |
3 | 3 | |
4 | 4 | |
5 | | class SignalTestCase(TestCase): |
| 5 | class SignalTestCase(DefaultSettingsTestCase): |
6 | 6 | urls = 'django.contrib.auth.tests.urls' |
7 | 7 | fixtures = ['authtestdata.json'] |
8 | 8 | |
diff --git a/django/contrib/auth/tests/tokens.py b/django/contrib/auth/tests/tokens.py
index f7fc32e..b26646a 100644
a
|
b
|
from datetime import date, timedelta
|
3 | 3 | from django.conf import settings |
4 | 4 | from django.contrib.auth.models import User |
5 | 5 | from django.contrib.auth.tokens import PasswordResetTokenGenerator |
6 | | from django.test import TestCase |
| 6 | from django.test import DefaultSettingsTestCase |
7 | 7 | |
8 | 8 | |
9 | | class TokenGeneratorTest(TestCase): |
| 9 | class TokenGeneratorTest(DefaultSettingsTestCase): |
10 | 10 | |
11 | 11 | def test_make_token(self): |
12 | 12 | """ |
diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py
index 1975266..91d3196 100644
a
|
b
|
from django.contrib.auth.models import User
|
9 | 9 | from django.core import mail |
10 | 10 | from django.core.urlresolvers import reverse, NoReverseMatch |
11 | 11 | from django.http import QueryDict |
| 12 | from django.test import DefaultSettingsTestCase |
| 13 | from django.test.utils import override_settings |
12 | 14 | from django.utils.encoding import force_unicode |
13 | 15 | from django.utils.html import escape |
14 | | from django.test import TestCase |
15 | 16 | |
16 | 17 | from django.contrib.auth import SESSION_KEY, REDIRECT_FIELD_NAME |
17 | 18 | from django.contrib.auth.forms import (AuthenticationForm, PasswordChangeForm, |
18 | 19 | SetPasswordForm, PasswordResetForm) |
19 | 20 | |
20 | 21 | |
21 | | class AuthViewsTestCase(TestCase): |
| 22 | class AuthViewsTestCase(DefaultSettingsTestCase): |
22 | 23 | """ |
23 | 24 | Helper base class for all the follow test cases. |
24 | 25 | """ |
25 | 26 | fixtures = ['authtestdata.json'] |
26 | 27 | urls = 'django.contrib.auth.tests.urls' |
27 | 28 | |
28 | | def setUp(self): |
29 | | self.old_LANGUAGES = settings.LANGUAGES |
30 | | self.old_LANGUAGE_CODE = settings.LANGUAGE_CODE |
31 | | settings.LANGUAGES = (('en', 'English'),) |
32 | | settings.LANGUAGE_CODE = 'en' |
33 | | self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS |
34 | | settings.TEMPLATE_DIRS = ( |
35 | | os.path.join(os.path.dirname(__file__), 'templates'), |
36 | | ) |
37 | | |
38 | | def tearDown(self): |
39 | | settings.LANGUAGES = self.old_LANGUAGES |
40 | | settings.LANGUAGE_CODE = self.old_LANGUAGE_CODE |
41 | | settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS |
42 | | |
43 | 29 | def login(self, password='password'): |
44 | 30 | response = self.client.post('/login/', { |
45 | 31 | 'username': 'testclient', |
… |
… |
class AuthViewsTestCase(TestCase):
|
52 | 38 | def assertContainsEscaped(self, response, text, **kwargs): |
53 | 39 | return self.assertContains(response, escape(force_unicode(text)), **kwargs) |
54 | 40 | |
| 41 | AuthViewsTestCase = override_settings( |
| 42 | SITE_ID = 1, |
| 43 | TEMPLATE_DIRS = ( |
| 44 | os.path.join(os.path.dirname(__file__), 'templates'), |
| 45 | ), |
| 46 | )(AuthViewsTestCase) |
| 47 | |
55 | 48 | |
56 | 49 | class AuthViewNamedURLTests(AuthViewsTestCase): |
57 | 50 | urls = 'django.contrib.auth.urls' |
… |
… |
class ChangePasswordTest(AuthViewsTestCase):
|
192 | 185 | self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login']) |
193 | 186 | |
194 | 187 | def logout(self): |
195 | | response = self.client.get('/logout/') |
| 188 | self.client.get('/logout/') |
196 | 189 | |
197 | 190 | def test_password_change_fails_with_invalid_old_password(self): |
198 | 191 | self.login() |
… |
… |
class LoginTest(AuthViewsTestCase):
|
303 | 296 | |
304 | 297 | class LoginURLSettings(AuthViewsTestCase): |
305 | 298 | |
306 | | def setUp(self): |
307 | | super(LoginURLSettings, self).setUp() |
308 | | self.old_LOGIN_URL = settings.LOGIN_URL |
309 | | |
310 | | def tearDown(self): |
311 | | super(LoginURLSettings, self).tearDown() |
312 | | settings.LOGIN_URL = self.old_LOGIN_URL |
313 | | |
314 | | def get_login_required_url(self, login_url): |
315 | | settings.LOGIN_URL = login_url |
| 299 | def get_login_required_url(self, login=settings.LOGIN_URL): |
316 | 300 | response = self.client.get('/login_required/') |
317 | 301 | self.assertEqual(response.status_code, 302) |
318 | 302 | return response['Location'] |
319 | 303 | |
| 304 | @override_settings(LOGIN_URL='/login/') |
320 | 305 | def test_standard_login_url(self): |
321 | | login_url = '/login/' |
322 | | login_required_url = self.get_login_required_url(login_url) |
| 306 | login_required_url = self.get_login_required_url() |
323 | 307 | querystring = QueryDict('', mutable=True) |
324 | 308 | querystring['next'] = '/login_required/' |
325 | 309 | self.assertEqual(login_required_url, 'http://testserver%s?%s' % |
326 | | (login_url, querystring.urlencode('/'))) |
| 310 | (settings.LOGIN_URL, querystring.urlencode('/'))) |
327 | 311 | |
| 312 | @override_settings(LOGIN_URL='http://remote.example.com/login') |
328 | 313 | def test_remote_login_url(self): |
329 | | login_url = 'http://remote.example.com/login' |
330 | | login_required_url = self.get_login_required_url(login_url) |
| 314 | login_required_url = self.get_login_required_url() |
331 | 315 | querystring = QueryDict('', mutable=True) |
332 | 316 | querystring['next'] = 'http://testserver/login_required/' |
333 | 317 | self.assertEqual(login_required_url, |
334 | | '%s?%s' % (login_url, querystring.urlencode('/'))) |
| 318 | '%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/'))) |
335 | 319 | |
| 320 | @override_settings(LOGIN_URL='https:///login/') |
336 | 321 | def test_https_login_url(self): |
337 | | login_url = 'https:///login/' |
338 | | login_required_url = self.get_login_required_url(login_url) |
| 322 | login_required_url = self.get_login_required_url() |
339 | 323 | querystring = QueryDict('', mutable=True) |
340 | 324 | querystring['next'] = 'http://testserver/login_required/' |
341 | 325 | self.assertEqual(login_required_url, |
342 | | '%s?%s' % (login_url, querystring.urlencode('/'))) |
| 326 | '%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/'))) |
343 | 327 | |
| 328 | @override_settings(LOGIN_URL='/login/?pretty=1') |
344 | 329 | def test_login_url_with_querystring(self): |
345 | | login_url = '/login/?pretty=1' |
346 | | login_required_url = self.get_login_required_url(login_url) |
| 330 | login_required_url = self.get_login_required_url() |
347 | 331 | querystring = QueryDict('pretty=1', mutable=True) |
348 | 332 | querystring['next'] = '/login_required/' |
349 | 333 | self.assertEqual(login_required_url, 'http://testserver/login/?%s' % |
350 | 334 | querystring.urlencode('/')) |
351 | 335 | |
| 336 | @override_settings(LOGIN_URL='http://remote.example.com/login/?next=/default/') |
352 | 337 | def test_remote_login_url_with_next_querystring(self): |
353 | | login_url = 'http://remote.example.com/login/' |
354 | | login_required_url = self.get_login_required_url('%s?next=/default/' % |
355 | | login_url) |
| 338 | login_required_url = self.get_login_required_url() |
356 | 339 | querystring = QueryDict('', mutable=True) |
357 | 340 | querystring['next'] = 'http://testserver/login_required/' |
358 | | self.assertEqual(login_required_url, '%s?%s' % (login_url, |
359 | | querystring.urlencode('/'))) |
| 341 | self.assertEqual(login_required_url, settings.LOGIN_URL.replace( |
| 342 | 'next=/default/', querystring.urlencode('/'))) |
360 | 343 | |
361 | 344 | |
362 | 345 | class LogoutTest(AuthViewsTestCase): |
diff --git a/django/test/__init__.py b/django/test/__init__.py
index 21a4841..50d80ae 100644
a
|
b
|
Django Unit Test and Doctest framework.
|
4 | 4 | |
5 | 5 | from django.test.client import Client, RequestFactory |
6 | 6 | from django.test.testcases import (TestCase, TransactionTestCase, |
7 | | SimpleTestCase, LiveServerTestCase, skipIfDBFeature, |
8 | | skipUnlessDBFeature) |
| 7 | SimpleTestCase, DefaultSettingsTestCase, LiveServerTestCase, |
| 8 | skipIfDBFeature, skipUnlessDBFeature) |
9 | 9 | from django.test.utils import Approximate |
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 4de4750..16a22ef 100644
a
|
b
|
import socket
|
11 | 11 | import threading |
12 | 12 | import errno |
13 | 13 | |
14 | | from django.conf import settings |
| 14 | from django.conf import settings, global_settings, UserSettingsHolder |
15 | 15 | from django.contrib.staticfiles.handlers import StaticFilesHandler |
16 | 16 | from django.core import mail |
17 | 17 | from django.core.exceptions import ValidationError, ImproperlyConfigured |
… |
… |
from django.test.utils import (get_warnings_state, restore_warnings_state,
|
32 | 32 | override_settings) |
33 | 33 | from django.utils import simplejson, unittest as ut2 |
34 | 34 | from django.utils.encoding import smart_str |
| 35 | from django.utils.translation import deactivate |
35 | 36 | from django.views.static import serve |
36 | 37 | |
37 | 38 | __all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase', |
… |
… |
class TransactionTestCase(SimpleTestCase):
|
364 | 365 | ROOT_URLCONF with it. |
365 | 366 | * Clearing the mail test outbox. |
366 | 367 | """ |
| 368 | self._settings_setup() |
367 | 369 | self._fixture_setup() |
368 | 370 | self._urlconf_setup() |
369 | 371 | mail.outbox = [] |
370 | 372 | |
| 373 | def _settings_setup(self): |
| 374 | pass |
| 375 | |
371 | 376 | def _fixture_setup(self): |
372 | 377 | # If the test case has a multi_db=True flag, flush all databases. |
373 | 378 | # Otherwise, just flush default. |
… |
… |
class TransactionTestCase(SimpleTestCase):
|
428 | 433 | """ |
429 | 434 | self._fixture_teardown() |
430 | 435 | self._urlconf_teardown() |
| 436 | self._settings_teardown() |
431 | 437 | # Some DB cursors include SQL statements as part of cursor |
432 | 438 | # creation. If you have a test that does rollback, the effect |
433 | 439 | # of these statements is lost, which can effect the operation |
… |
… |
class TransactionTestCase(SimpleTestCase):
|
446 | 452 | settings.ROOT_URLCONF = self._old_root_urlconf |
447 | 453 | clear_url_caches() |
448 | 454 | |
| 455 | def _settings_teardown(self): |
| 456 | pass |
| 457 | |
449 | 458 | def assertRedirects(self, response, expected_url, status_code=302, |
450 | 459 | target_status_code=200, host=None, msg_prefix=''): |
451 | 460 | """Asserts that a response redirected to a specific URL, and that the |
… |
… |
class TestCase(TransactionTestCase):
|
720 | 729 | transaction.leave_transaction_management(using=db) |
721 | 730 | |
722 | 731 | |
| 732 | class DefaultSettingsTestCase(TestCase): |
| 733 | """ |
| 734 | A testcase class that reset settings to Django defaults for its tests |
| 735 | """ |
| 736 | def _settings_setup(self): |
| 737 | self.wrapped = settings._wrapped |
| 738 | holder = UserSettingsHolder(global_settings) |
| 739 | for setting_name in ('ROOT_URLCONF', 'EMAIL_BACKEND'): |
| 740 | setattr(holder, setting_name, getattr(settings, setting_name, "")) |
| 741 | settings._wrapped = holder |
| 742 | deactivate() |
| 743 | super(DefaultSettingsTestCase, self)._settings_setup() |
| 744 | |
| 745 | def _settings_teardown(self): |
| 746 | super(DefaultSettingsTestCase, self)._settings_teardown() |
| 747 | settings._wrapped = self.wrapped |
| 748 | # Damned cached global variables :-( |
| 749 | from django.template import base |
| 750 | base.templatetags_modules = [] |
| 751 | |
| 752 | |
723 | 753 | def _deferredSkip(condition, reason): |
724 | 754 | def decorator(test_func): |
725 | 755 | if not (isinstance(test_func, type) and |
diff --git a/django/test/utils.py b/django/test/utils.py
index 87f2311..421a726 100644
a
|
b
|
class override_settings(object):
|
196 | 196 | def __call__(self, test_func): |
197 | 197 | from django.test import TransactionTestCase |
198 | 198 | if isinstance(test_func, type) and issubclass(test_func, TransactionTestCase): |
199 | | original_pre_setup = test_func._pre_setup |
200 | | original_post_teardown = test_func._post_teardown |
201 | | def _pre_setup(innerself): |
| 199 | original_settings_setup = test_func._settings_setup |
| 200 | original_settings_teardown = test_func._settings_teardown |
| 201 | def _settings_setup(innerself): |
| 202 | original_settings_setup(innerself) |
202 | 203 | self.enable() |
203 | | original_pre_setup(innerself) |
204 | | def _post_teardown(innerself): |
205 | | original_post_teardown(innerself) |
| 204 | def _settings_teardown(innerself): |
206 | 205 | self.disable() |
207 | | test_func._pre_setup = _pre_setup |
208 | | test_func._post_teardown = _post_teardown |
| 206 | original_settings_teardown(innerself) |
| 207 | test_func._settings_setup = _settings_setup |
| 208 | test_func._settings_teardown = _settings_teardown |
209 | 209 | return test_func |
210 | 210 | else: |
211 | 211 | @wraps(test_func) |