2 from openid
.consumer
.discover
import \
3 OpenIDServiceEndpoint
, OPENID_1_1_TYPE
, OPENID_1_0_TYPE
5 from openid
.yadis
.services
import applyFilter
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">
20 return XRDS_BOILERPLATE
% (services
,)
22 def mkService(uris
=None, type_uris
=None, local_id
=None, dent
=' '):
23 chunks
= [dent
, '<Service>\n']
26 for type_uri
in type_uris
:
27 chunks
.extend([dent2
+ '<Type>', type_uri
, '</Type>\n'])
31 if type(uri
) is tuple:
36 chunks
.extend([dent2
, '<URI'])
38 chunks
.extend([' priority="', str(prio
), '"'])
39 chunks
.extend(['>', uri
, '</URI>\n'])
43 [dent2
, '<openid:Delegate>', local_id
, '</openid:Delegate>\n'])
45 chunks
.extend([dent
, '</Service>\n'])
47 return ''.join(chunks
)
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/',
57 'http://example.server.url/'],
60 # Used for generating test data
62 """Generate all non-empty sublists of a list"""
65 subsets_list
+= [[x
] + t
for t
in subsets_list
]
68 # A couple of example extension type URIs. These are not at all
69 # official, but are just here for testing.
71 'http://janrain.com/extension/blah',
72 'http://openid.net/sreg/1.0',
75 # All valid combinations of Type tags that should produce an OpenID endpoint
79 # All non-empty sublists of the valid OpenID type URIs
80 for ts
in subsets([OPENID_1_0_TYPE
, OPENID_1_1_TYPE
])
83 # All combinations of extension types (including empty extenstion list)
84 for exts
in subsets(ext_types
)
87 # Range of valid Delegate tag values for generating test data
90 'http://vanity.domain/',
91 'https://somewhere/yadis/',
94 # All combinations of valid URIs, Type URIs and Delegate tags
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
102 class OpenIDYadisTest(unittest
.TestCase
):
103 def __init__(self
, uris
, type_uris
, local_id
):
104 unittest
.TestCase
.__init
__(self
)
106 self
.type_uris
= type_uris
107 self
.local_id
= local_id
109 def shortDescription(self
):
111 return 'Successful OpenID Yadis parsing case'
114 self
.yadis_url
= 'http://unit.test/'
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
)
123 # Parse into endpoint objects that we will check
124 endpoints
= applyFilter(
125 self
.yadis_url
, self
.xrds
, OpenIDServiceEndpoint
)
127 # make sure there are the same number of endpoints as
128 # URIs. This assumes that the type_uris contains at least one
130 self
.failUnlessEqual(len(self
.uris
), len(endpoints
))
132 # So that we can check equality on the endpoint types
133 type_uris
= list(self
.type_uris
)
137 for endpoint
in endpoints
:
138 seen_uris
.append(endpoint
.server_url
)
140 # All endpoints will have same yadis_url
141 self
.failUnlessEqual(self
.yadis_url
, endpoint
.claimed_id
)
144 self
.failUnlessEqual(self
.local_id
, endpoint
.local_id
)
147 actual_types
= list(endpoint
.type_uris
)
149 self
.failUnlessEqual(actual_types
, type_uris
)
151 # So that they will compare equal, because we don't care what
154 uris
= list(self
.uris
)
157 # Make sure we saw all URIs, and saw each one once
158 self
.failUnlessEqual(uris
, seen_uris
)
163 cases
.append(OpenIDYadisTest(*args
))
164 return unittest
.TestSuite(cases
)