2013-12-23 03:22:56 +08:00
|
|
|
import os
|
2014-01-21 11:25:33 +08:00
|
|
|
import sys
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2012-05-03 21:27:01 +08:00
|
|
|
import warnings
|
2015-01-28 20:35:27 +08:00
|
|
|
from types import ModuleType
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2015-07-28 22:37:41 +08:00
|
|
|
from django.conf import ENVIRONMENT_VARIABLE, LazySettings, Settings, settings
|
2012-03-31 21:01:59 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2011-12-17 06:06:06 +08:00
|
|
|
from django.http import HttpRequest
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.test import (
|
|
|
|
SimpleTestCase, TestCase, TransactionTestCase, modify_settings,
|
|
|
|
override_settings, signals,
|
|
|
|
)
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
|
2013-12-23 19:39:19 +08:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'prepend': ['b'],
|
|
|
|
'append': ['d'],
|
|
|
|
'remove': ['a', 'e']
|
|
|
|
})
|
2016-04-08 10:04:45 +08:00
|
|
|
@override_settings(ITEMS=['a', 'c', 'e'], ITEMS_OUTER=[1, 2, 3], TEST='override', TEST_OUTER='outer')
|
2011-08-11 06:26:34 +08:00
|
|
|
class FullyDecoratedTranTestCase(TransactionTestCase):
|
|
|
|
|
2013-06-04 14:09:29 +08:00
|
|
|
available_apps = []
|
|
|
|
|
2011-08-11 06:26:34 +08:00
|
|
|
def test_override(self):
|
2013-12-23 19:39:19 +08:00
|
|
|
self.assertListEqual(settings.ITEMS, ['b', 'c', 'd'])
|
|
|
|
self.assertListEqual(settings.ITEMS_OUTER, [1, 2, 3])
|
2011-08-11 06:26:34 +08:00
|
|
|
self.assertEqual(settings.TEST, 'override')
|
2013-04-19 17:44:47 +08:00
|
|
|
self.assertEqual(settings.TEST_OUTER, 'outer')
|
2011-08-11 06:26:34 +08:00
|
|
|
|
2013-12-23 19:39:19 +08:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': ['e', 'f'],
|
|
|
|
'prepend': ['a'],
|
|
|
|
'remove': ['d', 'c'],
|
|
|
|
})
|
|
|
|
def test_method_list_override(self):
|
|
|
|
self.assertListEqual(settings.ITEMS, ['a', 'b', 'e', 'f'])
|
|
|
|
self.assertListEqual(settings.ITEMS_OUTER, [1, 2, 3])
|
|
|
|
|
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': ['b'],
|
|
|
|
'prepend': ['d'],
|
|
|
|
'remove': ['a', 'c', 'e'],
|
|
|
|
})
|
|
|
|
def test_method_list_override_no_ops(self):
|
|
|
|
self.assertListEqual(settings.ITEMS, ['b', 'd'])
|
|
|
|
|
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': 'e',
|
|
|
|
'prepend': 'a',
|
|
|
|
'remove': 'c',
|
|
|
|
})
|
|
|
|
def test_method_list_override_strings(self):
|
|
|
|
self.assertListEqual(settings.ITEMS, ['a', 'b', 'd', 'e'])
|
|
|
|
|
|
|
|
@modify_settings(ITEMS={'remove': ['b', 'd']})
|
|
|
|
@modify_settings(ITEMS={'append': ['b'], 'prepend': ['d']})
|
|
|
|
def test_method_list_override_nested_order(self):
|
|
|
|
self.assertListEqual(settings.ITEMS, ['d', 'c', 'b'])
|
|
|
|
|
2011-08-11 06:26:34 +08:00
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
2013-04-19 17:44:47 +08:00
|
|
|
self.assertEqual(settings.TEST_OUTER, 'outer')
|
2011-08-11 06:26:34 +08:00
|
|
|
|
2011-08-23 08:32:35 +08:00
|
|
|
def test_decorated_testcase_name(self):
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(FullyDecoratedTranTestCase.__name__, 'FullyDecoratedTranTestCase')
|
2011-08-23 08:32:35 +08:00
|
|
|
|
2011-09-05 06:51:53 +08:00
|
|
|
def test_decorated_testcase_module(self):
|
2012-05-03 22:39:16 +08:00
|
|
|
self.assertEqual(FullyDecoratedTranTestCase.__module__, __name__)
|
2011-09-05 06:51:53 +08:00
|
|
|
|
2011-08-11 06:26:34 +08:00
|
|
|
|
2013-12-23 19:39:19 +08:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'prepend': ['b'],
|
|
|
|
'append': ['d'],
|
|
|
|
'remove': ['a', 'e']
|
|
|
|
})
|
|
|
|
@override_settings(ITEMS=['a', 'c', 'e'], TEST='override')
|
2011-06-12 04:40:02 +08:00
|
|
|
class FullyDecoratedTestCase(TestCase):
|
|
|
|
|
|
|
|
def test_override(self):
|
2013-12-23 19:39:19 +08:00
|
|
|
self.assertListEqual(settings.ITEMS, ['b', 'c', 'd'])
|
2011-06-12 04:40:02 +08:00
|
|
|
self.assertEqual(settings.TEST, 'override')
|
|
|
|
|
2013-12-23 19:39:19 +08:00
|
|
|
@modify_settings(ITEMS={
|
|
|
|
'append': 'e',
|
|
|
|
'prepend': 'a',
|
|
|
|
'remove': 'c',
|
|
|
|
})
|
2011-06-12 04:40:02 +08:00
|
|
|
@override_settings(TEST='override2')
|
|
|
|
def test_method_override(self):
|
2013-12-23 19:39:19 +08:00
|
|
|
self.assertListEqual(settings.ITEMS, ['a', 'b', 'd', 'e'])
|
2011-06-12 04:40:02 +08:00
|
|
|
self.assertEqual(settings.TEST, 'override2')
|
|
|
|
|
2011-10-08 16:16:17 +08: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 17:08:29 +08:00
|
|
|
@override_settings(TEST='override')
|
2011-10-08 16:16:17 +08:00
|
|
|
class ClassDecoratedTestCase(ClassDecoratedTestCaseSuper):
|
2014-10-19 02:03:10 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2015-05-10 06:23:57 +08:00
|
|
|
super(ClassDecoratedTestCase, cls).setUpClass()
|
2014-10-19 02:03:10 +08:00
|
|
|
cls.foo = getattr(settings, 'TEST', 'BUG')
|
|
|
|
|
2011-10-08 16:16:17 +08:00
|
|
|
def test_override(self):
|
|
|
|
self.assertEqual(settings.TEST, 'override')
|
|
|
|
|
2014-10-19 02:03:10 +08:00
|
|
|
def test_setupclass_override(self):
|
2015-01-20 22:54:12 +08:00
|
|
|
"""Test that settings are overridden within setUpClass -- refs #21281"""
|
2014-10-19 02:03:10 +08:00
|
|
|
self.assertEqual(self.foo, 'override')
|
|
|
|
|
2011-10-08 16:16:17 +08: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.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
super(ClassDecoratedTestCase, self).test_max_recursion_error()
|
2012-04-29 00:09:37 +08:00
|
|
|
except RuntimeError:
|
2011-10-08 16:16:17 +08:00
|
|
|
self.fail()
|
|
|
|
|
|
|
|
|
2013-12-23 19:39:19 +08:00
|
|
|
@modify_settings(ITEMS={'append': 'mother'})
|
|
|
|
@override_settings(ITEMS=['father'], TEST='override-parent')
|
2013-10-14 16:44:55 +08:00
|
|
|
class ParentDecoratedTestCase(TestCase):
|
|
|
|
pass
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2013-12-23 19:39:19 +08:00
|
|
|
@modify_settings(ITEMS={'append': ['child']})
|
2013-10-14 16:44:55 +08:00
|
|
|
@override_settings(TEST='override-child')
|
|
|
|
class ChildDecoratedTestCase(ParentDecoratedTestCase):
|
|
|
|
def test_override_settings_inheritance(self):
|
2013-12-23 19:39:19 +08:00
|
|
|
self.assertEqual(settings.ITEMS, ['father', 'mother', 'child'])
|
2013-10-14 16:44:55 +08:00
|
|
|
self.assertEqual(settings.TEST, 'override-child')
|
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class SettingsTests(SimpleTestCase):
|
2012-09-08 01:43:51 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.testvalue = None
|
|
|
|
signals.setting_changed.connect(self.signal_callback)
|
2011-05-18 20:08:53 +08:00
|
|
|
|
2012-09-08 01:43:51 +08:00
|
|
|
def tearDown(self):
|
|
|
|
signals.setting_changed.disconnect(self.signal_callback)
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2012-09-08 01:43:51 +08:00
|
|
|
def signal_callback(self, sender, setting, value, **kwargs):
|
|
|
|
if setting == 'TEST':
|
|
|
|
self.testvalue = value
|
2009-12-13 01:51:51 +08:00
|
|
|
|
2011-05-06 21:29:24 +08: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 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-06 21:29:24 +08:00
|
|
|
with self.settings(TEST='override'):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
settings.TEST = 'test'
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2009-12-13 01:51:51 +08:00
|
|
|
|
2011-05-18 20:08:53 +08:00
|
|
|
@override_settings(TEST='override')
|
|
|
|
def test_decorator(self):
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
|
|
|
|
def test_context_manager(self):
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 20:08:53 +08:00
|
|
|
override = override_settings(TEST='override')
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 20:08:53 +08:00
|
|
|
override.enable()
|
|
|
|
self.assertEqual('override', settings.TEST)
|
|
|
|
override.disable()
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
def test_class_decorator(self):
|
2012-11-25 06:47:41 +08:00
|
|
|
# SimpleTestCase can be decorated by override_settings, but not ut.TestCase
|
|
|
|
class SimpleTestCaseSubclass(SimpleTestCase):
|
2012-11-25 06:43:13 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
class UnittestTestCaseSubclass(unittest.TestCase):
|
|
|
|
pass
|
|
|
|
|
2012-11-25 06:47:41 +08:00
|
|
|
decorated = override_settings(TEST='override')(SimpleTestCaseSubclass)
|
2012-11-25 06:43:13 +08:00
|
|
|
self.assertIsInstance(decorated, type)
|
2012-11-25 06:47:41 +08:00
|
|
|
self.assertTrue(issubclass(decorated, SimpleTestCase))
|
2012-11-25 06:43:13 +08:00
|
|
|
|
2016-04-08 10:04:45 +08:00
|
|
|
with self.assertRaisesMessage(Exception, "Only subclasses of Django SimpleTestCase"):
|
2012-11-25 06:43:13 +08:00
|
|
|
decorated = override_settings(TEST='override')(UnittestTestCaseSubclass)
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
def test_signal_callback_context_manager(self):
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2011-05-18 20:08:53 +08:00
|
|
|
with self.settings(TEST='override'):
|
2012-09-08 01:43:51 +08:00
|
|
|
self.assertEqual(self.testvalue, 'override')
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIsNone(self.testvalue)
|
2011-05-18 20:08:53 +08:00
|
|
|
|
|
|
|
@override_settings(TEST='override')
|
|
|
|
def test_signal_callback_decorator(self):
|
2012-09-08 01:43:51 +08:00
|
|
|
self.assertEqual(self.testvalue, 'override')
|
2011-05-18 20:08:53 +08:00
|
|
|
|
2009-12-13 01:51:51 +08:00
|
|
|
#
|
|
|
|
# Regression tests for #10130: deleting settings.
|
|
|
|
#
|
|
|
|
|
|
|
|
def test_settings_delete(self):
|
|
|
|
settings.TEST = 'test'
|
|
|
|
self.assertEqual('test', settings.TEST)
|
|
|
|
del settings.TEST
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
2009-12-13 01:51:51 +08:00
|
|
|
|
|
|
|
def test_settings_delete_wrapped(self):
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(TypeError):
|
|
|
|
delattr(settings, '_wrapped')
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2012-09-04 15:41:12 +08:00
|
|
|
def test_override_settings_delete(self):
|
|
|
|
"""
|
2013-07-28 09:45:25 +08:00
|
|
|
Allow deletion of a setting in an overridden settings set (#18824)
|
2012-09-04 15:41:12 +08:00
|
|
|
"""
|
|
|
|
previous_i18n = settings.USE_I18N
|
2014-04-12 21:31:37 +08:00
|
|
|
previous_l10n = settings.USE_L10N
|
2012-09-04 15:41:12 +08:00
|
|
|
with self.settings(USE_I18N=False):
|
|
|
|
del settings.USE_I18N
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'USE_I18N')
|
2014-04-12 21:31:37 +08:00
|
|
|
# Should also work for a non-overridden setting
|
|
|
|
del settings.USE_L10N
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'USE_L10N')
|
2016-04-23 20:39:15 +08:00
|
|
|
self.assertNotIn('USE_I18N', dir(settings))
|
|
|
|
self.assertNotIn('USE_L10N', dir(settings))
|
2012-09-04 15:41:12 +08:00
|
|
|
self.assertEqual(settings.USE_I18N, previous_i18n)
|
2014-04-12 21:31:37 +08:00
|
|
|
self.assertEqual(settings.USE_L10N, previous_l10n)
|
2012-09-04 15:41:12 +08:00
|
|
|
|
2013-04-19 17:44:47 +08:00
|
|
|
def test_override_settings_nested(self):
|
|
|
|
"""
|
|
|
|
Test that override_settings uses the actual _wrapped attribute at
|
|
|
|
runtime, not when it was instantiated.
|
|
|
|
"""
|
|
|
|
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST2')
|
2013-04-19 17:44:47 +08: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 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST2')
|
2013-04-19 17:44:47 +08:00
|
|
|
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST')
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
getattr(settings, 'TEST2')
|
2013-04-19 17:44:47 +08:00
|
|
|
|
2011-05-06 21:29:24 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TestComplexSettingOverride(SimpleTestCase):
|
2013-05-18 19:43:51 +08: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 18:02:56 +08:00
|
|
|
self.assertNotIn('TEST_WARN', signals.COMPLEX_OVERRIDE_SETTINGS)
|
2013-05-18 19:43:51 +08:00
|
|
|
|
|
|
|
def test_complex_override_warning(self):
|
|
|
|
"""Regression test for #19031"""
|
|
|
|
with warnings.catch_warnings(record=True) as w:
|
|
|
|
warnings.simplefilter("always")
|
|
|
|
|
2013-12-20 18:04:58 +08:00
|
|
|
with override_settings(TEST_WARN='override'):
|
|
|
|
self.assertEqual(settings.TEST_WARN, 'override')
|
2013-05-18 19:43:51 +08:00
|
|
|
|
|
|
|
self.assertEqual(len(w), 1)
|
2013-12-23 03:22:56 +08:00
|
|
|
# File extension may by .py, .pyc, etc. Compare only basename.
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual(os.path.splitext(w[0].filename)[0], os.path.splitext(__file__)[0])
|
|
|
|
self.assertEqual(str(w[0].message), 'Overriding setting TEST_WARN can lead to unexpected behavior.')
|
2013-05-18 19:43:51 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class TrailingSlashURLTests(SimpleTestCase):
|
2012-03-31 21:11:02 +08: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 09:33:11 +08:00
|
|
|
settings_module = settings
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self._original_media_url = self.settings_module.MEDIA_URL
|
2012-03-31 21:11:02 +08:00
|
|
|
self._original_static_url = self.settings_module.STATIC_URL
|
2011-01-02 09:33:11 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.settings_module.MEDIA_URL = self._original_media_url
|
2012-03-31 21:11:02 +08:00
|
|
|
self.settings_module.STATIC_URL = self._original_static_url
|
2011-01-02 09:33:11 +08:00
|
|
|
|
|
|
|
def test_blank(self):
|
|
|
|
"""
|
2012-03-31 21:11:02 +08:00
|
|
|
The empty string is accepted, even though it doesn't end in a slash.
|
2011-01-02 09:33:11 +08:00
|
|
|
"""
|
|
|
|
self.settings_module.MEDIA_URL = ''
|
|
|
|
self.assertEqual('', self.settings_module.MEDIA_URL)
|
|
|
|
|
2012-03-31 21:11:02 +08:00
|
|
|
self.settings_module.STATIC_URL = ''
|
|
|
|
self.assertEqual('', self.settings_module.STATIC_URL)
|
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
def test_end_slash(self):
|
|
|
|
"""
|
2012-03-31 21:11:02 +08:00
|
|
|
It works if the value ends in a slash.
|
2011-01-02 09:33:11 +08:00
|
|
|
"""
|
|
|
|
self.settings_module.MEDIA_URL = '/foo/'
|
|
|
|
self.assertEqual('/foo/', self.settings_module.MEDIA_URL)
|
|
|
|
|
|
|
|
self.settings_module.MEDIA_URL = 'http://media.foo.com/'
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual('http://media.foo.com/', self.settings_module.MEDIA_URL)
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2012-03-31 21:11:02 +08:00
|
|
|
self.settings_module.STATIC_URL = '/foo/'
|
|
|
|
self.assertEqual('/foo/', self.settings_module.STATIC_URL)
|
|
|
|
|
|
|
|
self.settings_module.STATIC_URL = 'http://static.foo.com/'
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual('http://static.foo.com/', self.settings_module.STATIC_URL)
|
2012-03-31 21:11:02 +08:00
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
def test_no_end_slash(self):
|
|
|
|
"""
|
2012-03-31 21:11:02 +08:00
|
|
|
An ImproperlyConfigured exception is raised if the value doesn't end
|
|
|
|
in a slash.
|
2011-01-02 09:33:11 +08:00
|
|
|
"""
|
2012-03-31 21:11:02 +08:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.MEDIA_URL = '/foo'
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2012-03-31 21:11:02 +08:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.MEDIA_URL = 'http://media.foo.com'
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2012-03-31 21:11:02 +08:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.STATIC_URL = '/foo'
|
2012-03-31 21:01:59 +08:00
|
|
|
|
2012-03-31 21:11:02 +08:00
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
|
|
|
self.settings_module.STATIC_URL = 'http://static.foo.com'
|
2012-03-31 21:01:59 +08:00
|
|
|
|
2011-01-02 09:33:11 +08:00
|
|
|
def test_double_slash(self):
|
|
|
|
"""
|
2012-03-31 21:11:02 +08:00
|
|
|
If the value ends in more than one slash, presume they know what
|
2011-01-02 09:33:11 +08:00
|
|
|
they're doing.
|
|
|
|
"""
|
2014-09-09 03:16:59 +08:00
|
|
|
self.settings_module.MEDIA_URL = '/wrong//'
|
|
|
|
self.assertEqual('/wrong//', self.settings_module.MEDIA_URL)
|
2011-01-02 09:33:11 +08:00
|
|
|
|
2014-09-09 03:16:59 +08:00
|
|
|
self.settings_module.MEDIA_URL = 'http://media.foo.com/wrong//'
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual('http://media.foo.com/wrong//', self.settings_module.MEDIA_URL)
|
2011-05-13 20:15:52 +08:00
|
|
|
|
2014-09-09 03:16:59 +08:00
|
|
|
self.settings_module.STATIC_URL = '/wrong//'
|
|
|
|
self.assertEqual('/wrong//', self.settings_module.STATIC_URL)
|
2012-03-31 21:11:02 +08:00
|
|
|
|
2014-09-09 03:16:59 +08:00
|
|
|
self.settings_module.STATIC_URL = 'http://static.foo.com/wrong//'
|
2016-04-08 10:04:45 +08:00
|
|
|
self.assertEqual('http://static.foo.com/wrong//', self.settings_module.STATIC_URL)
|
2012-03-31 21:11:02 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class SecureProxySslHeaderTest(SimpleTestCase):
|
2011-12-17 06:06:06 +08:00
|
|
|
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()
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(req.is_secure(), False)
|
2011-12-17 06:06:06 +08:00
|
|
|
|
|
|
|
def test_set_without_xheader(self):
|
|
|
|
self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
|
|
|
|
req = HttpRequest()
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(req.is_secure(), False)
|
2011-12-17 06:06:06 +08:00
|
|
|
|
|
|
|
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'
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(req.is_secure(), False)
|
2011-12-17 06:06:06 +08:00
|
|
|
|
|
|
|
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'
|
2016-06-17 02:19:18 +08:00
|
|
|
self.assertIs(req.is_secure(), True)
|
2014-01-21 11:25:33 +08:00
|
|
|
|
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class IsOverriddenTest(SimpleTestCase):
|
2014-01-21 11:25:33 +08: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-18 05:10:57 +08:00
|
|
|
self.assertFalse(s.is_overridden('ALLOWED_HOSTS'))
|
2014-01-21 11:25:33 +08:00
|
|
|
finally:
|
|
|
|
del sys.modules['fake_settings_module']
|
|
|
|
|
|
|
|
def test_override(self):
|
2014-12-18 05:10:57 +08:00
|
|
|
self.assertFalse(settings.is_overridden('ALLOWED_HOSTS'))
|
|
|
|
with override_settings(ALLOWED_HOSTS=[]):
|
|
|
|
self.assertTrue(settings.is_overridden('ALLOWED_HOSTS'))
|
2014-06-11 04:52:14 +08:00
|
|
|
|
2015-07-28 22:37:41 +08: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-11 04:52:14 +08:00
|
|
|
|
2015-01-22 00:55:57 +08:00
|
|
|
class TestListSettings(unittest.TestCase):
|
2014-06-11 04:52:14 +08:00
|
|
|
"""
|
2015-01-22 00:55:57 +08: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-11 04:52:14 +08:00
|
|
|
"""
|
2015-01-22 00:55:57 +08:00
|
|
|
list_or_tuple_settings = (
|
2014-11-18 06:26:04 +08:00
|
|
|
"INSTALLED_APPS",
|
|
|
|
"TEMPLATE_DIRS",
|
|
|
|
"LOCALE_PATHS",
|
|
|
|
)
|
2014-06-11 04:52:14 +08:00
|
|
|
|
|
|
|
def test_tuple_settings(self):
|
|
|
|
settings_module = ModuleType('fake_settings_module')
|
|
|
|
settings_module.SECRET_KEY = 'foo'
|
2015-01-22 00:55:57 +08:00
|
|
|
for setting in self.list_or_tuple_settings:
|
|
|
|
setattr(settings_module, setting, ('non_list_or_tuple_value'))
|
2014-06-11 04:52:14 +08:00
|
|
|
sys.modules['fake_settings_module'] = settings_module
|
|
|
|
try:
|
|
|
|
with self.assertRaises(ImproperlyConfigured):
|
2014-06-11 15:22:30 +08:00
|
|
|
Settings('fake_settings_module')
|
2014-06-11 04:52:14 +08:00
|
|
|
finally:
|
|
|
|
del sys.modules['fake_settings_module']
|
|
|
|
delattr(settings_module, setting)
|