Home | History | Annotate | Download | only in tests
      1 # -*- coding: utf-8 -*-
      2 from webapp2_extras import securecookie
      3 
      4 import test_base
      5 
      6 
      7 class TestSecureCookie(test_base.BaseTestCase):
      8     def test_secure_cookie_serializer(self):
      9         serializer = securecookie.SecureCookieSerializer('secret-key')
     10         serializer._get_timestamp = lambda: 1
     11 
     12         value = ['a', 'b', 'c']
     13         result = 'WyJhIiwiYiIsImMiXQ==|1|38837d6af8ac1ded9292b83924fc8521ce76f47e'
     14 
     15         rv = serializer.serialize('foo', value)
     16         self.assertEqual(rv, result)
     17 
     18         rv = serializer.deserialize('foo', result)
     19         self.assertEqual(rv, value)
     20 
     21         # no value
     22         rv = serializer.deserialize('foo', None)
     23         self.assertEqual(rv, None)
     24 
     25         # not 3 parts
     26         rv = serializer.deserialize('foo', 'a|b')
     27         self.assertEqual(rv, None)
     28 
     29         # bad signature
     30         rv = serializer.deserialize('foo', result + 'foo')
     31         self.assertEqual(rv, None)
     32 
     33         # too old
     34         rv = serializer.deserialize('foo', result, max_age=-86400)
     35         self.assertEqual(rv, None)
     36 
     37         # not correctly encoded
     38         serializer2 = securecookie.SecureCookieSerializer('foo')
     39         serializer2._encode = lambda x: 'foo'
     40         result2 = serializer2.serialize('foo', value)
     41         rv2 = serializer2.deserialize('foo', result2)
     42         self.assertEqual(rv2, None)
     43 
     44 
     45 if __name__ == '__main__':
     46     test_base.main()
     47