From 98e9d1c6aba3670712887b0b20317dcb0e25f249 Mon Sep 17 00:00:00 2001 From: Aymeric Augustin Date: Sat, 31 Mar 2012 16:03:09 +0000 Subject: [PATCH] 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 --- django/contrib/auth/tests/auth_backends.py | 9 +-- django/contrib/auth/tests/views.py | 26 +++---- django/contrib/flatpages/tests/csrf.py | 37 ++++------ django/contrib/flatpages/tests/middleware.py | 74 ++++++++----------- .../contrib/flatpages/tests/templatetags.py | 30 ++++---- django/contrib/flatpages/tests/views.py | 74 ++++++++----------- django/contrib/formtools/templates/404.html | 1 + django/contrib/formtools/tests/__init__.py | 38 ++++------ 8 files changed, 120 insertions(+), 169 deletions(-) create mode 100644 django/contrib/formtools/templates/404.html diff --git a/django/contrib/auth/tests/auth_backends.py b/django/contrib/auth/tests/auth_backends.py index 0337ef14f3..2c6e39578e 100644 --- a/django/contrib/auth/tests/auth_backends.py +++ b/django/contrib/auth/tests/auth_backends.py @@ -3,6 +3,7 @@ from django.contrib.auth.models import User, Group, Permission, AnonymousUser 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 @@ class AnonymousUserBackendTest(TestCase): 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(),)) diff --git a/django/contrib/auth/tests/views.py b/django/contrib/auth/tests/views.py index ac1e225a3b..b8a16a8c17 100644 --- a/django/contrib/auth/tests/views.py +++ b/django/contrib/auth/tests/views.py @@ -18,7 +18,16 @@ from django.contrib.auth.forms import (AuthenticationForm, PasswordChangeForm, 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', diff --git a/django/contrib/flatpages/tests/csrf.py b/django/contrib/flatpages/tests/csrf.py index 969ae8d7e9..a8b9c2804c 100644 --- a/django/contrib/flatpages/tests/csrf.py +++ b/django/contrib/flatpages/tests/csrf.py @@ -2,34 +2,29 @@ import os 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" diff --git a/django/contrib/flatpages/tests/middleware.py b/django/contrib/flatpages/tests/middleware.py index 57433a5028..fa9d735c0c 100644 --- a/django/contrib/flatpages/tests/middleware.py +++ b/django/contrib/flatpages/tests/middleware.py @@ -3,31 +3,27 @@ from django.conf import settings 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 @@ class FlatpageMiddlewareTests(TestCase): self.assertContains(response, "

Isn't it special!

") +@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') diff --git a/django/contrib/flatpages/tests/templatetags.py b/django/contrib/flatpages/tests/templatetags.py index 282041cee9..de6a0a90fb 100644 --- a/django/contrib/flatpages/tests/templatetags.py +++ b/django/contrib/flatpages/tests/templatetags.py @@ -3,29 +3,29 @@ from django.conf import settings 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( diff --git a/django/contrib/flatpages/tests/views.py b/django/contrib/flatpages/tests/views.py index 8bc7544170..997bfabec8 100644 --- a/django/contrib/flatpages/tests/views.py +++ b/django/contrib/flatpages/tests/views.py @@ -3,31 +3,27 @@ from django.conf import settings 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 @@ class FlatpageViewTests(TestCase): self.assertContains(response, "

Isn't it special!

") +@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') diff --git a/django/contrib/formtools/templates/404.html b/django/contrib/formtools/templates/404.html new file mode 100644 index 0000000000..4aa12038b5 --- /dev/null +++ b/django/contrib/formtools/templates/404.html @@ -0,0 +1 @@ +Not found. diff --git a/django/contrib/formtools/tests/__init__.py b/django/contrib/formtools/tests/__init__.py index d659403704..e628690491 100644 --- a/django/contrib/formtools/tests/__init__.py +++ b/django/contrib/formtools/tests/__init__.py @@ -7,6 +7,7 @@ from django.conf import settings 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 @@ class TestFormPreview(preview.FormPreview): 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 @@ class DummyRequest(http.HttpRequest): 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