diff --git a/django/contrib/auth/tests/forms.py b/django/contrib/auth/tests/forms.py
index 429967c..4638a96 100644
a
|
b
|
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 | 6 | from django.test import TestCase |
| 7 | from django.test.utils import override_settings |
7 | 8 | |
8 | 9 | |
9 | | class UserCreationFormTest(TestCase): |
10 | | |
| 10 | class FormTestCase(TestCase): |
11 | 11 | fixtures = ['authtestdata.json'] |
12 | 12 | |
| 13 | FormTestCase = override_settings( |
| 14 | LANGUAGE_CODE = 'en', |
| 15 | )(FormTestCase) |
| 16 | |
| 17 | |
| 18 | class UserCreationFormTest(FormTestCase): |
| 19 | |
13 | 20 | def test_user_already_exists(self): |
14 | 21 | data = { |
15 | 22 | 'username': 'testclient', |
… |
… |
class UserCreationFormTest(TestCase):
|
77 | 84 | self.assertEqual(repr(u), '<User: jsmith@example.com>') |
78 | 85 | |
79 | 86 | |
80 | | class AuthenticationFormTest(TestCase): |
81 | | |
82 | | fixtures = ['authtestdata.json'] |
| 87 | class AuthenticationFormTest(FormTestCase): |
83 | 88 | |
84 | 89 | def test_invalid_username(self): |
85 | 90 | # The user submits an invalid username. |
… |
… |
class AuthenticationFormTest(TestCase):
|
116 | 121 | self.assertEqual(form.non_field_errors(), []) |
117 | 122 | |
118 | 123 | |
119 | | class SetPasswordFormTest(TestCase): |
120 | | |
121 | | fixtures = ['authtestdata.json'] |
| 124 | class SetPasswordFormTest(FormTestCase): |
122 | 125 | |
123 | 126 | def test_password_verification(self): |
124 | 127 | # The two new passwords do not match. |
… |
… |
class SetPasswordFormTest(TestCase):
|
142 | 145 | self.assertTrue(form.is_valid()) |
143 | 146 | |
144 | 147 | |
145 | | class PasswordChangeFormTest(TestCase): |
146 | | |
147 | | fixtures = ['authtestdata.json'] |
| 148 | class PasswordChangeFormTest(FormTestCase): |
148 | 149 | |
149 | 150 | def test_incorrect_password(self): |
150 | 151 | user = User.objects.get(username='testclient') |
… |
… |
class PasswordChangeFormTest(TestCase):
|
190 | 191 | self.assertEqual(PasswordChangeForm(user, {}).fields.keys(), |
191 | 192 | ['old_password', 'new_password1', 'new_password2']) |
192 | 193 | |
193 | | class UserChangeFormTest(TestCase): |
194 | 194 | |
195 | | fixtures = ['authtestdata.json'] |
| 195 | class UserChangeFormTest(FormTestCase): |
196 | 196 | |
197 | 197 | def test_username_validity(self): |
198 | 198 | user = User.objects.get(username='testclient') |
… |
… |
class UserChangeFormTest(TestCase):
|
218 | 218 | form = MyUserForm({}) |
219 | 219 | |
220 | 220 | |
221 | | class PasswordResetFormTest(TestCase): |
222 | | |
223 | | fixtures = ['authtestdata.json'] |
| 221 | class PasswordResetFormTest(FormTestCase): |
224 | 222 | |
225 | 223 | def create_dummy_user(self): |
226 | 224 | """creates a user and returns a tuple |
diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py
index 9fccb3e..1da772e 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 |
16 | | |
| 14 | from django.test import TestCase |
| 15 | from django.test.utils import override_settings |
17 | 16 | |
18 | 17 | class AuthViewsTestCase(TestCase): |
19 | 18 | """ |
… |
… |
class AuthViewsTestCase(TestCase):
|
22 | 21 | fixtures = ['authtestdata.json'] |
23 | 22 | urls = 'django.contrib.auth.tests.urls' |
24 | 23 | |
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 | 24 | def login(self, password='password'): |
41 | 25 | response = self.client.post('/login/', { |
42 | 26 | 'username': 'testclient', |
… |
… |
class AuthViewsTestCase(TestCase):
|
47 | 31 | self.assertTrue(response['Location'].endswith(settings.LOGIN_REDIRECT_URL)) |
48 | 32 | self.assertTrue(SESSION_KEY in self.client.session) |
49 | 33 | |
| 34 | AuthViewsTestCase = override_settings( |
| 35 | LANGUAGES = (('en', 'English'),), |
| 36 | LANGUAGE_CODE = 'en', |
| 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' |
diff --git a/django/test/utils.py b/django/test/utils.py
index 87f2311..dbfac7c 100644
a
|
b
|
from django.core import mail
|
6 | 6 | from django.test.signals import template_rendered, setting_changed |
7 | 7 | from django.template import Template, loader, TemplateDoesNotExist |
8 | 8 | from django.template.loaders import cached |
9 | | from django.utils.translation import deactivate |
| 9 | from django.utils.translation import activate, deactivate |
10 | 10 | from django.utils.functional import wraps |
11 | 11 | |
12 | 12 | |
… |
… |
class override_settings(object):
|
186 | 186 | def __init__(self, **kwargs): |
187 | 187 | self.options = kwargs |
188 | 188 | self.wrapped = settings._wrapped |
| 189 | self.old_language_code = None |
189 | 190 | |
190 | 191 | def __enter__(self): |
191 | 192 | self.enable() |
… |
… |
class override_settings(object):
|
218 | 219 | override = OverrideSettingsHolder(settings._wrapped) |
219 | 220 | for key, new_value in self.options.items(): |
220 | 221 | setattr(override, key, new_value) |
| 222 | if key == 'LANGUAGE_CODE': |
| 223 | self.old_language_code = settings._wrapped.LANGUAGE_CODE |
| 224 | activate(new_value) |
221 | 225 | settings._wrapped = override |
222 | 226 | |
223 | 227 | def disable(self): |
224 | 228 | settings._wrapped = self.wrapped |
| 229 | if self.old_language_code is not None: |
| 230 | activate(self.old_language_code) |