Ticket #13394: default_settings.diff

File default_settings.diff, 29.5 KB (added by Claude Paroz, 12 years ago)

Use a new DefaultSettingsTestCase

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

    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  
    22from django.contrib.auth.models import User, Group, Permission, AnonymousUser
    33from django.contrib.contenttypes.models import ContentType
    44from django.core.exceptions import ImproperlyConfigured
    5 from django.test import TestCase
     5from django.test import DefaultSettingsTestCase
     6from django.test.utils import override_settings
    67
    78
    8 class BackendTest(TestCase):
     9class CustomBackendTestCase(DefaultSettingsTestCase):
     10    def setUp(self):
     11        self.settings_override = override_settings(
     12            AUTHENTICATION_BACKENDS=(self.backend,)
     13        )
     14        self.settings_override.enable()
    915
    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()
    1122
     23
     24class AdditionalBackendTestCase(DefaultSettingsTestCase):
    1225    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()
    1730
    1831    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
    2134        # be cached; flush the cache to ensure there are no side effects
    2235        # Refs #14975, #14925
    2336        ContentType.objects.clear_cache()
    2437
     38
     39class 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
    2548    def test_has_perm(self):
    2649        user = User.objects.get(username='test')
    2750        self.assertEqual(user.has_perm('auth.test'), False)
    class SimpleRowlevelBackend(object):  
    154177            return ['none']
    155178
    156179
    157 class RowlevelBackendTest(TestCase):
     180class RowlevelBackendTest(AdditionalBackendTestCase):
    158181    """
    159182    Tests for auth backend that supports object level permissions
    160183    """
    161184    backend = 'django.contrib.auth.tests.auth_backends.SimpleRowlevelBackend'
    162185
    163186    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()
    166188        self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
    167189        self.user2 = User.objects.create_user('test2', 'test2@example.com', 'test')
    168190        self.user3 = User.objects.create_user('test3', 'test3@example.com', 'test')
    169191
    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 
    177192    def test_has_perm(self):
    178193        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
    179194        self.assertEqual(self.user2.has_perm('perm', TestObj()), True)
    class RowlevelBackendTest(TestCase):  
    189204        self.assertEqual(self.user2.get_all_permissions(), set([]))
    190205
    191206    def test_get_group_permissions(self):
    192         content_type=ContentType.objects.get_for_model(Group)
    193207        group = Group.objects.create(name='test_group')
    194208        self.user3.groups.add(group)
    195209        self.assertEqual(self.user3.get_group_permissions(TestObj()), set(['group_perm']))
    class AnonymousUserBackend(SimpleRowlevelBackend):  
    199213    supports_inactive_user = False
    200214
    201215
    202 class AnonymousUserBackendTest(TestCase):
     216class AnonymousUserBackendTest(CustomBackendTestCase):
    203217    """
    204218    Tests for AnonymousUser delegating to backend.
    205219    """
    class AnonymousUserBackendTest(TestCase):  
    207221    backend = 'django.contrib.auth.tests.auth_backends.AnonymousUserBackend'
    208222
    209223    def setUp(self):
    210         self.curr_auth = settings.AUTHENTICATION_BACKENDS
    211         settings.AUTHENTICATION_BACKENDS = (self.backend,)
     224        super(AnonymousUserBackendTest, self).setUp()
    212225        self.user1 = AnonymousUser()
    213226
    214     def tearDown(self):
    215         settings.AUTHENTICATION_BACKENDS = self.curr_auth
    216 
    217227    def test_has_perm(self):
    218228        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
    219229        self.assertEqual(self.user1.has_perm('anon', TestObj()), True)
    class AnonymousUserBackendTest(TestCase):  
    232242
    233243
    234244
    235 class NoBackendsTest(TestCase):
     245class NoBackendsTest(DefaultSettingsTestCase):
    236246    """
    237247    Tests that an appropriate error is raised if no auth backends are provided.
    238248    """
    239249    def setUp(self):
    240         self.old_AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS
    241         settings.AUTHENTICATION_BACKENDS = []
    242250        self.user = User.objects.create_user('test', 'test@example.com', 'test')
    243251
    244     def tearDown(self):
    245         settings.AUTHENTICATION_BACKENDS = self.old_AUTHENTICATION_BACKENDS
    246 
     252    @override_settings(AUTHENTICATION_BACKENDS=[])
    247253    def test_raises_exception(self):
    248254        self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),))
    249255
    class NoInActiveUserBackend(SimpleRowlevelBackend):  
    256262    supports_inactive_user = False
    257263
    258264
    259 class InActiveUserBackendTest(TestCase):
     265class InActiveUserBackendTest(CustomBackendTestCase):
    260266    """
    261267    Tests for a inactive user delegating to backend if it has 'supports_inactive_user' = True
    262268    """
    class InActiveUserBackendTest(TestCase):  
    264270    backend = 'django.contrib.auth.tests.auth_backends.InActiveUserBackend'
    265271
    266272    def setUp(self):
    267         self.curr_auth = settings.AUTHENTICATION_BACKENDS
    268         settings.AUTHENTICATION_BACKENDS = (self.backend,)
     273        super(InActiveUserBackendTest, self).setUp()
    269274        self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
    270275        self.user1.is_active = False
    271276        self.user1.save()
    272277
    273     def tearDown(self):
    274         settings.AUTHENTICATION_BACKENDS = self.curr_auth
    275 
    276278    def test_has_perm(self):
    277279        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
    278280        self.assertEqual(self.user1.has_perm('inactive', TestObj()), True)
    class InActiveUserBackendTest(TestCase):  
    282284        self.assertEqual(self.user1.has_module_perms("app2"), False)
    283285
    284286
    285 class NoInActiveUserBackendTest(TestCase):
     287class NoInActiveUserBackendTest(AdditionalBackendTestCase):
    286288    """
    287289    Tests that an inactive user does not delegate to backend if it has 'supports_inactive_user' = False
    288290    """
    289291    backend = 'django.contrib.auth.tests.auth_backends.NoInActiveUserBackend'
    290292
    291293    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()
    294295        self.user1 = User.objects.create_user('test', 'test@example.com', 'test')
    295296        self.user1.is_active = False
    296297        self.user1.save()
    297298
    298     def tearDown(self):
    299         settings.AUTHENTICATION_BACKENDS = self.curr_auth
    300 
    301299    def test_has_perm(self):
    302300        self.assertEqual(self.user1.has_perm('perm', TestObj()), False)
    303301        self.assertEqual(self.user1.has_perm('inactive', TestObj()), False)
    class NoInActiveUserBackendTest(TestCase):  
    305303    def test_has_module_perms(self):
    306304        self.assertEqual(self.user1.has_module_perms("app1"), False)
    307305        self.assertEqual(self.user1.has_module_perms("app2"), False)
    308 
  • django/contrib/auth/tests/basic.py

    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
     1from StringIO import StringIO
     2
    33from django.contrib.auth.models import User, AnonymousUser
    44from django.core.management import call_command
    5 from StringIO import StringIO
     5from django.test import DefaultSettingsTestCase
     6from django.utils.unittest import skipUnless
    67
    78try:
    89    import crypt as crypt_module
    except ImportError:  
    1011    crypt_module = None
    1112
    1213
    13 class BasicTestCase(TestCase):
     14class BasicTestCase(DefaultSettingsTestCase):
    1415    def test_user(self):
    1516        "Check that users can be created and can set their password"
    1617        u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
  • django/contrib/auth/tests/context_processors.py

    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
    41from django.contrib.auth import authenticate
     2from django.contrib.auth.tests.views import AuthViewsTestCase
    53from django.db.models import Q
    6 from django.test import TestCase
    74
    85
    9 class AuthContextProcessorTests(TestCase):
     6class AuthContextProcessorTests(AuthViewsTestCase):
    107    """
    118    Tests for the ``django.contrib.auth.context_processors.auth`` processor
    129    """
    13     urls = 'django.contrib.auth.tests.urls'
    1410    fixtures = ['context-processors-users.xml']
    1511
    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 
    2512    def test_session_not_accessed(self):
    2613        """
    2714        Tests that the session is not accessed simply by including
  • django/contrib/auth/tests/forms.py

    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  
    44from django.forms.fields import Field, EmailField
    55from django.contrib.auth.models import User
    66from django.contrib.auth.forms import UserCreationForm, AuthenticationForm,  PasswordChangeForm, SetPasswordForm, UserChangeForm, PasswordResetForm
    7 from django.test import TestCase
     7from django.test import DefaultSettingsTestCase
    88from django.utils.encoding import force_unicode
    99from django.utils import translation
    1010
    1111
    12 class UserCreationFormTest(TestCase):
     12class UserCreationFormTest(DefaultSettingsTestCase):
    1313
    1414    fixtures = ['authtestdata.json']
    1515
    class UserCreationFormTest(TestCase):  
    7575        self.assertEqual(repr(u), '<User: jsmith@example.com>')
    7676
    7777
    78 class AuthenticationFormTest(TestCase):
     78class AuthenticationFormTest(DefaultSettingsTestCase):
    7979
    8080    fixtures = ['authtestdata.json']
    8181
    class AuthenticationFormTest(TestCase):  
    126126        self.assertEqual(form.non_field_errors(), [])
    127127
    128128
    129 class SetPasswordFormTest(TestCase):
     129class SetPasswordFormTest(DefaultSettingsTestCase):
    130130
    131131    fixtures = ['authtestdata.json']
    132132
    class SetPasswordFormTest(TestCase):  
    152152        self.assertTrue(form.is_valid())
    153153
    154154
    155 class PasswordChangeFormTest(TestCase):
     155class PasswordChangeFormTest(DefaultSettingsTestCase):
    156156
    157157    fixtures = ['authtestdata.json']
    158158
    class PasswordChangeFormTest(TestCase):  
    199199                         ['old_password', 'new_password1', 'new_password2'])
    200200
    201201
    202 class UserChangeFormTest(TestCase):
     202class UserChangeFormTest(DefaultSettingsTestCase):
    203203
    204204    fixtures = ['authtestdata.json']
    205205
    class UserChangeFormTest(TestCase):  
    227227        form = MyUserForm({})
    228228
    229229
    230 class PasswordResetFormTest(TestCase):
     230class PasswordResetFormTest(DefaultSettingsTestCase):
    231231
    232232    fixtures = ['authtestdata.json']
    233233
  • django/contrib/auth/tests/management.py

    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
    21from django.contrib.auth import models, management
     2from django.test import DefaultSettingsTestCase
    33
    44
    5 class GetDefaultUsernameTestCase(TestCase):
     5class GetDefaultUsernameTestCase(DefaultSettingsTestCase):
    66
    77    def setUp(self):
    88        self._getpass_getuser = management.get_system_username
  • django/contrib/auth/tests/models.py

    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
    31from django.contrib.auth.models import User, SiteProfileNotAvailable
     2from django.test import DefaultSettingsTestCase
     3from django.test.utils import override_settings
    44
    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
    185
    19         if self.old_AUTH_PROFILE_MODULE is not None:
    20             settings.AUTH_PROFILE_MODULE = self.old_AUTH_PROFILE_MODULE
     6class ProfileTestCase(DefaultSettingsTestCase):
     7    fixtures = ['authtestdata.json']
    218
     9    @override_settings(AUTH_PROFILE_MODULE='')
    2210    def test_site_profile_not_available(self):
    2311        # calling get_profile without AUTH_PROFILE_MODULE set
    24         if hasattr(settings, 'AUTH_PROFILE_MODULE'):
    25             del settings.AUTH_PROFILE_MODULE
    2612        user = User.objects.get(username='testclient')
    2713        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
    2814
     15    @override_settings(AUTH_PROFILE_MODULE='foobar')
     16    def test_site_profile_bad_syntax(self):
    2917        # Bad syntax in AUTH_PROFILE_MODULE:
    30         settings.AUTH_PROFILE_MODULE = 'foobar'
     18        user = User.objects.get(username='testclient')
    3119        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
    3220
     21    @override_settings(AUTH_PROFILE_MODULE='foo.bar')
     22    def test_site_profile_missing_module(self):
    3323        # module that doesn't exist
    34         settings.AUTH_PROFILE_MODULE = 'foo.bar'
     24        user = User.objects.get(username='testclient')
    3525        self.assertRaises(SiteProfileNotAvailable, user.get_profile)
  • django/contrib/auth/tests/remote_user.py

    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  
    33from django.conf import settings
    44from django.contrib.auth.backends import RemoteUserBackend
    55from django.contrib.auth.models import User
    6 from django.test import TestCase
     6from django.test import DefaultSettingsTestCase
     7from django.test.utils import override_settings
    78from django.utils import timezone
    89
    910
    10 class RemoteUserTest(TestCase):
     11class RemoteUserTest(DefaultSettingsTestCase):
    1112
    1213    urls = 'django.contrib.auth.tests.urls'
    1314    middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
    class RemoteUserTest(TestCase):  
    1819    known_user2 = 'knownuser2'
    1920
    2021    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()
    2530
    2631    def test_no_remote_user(self):
    2732        """
    class RemoteUserTest(TestCase):  
    95100        response = self.client.get('/remote_user/', REMOTE_USER=self.known_user)
    96101        self.assertEqual(default_login, response.context['user'].last_login)
    97102
    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 
    103103
    104104class RemoteUserNoCreateBackend(RemoteUserBackend):
    105105    """Backend that doesn't create unknown users."""
  • django/contrib/auth/tests/signals.py

    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
    21from django.contrib.auth import signals
     2from django.test import DefaultSettingsTestCase
    33
    44
    5 class SignalTestCase(TestCase):
     5class SignalTestCase(DefaultSettingsTestCase):
    66    urls = 'django.contrib.auth.tests.urls'
    77    fixtures = ['authtestdata.json']
    88
  • django/contrib/auth/tests/tokens.py

    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  
    33from django.conf import settings
    44from django.contrib.auth.models import User
    55from django.contrib.auth.tokens import PasswordResetTokenGenerator
    6 from django.test import TestCase
     6from django.test import DefaultSettingsTestCase
    77
    88
    9 class TokenGeneratorTest(TestCase):
     9class TokenGeneratorTest(DefaultSettingsTestCase):
    1010
    1111    def test_make_token(self):
    1212        """
  • django/contrib/auth/tests/views.py

    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  
    99from django.core import mail
    1010from django.core.urlresolvers import reverse, NoReverseMatch
    1111from django.http import QueryDict
     12from django.test import DefaultSettingsTestCase
     13from django.test.utils import override_settings
    1214from django.utils.encoding import force_unicode
    1315from django.utils.html import escape
    14 from django.test import TestCase
    1516
    1617from django.contrib.auth import SESSION_KEY, REDIRECT_FIELD_NAME
    1718from django.contrib.auth.forms import (AuthenticationForm, PasswordChangeForm,
    1819                SetPasswordForm, PasswordResetForm)
    1920
    2021
    21 class AuthViewsTestCase(TestCase):
     22class AuthViewsTestCase(DefaultSettingsTestCase):
    2223    """
    2324    Helper base class for all the follow test cases.
    2425    """
    2526    fixtures = ['authtestdata.json']
    2627    urls = 'django.contrib.auth.tests.urls'
    2728
    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 
    4329    def login(self, password='password'):
    4430        response = self.client.post('/login/', {
    4531            'username': 'testclient',
    class AuthViewsTestCase(TestCase):  
    5238    def assertContainsEscaped(self, response, text, **kwargs):
    5339        return self.assertContains(response, escape(force_unicode(text)), **kwargs)
    5440
     41AuthViewsTestCase = override_settings(
     42    SITE_ID = 1,
     43    TEMPLATE_DIRS = (
     44        os.path.join(os.path.dirname(__file__), 'templates'),
     45    ),
     46)(AuthViewsTestCase)
     47
    5548
    5649class AuthViewNamedURLTests(AuthViewsTestCase):
    5750    urls = 'django.contrib.auth.urls'
    class ChangePasswordTest(AuthViewsTestCase):  
    192185        self.assertContainsEscaped(response, AuthenticationForm.error_messages['invalid_login'])
    193186
    194187    def logout(self):
    195         response = self.client.get('/logout/')
     188        self.client.get('/logout/')
    196189
    197190    def test_password_change_fails_with_invalid_old_password(self):
    198191        self.login()
    class LoginTest(AuthViewsTestCase):  
    303296
    304297class LoginURLSettings(AuthViewsTestCase):
    305298
    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):
    316300        response = self.client.get('/login_required/')
    317301        self.assertEqual(response.status_code, 302)
    318302        return response['Location']
    319303
     304    @override_settings(LOGIN_URL='/login/')
    320305    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()
    323307        querystring = QueryDict('', mutable=True)
    324308        querystring['next'] = '/login_required/'
    325309        self.assertEqual(login_required_url, 'http://testserver%s?%s' %
    326                          (login_url, querystring.urlencode('/')))
     310                         (settings.LOGIN_URL, querystring.urlencode('/')))
    327311
     312    @override_settings(LOGIN_URL='http://remote.example.com/login')
    328313    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()
    331315        querystring = QueryDict('', mutable=True)
    332316        querystring['next'] = 'http://testserver/login_required/'
    333317        self.assertEqual(login_required_url,
    334                          '%s?%s' % (login_url, querystring.urlencode('/')))
     318                         '%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/')))
    335319
     320    @override_settings(LOGIN_URL='https:///login/')
    336321    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()
    339323        querystring = QueryDict('', mutable=True)
    340324        querystring['next'] = 'http://testserver/login_required/'
    341325        self.assertEqual(login_required_url,
    342                          '%s?%s' % (login_url, querystring.urlencode('/')))
     326                         '%s?%s' % (settings.LOGIN_URL, querystring.urlencode('/')))
    343327
     328    @override_settings(LOGIN_URL='/login/?pretty=1')
    344329    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()
    347331        querystring = QueryDict('pretty=1', mutable=True)
    348332        querystring['next'] = '/login_required/'
    349333        self.assertEqual(login_required_url, 'http://testserver/login/?%s' %
    350334                         querystring.urlencode('/'))
    351335
     336    @override_settings(LOGIN_URL='http://remote.example.com/login/?next=/default/')
    352337    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()
    356339        querystring = QueryDict('', mutable=True)
    357340        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('/')))
    360343
    361344
    362345class LogoutTest(AuthViewsTestCase):
  • django/test/__init__.py

    diff --git a/django/test/__init__.py b/django/test/__init__.py
    index 21a4841..50d80ae 100644
    a b Django Unit Test and Doctest framework.  
    44
    55from django.test.client import Client, RequestFactory
    66from django.test.testcases import (TestCase, TransactionTestCase,
    7     SimpleTestCase, LiveServerTestCase, skipIfDBFeature,
    8     skipUnlessDBFeature)
     7    SimpleTestCase, DefaultSettingsTestCase, LiveServerTestCase,
     8    skipIfDBFeature, skipUnlessDBFeature)
    99from django.test.utils import Approximate
  • django/test/testcases.py

    diff --git a/django/test/testcases.py b/django/test/testcases.py
    index 4de4750..16a22ef 100644
    a b import socket  
    1111import threading
    1212import errno
    1313
    14 from django.conf import settings
     14from django.conf import settings, global_settings, UserSettingsHolder
    1515from django.contrib.staticfiles.handlers import StaticFilesHandler
    1616from django.core import mail
    1717from django.core.exceptions import ValidationError, ImproperlyConfigured
    from django.test.utils import (get_warnings_state, restore_warnings_state,  
    3232    override_settings)
    3333from django.utils import simplejson, unittest as ut2
    3434from django.utils.encoding import smart_str
     35from django.utils.translation import deactivate
    3536from django.views.static import serve
    3637
    3738__all__ = ('DocTestRunner', 'OutputChecker', 'TestCase', 'TransactionTestCase',
    class TransactionTestCase(SimpleTestCase):  
    364365              ROOT_URLCONF with it.
    365366            * Clearing the mail test outbox.
    366367        """
     368        self._settings_setup()
    367369        self._fixture_setup()
    368370        self._urlconf_setup()
    369371        mail.outbox = []
    370372
     373    def _settings_setup(self):
     374        pass
     375
    371376    def _fixture_setup(self):
    372377        # If the test case has a multi_db=True flag, flush all databases.
    373378        # Otherwise, just flush default.
    class TransactionTestCase(SimpleTestCase):  
    428433        """
    429434        self._fixture_teardown()
    430435        self._urlconf_teardown()
     436        self._settings_teardown()
    431437        # Some DB cursors include SQL statements as part of cursor
    432438        # creation. If you have a test that does rollback, the effect
    433439        # of these statements is lost, which can effect the operation
    class TransactionTestCase(SimpleTestCase):  
    446452            settings.ROOT_URLCONF = self._old_root_urlconf
    447453            clear_url_caches()
    448454
     455    def _settings_teardown(self):
     456        pass
     457
    449458    def assertRedirects(self, response, expected_url, status_code=302,
    450459                        target_status_code=200, host=None, msg_prefix=''):
    451460        """Asserts that a response redirected to a specific URL, and that the
    class TestCase(TransactionTestCase):  
    720729            transaction.leave_transaction_management(using=db)
    721730
    722731
     732class 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
    723753def _deferredSkip(condition, reason):
    724754    def decorator(test_func):
    725755        if not (isinstance(test_func, type) and
  • django/test/utils.py

    diff --git a/django/test/utils.py b/django/test/utils.py
    index 87f2311..421a726 100644
    a b class override_settings(object):  
    196196    def __call__(self, test_func):
    197197        from django.test import TransactionTestCase
    198198        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)
    202203                self.enable()
    203                 original_pre_setup(innerself)
    204             def _post_teardown(innerself):
    205                 original_post_teardown(innerself)
     204            def _settings_teardown(innerself):
    206205                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
    209209            return test_func
    210210        else:
    211211            @wraps(test_func)
Back to Top