<s> """<STR_LIT>""" <EOL> import Cookie <EOL> import sha <EOL> import unittest <EOL> import urllib <EOL> import wsgiref . util <EOL> from google . appengine . tools . devappserver2 import login <EOL> COOKIE_NAME = '<STR_LIT>' <EOL> NICKNAME = '<STR_LIT>' <EOL> EMAIL = '<STR_LIT>' <EOL> USER_ID = '<STR_LIT>' <EOL> SECRET = '<STR_LIT>' <EOL> class CookieTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def get_cookie_value ( self , email , nick , is_admin ) : <EOL> if is_admin : <EOL> app_id = "<STR_LIT>" <EOL> else : <EOL> app_id = "<STR_LIT>" <EOL> hashed_value = sha . new ( email + nick + app_id + SECRET ) . hexdigest ( ) <EOL> cookie_value = "<STR_LIT>" . format ( email , nick , app_id , hashed_value ) <EOL> return cookie_value <EOL> def test_get_user_info_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_value = self . get_cookie_value ( EMAIL , NICKNAME , True ) <EOL> http_cookie = '<STR_LIT>' % ( COOKIE_NAME , cookie_value ) <EOL> email , admin , _ = login . get_user_info ( http_cookie , <EOL> cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( EMAIL , email ) <EOL> self . assertTrue ( admin ) <EOL> def test_get_user_info_not_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_value = self . get_cookie_value ( EMAIL , NICKNAME , False ) <EOL> http_cookie = '<STR_LIT>' % ( COOKIE_NAME , cookie_value ) <EOL> email , admin , user_id = login . get_user_info ( http_cookie , <EOL> cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( EMAIL , email ) <EOL> self . assertFalse ( admin ) <EOL> def test_get_user_info_invalid_email ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_value = self . get_cookie_value ( '<STR_LIT:foo>' , NICKNAME , False ) <EOL> cookie_value = '<STR_LIT>' % USER_ID <EOL> http_cookie = '<STR_LIT>' % ( COOKIE_NAME , cookie_value ) <EOL> email , admin , user_id = login . get_user_info ( http_cookie , <EOL> cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( '<STR_LIT>' , email ) <EOL> self . assertFalse ( admin ) <EOL> def test_get_user_info_does_not_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> http_cookie = '<STR_LIT>' <EOL> email , admin , user_id = login . get_user_info ( http_cookie , <EOL> cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( '<STR_LIT>' , email ) <EOL> self . assertFalse ( admin ) <EOL> def test_get_user_info_bad_cookie ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_name = '<STR_LIT>' <EOL> cookie_value = '<STR_LIT>' <EOL> http_cookie = '<STR_LIT>' % ( cookie_name , cookie_value ) <EOL> email , admin , user_id = login . get_user_info ( http_cookie , <EOL> cookie_name = cookie_name ) <EOL> self . assertEqual ( '<STR_LIT>' , email ) <EOL> self . assertFalse ( admin ) <EOL> def test_get_user_info_from_dict_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_value = self . get_cookie_value ( EMAIL , NICKNAME , True ) <EOL> cookie_dict = { '<STR_LIT>' : '<STR_LIT>' , COOKIE_NAME : cookie_value , '<STR_LIT>' : '<STR_LIT>' } <EOL> email , admin , user_id = login . _get_user_info_from_dict ( <EOL> cookie_dict , cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( EMAIL , email ) <EOL> self . assertTrue ( admin ) <EOL> def test_get_user_info_from_dict_not_admin ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_value = self . get_cookie_value ( EMAIL , NICKNAME , False ) <EOL> cookie_dict = { '<STR_LIT>' : '<STR_LIT>' , COOKIE_NAME : cookie_value , '<STR_LIT>' : '<STR_LIT>' } <EOL> email , admin , user_id = login . _get_user_info_from_dict ( <EOL> cookie_dict , cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( EMAIL , email ) <EOL> self . assertFalse ( admin ) <EOL> def test_get_user_info_from_dict_does_not_exist ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_dict = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> email , admin , user_id = login . _get_user_info_from_dict ( <EOL> cookie_dict , cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( '<STR_LIT>' , email ) <EOL> self . assertFalse ( admin ) <EOL> def test_set_user_info_cookie ( self ) : <EOL> """<STR_LIT>""" <EOL> cookie_value = '<STR_LIT>' % ( EMAIL , USER_ID ) <EOL> expected_result = '<STR_LIT>' % ( COOKIE_NAME , cookie_value ) <EOL> result = login . _set_user_info_cookie ( EMAIL , True , cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( expected_result , result ) <EOL> def test_clear_user_info_cookie ( self ) : <EOL> """<STR_LIT>""" <EOL> expected_result = '<STR_LIT>' % COOKIE_NAME <EOL> result = login . _clear_user_info_cookie ( cookie_name = COOKIE_NAME ) <EOL> self . assertEqual ( expected_result , result ) <EOL> class LoginRedirectTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_basic ( self ) : <EOL> """<STR_LIT>""" <EOL> application_url = '<STR_LIT>' <EOL> continue_url = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> expected_location = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def start_response ( status , headers , exc_info = None ) : <EOL> self . assertTrue ( status . startswith ( '<STR_LIT>' ) ) <EOL> headers = dict ( headers ) <EOL> self . assertEqual ( { '<STR_LIT>' : expected_location } , headers ) <EOL> self . assertEqual ( None , exc_info ) <EOL> body = login . login_redirect ( application_url , continue_url , start_response ) <EOL> self . assertEqual ( '<STR_LIT>' , '<STR_LIT>' . join ( body ) ) <EOL> class LoginPageTest ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_no_params ( self ) : <EOL> """<STR_LIT>""" <EOL> host = '<STR_LIT>' <EOL> path_info = '<STR_LIT>' <EOL> cookie_dict = { } <EOL> action = '<STR_LIT>' <EOL> set_email = '<STR_LIT>' <EOL> set_admin = False <EOL> continue_url = '<STR_LIT>' <EOL> status , location , set_cookie , content_type = self . _run_test ( <EOL> host , path_info , cookie_dict , action , set_email , set_admin , <EOL> continue_url ) <EOL> self . assertEqual ( <NUM_LIT> , status ) <EOL> self . assertFalse ( set_cookie ) <EOL> self . assertEqual ( '<STR_LIT>' , content_type ) <EOL> def test_login ( self ) : <EOL> """<STR_LIT>""" <EOL> host = '<STR_LIT>' <EOL> path_info = '<STR_LIT>' <EOL> cookie_dict = { } <EOL> action = '<STR_LIT>' <EOL> set_email = EMAIL <EOL> set_admin = False <EOL> continue_url = '<STR_LIT>' <EOL> expected_set = login . _set_user_info_cookie ( set_email , set_admin ) . strip ( ) <EOL> status , location , set_cookie , _ = self . _run_test ( <EOL> host , path_info , cookie_dict , action , set_email , set_admin , <EOL> continue_url ) <EOL> self . assertEqual ( <NUM_LIT> , status ) <EOL> self . assertEqual ( '<STR_LIT>' % ( host , path_info ) , location ) <EOL> self . assertEqual ( expected_set , set_cookie ) <EOL> self . assertIsInstance ( location , str ) <EOL> self . assertIsInstance ( set_cookie , str ) <EOL> continue_url = '<STR_LIT>' <EOL> status , location , set_cookie , _ = self . _run_test ( <EOL> host , path_info , cookie_dict , action , set_email , set_admin , <EOL> continue_url ) <EOL> self . assertEqual ( <NUM_LIT> , status ) <EOL> self . assertEqual ( continue_url , location ) <EOL> self . assertEqual ( expected_set , set_cookie ) <EOL> self . assertIsInstance ( location , str ) <EOL> self . assertIsInstance ( set_cookie , str ) <EOL> def test_logout ( self ) : <EOL> """<STR_LIT>""" <EOL> host = '<STR_LIT>' <EOL> path_info = '<STR_LIT>' <EOL> cookie_dict = { '<STR_LIT>' : '<STR_LIT>' % ( EMAIL , USER_ID ) } <EOL> action = '<STR_LIT>' <EOL> set_email = '<STR_LIT>' <EOL> set_admin = False <EOL> continue_url = '<STR_LIT>' <EOL> expected_set = login . _clear_user_info_cookie ( ) . strip ( ) <EOL> status , location , set_cookie , _ = self . _run_test ( <EOL> host , path_info , cookie_dict , action , set_email , set_admin , <EOL> continue_url ) <EOL> self . assertEqual ( <NUM_LIT> , status ) <EOL> self . assertEqual ( '<STR_LIT>' % ( host , path_info ) , location ) <EOL> self . assertEqual ( expected_set , set_cookie ) <EOL> self . assertIsInstance ( location , str ) <EOL> self . assertIsInstance ( set_cookie , str ) <EOL> continue_url = '<STR_LIT>' <EOL> status , location , set_cookie , _ = self . _run_test ( <EOL> host , path_info , cookie_dict , action , set_email , set_admin , <EOL> continue_url ) <EOL> self . assertEqual ( <NUM_LIT> , status ) <EOL> self . assertEqual ( continue_url , location ) <EOL> self . assertEqual ( expected_set , set_cookie ) <EOL> self . assertIsInstance ( location , str ) <EOL> self . assertIsInstance ( set_cookie , str ) <EOL> def test_passive ( self ) : <EOL> """<STR_LIT>""" <EOL> host = '<STR_LIT>' <EOL> path_info = '<STR_LIT>' <EOL> cookie_dict = { '<STR_LIT>' : '<STR_LIT>' % ( EMAIL , USER_ID ) } <EOL> action = '<STR_LIT>' <EOL> set_email = '<STR_LIT>' <EOL> set_admin = False <EOL> continue_url = '<STR_LIT>' <EOL> continue_url = '<STR_LIT>' <EOL> status , location , set_cookie , content_type = self . _run_test ( <EOL> host , path_info , cookie_dict , action , set_email , set_admin , <EOL> continue_url ) <EOL> self . assertEqual ( <NUM_LIT> , status ) <EOL> self . assertFalse ( set_cookie ) <EOL> self . assertEqual ( '<STR_LIT>' , content_type ) <EOL> self . assertIsInstance ( content_type , str ) <EOL> def _run_test ( self , host , path_info = '<STR_LIT:/>' , cookie_dict = None , action = None , <EOL> set_email = None , set_admin = None , continue_url = None , <EOL> method = '<STR_LIT:GET>' ) : <EOL> """<STR_LIT>""" <EOL> environ = { } <EOL> wsgiref . util . setup_testing_defaults ( environ ) <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> environ [ '<STR_LIT>' ] = host <EOL> environ [ '<STR_LIT>' ] = path_info <EOL> environ [ '<STR_LIT>' ] = method <EOL> if cookie_dict : <EOL> cookie = Cookie . SimpleCookie ( cookie_dict ) <EOL> cookie_value = '<STR_LIT:;>' . join ( m . OutputString ( ) for m in cookie . values ( ) ) <EOL> environ [ '<STR_LIT>' ] = cookie_value <EOL> query_dict = { } <EOL> if action : <EOL> query_dict [ '<STR_LIT:action>' ] = action <EOL> if set_email : <EOL> query_dict [ '<STR_LIT:email>' ] = set_email <EOL> if set_admin : <EOL> query_dict [ '<STR_LIT>' ] = set_admin <EOL> if continue_url : <EOL> query_dict [ '<STR_LIT>' ] = continue_url <EOL> if query_dict : <EOL> environ [ '<STR_LIT>' ] = urllib . urlencode ( query_dict ) <EOL> response_dict = { } <EOL> def start_response ( status , headers ) : <EOL> response_dict [ '<STR_LIT:status>' ] = int ( status . split ( '<STR_LIT:U+0020>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] ) <EOL> response_dict [ '<STR_LIT>' ] = dict ( ( k . lower ( ) , v ) <EOL> for ( k , v ) in headers ) <EOL> login . application ( environ , start_response ) <EOL> return ( response_dict [ '<STR_LIT:status>' ] , <EOL> response_dict [ '<STR_LIT>' ] . get ( '<STR_LIT:location>' ) , <EOL> response_dict [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) , <EOL> response_dict [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
