<commit_before>import time
import pickle
import unittest
from datetime import datetime
import itsdangerous as idmod


class SerializerTestCase(unittest.TestCase):
    serializer_class = idmod.Serializer

    def make_serializer(self, *args, **kwargs):
        return self.serializer_class(*args, **kwargs)

    def test_dumps_loads(self):
        objects = (['a', 'list'], 'a string', u'a unicode string \u2019',
                   {'a': 'dictionary'}, 42, 42.5)
        s = self.make_serializer('Test')
        for o in objects:
            value = s.dumps(o)
            self.assertNotEqual(o, value)
            self.assertEqual(o, s.loads(value))

    def test_decode_detects_tampering(self):
        transforms = (
            lambda s: s.upper(),
            lambda s: s + 'a',
            lambda s: 'a' + s[1:],
            lambda s: s.replace('.', ''),
        )
        value = {
            'foo': 'bar',
            'baz': 1,
        }
        s = self.make_serializer('Test')
        encoded = s.dumps(value)
        self.assertEqual(value, s.loads(encoded))
        for transform in transforms:
            self.assertRaises(
                idmod.BadSignature, s.loads, transform(encoded))

    def test_accepts_unicode(self):
        objects = (['a', 'list'], 'a string', u'a unicode string \u2019',
                   {'a': 'dictionary'}, 42, 42.5)
        s = self.make_serializer('Test')
        for o in objects:
            value = s.dumps(o)
            self.assertNotEqual(o, value)
            self.assertEqual(o, s.loads(unicode(value)))

    def test_exception_attributes(self):
        secret_key = 'predictable-key'
        value = u'hello'

        s = self.make_serializer(secret_key)
        ts = s.dumps(value)

        try:
            s.loads(ts + 'x')
        except idmod.BadSignature, e:
            self.assertEqual(e.payload, ts.rsplit('.', 1)[0])
            self.assertEqual(s.load_payload(e.payload), value)
        else:
            self.fail('Did not get bad signature')

    def test_unsafe_load(self):
        secret_key = 'predictable-key'
        value = u'hello'

        s = self.make_serializer(secret_key)
        ts = s.dumps(value)
        self.assertEqual(s.loads_unsafe(ts), (True, u'hello'))<commit_msg>Added another test for passing unicode data to unsafe loads<commit_after>import time
import pickle
import unittest
from datetime import datetime
import itsdangerous as idmod


class SerializerTestCase(unittest.TestCase):
    serializer_class = idmod.Serializer

    def make_serializer(self, *args, **kwargs):
        return self.serializer_class(*args, **kwargs)

    def test_dumps_loads(self):
        objects = (['a', 'list'], 'a string', u'a unicode string \u2019',
                   {'a': 'dictionary'}, 42, 42.5)
        s = self.make_serializer('Test')
        for o in objects:
            value = s.dumps(o)
            self.assertNotEqual(o, value)
            self.assertEqual(o, s.loads(value))

    def test_decode_detects_tampering(self):
        transforms = (
            lambda s: s.upper(),
            lambda s: s + 'a',
            lambda s: 'a' + s[1:],
            lambda s: s.replace('.', ''),
        )
        value = {
            'foo': 'bar',
            'baz': 1,
        }
        s = self.make_serializer('Test')
        encoded = s.dumps(value)
        self.assertEqual(value, s.loads(encoded))
        for transform in transforms:
            self.assertRaises(
                idmod.BadSignature, s.loads, transform(encoded))

    def test_accepts_unicode(self):
        objects = (['a', 'list'], 'a string', u'a unicode string \u2019',
                   {'a': 'dictionary'}, 42, 42.5)
        s = self.make_serializer('Test')
        for o in objects:
            value = s.dumps(o)
            self.assertNotEqual(o, value)
            self.assertEqual(o, s.loads(unicode(value)))

    def test_exception_attributes(self):
        secret_key = 'predictable-key'
        value = u'hello'

        s = self.make_serializer(secret_key)
        ts = s.dumps(value)

        try:
            s.loads(ts + 'x')
        except idmod.BadSignature, e:
            self.assertEqual(e.payload, ts.rsplit('.', 1)[0])
            self.assertEqual(s.load_payload(e.payload), value)
        else:
            self.fail('Did not get bad signature')

    def test_unsafe_load(self):
        secret_key = 'predictable-key'
        value = u'hello'

        s = self.make_serializer(secret_key)
        ts = s.dumps(value)
        self.assertEqual(s.loads_unsafe(ts), (True, u'hello'))
        self.assertEqual(s.loads_unsafe(ts, salt='modified'), (False, u'hello'))

    def test_load_unsafe_with_unicode_strings(self):
        secret_key = 'predictable-key'
        value = u'hello'

        s = self.make_serializer(secret_key)
        ts = unicode(s.dumps(value))
        self.assertEqual(s.loads_unsafe(ts), (True, u'hello'))
{'fraction_non_alphanumeric': 0.12915053982481156, 'fraction_numerical': 0.00896312894683235, 'mean_word_length': 2.127388535031847, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3965881', 'n_tokens_mistral': 1567, 'n_tokens_neox': 1492, 'n_words': 335}