Ticket #16507: 16507.2.diff

File 16507.2.diff, 4.4 KB (added by aaugustin, 4 years ago)
  • django/contrib/messages/tests/cookie.py

     
    33from django.contrib.messages.storage.cookie import (CookieStorage,
    44    MessageEncoder, MessageDecoder)
    55from django.contrib.messages.storage.base import Message
     6from django.test.utils import override_settings
    67from django.utils import simplejson as json
    78from django.conf import settings
    89
     
    4142class CookieTest(BaseTest):
    4243    storage_class = CookieStorage
    4344
    44     def setUp(self):
    45         super(CookieTest, self).setUp()
    46         self.old_SESSION_COOKIE_DOMAIN = settings.SESSION_COOKIE_DOMAIN
    47         settings.SESSION_COOKIE_DOMAIN = '.lawrence.com'
    48 
    49     def tearDown(self):
    50         super(CookieTest, self).tearDown()
    51         settings.SESSION_COOKIE_DOMAIN = self.old_SESSION_COOKIE_DOMAIN
    52 
    5345    def stored_messages_count(self, storage, response):
    5446        return stored_cookie_messages_count(storage, response)
    5547
     
    139131        value = encoder.encode(messages)
    140132        decoded_messages = json.loads(value, cls=MessageDecoder)
    141133        self.assertEqual(messages, decoded_messages)
     134
     135
     136CookieTest = override_settings(
     137        SESSION_COOKIE_DOMAIN='.lawrence.com')(CookieTest)
  • django/contrib/messages/tests/base.py

     
    11from django import http
    22from django.test import TestCase
     3from django.test.utils import override_settings
    34from django.conf import settings
    45from django.utils.translation import ugettext_lazy
    56from django.utils.unittest import skipIf
     
    3132
    3233class BaseTest(TestCase):
    3334    storage_class = default_storage
    34     restore_settings = ['MESSAGE_LEVEL', 'MESSAGE_TAGS']
    3535    urls = 'django.contrib.messages.tests.urls'
    3636    levels = {
    3737        'debug': constants.DEBUG,
     
    4242    }
    4343
    4444    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.
    5145        self._middleware_classes = settings.MIDDLEWARE_CLASSES
    5246        self._template_context_processors = \
    5347           settings.TEMPLATE_CONTEXT_PROCESSORS
     
    5549        self._message_storage = settings.MESSAGE_STORAGE
    5650        settings.MESSAGE_STORAGE = '%s.%s' % (self.storage_class.__module__,
    5751                                              self.storage_class.__name__)
    58         self.old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
    59         settings.TEMPLATE_DIRS = ()
    6052
    6153    def tearDown(self):
    62         for setting in self.restore_settings:
    63             self.restore_setting(setting)
    64         # Restore these manually (see above).
    6554        settings.MIDDLEWARE_CLASSES = self._middleware_classes
    6655        settings.TEMPLATE_CONTEXT_PROCESSORS = \
    6756           self._template_context_processors
    6857        settings.INSTALLED_APPS = self._installed_apps
    6958        settings.MESSAGE_STORAGE = self._message_storage
    70         settings.TEMPLATE_DIRS = self.old_TEMPLATE_DIRS
    7159
    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)
    78 
    7960    def get_request(self):
    8061        return http.HttpRequest()
    8162
     
    390371                         ['info', 'custom', 'extra-tag', '', 'bad', 'success'])
    391372        finally:
    392373            # Ensure the level tags constant is put back like we found it.
    393             self.restore_setting('MESSAGE_TAGS')
     374            settings.MESSAGE_TAGS = {}
    394375            base.LEVEL_TAGS = utils.get_level_tags()
     376
     377
     378BaseTest = override_settings(
     379        DEBUG_PROPAGATE_EXCEPTIONS=True,
     380        # this is the default value, see storage.base.BaseStorage._get_level()
     381        MESSAGE_LEVEL=constants.INFO,
     382        # this is the default value, see utils.get_level_tags()
     383        MESSAGE_TAGS={},
     384        TEMPLATE_DIRS=())(BaseTest)
Back to Top