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
This commit is contained in:
Aymeric Augustin 2012-03-31 16:03:09 +00:00
parent 6d9227bb0f
commit 98e9d1c6ab
8 changed files with 120 additions and 169 deletions

View File

@ -3,6 +3,7 @@ from django.contrib.auth.models import User, Group, Permission, AnonymousUser
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings
class BackendTest(TestCase): class BackendTest(TestCase):
@ -230,20 +231,14 @@ class AnonymousUserBackendTest(TestCase):
self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['anon'])) self.assertEqual(self.user1.get_all_permissions(TestObj()), set(['anon']))
@override_settings(AUTHENTICATION_BACKENDS=[])
class NoBackendsTest(TestCase): class NoBackendsTest(TestCase):
""" """
Tests that an appropriate error is raised if no auth backends are provided. Tests that an appropriate error is raised if no auth backends are provided.
""" """
def setUp(self): def setUp(self):
self.old_AUTHENTICATION_BACKENDS = settings.AUTHENTICATION_BACKENDS
settings.AUTHENTICATION_BACKENDS = []
self.user = User.objects.create_user('test', 'test@example.com', 'test') 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): def test_raises_exception(self):
self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),)) self.assertRaises(ImproperlyConfigured, self.user.has_perm, ('perm', TestObj(),))

View File

@ -18,7 +18,16 @@ from django.contrib.auth.forms import (AuthenticationForm, PasswordChangeForm,
SetPasswordForm, PasswordResetForm) 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): class AuthViewsTestCase(TestCase):
""" """
Helper base class for all the follow test cases. Helper base class for all the follow test cases.
@ -26,21 +35,6 @@ class AuthViewsTestCase(TestCase):
fixtures = ['authtestdata.json'] fixtures = ['authtestdata.json']
urls = 'django.contrib.auth.tests.urls' 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'): def login(self, password='password'):
response = self.client.post('/login/', { response = self.client.post('/login/', {
'username': 'testclient', 'username': 'testclient',

View File

@ -2,34 +2,29 @@ import os
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase, Client 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): class FlatpageCSRFTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' urls = 'django.contrib.flatpages.tests.urls'
def setUp(self): def setUp(self):
self.client = Client(enforce_csrf_checks=True) 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): def test_view_flatpage(self):
"A flatpage can be served through a view, even when the middleware is in use" "A flatpage can be served through a view, even when the middleware is in use"

View File

@ -3,31 +3,27 @@ from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage from django.contrib.flatpages.models import FlatPage
from django.test import TestCase 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): class FlatpageMiddlewareTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' 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): def test_view_flatpage(self):
"A flatpage can be served through a view, even when the middleware is in use" "A flatpage can be served through a view, even when the middleware is in use"
response = self.client.get('/flatpage_root/flatpage/') response = self.client.get('/flatpage_root/flatpage/')
@ -86,33 +82,25 @@ class FlatpageMiddlewareTests(TestCase):
self.assertContains(response, "<p>Isn't it special!</p>") 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): class FlatpageMiddlewareAppendSlashTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' 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): def test_redirect_view_flatpage(self):
"A flatpage can be served through a view and should add a slash" "A flatpage can be served through a view and should add a slash"
response = self.client.get('/flatpage_root/flatpage') response = self.client.get('/flatpage_root/flatpage')

View File

@ -3,29 +3,29 @@ from django.conf import settings
from django.contrib.auth.models import AnonymousUser, User from django.contrib.auth.models import AnonymousUser, User
from django.template import Template, Context, TemplateSyntaxError from django.template import Template, Context, TemplateSyntaxError
from django.test import TestCase 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): class FlatpageTemplateTagTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' urls = 'django.contrib.flatpages.tests.urls'
def setUp(self): 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') 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): def test_get_flatpages_tag(self):
"The flatpage template tag retrives unregistered prefixed flatpages by default" "The flatpage template tag retrives unregistered prefixed flatpages by default"
out = Template( out = Template(

View File

@ -3,31 +3,27 @@ from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage from django.contrib.flatpages.models import FlatPage
from django.test import TestCase 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): class FlatpageViewTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' 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): def test_view_flatpage(self):
"A flatpage can be served through a view" "A flatpage can be served through a view"
response = self.client.get('/flatpage_root/flatpage/') response = self.client.get('/flatpage_root/flatpage/')
@ -75,33 +71,25 @@ class FlatpageViewTests(TestCase):
self.assertContains(response, "<p>Isn't it special!</p>") 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): class FlatpageViewAppendSlashTests(TestCase):
fixtures = ['sample_flatpages'] fixtures = ['sample_flatpages']
urls = 'django.contrib.flatpages.tests.urls' 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): def test_redirect_view_flatpage(self):
"A flatpage can be served through a view and should add a slash" "A flatpage can be served through a view and should add a slash"
response = self.client.get('/flatpage_root/flatpage') response = self.client.get('/flatpage_root/flatpage')

View File

@ -0,0 +1 @@
Not found.

View File

@ -7,6 +7,7 @@ from django.conf import settings
from django.contrib.formtools import preview, utils from django.contrib.formtools import preview, utils
from django.contrib.formtools.wizard import FormWizard from django.contrib.formtools.wizard import FormWizard
from django.test import TestCase from django.test import TestCase
from django.test.utils import override_settings
from django.utils import unittest from django.utils import unittest
from django.contrib.formtools.tests.wizard import * from django.contrib.formtools.tests.wizard import *
@ -29,19 +30,12 @@ class TestFormPreview(preview.FormPreview):
def done(self, request, cleaned_data): def done(self, request, cleaned_data):
return http.HttpResponse(success_string) return http.HttpResponse(success_string)
@override_settings(
class FormToolsTestCase(TestCase): TEMPLATE_DIRS=(
def setUp(self): os.path.join(os.path.dirname(__file__), 'templates'),
# 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) + [ class PreviewTests(TestCase):
os.path.join(os.path.dirname(__file__), 'templates')]
def tearDown(self):
settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
class PreviewTests(FormToolsTestCase):
urls = 'django.contrib.formtools.tests.urls' urls = 'django.contrib.formtools.tests.urls'
def setUp(self): def setUp(self):
@ -215,7 +209,13 @@ class DummyRequest(http.HttpRequest):
self._dont_enforce_csrf_checks = True 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' urls = 'django.contrib.formtools.tests.urls'
input_re = re.compile('name="([^"]+)" value="([^"]+)"') input_re = re.compile('name="([^"]+)" value="([^"]+)"')
wizard_step_data = ( 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): def test_step_starts_at_zero(self):
""" """
step should be zero for the first form step should be zero for the first form