Refs #32191 -- Removed for the pre-Django 3.2 format of messages in CookieStorage.

Per deprecation timeline.
This commit is contained in:
Mariusz Felisiak 2021-09-16 09:45:43 +02:00
parent 05f3a6186e
commit 737fa72ae3
3 changed files with 5 additions and 32 deletions

View File

@ -161,21 +161,8 @@ class CookieStorage(BaseStorage):
return None
try:
return self.signer.unsign_object(data, serializer=MessageSerializer)
# RemovedInDjango41Warning: when the deprecation ends, replace with:
#
# except (signing.BadSignature, json.JSONDecodeError):
# pass
except signing.BadSignature:
decoded = None
except (binascii.Error, json.JSONDecodeError):
decoded = self.signer.unsign(data)
if decoded:
# RemovedInDjango41Warning.
try:
return json.loads(decoded, cls=MessageDecoder)
except json.JSONDecodeError:
pass
except (signing.BadSignature, binascii.Error, json.JSONDecodeError):
pass
# Mark the data as used (so it gets removed) since something was wrong
# with the data.
self.used = True

View File

@ -263,3 +263,6 @@ to remove usage of these features.
* The ``django.core.cache.backends.memcached.MemcachedCache`` backend is
removed.
* Support for the pre-Django 3.2 format of messages used by
``django.contrib.messages.storage.cookie.CookieStorage`` is removed.

View File

@ -1,4 +1,3 @@
import binascii
import json
import random
@ -8,7 +7,6 @@ from django.contrib.messages.storage.base import Message
from django.contrib.messages.storage.cookie import (
CookieStorage, MessageDecoder, MessageEncoder,
)
from django.core.signing import b64_decode, get_cookie_signer
from django.test import SimpleTestCase, override_settings
from django.utils.crypto import get_random_string
from django.utils.safestring import SafeData, mark_safe
@ -179,18 +177,3 @@ class CookieTests(BaseTests, SimpleTestCase):
storage = self.get_storage()
self.assertIsInstance(encode_decode(mark_safe("<b>Hello Django!</b>")), SafeData)
self.assertNotIsInstance(encode_decode("<b>Hello Django!</b>"), SafeData)
def test_legacy_encode_decode(self):
# RemovedInDjango41Warning: pre-Django 3.2 encoded messages will be
# invalid.
storage = self.storage_class(self.get_request())
messages = ['this', Message(0, 'Successfully signed in as admin@example.org')]
# Encode/decode a message using the pre-Django 3.2 format.
encoder = MessageEncoder()
value = encoder.encode(messages)
with self.assertRaises(binascii.Error):
b64_decode(value.encode())
signer = get_cookie_signer(salt=storage.key_salt)
encoded_messages = signer.sign(value)
decoded_messages = storage._decode(encoded_messages)
self.assertEqual(messages, decoded_messages)