Ticket #16574: 16574-2.diff
File 16574-2.diff, 9.4 KB (added by , 13 years ago) |
---|
-
django/contrib/messages/tests/base.py
diff --git a/django/contrib/messages/tests/base.py b/django/contrib/messages/tests/base.py index 416d16c..b91ddbd 100644
a b 1 1 from django import http 2 from django.test import TestCase3 2 from django.conf import settings 4 from django.utils.translation import ugettext_lazy5 from django.utils.unittest import skipIf6 3 from django.contrib.messages import constants, utils, get_level, set_level 7 4 from django.contrib.messages.api import MessageFailure 8 5 from django.contrib.messages.storage import default_storage, base 9 6 from django.contrib.messages.storage.base import Message 10 7 from django.core.urlresolvers import reverse 8 from django.test import TestCase 9 from django.test.utils import override_settings 10 from django.utils.translation import ugettext_lazy 11 from django.utils.unittest import skipIf 11 12 12 13 13 14 def skipUnlessAuthIsInstalled(func): … … def add_level_messages(storage): 31 32 32 33 class BaseTest(TestCase): 33 34 storage_class = default_storage 34 restore_settings = ['MESSAGE_LEVEL', 'MESSAGE_TAGS']35 35 urls = 'django.contrib.messages.tests.urls' 36 36 levels = { 37 37 'debug': constants.DEBUG, … … class BaseTest(TestCase): 42 42 } 43 43 44 44 def setUp(self): 45 self._remembered_settings = {} 46 for setting in self.restore_settings: 47 if hasattr(settings, setting): 48 self._remembered_settings[setting] = getattr(settings, setting) 49 delattr(settings._wrapped, setting) 50 # Backup these manually because we do not want them deleted. 51 self._middleware_classes = settings.MIDDLEWARE_CLASSES 52 self._template_context_processors = \ 53 settings.TEMPLATE_CONTEXT_PROCESSORS 54 self._installed_apps = settings.INSTALLED_APPS 55 self._message_storage = settings.MESSAGE_STORAGE 56 settings.MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__, 57 self.storage_class.__name__) 58 self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS 59 settings.TEMPLATE_DIRS = () 45 self.settings_override = override_settings( 46 TEMPLATE_DIRS = (), 47 MESSAGE_TAGS = '', 48 MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__, 49 self.storage_class.__name__), 50 ) 51 self.settings_override.enable() 60 52 61 53 def tearDown(self): 62 for setting in self.restore_settings: 63 self.restore_setting(setting) 64 # Restore these manually (see above). 65 settings.MIDDLEWARE_CLASSES = self._middleware_classes 66 settings.TEMPLATE_CONTEXT_PROCESSORS = \ 67 self._template_context_processors 68 settings.INSTALLED_APPS = self._installed_apps 69 settings.MESSAGE_STORAGE = self._message_storage 70 settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS 71 72 def restore_setting(self, setting): 73 if setting in self._remembered_settings: 74 value = self._remembered_settings.pop(setting) 75 setattr(settings, setting, value) 76 elif hasattr(settings, setting): 77 delattr(settings._wrapped, setting) 54 self.settings_override.disable() 78 55 79 56 def get_request(self): 80 57 return http.HttpRequest() … … class BaseTest(TestCase): 153 130 storing = self.stored_messages_count(storage, response) 154 131 self.assertEqual(storing, 1) 155 132 133 @override_settings(MESSAGE_LEVEL=constants.DEBUG) 156 134 def test_full_request_response_cycle(self): 157 135 """ 158 136 With the message middleware enabled, tests that messages are properly 159 137 stored and then retrieved across the full request/redirect/response 160 138 cycle. 161 139 """ 162 settings.MESSAGE_LEVEL = constants.DEBUG163 140 data = { 164 141 'messages': ['Test message %d' % x for x in xrange(10)], 165 142 } … … class BaseTest(TestCase): 176 153 for msg in data['messages']: 177 154 self.assertContains(response, msg) 178 155 156 @override_settings(MESSAGE_LEVEL=constants.DEBUG) 179 157 def test_with_template_response(self): 180 settings.MESSAGE_LEVEL = constants.DEBUG181 158 data = { 182 159 'messages': ['Test message %d' % x for x in xrange(10)], 183 160 } … … class BaseTest(TestCase): 196 173 for msg in data['messages']: 197 174 self.assertNotContains(response, msg) 198 175 176 @override_settings(MESSAGE_LEVEL=constants.DEBUG) 199 177 def test_multiple_posts(self): 200 178 """ 201 179 Tests that messages persist properly when multiple POSTs are made 202 180 before a GET. 203 181 """ 204 settings.MESSAGE_LEVEL = constants.DEBUG205 182 data = { 206 183 'messages': ['Test message %d' % x for x in xrange(10)], 207 184 } … … class BaseTest(TestCase): 219 196 for msg in data['messages']: 220 197 self.assertContains(response, msg) 221 198 199 @override_settings( 200 INSTALLED_APPS=filter( 201 lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS), 202 MIDDLEWARE_CLASSES=filter( 203 lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES), 204 TEMPLATE_CONTEXT_PROCESSORS=filter( 205 lambda p:'context_processors.messages' not in p, 206 settings.TEMPLATE_CONTEXT_PROCESSORS), 207 MESSAGE_LEVEL=constants.DEBUG 208 ) 222 209 def test_middleware_disabled(self): 223 210 """ 224 211 Tests that, when the middleware is disabled, an exception is raised 225 212 when one attempts to store a message. 226 213 """ 227 settings.MESSAGE_LEVEL = constants.DEBUG228 settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)229 settings.INSTALLED_APPS.remove(230 'django.contrib.messages',231 )232 settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES)233 settings.MIDDLEWARE_CLASSES.remove(234 'django.contrib.messages.middleware.MessageMiddleware',235 )236 settings.TEMPLATE_CONTEXT_PROCESSORS = \237 list(settings.TEMPLATE_CONTEXT_PROCESSORS)238 settings.TEMPLATE_CONTEXT_PROCESSORS.remove(239 'django.contrib.messages.context_processors.messages',240 )241 214 data = { 242 215 'messages': ['Test message %d' % x for x in xrange(10)], 243 216 } … … class BaseTest(TestCase): 248 221 self.assertRaises(MessageFailure, self.client.post, add_url, 249 222 data, follow=True) 250 223 224 @override_settings( 225 INSTALLED_APPS=filter( 226 lambda app:app!='django.contrib.messages', settings.INSTALLED_APPS), 227 MIDDLEWARE_CLASSES=filter( 228 lambda m:'MessageMiddleware' not in m, settings.MIDDLEWARE_CLASSES), 229 TEMPLATE_CONTEXT_PROCESSORS=filter( 230 lambda p:'context_processors.messages' not in p, 231 settings.TEMPLATE_CONTEXT_PROCESSORS), 232 MESSAGE_LEVEL=constants.DEBUG 233 ) 251 234 def test_middleware_disabled_fail_silently(self): 252 235 """ 253 236 Tests that, when the middleware is disabled, an exception is not 254 237 raised if 'fail_silently' = True 255 238 """ 256 settings.MESSAGE_LEVEL = constants.DEBUG257 settings.INSTALLED_APPS = list(settings.INSTALLED_APPS)258 settings.INSTALLED_APPS.remove(259 'django.contrib.messages',260 )261 settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES)262 settings.MIDDLEWARE_CLASSES.remove(263 'django.contrib.messages.middleware.MessageMiddleware',264 )265 settings.TEMPLATE_CONTEXT_PROCESSORS = \266 list(settings.TEMPLATE_CONTEXT_PROCESSORS)267 settings.TEMPLATE_CONTEXT_PROCESSORS.remove(268 'django.contrib.messages.context_processors.messages',269 )270 239 data = { 271 240 'messages': ['Test message %d' % x for x in xrange(10)], 272 241 'fail_silently': True, … … class BaseTest(TestCase): 350 319 add_level_messages(storage) 351 320 self.assertEqual(len(storage), 2) 352 321 322 @override_settings(MESSAGE_LEVEL=29) 353 323 def test_settings_level(self): 354 324 request = self.get_request() 355 325 storage = self.storage_class(request) 356 326 357 settings.MESSAGE_LEVEL = 29358 327 self.assertEqual(get_level(request), 29) 359 328 360 329 add_level_messages(storage) … … class BaseTest(TestCase): 369 338 ['info', '', 'extra-tag debug', 'warning', 'error', 370 339 'success']) 371 340 372 def test_custom_tags(self): 373 settings.MESSAGE_TAGS = { 341 @override_settings(MESSAGE_TAGS={ 374 342 constants.INFO: 'info', 375 343 constants.DEBUG: '', 376 344 constants.WARNING: '', 377 345 constants.ERROR: 'bad', 378 346 29: 'custom', 379 347 } 348 ) 349 def test_custom_tags(self): 380 350 # LEVEL_TAGS is a constant defined in the 381 351 # django.contrib.messages.storage.base module, so after changing 382 352 # settings.MESSAGE_TAGS, we need to update that constant too. 353 old_level_tags = base.LEVEL_TAGS 383 354 base.LEVEL_TAGS = utils.get_level_tags() 384 355 try: 385 356 storage = self.get_storage() … … class BaseTest(TestCase): 390 361 ['info', 'custom', 'extra-tag', '', 'bad', 'success']) 391 362 finally: 392 363 # Ensure the level tags constant is put back like we found it. 393 self.restore_setting('MESSAGE_TAGS') 394 base.LEVEL_TAGS = utils.get_level_tags() 364 base.LEVEL_TAGS = old_level_tags