diff --git a/django/contrib/messages/storage/cookie.py b/django/contrib/messages/storage/cookie.py index 2cc6fa6716..45a20d0f94 100644 --- a/django/contrib/messages/storage/cookie.py +++ b/django/contrib/messages/storage/cookie.py @@ -75,7 +75,8 @@ class CookieStorage(BaseStorage): 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): """ diff --git a/django/contrib/messages/tests/cookie.py b/django/contrib/messages/tests/cookie.py index d5671755f2..95e1f99c79 100644 --- a/django/contrib/messages/tests/cookie.py +++ b/django/contrib/messages/tests/cookie.py @@ -4,6 +4,7 @@ from django.contrib.messages.storage.cookie import CookieStorage, \ 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): @@ -40,6 +41,15 @@ def stored_cookie_messages_count(storage, response): 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) @@ -51,6 +61,31 @@ class CookieTest(BaseTest): # 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)