2013-12-22 19:22:56 +00:00
|
|
|
import os
|
2014-01-21 03:25:33 +00:00
|
|
|
import sys
|
2013-07-01 12:22:27 +00:00
|
|
|
import unittest
|
2019-03-03 00:23:18 +00:00
|
|
|
from types import ModuleType, SimpleNamespace
|
2017-07-27 00:00:28 +00:00
|
|
|
from unittest import mock
|
2011-10-13 21:34:56 +00:00
|
|
|
|
2015-07-28 14:37:41 +00:00
|
|
|
from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings
|
2012-03-31 13:01:59 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-12-16 22:06:06 +00:00
|
|
|
from django.http import HttpRequest
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.test import (
|
|
|
|
SimpleTestCase, TestCase, TransactionTestCase, modify_settings,
|
|
|
|
override_settings, signals,
|
|
|
|
)
|
2017-07-27 00:00:28 +00:00
|
|
|
from django.test.utils import requires_tz_support
|
2011-05-18 12:08:53 +00:00
|
|
|
|
|
|
|
|
2013-12-23 11:39:19 +00:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'prepend': ['b'],
|
|
|
|
'append': ['d'],
|
|
|
|
'remove': ['a', 'e']
|
|
|
|
})
|
2016-04-08 02:04:45 +00:00
|
|
|
@override_settings(ITEMS=['a', 'c', 'e'], ITEMS_OUTER=[1, 2, 3], TEST='override', TEST_OUTER='outer')
|
2011-08-10 22:26:34 +00:00
|
|
|
class FullyDecoratedTranTestCase(TransactionTestCase):
|
|
|
|
|
2013-06-04 06:09:29 +00:00
|
|
|
available_apps = []
|
|
|
|
|
2011-08-10 22:26:34 +00:00
|
|
|
def test_override(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['b', 'c', 'd'])
|
|
|
|
self.assertEqual(settings.ITEMS_OUTER, [1, 2, 3])
|
2011-08-10 22:26:34 +00:00
|
|
|
self.assertEqual(settings.TEST, 'override')
|
2013-04-19 09:44:47 +00:00
|
|
|
self.assertEqual(settings.TEST_OUTER, 'outer')
|
2011-08-10 22:26:34 +00:00
|
|
|
|
2013-12-23 11:39:19 +00:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': ['e', 'f'],
|
|
|
|
'prepend': ['a'],
|
|
|
|
'remove': ['d', 'c'],
|
|
|
|
})
|
|
|
|
def test_method_list_override(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['a', 'b', 'e', 'f'])
|
|
|
|
self.assertEqual(settings.ITEMS_OUTER, [1, 2, 3])
|
2013-12-23 11:39:19 +00:00
|
|
|
|
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': ['b'],
|
|
|
|
'prepend': ['d'],
|
|
|
|
'remove': ['a', 'c', 'e'],
|
|
|
|
})
|
|
|
|
def test_method_list_override_no_ops(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['b', 'd'])
|
2013-12-23 11:39:19 +00:00
|
|
|
|
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': 'e',
|
|
|
|
'prepend': 'a',
|
|
|
|
'remove': 'c',
|
|
|
|
})
|
|
|
|
def test_method_list_override_strings(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['a', 'b', 'd', 'e'])
|
2013-12-23 11:39:19 +00:00
|
|
|
|
|
|
|
@modify_settings(ITEMS={'remove': ['b', 'd']})
|
|
|
|
@modify_settings(ITEMS={'append': ['b'], 'prepend': ['d']})
|
|
|
|
def test_method_list_override_nested_order(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['d', 'c', 'b'])
|
2013-12-23 11:39:19 +00:00
|
|
|
|
2011-08-10 22:26:34 +00:00
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
2013-04-19 09:44:47 +00:00
|
|
|
self.assertEqual(settings.TEST_OUTER, 'outer')
|
2011-08-10 22:26:34 +00:00
|
|
|
|
2011-08-23 00:32:35 +00:00
|
|
|
def test_decorated_testcase_name(self):
|
2012-05-03 14:39:16 +00:00
|
|
|
self.assertEqual(FullyDecoratedTranTestCase.__name__, 'FullyDecoratedTranTestCase')
|
2011-08-23 00:32:35 +00:00
|
|
|
|
2011-09-04 22:51:53 +00:00
|
|
|
def test_decorated_testcase_module(self):
|
2012-05-03 14:39:16 +00:00
|
|
|
self.assertEqual(FullyDecoratedTranTestCase.__module__, __name__)
|
2011-09-04 22:51:53 +00:00
|
|
|
|
2011-08-10 22:26:34 +00:00
|
|
|
|
2013-12-23 11:39:19 +00:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'prepend': ['b'],
|
|
|
|
'append': ['d'],
|
|
|
|
'remove': ['a', 'e']
|
|
|
|
})
|
|
|
|
@override_settings(ITEMS=['a', 'c', 'e'], TEST='override')
|
2011-06-11 20:40:02 +00:00
|
|
|
class FullyDecoratedTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_override(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['b', 'c', 'd'])
|
2011-06-11 20:40:02 +00:00
|
|
|
self.assertEqual(settings.TEST, 'override')
|
|
|
|
|
2013-12-23 11:39:19 +00:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': 'e',
|
|
|
|
'prepend': 'a',
|
|
|
|
'remove': 'c',
|
|
|
|
})
|
2011-06-11 20:40:02 +00:00
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
2017-03-17 11:51:48 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['a', 'b', 'd', 'e'])
|
2011-06-11 20:40:02 +00:00
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
|
|
|
|
2011-10-08 08:16:17 +00: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 09:08:29 +00:00
|
|
|
@override_settings(TEST='override')
|
2011-10-08 08:16:17 +00:00
|
|
|
class ClassDecoratedTestCase(ClassDecoratedTestCaseSuper):
|
2014-10-18 18:03:10 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 13:13:44 +00:00
|
|
|
super().setUpClass()
|
2014-10-18 18:03:10 +00:00
|
|
|
cls.foo = getattr(settings, 'TEST', 'BUG')
|
|
|
|
|
2011-10-08 08:16:17 +00:00
|
|
|
def test_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override')
|
|
|
|
|
2014-10-18 18:03:10 +00:00
|
|
|
def test_setupclass_override(self):
|
2016-10-27 07:53:39 +00:00
|
|
|
"""Settings are overridden within setUpClass (#21281)."""
|
2014-10-18 18:03:10 +00:00
|
|
|
self.assertEqual(self.foo, 'override')
|
|
|
|
|
2011-10-08 08:16:17 +00:00
|
|
|
@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.
|
|
|
|
"""
|
2017-01-21 13:13:44 +00:00
|
|
|
super().test_max_recursion_error()
|
2011-10-08 08:16:17 +00:00
|
|
|
|
|
|
|
|
2013-12-23 11:39:19 +00:00
|
|
|
@modify_settings(ITEMS={'append': 'mother'})
|
|
|
|
@override_settings(ITEMS=['father'], TEST='override-parent')
|
2013-10-14 08:44:55 +00:00
|
|
|
class ParentDecoratedTestCase(TestCase):
|
|
|
|
pass
|
|
|
|
|
2013-11-02 21:34:05 +00:00
|
|
|
|
2013-12-23 11:39:19 +00:00
|
|
|
@modify_settings(ITEMS={'append': ['child']})
|
2013-10-14 08:44:55 +00:00
|
|
|
@override_settings(TEST='override-child')
|
|
|
|
class ChildDecoratedTestCase(ParentDecoratedTestCase):
|
|
|
|
def test_override_settings_inheritance(self):
|
2013-12-23 11:39:19 +00:00
|
|
|
self.assertEqual(settings.ITEMS, ['father', 'mother', 'child'])
|
2013-10-14 08:44:55 +00:00
|
|
|
self.assertEqual(settings.TEST, 'override-child')
|
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class SettingsTests(SimpleTestCase):
|
2012-09-07 17:43:51 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.testvalue = None
|
|
|
|
signals.setting_changed.connect(self.signal_callback)
|
2011-05-18 12:08:53 +00:00
|
|
|
|
2012-09-07 17:43:51 +00:00
|
|
|
def tearDown(self):
|
|
|
|
signals.setting_changed.disconnect(self.signal_callback)
|
2011-01-02 01:33:11 +00:00
|
|
|
|
2012-09-07 17:43:51 +00:00
|
|
|
def signal_callback(self, sender, setting, value, **kwargs):
|
|
|
|
if setting == 'TEST':
|
|
|
|
self.testvalue = value
|
2009-12-12 17:51:51 +00:00
|
|
|
|
2011-05-06 13:29:24 +00: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):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-06 13:29:24 +00:00
|
|
|
with self.settings(TEST='override'):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
settings.TEST = 'test'
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2009-12-12 17:51:51 +00:00
|
|
|
|
2011-05-18 12:08:53 +00:00
|
|
|
@override_settings(TEST='override')
|
|
|
|
def test_decorator(self):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
|
|
|
|
def test_context_manager(self):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 12:08:53 +00:00
|
|
|
override = override_settings(TEST='override')
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 12:08:53 +00:00
|
|
|
override.enable()
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
override.disable()
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 12:08:53 +00:00
|
|
|
|
|
|
|
def test_class_decorator(self):
|
2012-11-24 22:47:41 +00:00
|
|
|
# SimpleTestCase can be decorated by override_settings, but not ut.TestCase
|
|
|
|
class SimpleTestCaseSubclass(SimpleTestCase):
|
2012-11-24 22:43:13 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
class UnittestTestCaseSubclass(unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
2012-11-24 22:47:41 +00:00
|
|
|
decorated = override_settings(TEST='override')(SimpleTestCaseSubclass)
|
2012-11-24 22:43:13 +00:00
|
|
|
self.assertIsInstance(decorated, type)
|
2012-11-24 22:47:41 +00:00
|
|
|
self.assertTrue(issubclass(decorated, SimpleTestCase))
|
2012-11-24 22:43:13 +00:00
|
|
|
|
2016-04-08 02:04:45 +00:00
|
|
|
with self.assertRaisesMessage(Exception, "Only subclasses of Django SimpleTestCase"):
|
2012-11-24 22:43:13 +00:00
|
|
|
decorated = override_settings(TEST='override')(UnittestTestCaseSubclass)
|
2011-05-18 12:08:53 +00:00
|
|
|
|
|
|
|
def test_signal_callback_context_manager(self):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 12:08:53 +00:00
|
|
|
with self.settings(TEST='override'):
|
2012-09-07 17:43:51 +00:00
|
|
|
self.assertEqual(self.testvalue, 'override')
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIsNone(self.testvalue)
|
2011-05-18 12:08:53 +00:00
|
|
|
|
|
|
|
@override_settings(TEST='override')
|
|
|
|
def test_signal_callback_decorator(self):
|
2012-09-07 17:43:51 +00:00
|
|
|
self.assertEqual(self.testvalue, 'override')
|
2011-05-18 12:08:53 +00:00
|
|
|
|
2009-12-12 17:51:51 +00:00
|
|
|
#
|
|
|
|
# Regression tests for #10130: deleting settings.
|
|
|
|
#
|
|
|
|
|
|
|
|
def test_settings_delete(self):
|
|
|
|
settings.TEST = 'test'
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
del settings.TEST
|
2018-04-17 16:55:12 +00:00
|
|
|
msg = "'Settings' object has no attribute 'TEST'"
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
getattr(settings, 'TEST')
|
2009-12-12 17:51:51 +00:00
|
|
|
|
|
|
|
def test_settings_delete_wrapped(self):
|
2017-05-28 19:37:21 +00:00
|
|
|
with self.assertRaisesMessage(TypeError, "can't delete _wrapped."):
|
2016-01-17 11:26:39 +00:00
|
|
|
delattr(settings, '_wrapped')
|
2011-01-02 01:33:11 +00:00
|
|
|
|
2012-09-04 07:41:12 +00:00
|
|
|
def test_override_settings_delete(self):
|
|
|
|
"""
|
2013-07-28 01:45:25 +00:00
|
|
|
Allow deletion of a setting in an overridden settings set (#18824)
|
2012-09-04 07:41:12 +00:00
|
|
|
"""
|
|
|
|
previous_i18n = settings.USE_I18N
|
2014-04-12 13:31:37 +00:00
|
|
|
previous_l10n = settings.USE_L10N
|
2012-09-04 07:41:12 +00:00
|
|
|
with self.settings(USE_I18N=False):
|
|
|
|
del settings.USE_I18N
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'USE_I18N')
|
2014-04-12 13:31:37 +00:00
|
|
|
# Should also work for a non-overridden setting
|
|
|
|
del settings.USE_L10N
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'USE_L10N')
|
2016-04-23 12:39:15 +00:00
|
|
|
self.assertNotIn('USE_I18N', dir(settings))
|
|
|
|
self.assertNotIn('USE_L10N', dir(settings))
|
2012-09-04 07:41:12 +00:00
|
|
|
self.assertEqual(settings.USE_I18N, previous_i18n)
|
2014-04-12 13:31:37 +00:00
|
|
|
self.assertEqual(settings.USE_L10N, previous_l10n)
|
2012-09-04 07:41:12 +00:00
|
|
|
|
2013-04-19 09:44:47 +00:00
|
|
|
def test_override_settings_nested(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
override_settings uses the actual _wrapped attribute at
|
2013-04-19 09:44:47 +00:00
|
|
|
runtime, not when it was instantiated.
|
|
|
|
"""
|
|
|
|
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST2')
|
2013-04-19 09:44:47 +00:00
|
|
|
|
|
|
|
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)
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST2')
|
2013-04-19 09:44:47 +00:00
|
|
|
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST2')
|
2013-04-19 09:44:47 +00:00
|
|
|
|
2017-07-27 00:00:28 +00:00
|
|
|
def test_no_secret_key(self):
|
|
|
|
settings_module = ModuleType('fake_settings_module')
|
|
|
|
sys.modules['fake_settings_module'] = settings_module
|
2018-05-27 00:58:41 +00:00
|
|
|
msg = 'The SECRET_KEY setting must not be empty.'
|
2017-07-27 00:00:28 +00:00
|
|
|
try:
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
Settings('fake_settings_module')
|
|
|
|
finally:
|
|
|
|
del sys.modules['fake_settings_module']
|
|
|
|
|
|
|
|
def test_no_settings_module(self):
|
|
|
|
msg = (
|
|
|
|
'Requested setting%s, but settings are not configured. You '
|
|
|
|
'must either define the environment variable DJANGO_SETTINGS_MODULE '
|
|
|
|
'or call settings.configure() before accessing settings.'
|
|
|
|
)
|
|
|
|
orig_settings = os.environ[ENVIRONMENT_VARIABLE]
|
|
|
|
os.environ[ENVIRONMENT_VARIABLE] = ''
|
|
|
|
try:
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg % 's'):
|
|
|
|
settings._setup()
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg % ' TEST'):
|
|
|
|
settings._setup('TEST')
|
|
|
|
finally:
|
|
|
|
os.environ[ENVIRONMENT_VARIABLE] = orig_settings
|
|
|
|
|
|
|
|
def test_already_configured(self):
|
|
|
|
with self.assertRaisesMessage(RuntimeError, 'Settings already configured.'):
|
|
|
|
settings.configure()
|
|
|
|
|
2019-03-03 00:23:18 +00:00
|
|
|
def test_nonupper_settings_prohibited_in_configure(self):
|
|
|
|
s = LazySettings()
|
|
|
|
with self.assertRaisesMessage(TypeError, "Setting 'foo' must be uppercase."):
|
|
|
|
s.configure(foo='bar')
|
|
|
|
|
|
|
|
def test_nonupper_settings_ignored_in_default_settings(self):
|
|
|
|
s = LazySettings()
|
|
|
|
s.configure(SimpleNamespace(foo='bar'))
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(s, 'foo')
|
|
|
|
|
2017-07-27 00:00:28 +00:00
|
|
|
@requires_tz_support
|
|
|
|
@mock.patch('django.conf.global_settings.TIME_ZONE', 'test')
|
|
|
|
def test_incorrect_timezone(self):
|
|
|
|
with self.assertRaisesMessage(ValueError, 'Incorrect timezone setting: test'):
|
|
|
|
settings._setup()
|
|
|
|
|
2011-05-06 13:29:24 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class TestComplexSettingOverride(SimpleTestCase):
|
2013-05-18 11:43:51 +00:00
|
|
|
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
|
2014-10-28 10:02:56 +00:00
|
|
|
self.assertNotIn('TEST_WARN', signals.COMPLEX_OVERRIDE_SETTINGS)
|
2013-05-18 11:43:51 +00:00
|
|
|
|
|
|
|
def test_complex_override_warning(self):
|
|
|
|
"""Regression test for #19031"""
|
2018-04-27 21:18:15 +00:00
|
|
|
msg = 'Overriding setting TEST_WARN can lead to unexpected behavior.'
|
|
|
|
with self.assertWarnsMessage(UserWarning, msg) as cm:
|
2013-12-20 10:04:58 +00:00
|
|
|
with override_settings(TEST_WARN='override'):
|
|
|
|
self.assertEqual(settings.TEST_WARN, 'override')
|
2018-04-27 21:18:15 +00:00
|
|
|
self.assertEqual(cm.filename, __file__)
|
2013-05-18 11:43:51 +00:00
|
|
|
|
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class SecureProxySslHeaderTest(SimpleTestCase):
|
2011-12-16 22:06:06 +00:00
|
|
|
|
2016-12-22 23:52:50 +00:00
|
|
|
@override_settings(SECURE_PROXY_SSL_HEADER=None)
|
2011-12-16 22:06:06 +00:00
|
|
|
def test_none(self):
|
|
|
|
req = HttpRequest()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(req.is_secure(), False)
|
2011-12-16 22:06:06 +00:00
|
|
|
|
2019-08-12 10:51:26 +00:00
|
|
|
@override_settings(SECURE_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO', 'https'))
|
2011-12-16 22:06:06 +00:00
|
|
|
def test_set_without_xheader(self):
|
|
|
|
req = HttpRequest()
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(req.is_secure(), False)
|
2011-12-16 22:06:06 +00:00
|
|
|
|
2019-08-12 10:51:26 +00:00
|
|
|
@override_settings(SECURE_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO', 'https'))
|
2011-12-16 22:06:06 +00:00
|
|
|
def test_set_with_xheader_wrong(self):
|
|
|
|
req = HttpRequest()
|
2019-08-12 10:51:26 +00:00
|
|
|
req.META['HTTP_X_FORWARDED_PROTO'] = 'wrongvalue'
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(req.is_secure(), False)
|
2011-12-16 22:06:06 +00:00
|
|
|
|
2019-08-12 10:51:26 +00:00
|
|
|
@override_settings(SECURE_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO', 'https'))
|
2011-12-16 22:06:06 +00:00
|
|
|
def test_set_with_xheader_right(self):
|
|
|
|
req = HttpRequest()
|
2019-08-12 10:51:26 +00:00
|
|
|
req.META['HTTP_X_FORWARDED_PROTO'] = 'https'
|
2016-06-16 18:19:18 +00:00
|
|
|
self.assertIs(req.is_secure(), True)
|
2014-01-21 03:25:33 +00:00
|
|
|
|
2019-08-12 10:51:26 +00:00
|
|
|
@override_settings(SECURE_PROXY_SSL_HEADER=('HTTP_X_FORWARDED_PROTO', 'https'))
|
2019-06-13 08:57:29 +00:00
|
|
|
def test_xheader_preferred_to_underlying_request(self):
|
|
|
|
class ProxyRequest(HttpRequest):
|
|
|
|
def _get_scheme(self):
|
|
|
|
"""Proxy always connecting via HTTPS"""
|
|
|
|
return 'https'
|
|
|
|
|
|
|
|
# Client connects via HTTP.
|
|
|
|
req = ProxyRequest()
|
2019-08-12 10:51:26 +00:00
|
|
|
req.META['HTTP_X_FORWARDED_PROTO'] = 'http'
|
2019-06-13 08:57:29 +00:00
|
|
|
self.assertIs(req.is_secure(), False)
|
|
|
|
|
2014-01-21 03:25:33 +00:00
|
|
|
|
2015-04-17 21:38:20 +00:00
|
|
|
class IsOverriddenTest(SimpleTestCase):
|
2014-01-21 03:25:33 +00:00
|
|
|
def test_configure(self):
|
|
|
|
s = LazySettings()
|
|
|
|
s.configure(SECRET_KEY='foo')
|
|
|
|
|
|
|
|
self.assertTrue(s.is_overridden('SECRET_KEY'))
|
|
|
|
|
|
|
|
def test_module(self):
|
|
|
|
settings_module = ModuleType('fake_settings_module')
|
|
|
|
settings_module.SECRET_KEY = 'foo'
|
|
|
|
sys.modules['fake_settings_module'] = settings_module
|
|
|
|
try:
|
|
|
|
s = Settings('fake_settings_module')
|
|
|
|
|
|
|
|
self.assertTrue(s.is_overridden('SECRET_KEY'))
|
2014-12-17 21:10:57 +00:00
|
|
|
self.assertFalse(s.is_overridden('ALLOWED_HOSTS'))
|
2014-01-21 03:25:33 +00:00
|
|
|
finally:
|
|
|
|
del sys.modules['fake_settings_module']
|
|
|
|
|
|
|
|
def test_override(self):
|
2014-12-17 21:10:57 +00:00
|
|
|
self.assertFalse(settings.is_overridden('ALLOWED_HOSTS'))
|
|
|
|
with override_settings(ALLOWED_HOSTS=[]):
|
|
|
|
self.assertTrue(settings.is_overridden('ALLOWED_HOSTS'))
|
2014-06-10 20:52:14 +00:00
|
|
|
|
2015-07-28 14:37:41 +00:00
|
|
|
def test_unevaluated_lazysettings_repr(self):
|
|
|
|
lazy_settings = LazySettings()
|
|
|
|
expected = '<LazySettings [Unevaluated]>'
|
|
|
|
self.assertEqual(repr(lazy_settings), expected)
|
|
|
|
|
|
|
|
def test_evaluated_lazysettings_repr(self):
|
|
|
|
lazy_settings = LazySettings()
|
|
|
|
module = os.environ.get(ENVIRONMENT_VARIABLE)
|
|
|
|
expected = '<LazySettings "%s">' % module
|
|
|
|
# Force evaluation of the lazy object.
|
|
|
|
lazy_settings.APPEND_SLASH
|
|
|
|
self.assertEqual(repr(lazy_settings), expected)
|
|
|
|
|
|
|
|
def test_usersettingsholder_repr(self):
|
|
|
|
lazy_settings = LazySettings()
|
|
|
|
lazy_settings.configure(APPEND_SLASH=False)
|
|
|
|
expected = '<UserSettingsHolder>'
|
|
|
|
self.assertEqual(repr(lazy_settings._wrapped), expected)
|
|
|
|
|
|
|
|
def test_settings_repr(self):
|
|
|
|
module = os.environ.get(ENVIRONMENT_VARIABLE)
|
|
|
|
lazy_settings = Settings(module)
|
|
|
|
expected = '<Settings "%s">' % module
|
|
|
|
self.assertEqual(repr(lazy_settings), expected)
|
|
|
|
|
2014-06-10 20:52:14 +00:00
|
|
|
|
2015-01-21 16:55:57 +00:00
|
|
|
class TestListSettings(unittest.TestCase):
|
2014-06-10 20:52:14 +00:00
|
|
|
"""
|
2015-01-21 16:55:57 +00:00
|
|
|
Make sure settings that should be lists or tuples throw
|
|
|
|
ImproperlyConfigured if they are set to a string instead of a list or tuple.
|
2014-06-10 20:52:14 +00:00
|
|
|
"""
|
2015-01-21 16:55:57 +00:00
|
|
|
list_or_tuple_settings = (
|
2014-11-17 22:26:04 +00:00
|
|
|
"INSTALLED_APPS",
|
|
|
|
"TEMPLATE_DIRS",
|
|
|
|
"LOCALE_PATHS",
|
|
|
|
)
|
2014-06-10 20:52:14 +00:00
|
|
|
|
|
|
|
def test_tuple_settings(self):
|
|
|
|
settings_module = ModuleType('fake_settings_module')
|
|
|
|
settings_module.SECRET_KEY = 'foo'
|
2015-01-21 16:55:57 +00:00
|
|
|
for setting in self.list_or_tuple_settings:
|
|
|
|
setattr(settings_module, setting, ('non_list_or_tuple_value'))
|
2014-06-10 20:52:14 +00:00
|
|
|
sys.modules['fake_settings_module'] = settings_module
|
|
|
|
try:
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
2014-06-11 07:22:30 +00:00
|
|
|
Settings('fake_settings_module')
|
2014-06-10 20:52:14 +00:00
|
|
|
finally:
|
|
|
|
del sys.modules['fake_settings_module']
|
|
|
|
delattr(settings_module, setting)
|
2018-06-19 21:10:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SettingChangeEnterException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class SettingChangeExitException(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class OverrideSettingsIsolationOnExceptionTests(SimpleTestCase):
|
|
|
|
"""
|
|
|
|
The override_settings context manager restore settings if one of the
|
|
|
|
receivers of "setting_changed" signal fails. Check the three cases of
|
|
|
|
receiver failure detailed in receiver(). In each case, ALL receivers are
|
|
|
|
called when exiting the context manager.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
signals.setting_changed.connect(self.receiver)
|
|
|
|
self.addCleanup(signals.setting_changed.disconnect, self.receiver)
|
|
|
|
# Create a spy that's connected to the `setting_changed` signal and
|
|
|
|
# executed AFTER `self.receiver`.
|
|
|
|
self.spy_receiver = mock.Mock()
|
|
|
|
signals.setting_changed.connect(self.spy_receiver)
|
|
|
|
self.addCleanup(signals.setting_changed.disconnect, self.spy_receiver)
|
|
|
|
|
|
|
|
def receiver(self, **kwargs):
|
|
|
|
"""
|
|
|
|
A receiver that fails while certain settings are being changed.
|
|
|
|
- SETTING_BOTH raises an error while receiving the signal
|
|
|
|
on both entering and exiting the context manager.
|
|
|
|
- SETTING_ENTER raises an error only on enter.
|
|
|
|
- SETTING_EXIT raises an error only on exit.
|
|
|
|
"""
|
|
|
|
setting = kwargs['setting']
|
|
|
|
enter = kwargs['enter']
|
|
|
|
if setting in ('SETTING_BOTH', 'SETTING_ENTER') and enter:
|
|
|
|
raise SettingChangeEnterException
|
|
|
|
if setting in ('SETTING_BOTH', 'SETTING_EXIT') and not enter:
|
|
|
|
raise SettingChangeExitException
|
|
|
|
|
|
|
|
def check_settings(self):
|
|
|
|
"""Assert that settings for these tests aren't present."""
|
|
|
|
self.assertFalse(hasattr(settings, 'SETTING_BOTH'))
|
|
|
|
self.assertFalse(hasattr(settings, 'SETTING_ENTER'))
|
|
|
|
self.assertFalse(hasattr(settings, 'SETTING_EXIT'))
|
|
|
|
self.assertFalse(hasattr(settings, 'SETTING_PASS'))
|
|
|
|
|
|
|
|
def check_spy_receiver_exit_calls(self, call_count):
|
|
|
|
"""
|
|
|
|
Assert that `self.spy_receiver` was called exactly `call_count` times
|
|
|
|
with the ``enter=False`` keyword argument.
|
|
|
|
"""
|
|
|
|
kwargs_with_exit = [
|
|
|
|
kwargs for args, kwargs in self.spy_receiver.call_args_list
|
|
|
|
if ('enter', False) in kwargs.items()
|
|
|
|
]
|
|
|
|
self.assertEqual(len(kwargs_with_exit), call_count)
|
|
|
|
|
|
|
|
def test_override_settings_both(self):
|
|
|
|
"""Receiver fails on both enter and exit."""
|
|
|
|
with self.assertRaises(SettingChangeEnterException):
|
|
|
|
with override_settings(SETTING_PASS='BOTH', SETTING_BOTH='BOTH'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.check_settings()
|
|
|
|
# Two settings were touched, so expect two calls of `spy_receiver`.
|
|
|
|
self.check_spy_receiver_exit_calls(call_count=2)
|
|
|
|
|
|
|
|
def test_override_settings_enter(self):
|
|
|
|
"""Receiver fails on enter only."""
|
|
|
|
with self.assertRaises(SettingChangeEnterException):
|
|
|
|
with override_settings(SETTING_PASS='ENTER', SETTING_ENTER='ENTER'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.check_settings()
|
|
|
|
# Two settings were touched, so expect two calls of `spy_receiver`.
|
|
|
|
self.check_spy_receiver_exit_calls(call_count=2)
|
|
|
|
|
|
|
|
def test_override_settings_exit(self):
|
|
|
|
"""Receiver fails on exit only."""
|
|
|
|
with self.assertRaises(SettingChangeExitException):
|
|
|
|
with override_settings(SETTING_PASS='EXIT', SETTING_EXIT='EXIT'):
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.check_settings()
|
|
|
|
# Two settings were touched, so expect two calls of `spy_receiver`.
|
|
|
|
self.check_spy_receiver_exit_calls(call_count=2)
|
|
|
|
|
|
|
|
def test_override_settings_reusable_on_enter(self):
|
|
|
|
"""
|
|
|
|
Error is raised correctly when reusing the same override_settings
|
|
|
|
instance.
|
|
|
|
"""
|
|
|
|
@override_settings(SETTING_ENTER='ENTER')
|
|
|
|
def decorated_function():
|
|
|
|
pass
|
|
|
|
|
|
|
|
with self.assertRaises(SettingChangeEnterException):
|
|
|
|
decorated_function()
|
|
|
|
signals.setting_changed.disconnect(self.receiver)
|
|
|
|
# This call shouldn't raise any errors.
|
|
|
|
decorated_function()
|