<s> import re <EOL> from time import time , gmtime <EOL> from calendar import timegm <EOL> from webtest import TestApp <EOL> from google . appengine . ext import webapp <EOL> from google . appengine . ext . webapp import util <EOL> import sys , os <EOL> sys . path . append ( os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' ) ) ) <EOL> from signedcookie import SignedCookie , SIG_LEN <EOL> from session import RequestHandler , SECRET_KEY , SESSION_TTL , SID_TTL <EOL> class User : <EOL> def __init__ ( self , nickname ) : <EOL> self . nickname = nickname <EOL> class Login ( RequestHandler ) : <EOL> def post ( self ) : <EOL> nickname = self . request . get ( '<STR_LIT>' ) <EOL> self . session . start ( User ( nickname ) ) <EOL> class Regen ( RequestHandler ) : <EOL> def post ( self ) : <EOL> self . session . regen ( ) <EOL> class Touch ( RequestHandler ) : <EOL> def get ( self ) : <EOL> pass <EOL> class Logout ( RequestHandler ) : <EOL> def get ( self ) : <EOL> self . session . end ( ) <EOL> def application ( ) : <EOL> return webapp . WSGIApplication ( <EOL> [ ( '<STR_LIT>' , Login ) , <EOL> ( '<STR_LIT>' , Regen ) , <EOL> ( '<STR_LIT>' , Touch ) , <EOL> ( '<STR_LIT>' , Logout ) ] , <EOL> debug = True ) <EOL> def test_login ( ) : <EOL> app = TestApp ( application ( ) ) <EOL> app = TestApp ( application ( ) ) <EOL> response = app . post ( '<STR_LIT>' , { '<STR_LIT>' : '<STR_LIT:foo>' } ) <EOL> res = str ( response ) <EOL> assert '<STR_LIT>' in res <EOL> assert '<STR_LIT>' in res <EOL> assert '<STR_LIT>' in res <EOL> def test_logout ( ) : <EOL> app = TestApp ( application ( ) ) <EOL> SID = '<STR_LIT>' <EOL> c = SignedCookie ( SECRET_KEY + SID ) <EOL> c [ '<STR_LIT>' ] = SID <EOL> c [ '<STR_LIT:user>' ] = '<STR_LIT:foo>' <EOL> c [ '<STR_LIT>' ] = timegm ( gmtime ( ) ) <EOL> s = '<STR_LIT>' + '<STR_LIT>' . join ( m . output ( ) [ <NUM_LIT:12> : ] for m in c . values ( ) ) <EOL> response = app . get ( '<STR_LIT>' , extra_environ = { '<STR_LIT>' : s } ) <EOL> res = str ( response ) <EOL> assert re . search ( '<STR_LIT>' , res ) <EOL> assert re . search ( '<STR_LIT>' , res ) <EOL> assert re . search ( '<STR_LIT>' , res ) <EOL> def test_regen ( ) : <EOL> app = TestApp ( application ( ) ) <EOL> SID = '<STR_LIT>' <EOL> c = SignedCookie ( SECRET_KEY + SID ) <EOL> c [ '<STR_LIT>' ] = SID <EOL> c [ '<STR_LIT:user>' ] = '<STR_LIT:foo>' <EOL> c [ '<STR_LIT>' ] = timegm ( gmtime ( ) ) <EOL> s = '<STR_LIT>' + '<STR_LIT>' . join ( m . output ( ) [ <NUM_LIT:12> : ] for m in c . values ( ) ) <EOL> response = app . post ( '<STR_LIT>' , extra_environ = { '<STR_LIT>' : s } ) <EOL> res = str ( response ) <EOL> new_SID = re . search ( '<STR_LIT>' , res ) . group ( <NUM_LIT:1> ) <EOL> new_user = re . search ( '<STR_LIT>' , res ) . group ( <NUM_LIT:1> ) <EOL> assert new_SID [ : - SIG_LEN ] != c [ '<STR_LIT>' ] . value <EOL> assert new_user [ : - SIG_LEN ] == c [ '<STR_LIT:user>' ] . value <EOL> assert new_user != c [ '<STR_LIT:user>' ] . coded_value <EOL> def test_autoregen ( ) : <EOL> app = TestApp ( application ( ) ) <EOL> SID = '<STR_LIT>' <EOL> c = SignedCookie ( SECRET_KEY + SID ) <EOL> c [ '<STR_LIT>' ] = SID <EOL> c [ '<STR_LIT:user>' ] = '<STR_LIT:foo>' <EOL> c [ '<STR_LIT>' ] = timegm ( gmtime ( ) ) - SID_TTL - <NUM_LIT:1> <EOL> s = '<STR_LIT>' + '<STR_LIT>' . join ( m . output ( ) [ <NUM_LIT:12> : ] for m in c . values ( ) ) <EOL> response = app . get ( '<STR_LIT>' , extra_environ = { '<STR_LIT>' : s } ) <EOL> res = str ( response ) <EOL> new_SID = re . search ( '<STR_LIT>' , res ) . group ( <NUM_LIT:1> ) <EOL> new_user = re . search ( '<STR_LIT>' , res ) . group ( <NUM_LIT:1> ) <EOL> assert new_SID [ : - SIG_LEN ] != c [ '<STR_LIT>' ] . value <EOL> assert new_user [ : - SIG_LEN ] == c [ '<STR_LIT:user>' ] . value <EOL> assert new_user != c [ '<STR_LIT:user>' ] . coded_value <EOL> def test_autoexpire ( ) : <EOL> app = TestApp ( application ( ) ) <EOL> SID = '<STR_LIT>' <EOL> c = SignedCookie ( SECRET_KEY + SID ) <EOL> c [ '<STR_LIT>' ] = SID <EOL> c [ '<STR_LIT:user>' ] = '<STR_LIT:foo>' <EOL> c [ '<STR_LIT>' ] = timegm ( gmtime ( ) ) - SESSION_TTL - <NUM_LIT:1> <EOL> s = '<STR_LIT>' + '<STR_LIT>' . join ( m . output ( ) [ <NUM_LIT:12> : ] for m in c . values ( ) ) <EOL> response = app . get ( '<STR_LIT>' , extra_environ = { '<STR_LIT>' : s } ) <EOL> res = str ( response ) <EOL> assert re . search ( '<STR_LIT>' , res ) <EOL> assert re . search ( '<STR_LIT>' , res ) <EOL> assert re . search ( '<STR_LIT>' , res ) </s>
