2011-06-11 22:40:02 +02:00
|
|
|
import os
|
2013-07-01 14:22:27 +02:00
|
|
|
import unittest
|
2012-05-03 15:27:01 +02:00
|
|
|
import warnings
|
2011-10-13 23:34:56 +02:00
|
|
|
|
2011-05-13 14:15:52 +02:00
|
|
|
from django.conf import settings, global_settings
|
2012-03-31 15:01:59 +02:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-12-16 23:06:06 +01:00
|
|
|
from django.http import HttpRequest
|
2012-11-24 23:47:41 +01:00
|
|
|
from django.test import SimpleTestCase, TransactionTestCase, TestCase, signals
|
2011-05-18 14:08:53 +02:00
|
|
|
from django.test.utils import override_settings
|
2013-07-01 14:22:27 +02:00
|
|
|
from django.utils import six
|
2011-05-18 14:08:53 +02:00
|
|
|
|
|
|
|
|
2013-04-19 11:44:47 +02:00
|
|
|
@override_settings(TEST='override', TEST_OUTER='outer')
|
2011-08-11 00:26:34 +02:00
|
|
|
class FullyDecoratedTranTestCase(TransactionTestCase):
|
|
|
|
|
2013-06-04 08:09:29 +02:00
|
|
|
available_apps = []
|
|
|
|
|
2011-08-11 00:26:34 +02:00
|
|
|
def test_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override')
|
2013-04-19 11:44:47 +02:00
|
|
|
self.assertEqual(settings.TEST_OUTER, 'outer')
|
2011-08-11 00:26:34 +02:00
|
|
|
|
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
2013-04-19 11:44:47 +02:00
|
|
|
self.assertEqual(settings.TEST_OUTER, 'outer')
|
2011-08-11 00:26:34 +02:00
|
|
|
|
2011-08-23 02:32:35 +02:00
|
|
|
def test_decorated_testcase_name(self):
|
2012-05-03 16:39:16 +02:00
|
|
|
self.assertEqual(FullyDecoratedTranTestCase.__name__, 'FullyDecoratedTranTestCase')
|
2011-08-23 02:32:35 +02:00
|
|
|
|
2011-09-05 00:51:53 +02:00
|
|
|
def test_decorated_testcase_module(self):
|
2012-05-03 16:39:16 +02:00
|
|
|
self.assertEqual(FullyDecoratedTranTestCase.__module__, __name__)
|
2011-09-05 00:51:53 +02:00
|
|
|
|
2011-08-11 00:26:34 +02:00
|
|
|
|
2012-03-30 11:08:29 +02:00
|
|
|
@override_settings(TEST='override')
|
2011-06-11 22:40:02 +02:00
|
|
|
class FullyDecoratedTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override')
|
|
|
|
|
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
|
|
|
|
2011-10-08 10:16:17 +02:00
|
|
|
|
|
|
|
class ClassDecoratedTestCaseSuper(TestCase):
|
|
|
|
"""
|
|
|
|
Dummy class for testing max recursion error in child class call to
|
|
|
|
super(). Refs #17011.
|
|
|
|
|
|
|
|
"""
|
|
|
|
def test_max_recursion_error(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-03-30 11:08:29 +02:00
|
|
|
@override_settings(TEST='override')
|
2011-10-08 10:16:17 +02:00
|
|
|
class ClassDecoratedTestCase(ClassDecoratedTestCaseSuper):
|
|
|
|
def test_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override')
|
|
|
|
|
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
|
|
|
|
|
|
|
def test_max_recursion_error(self):
|
|
|
|
"""
|
|
|
|
Overriding a method on a super class and then calling that method on
|
|
|
|
the super class should not trigger infinite recursion. See #17011.
|
|
|
|
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
super(ClassDecoratedTestCase, self).test_max_recursion_error()
|
2012-04-28 18:09:37 +02:00
|
|
|
except RuntimeError:
|
2011-10-08 10:16:17 +02:00
|
|
|
self.fail()
|
|
|
|
|
|
|
|
|
2012-09-07 19:43:51 +02:00
|
|
|
class SettingsTests(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.testvalue = None
|
|
|
|
signals.setting_changed.connect(self.signal_callback)
|
2011-05-18 14:08:53 +02:00
|
|
|
|
2012-09-07 19:43:51 +02:00
|
|
|
def tearDown(self):
|
|
|
|
signals.setting_changed.disconnect(self.signal_callback)
|
2011-01-02 02:33:11 +01:00
|
|
|
|
2012-09-07 19:43:51 +02:00
|
|
|
def signal_callback(self, sender, setting, value, **kwargs):
|
|
|
|
if setting == 'TEST':
|
|
|
|
self.testvalue = value
|
2009-12-12 18:51:51 +01:00
|
|
|
|
2011-05-06 15:29:24 +02:00
|
|
|
def test_override(self):
|
|
|
|
settings.TEST = 'test'
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
with self.settings(TEST='override'):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
del settings.TEST
|
|
|
|
|
|
|
|
def test_override_change(self):
|
|
|
|
settings.TEST = 'test'
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
with self.settings(TEST='override'):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
settings.TEST = 'test2'
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
del settings.TEST
|
|
|
|
|
|
|
|
def test_override_doesnt_leak(self):
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
with self.settings(TEST='override'):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
settings.TEST = 'test'
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
2009-12-12 18:51:51 +01:00
|
|
|
|
2011-05-18 14:08:53 +02:00
|
|
|
@override_settings(TEST='override')
|
|
|
|
def test_decorator(self):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
|
|
|
|
def test_context_manager(self):
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
override = override_settings(TEST='override')
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
override.enable()
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
override.disable()
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
|
|
|
|
def test_class_decorator(self):
|
2012-11-24 23:47:41 +01:00
|
|
|
# SimpleTestCase can be decorated by override_settings, but not ut.TestCase
|
|
|
|
class SimpleTestCaseSubclass(SimpleTestCase):
|
2012-11-24 23:43:13 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
class UnittestTestCaseSubclass(unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
2012-11-24 23:47:41 +01:00
|
|
|
decorated = override_settings(TEST='override')(SimpleTestCaseSubclass)
|
2012-11-24 23:43:13 +01:00
|
|
|
self.assertIsInstance(decorated, type)
|
2012-11-24 23:47:41 +01:00
|
|
|
self.assertTrue(issubclass(decorated, SimpleTestCase))
|
2012-11-24 23:43:13 +01:00
|
|
|
|
|
|
|
with six.assertRaisesRegex(self, Exception,
|
2012-11-24 23:47:41 +01:00
|
|
|
"Only subclasses of Django SimpleTestCase*"):
|
2012-11-24 23:43:13 +01:00
|
|
|
decorated = override_settings(TEST='override')(UnittestTestCaseSubclass)
|
2011-05-18 14:08:53 +02:00
|
|
|
|
|
|
|
def test_signal_callback_context_manager(self):
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
with self.settings(TEST='override'):
|
2012-09-07 19:43:51 +02:00
|
|
|
self.assertEqual(self.testvalue, 'override')
|
|
|
|
self.assertEqual(self.testvalue, None)
|
2011-05-18 14:08:53 +02:00
|
|
|
|
|
|
|
@override_settings(TEST='override')
|
|
|
|
def test_signal_callback_decorator(self):
|
2012-09-07 19:43:51 +02:00
|
|
|
self.assertEqual(self.testvalue, 'override')
|
2011-05-18 14:08:53 +02:00
|
|
|
|
2009-12-12 18:51:51 +01:00
|
|
|
#
|
|
|
|
# Regression tests for #10130: deleting settings.
|
|
|
|
#
|
|
|
|
|
|
|
|
def test_settings_delete(self):
|
|
|
|
settings.TEST = 'test'
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
del settings.TEST
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
|
|
|
|
def test_settings_delete_wrapped(self):
|
|
|
|
self.assertRaises(TypeError, delattr, settings, '_wrapped')
|
2011-01-02 02:33:11 +01:00
|
|
|
|
2012-09-04 09:41:12 +02:00
|
|
|
def test_override_settings_delete(self):
|
|
|
|
"""
|
2013-07-28 03:45:25 +02:00
|
|
|
Allow deletion of a setting in an overridden settings set (#18824)
|
2012-09-04 09:41:12 +02:00
|
|
|
"""
|
|
|
|
previous_i18n = settings.USE_I18N
|
|
|
|
with self.settings(USE_I18N=False):
|
|
|
|
del settings.USE_I18N
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'USE_I18N')
|
|
|
|
self.assertEqual(settings.USE_I18N, previous_i18n)
|
|
|
|
|
2013-04-19 11:44:47 +02:00
|
|
|
def test_override_settings_nested(self):
|
|
|
|
"""
|
|
|
|
Test that override_settings uses the actual _wrapped attribute at
|
|
|
|
runtime, not when it was instantiated.
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST2')
|
|
|
|
|
|
|
|
inner = override_settings(TEST2='override')
|
|
|
|
with override_settings(TEST='override'):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
with inner:
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
self.assertEqual('override', settings.TEST2)
|
|
|
|
# inner's __exit__ should have restored the settings of the outer
|
|
|
|
# context manager, not those when the class was instantiated
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST2')
|
|
|
|
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST')
|
|
|
|
self.assertRaises(AttributeError, getattr, settings, 'TEST2')
|
|
|
|
|
2012-02-22 01:42:19 +01:00
|
|
|
def test_allowed_include_roots_string(self):
|
|
|
|
"""
|
|
|
|
ALLOWED_INCLUDE_ROOTS is not allowed to be incorrectly set to a string
|
|
|
|
rather than a tuple.
|
|
|
|
"""
|
|
|
|
self.assertRaises(ValueError, setattr, settings,
|
|
|
|
'ALLOWED_INCLUDE_ROOTS', '/var/www/ssi/')
|
2011-01-02 02:33:11 +01:00
|
|
|
|
2011-05-06 15:29:24 +02:00
|
|
|
|
2013-05-18 13:43:51 +02:00
|
|
|
class TestComplexSettingOverride(TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.old_warn_override_settings = signals.COMPLEX_OVERRIDE_SETTINGS.copy()
|
|
|
|
signals.COMPLEX_OVERRIDE_SETTINGS.add('TEST_WARN')
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
signals.COMPLEX_OVERRIDE_SETTINGS = self.old_warn_override_settings
|
|
|
|
self.assertFalse('TEST_WARN' in signals.COMPLEX_OVERRIDE_SETTINGS)
|
|
|
|
|
|
|
|
def test_complex_override_warning(self):
|
|
|
|
"""Regression test for #19031"""
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
|
|
|
|
override = override_settings(TEST_WARN='override')
|
|
|
|
override.enable()
|
|
|
|
self.assertEqual('override', settings.TEST_WARN)
|
|
|
|
override.disable()
|
|
|
|
|
|
|
|
self.assertEqual(len(w), 1)
|
|
|
|
self.assertEqual('Overriding setting TEST_WARN can lead to unexpected behaviour.', str(w[-1].message))
|
|
|
|
|
|
|
|
|
2011-05-06 15:29:24 +02:00
|
|
|
class TrailingSlashURLTests(TestCase):
|
2012-03-31 15:11:02 +02:00
|
|
|
"""
|
|
|
|
Tests for the MEDIA_URL and STATIC_URL settings.
|
|
|
|
|
|
|
|
They must end with a slash to ensure there's a deterministic way to build
|
|
|
|
paths in templates.
|
|
|
|
"""
|
2011-01-02 02:33:11 +01:00
|
|
|
settings_module = settings
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._original_media_url = self.settings_module.MEDIA_URL
|
2012-03-31 15:11:02 +02:00
|
|
|
self._original_static_url = self.settings_module.STATIC_URL
|
2011-01-02 02:33:11 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.settings_module.MEDIA_URL = self._original_media_url
|
2012-03-31 15:11:02 +02:00
|
|
|
self.settings_module.STATIC_URL = self._original_static_url
|
2011-01-02 02:33:11 +01:00
|
|
|
|
|
|
|
def test_blank(self):
|
|
|
|
"""
|
2012-03-31 15:11:02 +02:00
|
|
|
The empty string is accepted, even though it doesn't end in a slash.
|
2011-01-02 02:33:11 +01:00
|
|
|
"""
|
|
|
|
self.settings_module.MEDIA_URL = ''
|
|
|
|
self.assertEqual('', self.settings_module.MEDIA_URL)
|
|
|
|
|
2012-03-31 15:11:02 +02:00
|
|
|
self.settings_module.STATIC_URL = ''
|
|
|
|
self.assertEqual('', self.settings_module.STATIC_URL)
|
|
|
|
|
2011-01-02 02:33:11 +01:00
|
|
|
def test_end_slash(self):
|
|
|
|
"""
|
2012-03-31 15:11:02 +02:00
|
|
|
It works if the value ends in a slash.
|
2011-01-02 02:33:11 +01:00
|
|
|
"""
|
|
|
|
self.settings_module.MEDIA_URL = '/foo/'
|
|
|
|
self.assertEqual('/foo/', self.settings_module.MEDIA_URL)
|
|
|
|
|
|
|
|
self.settings_module.MEDIA_URL = 'http://media.foo.com/'
|
|
|
|
self.assertEqual('http://media.foo.com/',
|
|
|
|
self.settings_module.MEDIA_URL)
|
|
|
|
|
2012-03-31 15:11:02 +02:00
|
|
|
self.settings_module.STATIC_URL = '/foo/'
|
|
|
|
self.assertEqual('/foo/', self.settings_module.STATIC_URL)
|
|
|
|
|
|
|
|
self.settings_module.STATIC_URL = 'http://static.foo.com/'
|
|
|
|
self.assertEqual('http://static.foo.com/',
|
|
|
|
self.settings_module.STATIC_URL)
|
|
|
|
|
2011-01-02 02:33:11 +01:00
|
|
|
def test_no_end_slash(self):
|
|
|
|
"""
|
2012-03-31 15:11:02 +02:00
|
|
|
An ImproperlyConfigured exception is raised if the value doesn't end
|
|
|
|
in a slash.
|
2011-01-02 02:33:11 +01:00
|
|
|
"""
|
2012-03-31 15:11:02 +02:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.MEDIA_URL = '/foo'
|
2011-01-02 02:33:11 +01:00
|
|
|
|
2012-03-31 15:11:02 +02:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.MEDIA_URL = 'http://media.foo.com'
|
2011-01-02 02:33:11 +01:00
|
|
|
|
2012-03-31 15:11:02 +02:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.STATIC_URL = '/foo'
|
2012-03-31 15:01:59 +02:00
|
|
|
|
2012-03-31 15:11:02 +02:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.STATIC_URL = 'http://static.foo.com'
|
2012-03-31 15:01:59 +02:00
|
|
|
|
2011-01-02 02:33:11 +01:00
|
|
|
def test_double_slash(self):
|
|
|
|
"""
|
2012-03-31 15:11:02 +02:00
|
|
|
If the value ends in more than one slash, presume they know what
|
2011-01-02 02:33:11 +01:00
|
|
|
they're doing.
|
|
|
|
"""
|
|
|
|
self.settings_module.MEDIA_URL = '/stupid//'
|
|
|
|
self.assertEqual('/stupid//', self.settings_module.MEDIA_URL)
|
|
|
|
|
|
|
|
self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
|
|
|
|
self.assertEqual('http://media.foo.com/stupid//',
|
|
|
|
self.settings_module.MEDIA_URL)
|
2011-05-13 14:15:52 +02:00
|
|
|
|
2012-03-31 15:11:02 +02:00
|
|
|
self.settings_module.STATIC_URL = '/stupid//'
|
|
|
|
self.assertEqual('/stupid//', self.settings_module.STATIC_URL)
|
|
|
|
|
|
|
|
self.settings_module.STATIC_URL = 'http://static.foo.com/stupid//'
|
|
|
|
self.assertEqual('http://static.foo.com/stupid//',
|
|
|
|
self.settings_module.STATIC_URL)
|
|
|
|
|
|
|
|
|
2011-12-16 23:06:06 +01:00
|
|
|
class SecureProxySslHeaderTest(TestCase):
|
|
|
|
settings_module = settings
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._original_setting = self.settings_module.SECURE_PROXY_SSL_HEADER
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.settings_module.SECURE_PROXY_SSL_HEADER = self._original_setting
|
|
|
|
|
|
|
|
def test_none(self):
|
|
|
|
self.settings_module.SECURE_PROXY_SSL_HEADER = None
|
|
|
|
req = HttpRequest()
|
|
|
|
self.assertEqual(req.is_secure(), False)
|
|
|
|
|
|
|
|
def test_set_without_xheader(self):
|
|
|
|
self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
|
|
|
|
req = HttpRequest()
|
|
|
|
self.assertEqual(req.is_secure(), False)
|
|
|
|
|
|
|
|
def test_set_with_xheader_wrong(self):
|
|
|
|
self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
|
|
|
|
req = HttpRequest()
|
|
|
|
req.META['HTTP_X_FORWARDED_PROTOCOL'] = 'wrongvalue'
|
|
|
|
self.assertEqual(req.is_secure(), False)
|
|
|
|
|
|
|
|
def test_set_with_xheader_right(self):
|
|
|
|
self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
|
|
|
|
req = HttpRequest()
|
|
|
|
req.META['HTTP_X_FORWARDED_PROTOCOL'] = 'https'
|
|
|
|
self.assertEqual(req.is_secure(), True)
|