Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Used the override_settings decorator in the tests of auth, flatpages,…

… and formtools.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17851 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 98e9d1c6aba3670712887b0b20317dcb0e25f249 1 parent 6d9227b
Aymeric Augustin aaugustin authored
9 django/contrib/auth/tests/auth_backends.py
View
@@ -3,6 +3,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
+from django.test.utils import override_settings
class BackendTest(TestCase):
@@ -230,20 +231,14 @@ def test_get_all_permissions(self):
self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['anon']))
-
-
+@override_settings(AUTHENTICATION_BACKENDS=[])
class NoBackendsTest(TestCase):
"""
Tests that an appropriate error is raised if no auth backends are provided.
"""
def setUp(self):
- self.old_AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS
- settings.AUTHENTICATION_BACKENDS = []
self.user = User.objects.create_user('test', 'test@example.com', 'test')
- def tearDown(self):
- settings.AUTHENTICATION_BACKENDS = self.old_AUTHENTICATION_BACKENDS
-
def test_raises_exception(self):
self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),))
26 django/contrib/auth/tests/views.py
View
@@ -18,7 +18,16 @@
SetPasswordForm, PasswordResetForm)
-@override_settings(USE_TZ=False)
+@override_settings(
+ LANGUAGES=(
+ ('en', 'English'),
+ ),
+ LANGUAGE_CODE='en',
+ TEMPLATE_DIRS = (
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+ USE_TZ=False,
+)
class AuthViewsTestCase(TestCase):
"""
Helper base class for all the follow test cases.
@@ -26,21 +35,6 @@ class AuthViewsTestCase(TestCase):
fixtures = ['authtestdata.json']
urls = 'django.contrib.auth.tests.urls'
- def setUp(self):
- self.old_LANGUAGES = settings.LANGUAGES
- self.old_LANGUAGE_CODE = settings.LANGUAGE_CODE
- settings.LANGUAGES = (('en', 'English'),)
- settings.LANGUAGE_CODE = 'en'
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(os.path.dirname(__file__), 'templates'),
- )
-
- def tearDown(self):
- settings.LANGUAGES = self.old_LANGUAGES
- settings.LANGUAGE_CODE = self.old_LANGUAGE_CODE
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
-
def login(self, password='password'):
response = self.client.post('/login/', {
'username': 'testclient',
37 django/contrib/flatpages/tests/csrf.py
View
@@ -2,34 +2,29 @@
from django.conf import settings
from django.contrib.auth.models import User
from django.test import TestCase, Client
+from django.test.utils import override_settings
+
+@override_settings(
+ LOGIN_URL='/accounts/login/',
+ MIDDLEWARE_CLASSES=(
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
+ ),
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
class FlatpageCSRFTests(TestCase):
fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls'
def setUp(self):
self.client = Client(enforce_csrf_checks=True)
- self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
- flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
- csrf_middleware_class = 'django.middleware.csrf.CsrfViewMiddleware'
- if csrf_middleware_class not in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES += (csrf_middleware_class,)
- if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(
- os.path.dirname(__file__),
- 'templates'
- ),
- )
- self.old_LOGIN_URL = settings.LOGIN_URL
- settings.LOGIN_URL = '/accounts/login/'
-
- def tearDown(self):
- settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
- settings.LOGIN_URL = self.old_LOGIN_URL
def test_view_flatpage(self):
"A flatpage can be served through a view, even when the middleware is in use"
76 django/contrib/flatpages/tests/middleware.py
View
@@ -3,31 +3,27 @@
from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage
from django.test import TestCase
-
+from django.test.utils import override_settings
+
+
+@override_settings(
+ LOGIN_URL='/accounts/login/',
+ MIDDLEWARE_CLASSES=(
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
+ ),
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
class FlatpageMiddlewareTests(TestCase):
fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls'
- def setUp(self):
- self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
- flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
- if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(
- os.path.dirname(__file__),
- 'templates'
- ),
- )
- self.old_LOGIN_URL = settings.LOGIN_URL
- settings.LOGIN_URL = '/accounts/login/'
-
- def tearDown(self):
- settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
- settings.LOGIN_URL = self.old_LOGIN_URL
-
def test_view_flatpage(self):
"A flatpage can be served through a view, even when the middleware is in use"
response = self.client.get('/flatpage_root/flatpage/')
@@ -86,33 +82,25 @@ def test_fallback_flatpage_special_chars(self):
self.assertContains(response, "<p>Isn't it special!</p>")
+@override_settings(
+ APPEND_SLASH = True,
+ LOGIN_URL='/accounts/login/',
+ MIDDLEWARE_CLASSES=(
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
+ ),
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
class FlatpageMiddlewareAppendSlashTests(TestCase):
fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls'
- def setUp(self):
- self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
- flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
- if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(
- os.path.dirname(__file__),
- 'templates'
- ),
- )
- self.old_LOGIN_URL = settings.LOGIN_URL
- settings.LOGIN_URL = '/accounts/login/'
- self.old_APPEND_SLASH = settings.APPEND_SLASH
- settings.APPEND_SLASH = True
-
- def tearDown(self):
- settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
- settings.LOGIN_URL = self.old_LOGIN_URL
- settings.APPEND_SLASH = self.old_APPEND_SLASH
-
def test_redirect_view_flatpage(self):
"A flatpage can be served through a view and should add a slash"
response = self.client.get('/flatpage_root/flatpage')
30 django/contrib/flatpages/tests/templatetags.py
View
@@ -3,29 +3,29 @@
from django.contrib.auth.models import AnonymousUser, User
from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase
+from django.test.utils import override_settings
+
+@override_settings(
+ MIDDLEWARE_CLASSES=(
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
+ ),
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
class FlatpageTemplateTagTests(TestCase):
fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls'
def setUp(self):
- self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
- flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
- if flatpage_middleware_class not in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES += (flatpage_middleware_class,)
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(
- os.path.dirname(__file__),
- 'templates'
- ),
- )
self.me = User.objects.create_user('testuser', 'test@example.com', 's3krit')
- def tearDown(self):
- settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
-
def test_get_flatpages_tag(self):
"The flatpage template tag retrives unregistered prefixed flatpages by default"
out = Template(
76 django/contrib/flatpages/tests/views.py
View
@@ -3,31 +3,27 @@
from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage
from django.test import TestCase
-
+from django.test.utils import override_settings
+
+
+@override_settings(
+ LOGIN_URL='/accounts/login/',
+ MIDDLEWARE_CLASSES=(
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ # no 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
+ ),
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
class FlatpageViewTests(TestCase):
fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls'
- def setUp(self):
- self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
- flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
- if flatpage_middleware_class in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES = tuple(m for m in settings.MIDDLEWARE_CLASSES if m != flatpage_middleware_class)
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(
- os.path.dirname(__file__),
- 'templates'
- ),
- )
- self.old_LOGIN_URL = settings.LOGIN_URL
- settings.LOGIN_URL = '/accounts/login/'
-
- def tearDown(self):
- settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
- settings.LOGIN_URL = self.old_LOGIN_URL
-
def test_view_flatpage(self):
"A flatpage can be served through a view"
response = self.client.get('/flatpage_root/flatpage/')
@@ -75,33 +71,25 @@ def test_view_flatpage_special_chars(self):
self.assertContains(response, "<p>Isn't it special!</p>")
+@override_settings(
+ APPEND_SLASH = True,
+ LOGIN_URL='/accounts/login/',
+ MIDDLEWARE_CLASSES=(
+ 'django.middleware.common.CommonMiddleware',
+ 'django.contrib.sessions.middleware.SessionMiddleware',
+ 'django.middleware.csrf.CsrfViewMiddleware',
+ 'django.contrib.auth.middleware.AuthenticationMiddleware',
+ 'django.contrib.messages.middleware.MessageMiddleware',
+ # no 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
+ ),
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
class FlatpageViewAppendSlashTests(TestCase):
fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls'
- def setUp(self):
- self.old_MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
- flatpage_middleware_class = 'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware'
- if flatpage_middleware_class in settings.MIDDLEWARE_CLASSES:
- settings.MIDDLEWARE_CLASSES = tuple(m for m in settings.MIDDLEWARE_CLASSES if m != flatpage_middleware_class)
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = (
- os.path.join(
- os.path.dirname(__file__),
- 'templates'
- ),
- )
- self.old_LOGIN_URL = settings.LOGIN_URL
- settings.LOGIN_URL = '/accounts/login/'
- self.old_APPEND_SLASH = settings.APPEND_SLASH
- settings.APPEND_SLASH = True
-
- def tearDown(self):
- settings.MIDDLEWARE_CLASSES = self.old_MIDDLEWARE_CLASSES
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
- settings.LOGIN_URL = self.old_LOGIN_URL
- settings.APPEND_SLASH = self.old_APPEND_SLASH
-
def test_redirect_view_flatpage(self):
"A flatpage can be served through a view and should add a slash"
response = self.client.get('/flatpage_root/flatpage')
1  django/contrib/formtools/templates/404.html
View
@@ -0,0 +1 @@
+Not found.
38 django/contrib/formtools/tests/__init__.py
View
@@ -7,6 +7,7 @@
from django.contrib.formtools import preview, utils
from django.contrib.formtools.wizard import FormWizard
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils import unittest
from django.contrib.formtools.tests.wizard import *
@@ -29,19 +30,12 @@ def get_initial(self, request):
def done(self, request, cleaned_data):
return http.HttpResponse(success_string)
-
-class FormToolsTestCase(TestCase):
- def setUp(self):
- # in the test runner use templates/tests/ to provide base.html
- self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
- settings.TEMPLATE_DIRS = list(settings.TEMPLATE_DIRS) + [
- os.path.join(os.path.dirname(__file__), 'templates')]
-
- def tearDown(self):
- settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
-
-
-class PreviewTests(FormToolsTestCase):
+@override_settings(
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
+class PreviewTests(TestCase):
urls = 'django.contrib.formtools.tests.urls'
def setUp(self):
@@ -215,7 +209,13 @@ def __init__(self, POST=None):
self._dont_enforce_csrf_checks = True
-class WizardTests(FormToolsTestCase):
+@override_settings(
+ SECRET_KEY="123",
+ TEMPLATE_DIRS=(
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ ),
+)
+class WizardTests(TestCase):
urls = 'django.contrib.formtools.tests.urls'
input_re = re.compile('name="([^"]+)" value="([^"]+)"')
wizard_step_data = (
@@ -232,16 +232,6 @@ class WizardTests(FormToolsTestCase):
}
)
- def setUp(self):
- super(WizardTests, self).setUp()
- # Use a known SECRET_KEY to make security_hash tests deterministic
- self.old_SECRET_KEY = settings.SECRET_KEY
- settings.SECRET_KEY = "123"
-
- def tearDown(self):
- super(WizardTests, self).tearDown()
- settings.SECRET_KEY = self.old_SECRET_KEY
-
def test_step_starts_at_zero(self):
"""
step should be zero for the first form
Please sign in to comment.
Something went wrong with that request. Please try again.