Package openid :: Package test :: Module test_openidyadis
[frames] | no frames]

Source Code for Module openid.test.test_openidyadis

  1  import unittest 
  2  from openid.consumer.discover import \ 
  3       OpenIDServiceEndpoint, OPENID_1_1_TYPE, OPENID_1_0_TYPE 
  4   
  5  from openid.yadis.services import applyFilter 
  6   
  7   
  8  XRDS_BOILERPLATE = '''\ 
  9  <?xml version="1.0" encoding="UTF-8"?> 
 10  <xrds:XRDS xmlns:xrds="xri://$xrds" 
 11             xmlns="xri://$xrd*($v*2.0)" 
 12             xmlns:openid="http://openid.net/xmlns/1.0"> 
 13      <XRD> 
 14  %s\ 
 15      </XRD> 
 16  </xrds:XRDS> 
 17  ''' 
 18   
19 -def mkXRDS(services):
20 return XRDS_BOILERPLATE % (services,)
21
22 -def mkService(uris=None, type_uris=None, local_id=None, dent=' '):
23 chunks = [dent, '<Service>\n'] 24 dent2 = dent + ' ' 25 if type_uris: 26 for type_uri in type_uris: 27 chunks.extend([dent2 + '<Type>', type_uri, '</Type>\n']) 28 29 if uris: 30 for uri in uris: 31 if type(uri) is tuple: 32 uri, prio = uri 33 else: 34 prio = None 35 36 chunks.extend([dent2, '<URI']) 37 if prio is not None: 38 chunks.extend([' priority="', str(prio), '"']) 39 chunks.extend(['>', uri, '</URI>\n']) 40 41 if local_id: 42 chunks.extend( 43 [dent2, '<openid:Delegate>', local_id, '</openid:Delegate>\n']) 44 45 chunks.extend([dent, '</Service>\n']) 46 47 return ''.join(chunks)
48 49 # Different sets of server URLs for use in the URI tag 50 server_url_options = [ 51 [], # This case should not generate an endpoint object 52 ['http://server.url/'], 53 ['https://server.url/'], 54 ['https://server.url/', 'http://server.url/'], 55 ['https://server.url/', 56 'http://server.url/', 57 'http://example.server.url/'], 58 ] 59 60 # Used for generating test data
61 -def subsets(l):
62 """Generate all non-empty sublists of a list""" 63 subsets_list = [[]] 64 for x in l: 65 subsets_list += [[x] + t for t in subsets_list] 66 return subsets_list
67 68 # A couple of example extension type URIs. These are not at all 69 # official, but are just here for testing. 70 ext_types = [ 71 'http://janrain.com/extension/blah', 72 'http://openid.net/sreg/1.0', 73 ] 74 75 # All valid combinations of Type tags that should produce an OpenID endpoint 76 type_uri_options = [ 77 exts + ts 78 79 # All non-empty sublists of the valid OpenID type URIs 80 for ts in subsets([OPENID_1_0_TYPE, OPENID_1_1_TYPE]) 81 if ts 82 83 # All combinations of extension types (including empty extenstion list) 84 for exts in subsets(ext_types) 85 ] 86 87 # Range of valid Delegate tag values for generating test data 88 local_id_options = [ 89 None, 90 'http://vanity.domain/', 91 'https://somewhere/yadis/', 92 ] 93 94 # All combinations of valid URIs, Type URIs and Delegate tags 95 data = [ 96 (uris, type_uris, local_id) 97 for uris in server_url_options 98 for type_uris in type_uri_options 99 for local_id in local_id_options 100 ] 101
102 -class OpenIDYadisTest(unittest.TestCase):
103 - def __init__(self, uris, type_uris, local_id):
104 unittest.TestCase.__init__(self) 105 self.uris = uris 106 self.type_uris = type_uris 107 self.local_id = local_id
108
109 - def shortDescription(self):
110 # XXX: 111 return 'Successful OpenID Yadis parsing case'
112
113 - def setUp(self):
114 self.yadis_url = 'http://unit.test/' 115 116 # Create an XRDS document to parse 117 services = mkService(uris=self.uris, 118 type_uris=self.type_uris, 119 local_id=self.local_id) 120 self.xrds = mkXRDS(services)
121
122 - def runTest(self):
123 # Parse into endpoint objects that we will check 124 endpoints = applyFilter( 125 self.yadis_url, self.xrds, OpenIDServiceEndpoint) 126 127 # make sure there are the same number of endpoints as 128 # URIs. This assumes that the type_uris contains at least one 129 # OpenID type. 130 self.failUnlessEqual(len(self.uris), len(endpoints)) 131 132 # So that we can check equality on the endpoint types 133 type_uris = list(self.type_uris) 134 type_uris.sort() 135 136 seen_uris = [] 137 for endpoint in endpoints: 138 seen_uris.append(endpoint.server_url) 139 140 # All endpoints will have same yadis_url 141 self.failUnlessEqual(self.yadis_url, endpoint.claimed_id) 142 143 # and local_id 144 self.failUnlessEqual(self.local_id, endpoint.local_id) 145 146 # and types 147 actual_types = list(endpoint.type_uris) 148 actual_types.sort() 149 self.failUnlessEqual(actual_types, type_uris) 150 151 # So that they will compare equal, because we don't care what 152 # order they are in 153 seen_uris.sort() 154 uris = list(self.uris) 155 uris.sort() 156 157 # Make sure we saw all URIs, and saw each one once 158 self.failUnlessEqual(uris, seen_uris)
159
160 -def pyUnitTests():
161 cases = [] 162 for args in data: 163 cases.append(OpenIDYadisTest(*args)) 164 return unittest.TestSuite(cases)
165