<s> import unittest <EOL> from openid . yadis import services , etxrd , xri <EOL> import os . path <EOL> def datapath ( filename ) : <EOL> module_directory = os . path . dirname ( os . path . abspath ( __file__ ) ) <EOL> return os . path . join ( module_directory , '<STR_LIT:data>' , '<STR_LIT>' , filename ) <EOL> XRD_FILE = datapath ( '<STR_LIT>' ) <EOL> NOXRDS_FILE = datapath ( '<STR_LIT>' ) <EOL> NOXRD_FILE = datapath ( '<STR_LIT>' ) <EOL> LID_2_0 = "<STR_LIT>" <EOL> TYPEKEY_1_0 = "<STR_LIT>" <EOL> def simpleOpenIDTransformer ( endpoint ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' not in endpoint . type_uris : <EOL> return None <EOL> delegates = list ( endpoint . service_element . findall ( <EOL> '<STR_LIT>' ) ) <EOL> assert len ( delegates ) == <NUM_LIT:1> <EOL> delegate = delegates [ <NUM_LIT:0> ] . text <EOL> return ( endpoint . uri , delegate ) <EOL> class TestServiceParser ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . xmldoc = file ( XRD_FILE ) . read ( ) <EOL> self . yadis_url = '<STR_LIT>' <EOL> def _getServices ( self , flt = None ) : <EOL> return list ( services . applyFilter ( self . yadis_url , self . xmldoc , flt ) ) <EOL> def testParse ( self ) : <EOL> """<STR_LIT>""" <EOL> services = self . _getServices ( ) <EOL> def testParseOpenID ( self ) : <EOL> """<STR_LIT>""" <EOL> services = self . _getServices ( simpleOpenIDTransformer ) <EOL> expectedServices = [ <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> ( "<STR_LIT>" , <EOL> "<STR_LIT>" ) , <EOL> ] <EOL> it = iter ( services ) <EOL> for ( server_url , delegate ) in expectedServices : <EOL> for ( actual_url , actual_delegate ) in it : <EOL> self . failUnlessEqual ( server_url , actual_url ) <EOL> self . failUnlessEqual ( delegate , actual_delegate ) <EOL> break <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def _checkServices ( self , expectedServices ) : <EOL> """<STR_LIT>""" <EOL> it = iter ( self . _getServices ( ) ) <EOL> for ( type_uri , uri ) in expectedServices : <EOL> for service in it : <EOL> if type_uri in service . type_uris : <EOL> self . failUnlessEqual ( service . uri , uri ) <EOL> break <EOL> else : <EOL> self . fail ( '<STR_LIT>' % ( type_uri , ) ) <EOL> def testGetSeveral ( self ) : <EOL> """<STR_LIT>""" <EOL> expectedServices = [ <EOL> ( TYPEKEY_1_0 , None ) , <EOL> ( LID_2_0 , "<STR_LIT>" ) , <EOL> ] <EOL> self . _checkServices ( expectedServices ) <EOL> def testGetSeveralForOne ( self ) : <EOL> """<STR_LIT>""" <EOL> types = [ '<STR_LIT>' <EOL> , '<STR_LIT>' <EOL> ] <EOL> uri = "<STR_LIT>" <EOL> for service in self . _getServices ( ) : <EOL> if service . uri == uri : <EOL> found_types = service . matchTypes ( types ) <EOL> if found_types == types : <EOL> break <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> def testNoXRDS ( self ) : <EOL> """<STR_LIT>""" <EOL> self . xmldoc = file ( NOXRDS_FILE ) . read ( ) <EOL> self . failUnlessRaises ( <EOL> etxrd . XRDSError , <EOL> services . applyFilter , self . yadis_url , self . xmldoc , None ) <EOL> def testEmpty ( self ) : <EOL> """<STR_LIT>""" <EOL> self . xmldoc = '<STR_LIT>' <EOL> self . failUnlessRaises ( <EOL> etxrd . XRDSError , <EOL> services . applyFilter , self . yadis_url , self . xmldoc , None ) <EOL> def testNoXRD ( self ) : <EOL> """<STR_LIT>""" <EOL> self . xmldoc = file ( NOXRD_FILE ) . read ( ) <EOL> self . failUnlessRaises ( <EOL> etxrd . XRDSError , <EOL> services . applyFilter , self . yadis_url , self . xmldoc , None ) <EOL> class TestCanonicalID ( unittest . TestCase ) : <EOL> def mkTest ( iname , filename , expectedID ) : <EOL> """<STR_LIT>""" <EOL> filename = datapath ( filename ) <EOL> def test ( self ) : <EOL> xrds = etxrd . parseXRDS ( file ( filename ) . read ( ) ) <EOL> self . _getCanonicalID ( iname , xrds , expectedID ) <EOL> return test <EOL> test_delegated = mkTest ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> test_delegated_r1 = mkTest ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> test_delegated_r2 = mkTest ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> test_sometimesprefix = mkTest ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> test_prefixsometimes = mkTest ( <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> test_spoof1 = mkTest ( "<STR_LIT>" , "<STR_LIT>" , etxrd . XRDSFraud ) <EOL> test_spoof2 = mkTest ( "<STR_LIT>" , "<STR_LIT>" , etxrd . XRDSFraud ) <EOL> test_spoof3 = mkTest ( "<STR_LIT>" , "<STR_LIT>" , etxrd . XRDSFraud ) <EOL> test_status222 = mkTest ( "<STR_LIT>" , "<STR_LIT>" , None ) <EOL> test_iri_auth_not_allowed = mkTest ( <EOL> "<STR_LIT>" , "<STR_LIT>" , etxrd . XRDSFraud ) <EOL> test_iri_auth_not_allowed . __doc__ = "<STR_LIT>" <EOL> def _getCanonicalID ( self , iname , xrds , expectedID ) : <EOL> if isinstance ( expectedID , ( str , unicode , type ( None ) ) ) : <EOL> cid = etxrd . getCanonicalID ( iname , xrds ) <EOL> self . failUnlessEqual ( cid , expectedID and xri . XRI ( expectedID ) ) <EOL> elif issubclass ( expectedID , etxrd . XRDSError ) : <EOL> self . failUnlessRaises ( expectedID , etxrd . getCanonicalID , <EOL> iname , xrds ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" <EOL> % ( expectedID , ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
