5 from openid
import oidutil
8 allowed_s
= string
.ascii_letters
+ string
.digits
+ '+/='
12 isAllowed
= allowed_d
.has_key
16 assert isAllowed(c
), s
24 ''.join(map(chr, range(256))),
28 b64
= oidutil
.toBase64(s
)
30 s_prime
= oidutil
.fromBase64(b64
)
31 assert s_prime
== s
, (s
, b64
, s_prime
)
35 n
= random
.randrange(2048)
36 s
= ''.join(map(chr, map(lambda _
: random
.randrange(256), range(n
))))
37 b64
= oidutil
.toBase64(s
)
39 s_prime
= oidutil
.fromBase64(b64
)
40 assert s_prime
== s
, (s
, b64
, s_prime
)
42 class AppendArgsTest(unittest
.TestCase
):
43 def __init__(self
, desc
, args
, expected
):
44 unittest
.TestCase
.__init
__(self
)
47 self
.expected
= expected
50 result
= oidutil
.appendArgs(*self
.args
)
51 self
.assertEqual(self
.expected
, result
, self
.args
)
53 def shortDescription(self
):
58 class TestSymbol(unittest
.TestCase
):
59 def testCopyHash(self
):
61 s
= oidutil
.Symbol("Foo")
63 d_prime
= copy
.deepcopy(d
)
64 self
.failUnless(s
in d_prime
, "%r isn't in %r" % (s
, d_prime
))
66 t
= oidutil
.Symbol("Bar")
67 self
.failIfEqual(hash(s
), hash(t
))
70 def buildAppendTests():
71 simple
= 'http://www.example.com/'
82 (simple
, [('a', 'b')]),
90 (simple
, [('a', 'b'), ('a', 'c')]),
94 (simple
, [('a', 'b'), ('b', 'c')]),
98 (simple
, [('b', 'c'), ('a', 'b')]),
102 (simple
, {'b':'c', 'a':'b'}),
103 simple
+ '?a=b&b=c'),
106 (simple
, [('=', '=')]),
107 simple
+ '?%3D=%3D'),
110 (simple
, [('this_url', simple
)]),
111 simple
+ '?this_url=http%3A%2F%2Fwww.example.com%2F'),
114 (simple
, [('openid.stuff', 'bother')]),
115 simple
+ '?openid.stuff=bother'),
117 ('args exist (empty)',
118 (simple
+ '?stuff=bother', []),
119 simple
+ '?stuff=bother'),
122 (simple
+ '?stuff=bother', [('ack', 'ack')]),
123 simple
+ '?stuff=bother&ack=ack'),
126 (simple
+ '?stuff=bother', [('ack', 'ack')]),
127 simple
+ '?stuff=bother&ack=ack'),
129 ('args exist (dict)',
130 (simple
+ '?stuff=bother', {'ack': 'ack'}),
131 simple
+ '?stuff=bother&ack=ack'),
133 ('args exist (dict 2)',
134 (simple
+ '?stuff=bother', {'ack': 'ack', 'zebra':'lion'}),
135 simple
+ '?stuff=bother&ack=ack&zebra=lion'),
137 ('three args (dict)',
138 (simple
, {'stuff': 'bother', 'ack': 'ack', 'zebra':'lion'}),
139 simple
+ '?ack=ack&stuff=bother&zebra=lion'),
141 ('three args (list)',
142 (simple
, [('stuff', 'bother'), ('ack', 'ack'), ('zebra', 'lion')]),
143 simple
+ '?stuff=bother&ack=ack&zebra=lion'),
148 for name
, args
, expected
in cases
:
149 test
= AppendArgsTest(name
, args
, expected
)
152 return unittest
.TestSuite(tests
)
155 some
= buildAppendTests()
156 some
.addTest(unittest
.defaultTestLoader
.loadTestsFromTestCase(TestSymbol
))
159 def test_appendArgs():
160 suite
= buildAppendTests()
161 suite
.addTest(unittest
.defaultTestLoader
.loadTestsFromTestCase(TestSymbol
))
162 runner
= unittest
.TextTestRunner()
163 result
= runner
.run(suite
)
164 assert result
.wasSuccessful()
166 # XXX: there are more functions that could benefit from being better
167 # specified and tested in oidutil.py These include, but are not
168 # limited to appendArgs
170 def test(skipPyUnit
=True):
175 if __name__
== '__main__':
176 test(skipPyUnit
=False)