<s> import unittest <EOL> from openid import message <EOL> from openid . test . support import OpenIDTestMixin <EOL> from openid . consumer import consumer <EOL> from openid . test . test_consumer import TestIdRes <EOL> from openid . consumer import discover <EOL> def const ( result ) : <EOL> """<STR_LIT>""" <EOL> def constResult ( * args , ** kwargs ) : <EOL> return result <EOL> return constResult <EOL> class DiscoveryVerificationTest ( OpenIDTestMixin , TestIdRes ) : <EOL> def failUnlessProtocolError ( self , prefix , callable , * args , ** kwargs ) : <EOL> try : <EOL> result = callable ( * args , ** kwargs ) <EOL> except consumer . ProtocolError as e : <EOL> e_arg = e . args [ <NUM_LIT:0> ] <EOL> self . assertTrue ( <EOL> e_arg . startswith ( prefix ) , <EOL> '<STR_LIT>' % ( prefix , e_arg ) ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( prefix , result ) ) <EOL> def test_openID1NoLocalID ( self ) : <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . claimed_id = '<STR_LIT>' <EOL> msg = message . Message . fromOpenIDArgs ( { } ) <EOL> self . failUnlessProtocolError ( <EOL> '<STR_LIT>' , <EOL> self . consumer . _verifyDiscoveryResults , msg , endpoint ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openID1NoEndpoint ( self ) : <EOL> msg = message . Message . fromOpenIDArgs ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertRaises ( RuntimeError , <EOL> self . consumer . _verifyDiscoveryResults , msg ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openID2NoOPEndpointArg ( self ) : <EOL> msg = message . Message . fromOpenIDArgs ( { '<STR_LIT>' : message . OPENID2_NS } ) <EOL> self . assertRaises ( KeyError , <EOL> self . consumer . _verifyDiscoveryResults , msg ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openID2LocalIDNoClaimed ( self ) : <EOL> msg = message . Message . fromOpenIDArgs ( { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . failUnlessProtocolError ( <EOL> '<STR_LIT>' , <EOL> self . consumer . _verifyDiscoveryResults , msg ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openID2NoLocalIDClaimed ( self ) : <EOL> msg = message . Message . fromOpenIDArgs ( { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . failUnlessProtocolError ( <EOL> '<STR_LIT>' , <EOL> self . consumer . _verifyDiscoveryResults , msg ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openID2NoIdentifiers ( self ) : <EOL> op_endpoint = '<STR_LIT>' <EOL> msg = message . Message . fromOpenIDArgs ( { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : op_endpoint } ) <EOL> result_endpoint = self . consumer . _verifyDiscoveryResults ( msg ) <EOL> self . assertTrue ( result_endpoint . isOPIdentifier ( ) ) <EOL> self . assertEqual ( op_endpoint , result_endpoint . server_url ) <EOL> self . assertEqual ( None , result_endpoint . claimed_id ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openID2NoEndpointDoesDisco ( self ) : <EOL> op_endpoint = '<STR_LIT>' <EOL> sentinel = discover . OpenIDServiceEndpoint ( ) <EOL> sentinel . claimed_id = '<STR_LIT>' <EOL> self . consumer . _discoverAndVerify = const ( sentinel ) <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : op_endpoint } ) <EOL> result = self . consumer . _verifyDiscoveryResults ( msg ) <EOL> self . assertEqual ( sentinel , result ) <EOL> self . failUnlessLogMatches ( '<STR_LIT>' ) <EOL> def test_openID2MismatchedDoesDisco ( self ) : <EOL> mismatched = discover . OpenIDServiceEndpoint ( ) <EOL> mismatched . identity = '<STR_LIT>' <EOL> mismatched . local_id = '<STR_LIT>' <EOL> op_endpoint = '<STR_LIT>' <EOL> sentinel = discover . OpenIDServiceEndpoint ( ) <EOL> sentinel . claimed_id = '<STR_LIT>' <EOL> self . consumer . _discoverAndVerify = const ( sentinel ) <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : op_endpoint } ) <EOL> result = self . consumer . _verifyDiscoveryResults ( msg , mismatched ) <EOL> self . assertEqual ( sentinel , result ) <EOL> self . failUnlessLogMatches ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_openid2UsePreDiscovered ( self ) : <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . local_id = '<STR_LIT>' <EOL> endpoint . claimed_id = '<STR_LIT>' <EOL> endpoint . server_url = '<STR_LIT>' <EOL> endpoint . type_uris = [ discover . OPENID_2_0_TYPE ] <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : endpoint . local_id , <EOL> '<STR_LIT>' : endpoint . claimed_id , <EOL> '<STR_LIT>' : endpoint . server_url } ) <EOL> result = self . consumer . _verifyDiscoveryResults ( msg , endpoint ) <EOL> self . assertTrue ( result is endpoint ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openid2UsePreDiscoveredWrongType ( self ) : <EOL> text = "<STR_LIT>" <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . local_id = '<STR_LIT>' <EOL> endpoint . claimed_id = '<STR_LIT>' <EOL> endpoint . server_url = '<STR_LIT>' <EOL> endpoint . type_uris = [ discover . OPENID_1_1_TYPE ] <EOL> def discoverAndVerify ( claimed_id , to_match_endpoints ) : <EOL> self . assertEqual ( claimed_id , endpoint . claimed_id ) <EOL> for to_match in to_match_endpoints : <EOL> self . assertEqual ( claimed_id , to_match . claimed_id ) <EOL> raise consumer . ProtocolError ( text ) <EOL> self . consumer . _discoverAndVerify = discoverAndVerify <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : endpoint . local_id , <EOL> '<STR_LIT>' : endpoint . claimed_id , <EOL> '<STR_LIT>' : endpoint . server_url } ) <EOL> try : <EOL> r = self . consumer . _verifyDiscoveryResults ( msg , endpoint ) <EOL> except consumer . ProtocolError as e : <EOL> self . assertTrue ( str ( e ) , text ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" % ( r , ) ) <EOL> self . failUnlessLogMatches ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_openid1UsePreDiscovered ( self ) : <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . local_id = '<STR_LIT>' <EOL> endpoint . claimed_id = '<STR_LIT>' <EOL> endpoint . server_url = '<STR_LIT>' <EOL> endpoint . type_uris = [ discover . OPENID_1_1_TYPE ] <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID1_NS , <EOL> '<STR_LIT>' : endpoint . local_id } ) <EOL> result = self . consumer . _verifyDiscoveryResults ( msg , endpoint ) <EOL> self . assertTrue ( result is endpoint ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openid1UsePreDiscoveredWrongType ( self ) : <EOL> class VerifiedError ( Exception ) : <EOL> pass <EOL> def discoverAndVerify ( claimed_id , _to_match ) : <EOL> raise VerifiedError <EOL> self . consumer . _discoverAndVerify = discoverAndVerify <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . local_id = '<STR_LIT>' <EOL> endpoint . claimed_id = '<STR_LIT>' <EOL> endpoint . server_url = '<STR_LIT>' <EOL> endpoint . type_uris = [ discover . OPENID_2_0_TYPE ] <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID1_NS , <EOL> '<STR_LIT>' : endpoint . local_id } ) <EOL> self . assertRaises ( <EOL> VerifiedError , <EOL> self . consumer . _verifyDiscoveryResults , msg , endpoint ) <EOL> self . failUnlessLogMatches ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def test_openid2Fragment ( self ) : <EOL> claimed_id = "<STR_LIT>" <EOL> claimed_id_frag = claimed_id + "<STR_LIT>" <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . local_id = '<STR_LIT>' <EOL> endpoint . claimed_id = claimed_id <EOL> endpoint . server_url = '<STR_LIT>' <EOL> endpoint . type_uris = [ discover . OPENID_2_0_TYPE ] <EOL> msg = message . Message . fromOpenIDArgs ( <EOL> { '<STR_LIT>' : message . OPENID2_NS , <EOL> '<STR_LIT>' : endpoint . local_id , <EOL> '<STR_LIT>' : claimed_id_frag , <EOL> '<STR_LIT>' : endpoint . server_url } ) <EOL> result = self . consumer . _verifyDiscoveryResults ( msg , endpoint ) <EOL> self . assertEqual ( result . local_id , endpoint . local_id ) <EOL> self . assertEqual ( result . server_url , endpoint . server_url ) <EOL> self . assertEqual ( result . type_uris , endpoint . type_uris ) <EOL> self . assertEqual ( result . claimed_id , claimed_id_frag ) <EOL> self . failUnlessLogEmpty ( ) <EOL> def test_openid1Fallback1_0 ( self ) : <EOL> claimed_id = '<STR_LIT>' <EOL> endpoint = None <EOL> resp_mesg = message . Message . fromOpenIDArgs ( { <EOL> '<STR_LIT>' : message . OPENID1_NS , <EOL> '<STR_LIT>' : claimed_id } ) <EOL> resp_mesg . setArg ( message . BARE_NS , '<STR_LIT>' , claimed_id ) <EOL> expected_endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> expected_endpoint . type_uris = [ discover . OPENID_1_0_TYPE ] <EOL> expected_endpoint . local_id = None <EOL> expected_endpoint . claimed_id = claimed_id <EOL> discovered_services = [ expected_endpoint ] <EOL> self . consumer . _discover = lambda * args : ( '<STR_LIT>' , discovered_services ) <EOL> actual_endpoint = self . consumer . _verifyDiscoveryResults ( <EOL> resp_mesg , endpoint ) <EOL> self . assertTrue ( actual_endpoint is expected_endpoint ) <EOL> class TestVerifyDiscoverySingle ( TestIdRes ) : <EOL> def test_endpointWithoutLocalID ( self ) : <EOL> endpoint = discover . OpenIDServiceEndpoint ( ) <EOL> endpoint . server_url = "<STR_LIT>" <EOL> endpoint . claimed_id = "<STR_LIT>" <EOL> to_match = discover . OpenIDServiceEndpoint ( ) <EOL> to_match . server_url = "<STR_LIT>" <EOL> to_match . claimed_id = "<STR_LIT>" <EOL> to_match . local_id = "<STR_LIT>" <EOL> result = self . consumer . _verifyDiscoverySingle ( endpoint , to_match ) <EOL> self . assertEqual ( result , None ) <EOL> self . failUnlessLogEmpty ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> unittest . main ( ) </s>
