Ticket #15618: 15618_message_cookies_domain.diff

File 15618_message_cookies_domain.diff, 3.3 KB (added by Julien Phalip, 14 years ago)
  • django/contrib/messages/storage/cookie.py

    diff --git a/django/contrib/messages/storage/cookie.py b/django/contrib/messages/storage/cookie.py
    index 2cc6fa6..45a20d0 100644
    a b class CookieStorage(BaseStorage):  
    7575            response.set_cookie(self.cookie_name, encoded_data,
    7676                domain=settings.SESSION_COOKIE_DOMAIN)
    7777        else:
    78             response.delete_cookie(self.cookie_name)
     78            response.delete_cookie(self.cookie_name,
     79                domain=settings.SESSION_COOKIE_DOMAIN)
    7980
    8081    def _store(self, messages, response, remove_oldest=True, *args, **kwargs):
    8182        """
  • django/contrib/messages/tests/cookie.py

    diff --git a/django/contrib/messages/tests/cookie.py b/django/contrib/messages/tests/cookie.py
    index d567175..c8ec558 100644
    a b from django.contrib.messages.storage.cookie import CookieStorage, \  
    44                                            MessageEncoder, MessageDecoder
    55from django.contrib.messages.storage.base import Message
    66from django.utils import simplejson as json
     7from django.conf import settings
    78
    89
    910def set_cookie_data(storage, messages, invalid=False, encode_empty=False):
    def stored_cookie_messages_count(storage, response):  
    3940
    4041class CookieTest(BaseTest):
    4142    storage_class = CookieStorage
     43   
     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
    4252
    4353    def stored_messages_count(self, storage, response):
    4454        return stored_cookie_messages_count(storage, response)
    class CookieTest(BaseTest):  
    5060        set_cookie_data(storage, example_messages)
    5161        # Test that the message actually contains what we expect.
    5262        self.assertEqual(list(storage), example_messages)
    53 
     63       
     64    def test_domain(self):
     65        """
     66        Ensure that CookieStorage honors SESSION_COOKIE_DOMAIN.
     67        Refs #15618.
     68        """
     69        # Test before the messages have been consumed
     70        storage = self.get_storage()
     71        response = self.get_response()
     72        storage.add(constants.INFO, 'test')
     73        storage.update(response)
     74        self.assertTrue('test' in response.cookies['messages'].value)
     75        self.assertEqual(response.cookies['messages']['domain'], '.lawrence.com')
     76        self.assertEqual(response.cookies['messages']['expires'], '')
     77       
     78        # Test after the messages have been consumed
     79        storage = self.get_storage()
     80        response = self.get_response()
     81        storage.add(constants.INFO, 'test')
     82        for m in storage:
     83            pass # Iterate through the storage to simulate consumption of messages.
     84        storage.update(response)
     85        self.assertEqual(response.cookies['messages'].value, '')
     86        self.assertEqual(response.cookies['messages']['domain'], '.lawrence.com')
     87        self.assertEqual(response.cookies['messages']['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
     88       
    5489    def test_get_bad_cookie(self):
    5590        request = self.get_request()
    5691        storage = self.storage_class(request)
Back to Top