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