<s> "<STR_LIT>" <EOL> import socket <EOL> import os . path , unittest , sys , time <EOL> from cStringIO import StringIO <EOL> import twill . commands , twill . parse , twill . unit <EOL> from openid . consumer . discover import OpenIDServiceEndpoint , OPENID_1_1_TYPE <EOL> from openid . consumer . consumer import AuthRequest <EOL> class TwillTest ( twill . unit . TestInfo ) : <EOL> """<STR_LIT>""" <EOL> def run_script ( self ) : <EOL> time . sleep ( self . sleep ) <EOL> self . script ( self ) <EOL> def splitDir ( d , count ) : <EOL> for i in xrange ( count ) : <EOL> d = os . path . dirname ( d ) <EOL> return d <EOL> def runExampleServer ( host , port , data_path ) : <EOL> thisfile = os . path . abspath ( sys . modules [ __name__ ] . __file__ ) <EOL> topDir = splitDir ( thisfile , <NUM_LIT:3> ) <EOL> exampleDir = os . path . join ( topDir , '<STR_LIT>' ) <EOL> serverExample = os . path . join ( exampleDir , '<STR_LIT>' ) <EOL> serverModule = { } <EOL> execfile ( serverExample , serverModule ) <EOL> serverMain = serverModule [ '<STR_LIT>' ] <EOL> serverMain ( host , port , data_path ) <EOL> class TestServer ( unittest . TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> self . twillOutput = StringIO ( ) <EOL> self . twillErr = StringIO ( ) <EOL> twill . set_output ( self . twillOutput ) <EOL> twill . set_errout ( self . twillErr ) <EOL> self . server_port = <NUM_LIT> <EOL> self . realm = '<STR_LIT>' % ( self . id ( ) , ) <EOL> self . return_to = self . realm + '<STR_LIT>' <EOL> twill . commands . reset_browser ( ) <EOL> def runExampleServer ( self ) : <EOL> """<STR_LIT>""" <EOL> runExampleServer ( '<STR_LIT:127.0.0.1>' , self . server_port , '<STR_LIT>' ) <EOL> def v1endpoint ( self , port ) : <EOL> """<STR_LIT>""" <EOL> base = "<STR_LIT>" % ( socket . getfqdn ( '<STR_LIT:127.0.0.1>' ) , port ) <EOL> ep = OpenIDServiceEndpoint ( ) <EOL> ep . claimed_id = base + "<STR_LIT>" <EOL> ep . server_url = base + "<STR_LIT>" <EOL> ep . type_uris = [ OPENID_1_1_TYPE ] <EOL> return ep <EOL> def test_checkidv1 ( self ) : <EOL> """<STR_LIT>""" <EOL> ti = TwillTest ( self . twill_checkidv1 , self . runExampleServer , <EOL> self . server_port , sleep = <NUM_LIT> ) <EOL> twill . unit . run_test ( ti ) <EOL> if self . twillErr . getvalue ( ) : <EOL> self . fail ( self . twillErr . getvalue ( ) ) <EOL> def test_allowed ( self ) : <EOL> """<STR_LIT>""" <EOL> ti = TwillTest ( self . twill_allowed , self . runExampleServer , <EOL> self . server_port , sleep = <NUM_LIT> ) <EOL> twill . unit . run_test ( ti ) <EOL> if self . twillErr . getvalue ( ) : <EOL> self . fail ( self . twillErr . getvalue ( ) ) <EOL> def twill_checkidv1 ( self , twillInfo ) : <EOL> endpoint = self . v1endpoint ( self . server_port ) <EOL> authreq = AuthRequest ( endpoint , assoc = None ) <EOL> url = authreq . redirectURL ( self . realm , self . return_to ) <EOL> c = twill . commands <EOL> try : <EOL> c . go ( url ) <EOL> c . get_browser ( ) . _browser . set_handle_redirect ( False ) <EOL> c . submit ( "<STR_LIT:yes>" ) <EOL> c . code ( <NUM_LIT> ) <EOL> headers = c . get_browser ( ) . _browser . response ( ) . info ( ) <EOL> finalURL = headers [ '<STR_LIT>' ] <EOL> self . failUnless ( '<STR_LIT>' in finalURL , finalURL ) <EOL> self . failUnless ( '<STR_LIT>' in finalURL , finalURL ) <EOL> except twill . commands . TwillAssertionError , e : <EOL> msg = '<STR_LIT>' % ( <EOL> str ( e ) , c . get_browser ( ) . get_html ( ) ) <EOL> self . fail ( msg ) <EOL> def twill_allowed ( self , twillInfo ) : <EOL> endpoint = self . v1endpoint ( self . server_port ) <EOL> authreq = AuthRequest ( endpoint , assoc = None ) <EOL> url = authreq . redirectURL ( self . realm , self . return_to ) <EOL> c = twill . commands <EOL> try : <EOL> c . go ( url ) <EOL> c . code ( <NUM_LIT:200> ) <EOL> c . get_browser ( ) . _browser . set_handle_redirect ( False ) <EOL> c . formvalue ( <NUM_LIT:1> , '<STR_LIT>' , '<STR_LIT:true>' ) <EOL> c . find ( '<STR_LIT>' ) <EOL> c . submit ( "<STR_LIT:yes>" ) <EOL> c . code ( <NUM_LIT> ) <EOL> c . go ( url ) <EOL> c . code ( <NUM_LIT> ) <EOL> headers = c . get_browser ( ) . _browser . response ( ) . info ( ) <EOL> finalURL = headers [ '<STR_LIT>' ] <EOL> self . failUnless ( finalURL . startswith ( self . return_to ) ) <EOL> except twill . commands . TwillAssertionError , e : <EOL> from traceback import format_exc <EOL> msg = '<STR_LIT>' % ( <EOL> format_exc ( ) , <EOL> self . twillOutput . getvalue ( ) , <EOL> self . twillErr . getvalue ( ) , <EOL> c . get_browser ( ) . get_html ( ) ) <EOL> self . fail ( msg ) <EOL> def tearDown ( self ) : <EOL> twill . set_output ( None ) <EOL> twill . set_errout ( None ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
