diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py
index afef0e7..d203fc8 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()), True) |
… |
… |
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 9f94c2a..f44be08 100644
a
|
b
|
|
1 | | from django.test import TestCase |
2 | | from django.utils.unittest import skipUnless |
3 | | from django.contrib.auth.models import User, AnonymousUser |
| 1 | from StringIO import StringIO |
| 2 | |
4 | 3 | from django.contrib.auth import utils |
| 4 | from django.contrib.auth.models import User, AnonymousUser |
5 | 5 | from django.core.management import call_command |
6 | | from StringIO import StringIO |
| 6 | from django.test import DefaultSettingsTestCase |
| 7 | from django.utils.unittest import skipUnless |
7 | 8 | |
8 | 9 | try: |
9 | 10 | import crypt as crypt_module |
… |
… |
except ImportError:
|
11 | 12 | crypt_module = None |
12 | 13 | |
13 | 14 | |
14 | | class BasicTestCase(TestCase): |
| 15 | class BasicTestCase(DefaultSettingsTestCase): |
15 | 16 | def test_user(self): |
16 | 17 | "Check that users can be created and can set their password" |
17 | 18 | u = User.objects.create_user('testuser', 'test@example.com', 'testpw') |
… |
… |
class BasicTestCase(TestCase):
|
113 | 114 | self.assertFalse(u.has_usable_password()) |
114 | 115 | |
115 | 116 | |
116 | | class PasswordUtilsTestCase(TestCase): |
| 117 | class PasswordUtilsTestCase(DefaultSettingsTestCase): |
117 | 118 | |
118 | 119 | def _test_make_password(self, algo): |
119 | 120 | password = utils.make_password(algo, "foobar") |
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 429967c..85f7d4b 100644
a
|
b
|
import os
|
3 | 3 | from django.core import mail |
4 | 4 | from django.contrib.auth.models import User |
5 | 5 | from django.contrib.auth.forms import UserCreationForm, AuthenticationForm, PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm |
6 | | from django.test import TestCase |
| 6 | from django.test import DefaultSettingsTestCase |
7 | 7 | |
8 | 8 | |
9 | | class UserCreationFormTest(TestCase): |
| 9 | class UserCreationFormTest(DefaultSettingsTestCase): |
10 | 10 | |
11 | 11 | fixtures = ['authtestdata.json'] |
12 | 12 | |
… |
… |
class UserCreationFormTest(TestCase):
|
77 | 77 | self.assertEqual(repr(u), '<User: jsmith@example.com>') |
78 | 78 | |
79 | 79 | |
80 | | class AuthenticationFormTest(TestCase): |
| 80 | class AuthenticationFormTest(DefaultSettingsTestCase): |
81 | 81 | |
82 | 82 | fixtures = ['authtestdata.json'] |
83 | 83 | |
… |
… |
class AuthenticationFormTest(TestCase):
|
116 | 116 | self.assertEqual(form.non_field_errors(), []) |
117 | 117 | |
118 | 118 | |
119 | | class SetPasswordFormTest(TestCase): |
| 119 | class SetPasswordFormTest(DefaultSettingsTestCase): |
120 | 120 | |
121 | 121 | fixtures = ['authtestdata.json'] |
122 | 122 | |
… |
… |
class SetPasswordFormTest(TestCase):
|
142 | 142 | self.assertTrue(form.is_valid()) |
143 | 143 | |
144 | 144 | |
145 | | class PasswordChangeFormTest(TestCase): |
| 145 | class PasswordChangeFormTest(DefaultSettingsTestCase): |
146 | 146 | |
147 | 147 | fixtures = ['authtestdata.json'] |
148 | 148 | |
… |
… |
class PasswordChangeFormTest(TestCase):
|
190 | 190 | self.assertEqual(PasswordChangeForm(user, {}).fields.keys(), |
191 | 191 | ['old_password', 'new_password1', 'new_password2']) |
192 | 192 | |
193 | | class UserChangeFormTest(TestCase): |
| 193 | class UserChangeFormTest(DefaultSettingsTestCase): |
194 | 194 | |
195 | 195 | fixtures = ['authtestdata.json'] |
196 | 196 | |
… |
… |
class UserChangeFormTest(TestCase):
|
218 | 218 | form = MyUserForm({}) |
219 | 219 | |
220 | 220 | |
221 | | class PasswordResetFormTest(TestCase): |
| 221 | class PasswordResetFormTest(DefaultSettingsTestCase): |
222 | 222 | |
223 | 223 | fixtures = ['authtestdata.json'] |
224 | 224 | |
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 6ca6a2b..201a434 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 | |
8 | 9 | |
9 | | class RemoteUserTest(TestCase): |
| 10 | class RemoteUserTest(DefaultSettingsTestCase): |
10 | 11 | |
11 | 12 | urls = 'django.contrib.auth.tests.urls' |
12 | 13 | middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware' |
… |
… |
class RemoteUserTest(TestCase):
|
17 | 18 | known_user2 = 'knownuser2' |
18 | 19 | |
19 | 20 | def setUp(self): |
20 | | self.curr_middleware = settings.MIDDLEWARE_CLASSES |
21 | | self.curr_auth = settings.AUTHENTICATION_BACKENDS |
22 | | settings.MIDDLEWARE_CLASSES += (self.middleware,) |
23 | | settings.AUTHENTICATION_BACKENDS = (self.backend,) |
| 21 | self.settings_override = override_settings( |
| 22 | MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES + (self.middleware,), |
| 23 | AUTHENTICATION_BACKENDS = (self.backend,), |
| 24 | ) |
| 25 | self.settings_override.enable() |
| 26 | |
| 27 | def tearDown(self): |
| 28 | self.settings_override.disable() |
24 | 29 | |
25 | 30 | def test_no_remote_user(self): |
26 | 31 | """ |
… |
… |
class RemoteUserTest(TestCase):
|
92 | 97 | response = self.client.get('/remote_user/', REMOTE_USER=self.known_user) |
93 | 98 | self.assertEqual(default_login, response.context['user'].last_login) |
94 | 99 | |
95 | | def tearDown(self): |
96 | | """Restores settings to avoid breaking other tests.""" |
97 | | settings.MIDDLEWARE_CLASSES = self.curr_middleware |
98 | | settings.AUTHENTICATION_BACKENDS = self.curr_auth |
99 | | |
100 | 100 | |
101 | 101 | class RemoteUserNoCreateBackend(RemoteUserBackend): |
102 | 102 | """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 9fccb3e..da598b0 100644
a
|
b
|
import urllib
|
6 | 6 | from django.conf import settings |
7 | 7 | from django.contrib.auth import SESSION_KEY, REDIRECT_FIELD_NAME |
8 | 8 | from django.contrib.auth.forms import AuthenticationForm |
9 | | from django.contrib.sites.models import Site, RequestSite |
10 | 9 | from django.contrib.auth.models import User |
11 | | from django.core.urlresolvers import NoReverseMatch |
12 | | from django.test import TestCase |
| 10 | from django.contrib.sites.models import Site, RequestSite |
13 | 11 | from django.core import mail |
14 | | from django.core.urlresolvers import reverse |
| 12 | from django.core.urlresolvers import reverse, NoReverseMatch |
15 | 13 | from django.http import QueryDict |
| 14 | from django.test import DefaultSettingsTestCase |
| 15 | from django.test.utils import override_settings |
16 | 16 | |
17 | 17 | |
18 | | class AuthViewsTestCase(TestCase): |
| 18 | class AuthViewsTestCase(DefaultSettingsTestCase): |
19 | 19 | """ |
20 | 20 | Helper base class for all the follow test cases. |
21 | 21 | """ |
22 | 22 | fixtures = ['authtestdata.json'] |
23 | 23 | urls = 'django.contrib.auth.tests.urls' |
24 | 24 | |
25 | | def setUp(self): |
26 | | self.old_LANGUAGES = settings.LANGUAGES |
27 | | self.old_LANGUAGE_CODE = settings.LANGUAGE_CODE |
28 | | settings.LANGUAGES = (('en', 'English'),) |
29 | | settings.LANGUAGE_CODE = 'en' |
30 | | self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS |
31 | | settings.TEMPLATE_DIRS = ( |
32 | | os.path.join(os.path.dirname(__file__), 'templates'), |
33 | | ) |
34 | | |
35 | | def tearDown(self): |
36 | | settings.LANGUAGES = self.old_LANGUAGES |
37 | | settings.LANGUAGE_CODE = self.old_LANGUAGE_CODE |
38 | | settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS |
39 | | |
40 | 25 | def login(self, password='password'): |
41 | 26 | response = self.client.post('/login/', { |
42 | 27 | 'username': 'testclient', |
… |
… |
class AuthViewsTestCase(TestCase):
|
47 | 32 | self.assertTrue(response['Location'].endswith(settings.LOGIN_REDIRECT_URL)) |
48 | 33 | self.assertTrue(SESSION_KEY in self.client.session) |
49 | 34 | |
| 35 | AuthViewsTestCase = override_settings( |
| 36 | SITE_ID = 1, |
| 37 | TEMPLATE_DIRS = ( |
| 38 | os.path.join(os.path.dirname(__file__), 'templates'), |
| 39 | ), |
| 40 | )(AuthViewsTestCase) |
| 41 | |
50 | 42 | |
51 | 43 | class AuthViewNamedURLTests(AuthViewsTestCase): |
52 | 44 | urls = 'django.contrib.auth.urls' |
… |
… |
class PasswordResetTest(AuthViewsTestCase):
|
145 | 137 | url, path = self._test_confirm_start() |
146 | 138 | path = path[:-5] + ("0"*4) + path[-1] |
147 | 139 | |
148 | | response = self.client.post(path, {'new_password1': 'anewpassword', |
149 | | 'new_password2':' anewpassword'}) |
| 140 | self.client.post(path, {'new_password1': 'anewpassword', |
| 141 | 'new_password2':' anewpassword'}) |
150 | 142 | # Check the password has not been changed |
151 | 143 | u = User.objects.get(email='staffmember@example.com') |
152 | 144 | self.assertTrue(not u.check_password("anewpassword")) |
… |
… |
class ChangePasswordTest(AuthViewsTestCase):
|
185 | 177 | self.assertTrue("Please enter a correct username and password. Note that both fields are case-sensitive." in response.content) |
186 | 178 | |
187 | 179 | def logout(self): |
188 | | response = self.client.get('/logout/') |
| 180 | self.client.get('/logout/') |
189 | 181 | |
190 | 182 | def test_password_change_fails_with_invalid_old_password(self): |
191 | 183 | self.login() |
… |
… |
class LoginTest(AuthViewsTestCase):
|
304 | 296 | class LoginURLSettings(AuthViewsTestCase): |
305 | 297 | urls = 'django.contrib.auth.tests.urls' |
306 | 298 | |
307 | | def setUp(self): |
308 | | super(LoginURLSettings, self).setUp() |
309 | | self.old_LOGIN_URL = settings.LOGIN_URL |
310 | | |
311 | | def tearDown(self): |
312 | | super(LoginURLSettings, self).tearDown() |
313 | | settings.LOGIN_URL = self.old_LOGIN_URL |
314 | | |
315 | | def get_login_required_url(self, login_url): |
316 | | settings.LOGIN_URL = login_url |
| 299 | def get_login_required_url(self, login=settings.LOGIN_URL): |
317 | 300 | response = self.client.get('/login_required/') |
318 | 301 | self.assertEqual(response.status_code, 302) |
319 | 302 | return response['Location'] |
320 | 303 | |
| 304 | @override_settings(LOGIN_URL='/login/') |
321 | 305 | def test_standard_login_url(self): |
322 | | login_url = '/login/' |
323 | | login_required_url = self.get_login_required_url(login_url) |
| 306 | login_required_url = self.get_login_required_url() |
324 | 307 | querystring = QueryDict('', mutable=True) |
325 | 308 | querystring['next'] = '/login_required/' |
326 | 309 | self.assertEqual(login_required_url, |
327 | | 'http://testserver%s?%s' % (login_url, querystring.urlencode('/'))) |
| 310 | 'http://testserver%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/'))) |
328 | 311 | |
| 312 | @override_settings(LOGIN_URL='http://remote.example.com/login') |
329 | 313 | def test_remote_login_url(self): |
330 | | login_url = 'http://remote.example.com/login' |
331 | | login_required_url = self.get_login_required_url(login_url) |
| 314 | login_required_url = self.get_login_required_url() |
332 | 315 | querystring = QueryDict('', mutable=True) |
333 | 316 | querystring['next'] = 'http://testserver/login_required/' |
334 | 317 | self.assertEqual(login_required_url, |
335 | | '%s?%s' % (login_url, querystring.urlencode('/'))) |
| 318 | '%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/'))) |
336 | 319 | |
| 320 | @override_settings(LOGIN_URL='https:///login/') |
337 | 321 | def test_https_login_url(self): |
338 | | login_url = 'https:///login/' |
339 | | login_required_url = self.get_login_required_url(login_url) |
| 322 | login_required_url = self.get_login_required_url() |
340 | 323 | querystring = QueryDict('', mutable=True) |
341 | 324 | querystring['next'] = 'http://testserver/login_required/' |
342 | 325 | self.assertEqual(login_required_url, |
343 | | '%s?%s' % (login_url, querystring.urlencode('/'))) |
| 326 | '%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/'))) |
344 | 327 | |
| 328 | @override_settings(LOGIN_URL='/login/?pretty=1') |
345 | 329 | def test_login_url_with_querystring(self): |
346 | | login_url = '/login/?pretty=1' |
347 | | login_required_url = self.get_login_required_url(login_url) |
| 330 | login_required_url = self.get_login_required_url() |
348 | 331 | querystring = QueryDict('pretty=1', mutable=True) |
349 | 332 | querystring['next'] = '/login_required/' |
350 | 333 | self.assertEqual(login_required_url, 'http://testserver/login/?%s' % |
351 | 334 | querystring.urlencode('/')) |
352 | 335 | |
| 336 | @override_settings(LOGIN_URL='http://remote.example.com/login/?next=/default/') |
353 | 337 | def test_remote_login_url_with_next_querystring(self): |
354 | | login_url = 'http://remote.example.com/login/' |
355 | | login_required_url = self.get_login_required_url('%s?next=/default/' % |
356 | | login_url) |
| 338 | login_required_url = self.get_login_required_url() |
357 | 339 | querystring = QueryDict('', mutable=True) |
358 | 340 | querystring['next'] = 'http://testserver/login_required/' |
359 | | self.assertEqual(login_required_url, '%s?%s' % (login_url, |
360 | | querystring.urlencode('/'))) |
| 341 | self.assertEqual(login_required_url, settings.LOGIN_URL.replace( |
| 342 | 'next=/default/', querystring.urlencode('/'))) |
361 | 343 | |
362 | 344 | |
363 | 345 | class LogoutTest(AuthViewsTestCase): |
diff --git a/django/test/__init__.py b/django/test/__init__.py
index a3a03e3..df75a21 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, skipIfDBFeature, skipUnlessDBFeature) |
| 7 | SimpleTestCase, DefaultSettingsTestCase, skipIfDBFeature, skipUnlessDBFeature) |
8 | 8 | from django.test.utils import Approximate |
diff --git a/django/test/testcases.py b/django/test/testcases.py
index ee22ac2..b9b851f 100644
a
|
b
|
from functools import wraps
|
6 | 6 | from urlparse import urlsplit, urlunsplit |
7 | 7 | from xml.dom.minidom import parseString, Node |
8 | 8 | |
9 | | from django.conf import settings |
| 9 | from django.conf import settings, global_settings, UserSettingsHolder |
10 | 10 | from django.core import mail |
11 | 11 | from django.core.exceptions import ValidationError |
12 | 12 | from django.core.management import call_command |
… |
… |
from django.test.utils import (get_warnings_state, restore_warnings_state,
|
23 | 23 | override_settings) |
24 | 24 | from django.utils import simplejson, unittest as ut2 |
25 | 25 | from django.utils.encoding import smart_str |
| 26 | from django.utils.translation import deactivate |
26 | 27 | |
27 | 28 | __all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase', |
28 | 29 | 'SimpleTestCase', 'skipIfDBFeature', 'skipUnlessDBFeature') |
… |
… |
class TransactionTestCase(SimpleTestCase):
|
350 | 351 | ROOT_URLCONF with it. |
351 | 352 | * Clearing the mail test outbox. |
352 | 353 | """ |
| 354 | self._settings_setup() |
353 | 355 | self._fixture_setup() |
354 | 356 | self._urlconf_setup() |
355 | 357 | mail.outbox = [] |
356 | 358 | |
| 359 | def _settings_setup(self): |
| 360 | pass |
| 361 | |
357 | 362 | def _fixture_setup(self): |
358 | 363 | # If the test case has a multi_db=True flag, flush all databases. |
359 | 364 | # Otherwise, just flush default. |
… |
… |
class TransactionTestCase(SimpleTestCase):
|
414 | 419 | """ |
415 | 420 | self._fixture_teardown() |
416 | 421 | self._urlconf_teardown() |
| 422 | self._settings_teardown() |
417 | 423 | # Some DB cursors include SQL statements as part of cursor |
418 | 424 | # creation. If you have a test that does rollback, the effect |
419 | 425 | # of these statements is lost, which can effect the operation |
… |
… |
class TransactionTestCase(SimpleTestCase):
|
432 | 438 | settings.ROOT_URLCONF = self._old_root_urlconf |
433 | 439 | clear_url_caches() |
434 | 440 | |
| 441 | def _settings_teardown(self): |
| 442 | pass |
| 443 | |
435 | 444 | def assertRedirects(self, response, expected_url, status_code=302, |
436 | 445 | target_status_code=200, host=None, msg_prefix=''): |
437 | 446 | """Asserts that a response redirected to a specific URL, and that the |
… |
… |
class TestCase(TransactionTestCase):
|
703 | 712 | transaction.rollback(using=db) |
704 | 713 | transaction.leave_transaction_management(using=db) |
705 | 714 | |
| 715 | |
| 716 | class DefaultSettingsTestCase(TestCase): |
| 717 | """ |
| 718 | A testcase class that reset settings to Django defaults for its tests |
| 719 | """ |
| 720 | def _settings_setup(self): |
| 721 | self.wrapped = settings._wrapped |
| 722 | holder = UserSettingsHolder(global_settings) |
| 723 | for setting_name in ('ROOT_URLCONF', 'EMAIL_BACKEND'): |
| 724 | setattr(holder, setting_name, getattr(settings, setting_name, "")) |
| 725 | settings._wrapped = holder |
| 726 | deactivate() |
| 727 | super(DefaultSettingsTestCase, self)._settings_setup() |
| 728 | |
| 729 | def _settings_teardown(self): |
| 730 | super(DefaultSettingsTestCase, self)._settings_teardown() |
| 731 | settings._wrapped = self.wrapped |
| 732 | # Damned cached global variables :-( |
| 733 | from django.template import base |
| 734 | base.templatetags_modules = [] |
| 735 | |
| 736 | |
706 | 737 | def _deferredSkip(condition, reason): |
707 | 738 | def decorator(test_func): |
708 | 739 | 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) |