Пример #1
0
 def test_legacy_hash_decode(self):
     # RemovedInDjango40Warning: pre-Django 3.1 hashes will be invalid.
     storage = self.storage_class(self.get_request())
     messages = ['this', 'that']
     # Encode/decode a message using the pre-Django 3.1 hash.
     encoder = MessageEncoder(separators=(',', ':'))
     value = encoder.encode(messages)
     encoded_messages = '%s$%s' % (storage._legacy_hash(value), value)
     decoded_messages = storage._decode(encoded_messages)
     self.assertEqual(messages, decoded_messages)
Пример #2
0
 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', 'that']
     # Encode/decode a message using the pre-Django 3.2 format.
     encoder = MessageEncoder()
     value = encoder.encode(messages)
     signer = get_cookie_signer(salt=storage.key_salt)
     encoded_messages = signer.sign(value)
     decoded_messages = storage._decode(encoded_messages)
     self.assertEqual(messages, decoded_messages)
Пример #3
0
 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 [email protected]')
     ]
     # 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)
Пример #4
0
 def test_json_encoder_decoder(self):
     """
     Tests that a complex nested data structure containing Message
     instances is properly encoded/decoded by the custom JSON
     encoder/decoder classes.
     """
     messages = [
         {
             "message": Message(constants.INFO, "Test message"),
             "message_list": [Message(constants.INFO, "message %s") for x in range(5)]
             + [{"another-message": Message(constants.ERROR, "error")}],
         },
         Message(constants.INFO, "message %s"),
     ]
     encoder = MessageEncoder(separators=(",", ":"))
     value = encoder.encode(messages)
     decoded_messages = json.loads(value, cls=MessageDecoder)
     self.assertEqual(messages, decoded_messages)
Пример #5
0
 def test_json_encoder_decoder(self):
     """
     Tests that a complex nested data structure containing Message
     instances is properly encoded/decoded by the custom JSON
     encoder/decoder classes.
     """
     messages = [
         {
             'message': Message(constants.INFO, 'Test message'),
             'message_list': [Message(constants.INFO, 'message %s') \
                              for x in xrange(5)] + [{'another-message': \
                              Message(constants.ERROR, 'error')}],
         },
         Message(constants.INFO, 'message %s'),
     ]
     encoder = MessageEncoder(separators=(',', ':'))
     value = encoder.encode(messages)
     decoded_messages = json.loads(value, cls=MessageDecoder)
     self.assertEqual(messages, decoded_messages)
Пример #6
0
 def test_json_encoder_decoder(self):
     """
     Tests that a complex nested data structure containing Message
     instances is properly encoded/decoded by the custom JSON
     encoder/decoder classes.
     """
     messages = [
         {
             'message': Message(constants.INFO, 'Test message'),
             'message_list': [Message(constants.INFO, 'message %s') \
                              for x in xrange(5)] + [{'another-message': \
                              Message(constants.ERROR, 'error')}],
         },
         Message(constants.INFO, 'message %s'),
     ]
     encoder = MessageEncoder(separators=(',', ':'))
     value = encoder.encode(messages)
     decoded_messages = json.loads(value, cls=MessageDecoder)
     self.assertEqual(messages, decoded_messages)
    def test_pre_1_5_message_format(self):
        """
        Messages that were set in the cookie before the addition of is_safedata
        are decoded correctly (#22426).
        """
        # Encode the messages using the current encoder.
        messages = [Message(constants.INFO, 'message %s') for x in range(5)]
        encoder = MessageEncoder(separators=(',', ':'))
        encoded_messages = encoder.encode(messages)

        # Remove the is_safedata flag from the messages in order to imitate
        # the behavior of before 1.5 (monkey patching).
        encoded_messages = json.loads(encoded_messages)
        for obj in encoded_messages:
            obj.pop(1)
        encoded_messages = json.dumps(encoded_messages, separators=(',', ':'))

        # Decode the messages in the old format (without is_safedata)
        decoded_messages = json.loads(encoded_messages, cls=MessageDecoder)
        self.assertEqual(messages, decoded_messages)
Пример #8
0
    def test_pre_1_5_message_format(self):
        """
        Messages that were set in the cookie before the addition of is_safedata
        are decoded correctly (#22426).
        """
        # Encode the messages using the current encoder.
        messages = [Message(constants.INFO, 'message %s') for x in range(5)]
        encoder = MessageEncoder(separators=(',', ':'))
        encoded_messages = encoder.encode(messages)

        # Remove the is_safedata flag from the messages in order to imitate
        # the behavior of before 1.5 (monkey patching).
        encoded_messages = json.loads(encoded_messages)
        for obj in encoded_messages:
            obj.pop(1)
        encoded_messages = json.dumps(encoded_messages, separators=(',', ':'))

        # Decode the messages in the old format (without is_safedata)
        decoded_messages = json.loads(encoded_messages, cls=MessageDecoder)
        self.assertEqual(messages, decoded_messages)
Пример #9
0
 def test_json_encoder_decoder(self):
     """
     A complex nested data structure containing Message
     instances is properly encoded/decoded by the custom JSON
     encoder/decoder classes.
     """
     messages = [
         {
             "message":
             Message(constants.INFO, "Test message"),
             "message_list":
             [Message(constants.INFO, "message %s") for x in range(5)] +
             [{
                 "another-message": Message(constants.ERROR, "error")
             }],
         },
         Message(constants.INFO, "message %s"),
     ]
     encoder = MessageEncoder()
     value = encoder.encode(messages)
     decoded_messages = json.loads(value, cls=MessageDecoder)
     self.assertEqual(messages, decoded_messages)
Пример #10
0
 def serialize_messages(self, messages):
     encoder = MessageEncoder(separators=(",", ":"))
     return encoder.encode(messages)
Пример #11
0
 def serialize_messages(self, messages):
     encoder = MessageEncoder(separators=(',', ':'))
     return encoder.encode(messages)
Пример #12
0
 def serialize_messages(self, messages):
     encoder = MessageEncoder()
     return encoder.encode(messages)
Пример #13
0
import json