<s> import time <EOL> from django . core import signing <EOL> from django . http import HttpRequest , HttpResponse <EOL> from django . test import TestCase <EOL> class SignedCookieTest ( TestCase ) : <EOL> def test_can_set_and_read_signed_cookies ( self ) : <EOL> response = HttpResponse ( ) <EOL> response . set_signed_cookie ( '<STR_LIT:c>' , '<STR_LIT:hello>' ) <EOL> self . assertIn ( '<STR_LIT:c>' , response . cookies ) <EOL> self . assertTrue ( response . cookies [ '<STR_LIT:c>' ] . value . startswith ( '<STR_LIT>' ) ) <EOL> request = HttpRequest ( ) <EOL> request . COOKIES [ '<STR_LIT:c>' ] = response . cookies [ '<STR_LIT:c>' ] . value <EOL> value = request . get_signed_cookie ( '<STR_LIT:c>' ) <EOL> self . assertEqual ( value , u'<STR_LIT:hello>' ) <EOL> def test_can_use_salt ( self ) : <EOL> response = HttpResponse ( ) <EOL> response . set_signed_cookie ( '<STR_LIT:a>' , '<STR_LIT:hello>' , salt = '<STR_LIT>' ) <EOL> request = HttpRequest ( ) <EOL> request . COOKIES [ '<STR_LIT:a>' ] = response . cookies [ '<STR_LIT:a>' ] . value <EOL> value = request . get_signed_cookie ( '<STR_LIT:a>' , salt = '<STR_LIT>' ) <EOL> self . assertEqual ( value , u'<STR_LIT:hello>' ) <EOL> self . assertRaises ( signing . BadSignature , <EOL> request . get_signed_cookie , '<STR_LIT:a>' , salt = '<STR_LIT>' ) <EOL> def test_detects_tampering ( self ) : <EOL> response = HttpResponse ( ) <EOL> response . set_signed_cookie ( '<STR_LIT:c>' , '<STR_LIT:hello>' ) <EOL> request = HttpRequest ( ) <EOL> request . COOKIES [ '<STR_LIT:c>' ] = response . cookies [ '<STR_LIT:c>' ] . value [ : - <NUM_LIT:2> ] + '<STR_LIT>' <EOL> self . assertRaises ( signing . BadSignature , <EOL> request . get_signed_cookie , '<STR_LIT:c>' ) <EOL> def test_default_argument_supresses_exceptions ( self ) : <EOL> response = HttpResponse ( ) <EOL> response . set_signed_cookie ( '<STR_LIT:c>' , '<STR_LIT:hello>' ) <EOL> request = HttpRequest ( ) <EOL> request . COOKIES [ '<STR_LIT:c>' ] = response . cookies [ '<STR_LIT:c>' ] . value [ : - <NUM_LIT:2> ] + '<STR_LIT>' <EOL> self . assertEqual ( request . get_signed_cookie ( '<STR_LIT:c>' , default = None ) , None ) <EOL> def test_max_age_argument ( self ) : <EOL> value = u'<STR_LIT:hello>' <EOL> _time = time . time <EOL> time . time = lambda : <NUM_LIT> <EOL> try : <EOL> response = HttpResponse ( ) <EOL> response . set_signed_cookie ( '<STR_LIT:c>' , value ) <EOL> request = HttpRequest ( ) <EOL> request . COOKIES [ '<STR_LIT:c>' ] = response . cookies [ '<STR_LIT:c>' ] . value <EOL> self . assertEqual ( request . get_signed_cookie ( '<STR_LIT:c>' ) , value ) <EOL> time . time = lambda : <NUM_LIT> <EOL> self . assertEqual ( request . get_signed_cookie ( '<STR_LIT:c>' , max_age = <NUM_LIT:12> ) , value ) <EOL> self . assertEqual ( request . get_signed_cookie ( '<STR_LIT:c>' , max_age = <NUM_LIT:11> ) , value ) <EOL> self . assertRaises ( signing . SignatureExpired , <EOL> request . get_signed_cookie , '<STR_LIT:c>' , max_age = <NUM_LIT:10> ) <EOL> finally : <EOL> time . time = _time </s>
