App Engine Python SDK version 1.7.4 (2)
[gae.git] / python / lib / django_1_4 / tests / regressiontests / settings_tests / tests.py
bloba2e6c490b4ee4519a3326546a93bcf7146430c31
1 from __future__ import with_statement
3 import os
5 from django.conf import settings, global_settings
6 from django.http import HttpRequest
7 from django.test import TransactionTestCase, TestCase, signals
8 from django.test.utils import override_settings
11 # @override_settings(TEST='override')
12 class FullyDecoratedTranTestCase(TransactionTestCase):
14 def test_override(self):
15 self.assertEqual(settings.TEST, 'override')
17 @override_settings(TEST='override2')
18 def test_method_override(self):
19 self.assertEqual(settings.TEST, 'override2')
21 def test_decorated_testcase_name(self):
22 self.assertEquals(FullyDecoratedTranTestCase.__name__, 'FullyDecoratedTranTestCase')
24 def test_decorated_testcase_module(self):
25 self.assertEquals(FullyDecoratedTranTestCase.__module__, __name__)
27 FullyDecoratedTranTestCase = override_settings(TEST='override')(FullyDecoratedTranTestCase)
29 # @override_settings(TEST='override')
30 class FullyDecoratedTestCase(TestCase):
32 def test_override(self):
33 self.assertEqual(settings.TEST, 'override')
35 @override_settings(TEST='override2')
36 def test_method_override(self):
37 self.assertEqual(settings.TEST, 'override2')
39 FullyDecoratedTestCase = override_settings(TEST='override')(FullyDecoratedTestCase)
42 class ClassDecoratedTestCaseSuper(TestCase):
43 """
44 Dummy class for testing max recursion error in child class call to
45 super(). Refs #17011.
47 """
48 def test_max_recursion_error(self):
49 pass
52 class ClassDecoratedTestCase(ClassDecoratedTestCaseSuper):
53 def test_override(self):
54 self.assertEqual(settings.TEST, 'override')
56 @override_settings(TEST='override2')
57 def test_method_override(self):
58 self.assertEqual(settings.TEST, 'override2')
60 def test_max_recursion_error(self):
61 """
62 Overriding a method on a super class and then calling that method on
63 the super class should not trigger infinite recursion. See #17011.
65 """
66 try:
67 super(ClassDecoratedTestCase, self).test_max_recursion_error()
68 except RuntimeError, e:
69 self.fail()
71 ClassDecoratedTestCase = override_settings(TEST='override')(ClassDecoratedTestCase)
73 class SettingGetter(object):
74 def __init__(self):
75 self.test = getattr(settings, 'TEST', 'undefined')
77 testvalue = None
79 def signal_callback(sender, setting, value, **kwargs):
80 if setting == 'TEST':
81 global testvalue
82 testvalue = value
84 signals.setting_changed.connect(signal_callback)
86 class SettingsTests(TestCase):
88 def test_override(self):
89 settings.TEST = 'test'
90 self.assertEqual('test', settings.TEST)
91 with self.settings(TEST='override'):
92 self.assertEqual('override', settings.TEST)
93 self.assertEqual('test', settings.TEST)
94 del settings.TEST
96 def test_override_change(self):
97 settings.TEST = 'test'
98 self.assertEqual('test', settings.TEST)
99 with self.settings(TEST='override'):
100 self.assertEqual('override', settings.TEST)
101 settings.TEST = 'test2'
102 self.assertEqual('test', settings.TEST)
103 del settings.TEST
105 def test_override_doesnt_leak(self):
106 self.assertRaises(AttributeError, getattr, settings, 'TEST')
107 with self.settings(TEST='override'):
108 self.assertEqual('override', settings.TEST)
109 settings.TEST = 'test'
110 self.assertRaises(AttributeError, getattr, settings, 'TEST')
112 @override_settings(TEST='override')
113 def test_decorator(self):
114 self.assertEqual('override', settings.TEST)
116 def test_context_manager(self):
117 self.assertRaises(AttributeError, getattr, settings, 'TEST')
118 override = override_settings(TEST='override')
119 self.assertRaises(AttributeError, getattr, settings, 'TEST')
120 override.enable()
121 self.assertEqual('override', settings.TEST)
122 override.disable()
123 self.assertRaises(AttributeError, getattr, settings, 'TEST')
125 def test_class_decorator(self):
126 self.assertEqual(SettingGetter().test, 'undefined')
127 DecoratedSettingGetter = override_settings(TEST='override')(SettingGetter)
128 self.assertEqual(DecoratedSettingGetter().test, 'override')
129 self.assertRaises(AttributeError, getattr, settings, 'TEST')
131 def test_signal_callback_context_manager(self):
132 self.assertRaises(AttributeError, getattr, settings, 'TEST')
133 with self.settings(TEST='override'):
134 self.assertEqual(testvalue, 'override')
135 self.assertEqual(testvalue, None)
137 @override_settings(TEST='override')
138 def test_signal_callback_decorator(self):
139 self.assertEqual(testvalue, 'override')
142 # Regression tests for #10130: deleting settings.
145 def test_settings_delete(self):
146 settings.TEST = 'test'
147 self.assertEqual('test', settings.TEST)
148 del settings.TEST
149 self.assertRaises(AttributeError, getattr, settings, 'TEST')
151 def test_settings_delete_wrapped(self):
152 self.assertRaises(TypeError, delattr, settings, '_wrapped')
154 def test_allowed_include_roots_string(self):
156 ALLOWED_INCLUDE_ROOTS is not allowed to be incorrectly set to a string
157 rather than a tuple.
159 self.assertRaises(ValueError, setattr, settings,
160 'ALLOWED_INCLUDE_ROOTS', '/var/www/ssi/')
163 class TrailingSlashURLTests(TestCase):
164 settings_module = settings
166 def setUp(self):
167 self._original_media_url = self.settings_module.MEDIA_URL
169 def tearDown(self):
170 self.settings_module.MEDIA_URL = self._original_media_url
172 def test_blank(self):
174 If blank, no DeprecationWarning error will be raised, even though it
175 doesn't end in a slash.
177 self.settings_module.MEDIA_URL = ''
178 self.assertEqual('', self.settings_module.MEDIA_URL)
180 def test_end_slash(self):
182 MEDIA_URL works if you end in a slash.
184 self.settings_module.MEDIA_URL = '/foo/'
185 self.assertEqual('/foo/', self.settings_module.MEDIA_URL)
187 self.settings_module.MEDIA_URL = 'http://media.foo.com/'
188 self.assertEqual('http://media.foo.com/',
189 self.settings_module.MEDIA_URL)
191 def test_no_end_slash(self):
193 MEDIA_URL raises an DeprecationWarning error if it doesn't end in a
194 slash.
196 import warnings
197 warnings.filterwarnings('error', 'If set, MEDIA_URL must end with a slash', DeprecationWarning)
199 def setattr_settings(settings_module, attr, value):
200 setattr(settings_module, attr, value)
202 self.assertRaises(DeprecationWarning, setattr_settings,
203 self.settings_module, 'MEDIA_URL', '/foo')
205 self.assertRaises(DeprecationWarning, setattr_settings,
206 self.settings_module, 'MEDIA_URL',
207 'http://media.foo.com')
209 def test_double_slash(self):
211 If a MEDIA_URL ends in more than one slash, presume they know what
212 they're doing.
214 self.settings_module.MEDIA_URL = '/stupid//'
215 self.assertEqual('/stupid//', self.settings_module.MEDIA_URL)
217 self.settings_module.MEDIA_URL = 'http://media.foo.com/stupid//'
218 self.assertEqual('http://media.foo.com/stupid//',
219 self.settings_module.MEDIA_URL)
221 class SecureProxySslHeaderTest(TestCase):
222 settings_module = settings
224 def setUp(self):
225 self._original_setting = self.settings_module.SECURE_PROXY_SSL_HEADER
227 def tearDown(self):
228 self.settings_module.SECURE_PROXY_SSL_HEADER = self._original_setting
230 def test_none(self):
231 self.settings_module.SECURE_PROXY_SSL_HEADER = None
232 req = HttpRequest()
233 self.assertEqual(req.is_secure(), False)
235 def test_set_without_xheader(self):
236 self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
237 req = HttpRequest()
238 self.assertEqual(req.is_secure(), False)
240 def test_set_with_xheader_wrong(self):
241 self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
242 req = HttpRequest()
243 req.META['HTTP_X_FORWARDED_PROTOCOL'] = 'wrongvalue'
244 self.assertEqual(req.is_secure(), False)
246 def test_set_with_xheader_right(self):
247 self.settings_module.SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
248 req = HttpRequest()
249 req.META['HTTP_X_FORWARDED_PROTOCOL'] = 'https'
250 self.assertEqual(req.is_secure(), True)
252 class EnvironmentVariableTest(TestCase):
254 Ensures proper settings file is used in setup_environ if
255 DJANGO_SETTINGS_MODULE is set in the environment.
257 def setUp(self):
258 self.original_value = os.environ.get('DJANGO_SETTINGS_MODULE')
260 def tearDown(self):
261 if self.original_value:
262 os.environ['DJANGO_SETTINGS_MODULE'] = self.original_value
263 elif 'DJANGO_SETTINGS_MODULE' in os.environ:
264 del(os.environ['DJANGO_SETTINGS_MODULE'])
266 def test_env_var_used(self):
268 If the environment variable is set, do not ignore it. However, the
269 kwarg original_settings_path takes precedence.
271 This tests both plus the default (neither set).
273 from django.core.management import setup_environ
275 # whatever was already there
276 original_module = os.environ.get(
277 'DJANGO_SETTINGS_MODULE',
278 'the default'
281 # environment variable set by user
282 user_override = 'custom.settings'
284 # optional argument to setup_environ
285 orig_path = 'original.path'
287 # expect default
288 setup_environ(global_settings)
289 self.assertEqual(
290 os.environ.get('DJANGO_SETTINGS_MODULE'),
291 original_module
294 # override with environment variable
295 os.environ['DJANGO_SETTINGS_MODULE'] = user_override
296 setup_environ(global_settings)
298 self.assertEqual(
299 os.environ.get('DJANGO_SETTINGS_MODULE'),
300 user_override
303 # pass in original_settings_path (should take precedence)
304 os.environ['DJANGO_SETTINGS_MODULE'] = user_override
305 setup_environ(global_settings, original_settings_path = orig_path)
307 self.assertEqual(
308 os.environ.get('DJANGO_SETTINGS_MODULE'),
309 orig_path