Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Used @override_settings in several tests.

  • Loading branch information...
commit d7c6a57d606bbb8185f9215ffce4c0516a9ae438 1 parent 2f722d9
Aymeric Augustin aaugustin authored
7 tests/regressiontests/forms/tests/extra.py
View
@@ -3,11 +3,11 @@
import datetime
-from django.conf import settings
from django.forms import *
from django.forms.extras import SelectDateWidget
from django.forms.util import ErrorList
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils import six
from django.utils import translation
from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible
@@ -642,17 +642,14 @@ def test_field_not_required(self):
self.assertFalse(b.has_changed())
-
+@override_settings(USE_L10N=True)
class FormsExtraL10NTestCase(TestCase):
def setUp(self):
super(FormsExtraL10NTestCase, self).setUp()
- self.old_use_l10n = getattr(settings, 'USE_L10N', False)
- settings.USE_L10N = True
translation.activate('nl')
def tearDown(self):
translation.deactivate()
- settings.USE_L10N = self.old_use_l10n
super(FormsExtraL10NTestCase, self).tearDown()
def test_l10n(self):
58 tests/regressiontests/forms/tests/input_formats.py
View
@@ -1,25 +1,17 @@
from datetime import time, date, datetime
from django import forms
-from django.conf import settings
+from django.test.utils import override_settings
from django.utils.translation import activate, deactivate
from django.utils.unittest import TestCase
+@override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"], USE_L10N=True)
class LocalizedTimeTests(TestCase):
def setUp(self):
- self.old_TIME_INPUT_FORMATS = settings.TIME_INPUT_FORMATS
- self.old_USE_L10N = settings.USE_L10N
-
- settings.TIME_INPUT_FORMATS = ["%I:%M:%S %p", "%I:%M %p"]
- settings.USE_L10N = True
-
activate('de')
def tearDown(self):
- settings.TIME_INPUT_FORMATS = self.old_TIME_INPUT_FORMATS
- settings.USE_L10N = self.old_USE_L10N
-
deactivate()
def test_timeField(self):
@@ -113,14 +105,8 @@ def test_localized_timeField_with_inputformat(self):
self.assertEqual(text, "13:30:00")
+@override_settings(TIME_INPUT_FORMATS=["%I:%M:%S %p", "%I:%M %p"])
class CustomTimeInputFormatsTests(TestCase):
- def setUp(self):
- self.old_TIME_INPUT_FORMATS = settings.TIME_INPUT_FORMATS
- settings.TIME_INPUT_FORMATS = ["%I:%M:%S %p", "%I:%M %p"]
-
- def tearDown(self):
- settings.TIME_INPUT_FORMATS = self.old_TIME_INPUT_FORMATS
-
def test_timeField(self):
"TimeFields can parse dates in the default format"
f = forms.TimeField()
@@ -302,20 +288,12 @@ def test_localized_timeField_with_inputformat(self):
self.assertEqual(text, "13:30:00")
+@override_settings(DATE_INPUT_FORMATS=["%d/%m/%Y", "%d-%m-%Y"], USE_L10N=True)
class LocalizedDateTests(TestCase):
def setUp(self):
- self.old_DATE_INPUT_FORMATS = settings.DATE_INPUT_FORMATS
- self.old_USE_L10N = settings.USE_L10N
-
- settings.DATE_INPUT_FORMATS = ["%d/%m/%Y", "%d-%m-%Y"]
- settings.USE_L10N = True
-
activate('de')
def tearDown(self):
- settings.DATE_INPUT_FORMATS = self.old_DATE_INPUT_FORMATS
- settings.USE_L10N = self.old_USE_L10N
-
deactivate()
def test_dateField(self):
@@ -410,14 +388,9 @@ def test_localized_dateField_with_inputformat(self):
text = f.widget._format_value(result)
self.assertEqual(text, "21.12.2010")
-class CustomDateInputFormatsTests(TestCase):
- def setUp(self):
- self.old_DATE_INPUT_FORMATS = settings.DATE_INPUT_FORMATS
- settings.DATE_INPUT_FORMATS = ["%d.%m.%Y", "%d-%m-%Y"]
-
- def tearDown(self):
- settings.DATE_INPUT_FORMATS = self.old_DATE_INPUT_FORMATS
+@override_settings(DATE_INPUT_FORMATS=["%d.%m.%Y", "%d-%m-%Y"])
+class CustomDateInputFormatsTests(TestCase):
def test_dateField(self):
"DateFields can parse dates in the default format"
f = forms.DateField()
@@ -597,20 +570,13 @@ def test_localized_dateField_with_inputformat(self):
text = f.widget._format_value(result)
self.assertEqual(text, "2010-12-21")
+
+@override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"], USE_L10N=True)
class LocalizedDateTimeTests(TestCase):
def setUp(self):
- self.old_DATETIME_INPUT_FORMATS = settings.DATETIME_INPUT_FORMATS
- self.old_USE_L10N = settings.USE_L10N
-
- settings.DATETIME_INPUT_FORMATS = ["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]
- settings.USE_L10N = True
-
activate('de')
def tearDown(self):
- settings.DATETIME_INPUT_FORMATS = self.old_DATETIME_INPUT_FORMATS
- settings.USE_L10N = self.old_USE_L10N
-
deactivate()
def test_dateTimeField(self):
@@ -706,14 +672,8 @@ def test_localized_dateTimeField_with_inputformat(self):
self.assertEqual(text, "21.12.2010 13:30:00")
+@override_settings(DATETIME_INPUT_FORMATS=["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"])
class CustomDateTimeInputFormatsTests(TestCase):
- def setUp(self):
- self.old_DATETIME_INPUT_FORMATS = settings.DATETIME_INPUT_FORMATS
- settings.DATETIME_INPUT_FORMATS = ["%I:%M:%S %p %d/%m/%Y", "%I:%M %p %d-%m-%Y"]
-
- def tearDown(self):
- settings.DATETIME_INPUT_FORMATS = self.old_DATETIME_INPUT_FORMATS
-
def test_dateTimeField(self):
"DateTimeFields can parse dates in the default format"
f = forms.DateTimeField()
6 tests/regressiontests/forms/tests/widgets.py
View
@@ -4,7 +4,6 @@
import copy
import datetime
-from django.conf import settings
from django.core.files.uploadedfile import SimpleUploadedFile
from django.forms import *
from django.forms.widgets import RadioFieldRenderer
@@ -13,6 +12,7 @@
from django.utils import six
from django.utils.translation import activate, deactivate
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils.encoding import python_2_unicode_compatible
@@ -989,16 +989,14 @@ class NullBooleanSelectLazyForm(Form):
"""Form to test for lazy evaluation. Refs #17190"""
bool = BooleanField(widget=NullBooleanSelect())
+@override_settings(USE_L10N=True)
class FormsI18NWidgetsTestCase(TestCase):
def setUp(self):
super(FormsI18NWidgetsTestCase, self).setUp()
- self.old_use_l10n = getattr(settings, 'USE_L10N', False)
- settings.USE_L10N = True
activate('de-at')
def tearDown(self):
deactivate()
- settings.USE_L10N = self.old_use_l10n
super(FormsI18NWidgetsTestCase, self).tearDown()
def test_splitdatetime(self):
10 tests/regressiontests/handlers/tests.py
View
@@ -1,27 +1,23 @@
-from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
from django.test import RequestFactory
+from django.test.utils import override_settings
from django.utils import unittest
-
class HandlerTests(unittest.TestCase):
+ # Mangle settings so the handler will fail
+ @override_settings(MIDDLEWARE_CLASSES=42)
def test_lock_safety(self):
"""
Tests for bug #11193 (errors inside middleware shouldn't leave
the initLock locked).
"""
- # Mangle settings so the handler will fail
- old_middleware_classes = settings.MIDDLEWARE_CLASSES
- settings.MIDDLEWARE_CLASSES = 42
# Try running the handler, it will fail in load_middleware
handler = WSGIHandler()
self.assertEqual(handler.initLock.locked(), False)
with self.assertRaises(Exception):
handler(None, None)
self.assertEqual(handler.initLock.locked(), False)
- # Reset settings
- settings.MIDDLEWARE_CLASSES = old_middleware_classes
def test_bad_path_info(self):
"""Tests for bug #15672 ('request' referenced before assignment)"""
15 tests/regressiontests/multiple_database/tests.py
View
@@ -4,13 +4,13 @@
import pickle
from operator import attrgetter
-from django.conf import settings
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.core import management
from django.db import connections, router, DEFAULT_DB_ALIAS
from django.db.models import signals
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils.six import StringIO
from .models import Book, Person, Pet, Review, UserProfile
@@ -1620,20 +1620,11 @@ def test_dumpdata(self):
command_output = new_io.getvalue().strip()
self.assertTrue('"email": "alice@example.com",' in command_output)
-_missing = object()
-class UserProfileTestCase(TestCase):
- def setUp(self):
- self.old_auth_profile_module = getattr(settings, 'AUTH_PROFILE_MODULE', _missing)
- settings.AUTH_PROFILE_MODULE = 'multiple_database.UserProfile'
- def tearDown(self):
- if self.old_auth_profile_module is _missing:
- del settings.AUTH_PROFILE_MODULE
- else:
- settings.AUTH_PROFILE_MODULE = self.old_auth_profile_module
+@override_settings(AUTH_PROFILE_MODULE='multiple_database.UserProfile')
+class UserProfileTestCase(TestCase):
def test_user_profiles(self):
-
alice = User.objects.create_user('alice', 'alice@example.com')
bob = User.objects.db_manager('other').create_user('bob', 'bob@example.com')
246 tests/regressiontests/requests/tests.py
View
@@ -5,12 +5,11 @@
from datetime import datetime, timedelta
from io import BytesIO
-from django.conf import settings
from django.core.exceptions import SuspiciousOperation
from django.core.handlers.wsgi import WSGIRequest, LimitedStream
from django.http import HttpRequest, HttpResponse, parse_cookie, build_request_repr, UnreadablePostError
from django.test.client import FakePayload
-from django.test.utils import str_prefix
+from django.test.utils import override_settings, str_prefix
from django.utils import unittest
from django.utils.http import cookie_date
from django.utils.timezone import utc
@@ -70,157 +69,146 @@ def test_httprequest_location(self):
self.assertEqual(request.build_absolute_uri(location="/path/with:colons"),
'http://www.example.com/path/with:colons')
+ @override_settings(USE_X_FORWARDED_HOST=False)
def test_http_get_host(self):
- old_USE_X_FORWARDED_HOST = settings.USE_X_FORWARDED_HOST
- try:
- settings.USE_X_FORWARDED_HOST = False
-
- # Check if X_FORWARDED_HOST is provided.
- request = HttpRequest()
- request.META = {
- 'HTTP_X_FORWARDED_HOST': 'forward.com',
- 'HTTP_HOST': 'example.com',
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 80,
- }
- # X_FORWARDED_HOST is ignored.
- self.assertEqual(request.get_host(), 'example.com')
-
- # Check if X_FORWARDED_HOST isn't provided.
- request = HttpRequest()
- request.META = {
- 'HTTP_HOST': 'example.com',
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 80,
- }
- self.assertEqual(request.get_host(), 'example.com')
+ # Check if X_FORWARDED_HOST is provided.
+ request = HttpRequest()
+ request.META = {
+ 'HTTP_X_FORWARDED_HOST': 'forward.com',
+ 'HTTP_HOST': 'example.com',
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 80,
+ }
+ # X_FORWARDED_HOST is ignored.
+ self.assertEqual(request.get_host(), 'example.com')
+
+ # Check if X_FORWARDED_HOST isn't provided.
+ request = HttpRequest()
+ request.META = {
+ 'HTTP_HOST': 'example.com',
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 80,
+ }
+ self.assertEqual(request.get_host(), 'example.com')
+
+ # Check if HTTP_HOST isn't provided.
+ request = HttpRequest()
+ request.META = {
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 80,
+ }
+ self.assertEqual(request.get_host(), 'internal.com')
- # Check if HTTP_HOST isn't provided.
+ # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
+ request = HttpRequest()
+ request.META = {
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 8042,
+ }
+ self.assertEqual(request.get_host(), 'internal.com:8042')
+
+ # Poisoned host headers are rejected as suspicious
+ legit_hosts = [
+ 'example.com',
+ 'example.com:80',
+ '12.34.56.78',
+ '12.34.56.78:443',
+ '[2001:19f0:feee::dead:beef:cafe]',
+ '[2001:19f0:feee::dead:beef:cafe]:8080',
+ ]
+
+ poisoned_hosts = [
+ 'example.com@evil.tld',
+ 'example.com:dr.frankenstein@evil.tld',
+ 'example.com:someone@somestie.com:80',
+ 'example.com:80/badpath'
+ ]
+
+ for host in legit_hosts:
request = HttpRequest()
request.META = {
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 80,
+ 'HTTP_HOST': host,
}
- self.assertEqual(request.get_host(), 'internal.com')
+ request.get_host()
- # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
- request = HttpRequest()
- request.META = {
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 8042,
- }
- self.assertEqual(request.get_host(), 'internal.com:8042')
-
- # Poisoned host headers are rejected as suspicious
- legit_hosts = [
- 'example.com',
- 'example.com:80',
- '12.34.56.78',
- '12.34.56.78:443',
- '[2001:19f0:feee::dead:beef:cafe]',
- '[2001:19f0:feee::dead:beef:cafe]:8080',
- ]
-
- poisoned_hosts = [
- 'example.com@evil.tld',
- 'example.com:dr.frankenstein@evil.tld',
- 'example.com:someone@somestie.com:80',
- 'example.com:80/badpath'
- ]
-
- for host in legit_hosts:
+ for host in poisoned_hosts:
+ with self.assertRaises(SuspiciousOperation):
request = HttpRequest()
request.META = {
'HTTP_HOST': host,
}
request.get_host()
- for host in poisoned_hosts:
- with self.assertRaises(SuspiciousOperation):
- request = HttpRequest()
- request.META = {
- 'HTTP_HOST': host,
- }
- request.get_host()
-
- finally:
- settings.USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST
-
+ @override_settings(USE_X_FORWARDED_HOST=True)
def test_http_get_host_with_x_forwarded_host(self):
- old_USE_X_FORWARDED_HOST = settings.USE_X_FORWARDED_HOST
- try:
- settings.USE_X_FORWARDED_HOST = True
-
- # Check if X_FORWARDED_HOST is provided.
- request = HttpRequest()
- request.META = {
- 'HTTP_X_FORWARDED_HOST': 'forward.com',
- 'HTTP_HOST': 'example.com',
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 80,
- }
- # X_FORWARDED_HOST is obeyed.
- self.assertEqual(request.get_host(), 'forward.com')
-
- # Check if X_FORWARDED_HOST isn't provided.
- request = HttpRequest()
- request.META = {
- 'HTTP_HOST': 'example.com',
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 80,
- }
- self.assertEqual(request.get_host(), 'example.com')
+ # Check if X_FORWARDED_HOST is provided.
+ request = HttpRequest()
+ request.META = {
+ 'HTTP_X_FORWARDED_HOST': 'forward.com',
+ 'HTTP_HOST': 'example.com',
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 80,
+ }
+ # X_FORWARDED_HOST is obeyed.
+ self.assertEqual(request.get_host(), 'forward.com')
+
+ # Check if X_FORWARDED_HOST isn't provided.
+ request = HttpRequest()
+ request.META = {
+ 'HTTP_HOST': 'example.com',
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 80,
+ }
+ self.assertEqual(request.get_host(), 'example.com')
+
+ # Check if HTTP_HOST isn't provided.
+ request = HttpRequest()
+ request.META = {
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 80,
+ }
+ self.assertEqual(request.get_host(), 'internal.com')
- # Check if HTTP_HOST isn't provided.
+ # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
+ request = HttpRequest()
+ request.META = {
+ 'SERVER_NAME': 'internal.com',
+ 'SERVER_PORT': 8042,
+ }
+ self.assertEqual(request.get_host(), 'internal.com:8042')
+
+ # Poisoned host headers are rejected as suspicious
+ legit_hosts = [
+ 'example.com',
+ 'example.com:80',
+ '12.34.56.78',
+ '12.34.56.78:443',
+ '[2001:19f0:feee::dead:beef:cafe]',
+ '[2001:19f0:feee::dead:beef:cafe]:8080',
+ ]
+
+ poisoned_hosts = [
+ 'example.com@evil.tld',
+ 'example.com:dr.frankenstein@evil.tld',
+ 'example.com:dr.frankenstein@evil.tld:80',
+ 'example.com:80/badpath'
+ ]
+
+ for host in legit_hosts:
request = HttpRequest()
request.META = {
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 80,
+ 'HTTP_HOST': host,
}
- self.assertEqual(request.get_host(), 'internal.com')
+ request.get_host()
- # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
- request = HttpRequest()
- request.META = {
- 'SERVER_NAME': 'internal.com',
- 'SERVER_PORT': 8042,
- }
- self.assertEqual(request.get_host(), 'internal.com:8042')
-
- # Poisoned host headers are rejected as suspicious
- legit_hosts = [
- 'example.com',
- 'example.com:80',
- '12.34.56.78',
- '12.34.56.78:443',
- '[2001:19f0:feee::dead:beef:cafe]',
- '[2001:19f0:feee::dead:beef:cafe]:8080',
- ]
-
- poisoned_hosts = [
- 'example.com@evil.tld',
- 'example.com:dr.frankenstein@evil.tld',
- 'example.com:dr.frankenstein@evil.tld:80',
- 'example.com:80/badpath'
- ]
-
- for host in legit_hosts:
+ for host in poisoned_hosts:
+ with self.assertRaises(SuspiciousOperation):
request = HttpRequest()
request.META = {
'HTTP_HOST': host,
}
request.get_host()
- for host in poisoned_hosts:
- with self.assertRaises(SuspiciousOperation):
- request = HttpRequest()
- request.META = {
- 'HTTP_HOST': host,
- }
- request.get_host()
-
- finally:
- settings.USE_X_FORWARDED_HOST = old_USE_X_FORWARDED_HOST
def test_near_expiration(self):
"Cookie will expire when an near expiration time is provided"
10 tests/regressiontests/templates/tests.py
View
@@ -155,16 +155,8 @@ class UTF8Class:
def __str__(self):
return 'ŠĐĆŽćžšđ'
+@override_settings(MEDIA_URL="/media/", STATIC_URL="/static/")
class Templates(unittest.TestCase):
- def setUp(self):
- self.old_static_url = settings.STATIC_URL
- self.old_media_url = settings.MEDIA_URL
- settings.STATIC_URL = "/static/"
- settings.MEDIA_URL = "/media/"
-
- def tearDown(self):
- settings.STATIC_URL = self.old_static_url
- settings.MEDIA_URL = self.old_media_url
def test_loaders_security(self):
ad_loader = app_directories.Loader()
12 tests/regressiontests/views/tests/static.py
View
@@ -4,10 +4,10 @@
from os import path
import unittest
-from django.conf import settings
from django.conf.urls.static import static
from django.http import HttpResponseNotModified
from django.test import TestCase
+from django.test.utils import override_settings
from django.utils.http import http_date
from django.views.static import was_modified_since
@@ -15,16 +15,11 @@
from ..urls import media_dir
+@override_settings(DEBUG=True)
class StaticTests(TestCase):
"""Tests django views in django/views/static.py"""
- def setUp(self):
- self.prefix = 'site_media'
- self.old_debug = settings.DEBUG
- settings.DEBUG = True
-
- def tearDown(self):
- settings.DEBUG = self.old_debug
+ prefix = 'site_media'
def test_serve(self):
"The static view can serve static media"
@@ -112,7 +107,6 @@ class StaticHelperTest(StaticTests):
"""
def setUp(self):
super(StaticHelperTest, self).setUp()
- self.prefix = 'media'
self._old_views_urlpatterns = urls.urlpatterns[:]
urls.urlpatterns += static('/media/', document_root=media_dir)

1 comment on commit d7c6a57

Claude Paroz
Collaborator

Unfortunately, some of those test cases are affected by https://code.djangoproject.com/ticket/18417. With your patch, they are silently dropped. We'll have to talk :-)

Please sign in to comment.
Something went wrong with that request. Please try again.