Commits

Anonymous committed 91dc631

Fixed #15618 -- Fixed regression introduced in r15848 regarding not properly deleting messages cookies when honoring SESSION_COOKIE_DOMAIN. Thanks, Julien.

  • Participants
  • Parent commits ef3e59a

Comments (0)

Files changed (2)

File django/contrib/messages/storage/cookie.py

             response.set_cookie(self.cookie_name, encoded_data,
                 domain=settings.SESSION_COOKIE_DOMAIN)
         else:
-            response.delete_cookie(self.cookie_name)
+            response.delete_cookie(self.cookie_name,
+                domain=settings.SESSION_COOKIE_DOMAIN)
 
     def _store(self, messages, response, remove_oldest=True, *args, **kwargs):
         """

File django/contrib/messages/tests/cookie.py

                                             MessageEncoder, MessageDecoder
 from django.contrib.messages.storage.base import Message
 from django.utils import simplejson as json
+from django.conf import settings
 
 
 def set_cookie_data(storage, messages, invalid=False, encode_empty=False):
 class CookieTest(BaseTest):
     storage_class = CookieStorage
 
+    def setUp(self):
+        super(CookieTest, self).setUp()
+        self.old_SESSION_COOKIE_DOMAIN = settings.SESSION_COOKIE_DOMAIN
+        settings.SESSION_COOKIE_DOMAIN = '.lawrence.com'
+
+    def tearDown(self):
+        super(CookieTest, self).tearDown()
+        settings.SESSION_COOKIE_DOMAIN = self.old_SESSION_COOKIE_DOMAIN
+
     def stored_messages_count(self, storage, response):
         return stored_cookie_messages_count(storage, response)
 
         # Test that the message actually contains what we expect.
         self.assertEqual(list(storage), example_messages)
 
+    def test_domain(self):
+        """
+        Ensure that CookieStorage honors SESSION_COOKIE_DOMAIN.
+        Refs #15618.
+        """
+        # Test before the messages have been consumed
+        storage = self.get_storage()
+        response = self.get_response()
+        storage.add(constants.INFO, 'test')
+        storage.update(response)
+        self.assertTrue('test' in response.cookies['messages'].value)
+        self.assertEqual(response.cookies['messages']['domain'], '.lawrence.com')
+        self.assertEqual(response.cookies['messages']['expires'], '')
+
+        # Test after the messages have been consumed
+        storage = self.get_storage()
+        response = self.get_response()
+        storage.add(constants.INFO, 'test')
+        for m in storage:
+            pass # Iterate through the storage to simulate consumption of messages.
+        storage.update(response)
+        self.assertEqual(response.cookies['messages'].value, '')
+        self.assertEqual(response.cookies['messages']['domain'], '.lawrence.com')
+        self.assertEqual(response.cookies['messages']['expires'], 'Thu, 01-Jan-1970 00:00:00 GMT')
+
     def test_get_bad_cookie(self):
         request = self.get_request()
         storage = self.storage_class(request)