Ticket #17194: 17194-1.diff

File 17194-1.diff, 6.1 KB (added by Claude Paroz, 12 years ago)

Fix language isolation of contrib.auth tests

  • django/contrib/auth/tests/forms.py

    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  
    44from django.contrib.auth.models import User
    55from django.contrib.auth.forms import UserCreationForm, AuthenticationForm,  PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm
    66from django.test import TestCase
     7from django.test.utils import override_settings
    78
    89
    9 class UserCreationFormTest(TestCase):
    10 
     10class FormTestCase(TestCase):
    1111    fixtures = ['authtestdata.json']
    1212
     13FormTestCase = override_settings(
     14    LANGUAGE_CODE = 'en',
     15)(FormTestCase)
     16
     17
     18class UserCreationFormTest(FormTestCase):
     19
    1320    def test_user_already_exists(self):
    1421        data = {
    1522            'username': 'testclient',
    class UserCreationFormTest(TestCase):  
    7784        self.assertEqual(repr(u), '<User: jsmith@example.com>')
    7885
    7986
    80 class AuthenticationFormTest(TestCase):
    81 
    82     fixtures = ['authtestdata.json']
     87class AuthenticationFormTest(FormTestCase):
    8388
    8489    def test_invalid_username(self):
    8590        # The user submits an invalid username.
    class AuthenticationFormTest(TestCase):  
    116121        self.assertEqual(form.non_field_errors(), [])
    117122
    118123
    119 class SetPasswordFormTest(TestCase):
    120 
    121     fixtures = ['authtestdata.json']
     124class SetPasswordFormTest(FormTestCase):
    122125
    123126    def test_password_verification(self):
    124127        # The two new passwords do not match.
    class SetPasswordFormTest(TestCase):  
    142145        self.assertTrue(form.is_valid())
    143146
    144147
    145 class PasswordChangeFormTest(TestCase):
    146 
    147     fixtures = ['authtestdata.json']
     148class PasswordChangeFormTest(FormTestCase):
    148149
    149150    def test_incorrect_password(self):
    150151        user = User.objects.get(username='testclient')
    class PasswordChangeFormTest(TestCase):  
    190191        self.assertEqual(PasswordChangeForm(user, {}).fields.keys(),
    191192                         ['old_password', 'new_password1', 'new_password2'])
    192193
    193 class UserChangeFormTest(TestCase):
    194194
    195     fixtures = ['authtestdata.json']
     195class UserChangeFormTest(FormTestCase):
    196196
    197197    def test_username_validity(self):
    198198        user = User.objects.get(username='testclient')
    class UserChangeFormTest(TestCase):  
    218218        form = MyUserForm({})
    219219
    220220
    221 class PasswordResetFormTest(TestCase):
    222 
    223     fixtures = ['authtestdata.json']
     221class PasswordResetFormTest(FormTestCase):
    224222
    225223    def create_dummy_user(self):
    226224        """creates a user and returns a tuple
  • django/contrib/auth/tests/views.py

    diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py
    index 9fccb3e..1da772e 100644
    a b import urllib  
    66from django.conf import settings
    77from django.contrib.auth import SESSION_KEY, REDIRECT_FIELD_NAME
    88from django.contrib.auth.forms import AuthenticationForm
    9 from django.contrib.sites.models import Site, RequestSite
    109from django.contrib.auth.models import User
    11 from django.core.urlresolvers import NoReverseMatch
    12 from django.test import TestCase
     10from django.contrib.sites.models import Site, RequestSite
    1311from django.core import mail
    14 from django.core.urlresolvers import reverse
     12from django.core.urlresolvers import reverse, NoReverseMatch
    1513from django.http import QueryDict
    16 
     14from django.test import TestCase
     15from django.test.utils import override_settings
    1716
    1817class AuthViewsTestCase(TestCase):
    1918    """
    class AuthViewsTestCase(TestCase):  
    2221    fixtures = ['authtestdata.json']
    2322    urls = 'django.contrib.auth.tests.urls'
    2423
    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 
    4024    def login(self, password='password'):
    4125        response = self.client.post('/login/', {
    4226            'username': 'testclient',
    class AuthViewsTestCase(TestCase):  
    4731        self.assertTrue(response['Location'].endswith(settings.LOGIN_REDIRECT_URL))
    4832        self.assertTrue(SESSION_KEY in self.client.session)
    4933
     34AuthViewsTestCase = 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
    5042
    5143class AuthViewNamedURLTests(AuthViewsTestCase):
    5244    urls = 'django.contrib.auth.urls'
  • django/test/utils.py

    diff --git a/django/test/utils.py b/django/test/utils.py
    index 87f2311..dbfac7c 100644
    a b from django.core import mail  
    66from django.test.signals import template_rendered, setting_changed
    77from django.template import Template, loader, TemplateDoesNotExist
    88from django.template.loaders import cached
    9 from django.utils.translation import deactivate
     9from django.utils.translation import activate, deactivate
    1010from django.utils.functional import wraps
    1111
    1212
    class override_settings(object):  
    186186    def __init__(self, **kwargs):
    187187        self.options = kwargs
    188188        self.wrapped = settings._wrapped
     189        self.old_language_code = None
    189190
    190191    def __enter__(self):
    191192        self.enable()
    class override_settings(object):  
    218219        override = OverrideSettingsHolder(settings._wrapped)
    219220        for key, new_value in self.options.items():
    220221            setattr(override, key, new_value)
     222            if key == 'LANGUAGE_CODE':
     223                self.old_language_code = settings._wrapped.LANGUAGE_CODE
     224                activate(new_value)
    221225        settings._wrapped = override
    222226
    223227    def disable(self):
    224228        settings._wrapped = self.wrapped
     229        if self.old_language_code is not None:
     230            activate(self.old_language_code)
Back to Top