1 from openid import message
2 from openid import oidutil
3 from openid.extensions import sreg
4
5 import urllib
6 import cgi
7 import unittest
8
10 def test(self):
11 a_default = object()
12 self.failUnlessEqual(self.msg.getArg(ns, key), expected)
13 if expected is None:
14 self.failUnlessEqual(
15 self.msg.getArg(ns, key, a_default), a_default)
16 self.failUnlessRaises(
17 KeyError, self.msg.getArg, ns, key, message.no_default)
18 else:
19 self.failUnlessEqual(
20 self.msg.getArg(ns, key, a_default), expected)
21 self.failUnlessEqual(
22 self.msg.getArg(ns, key, message.no_default), expected)
23
24 return test
25
29
31 self.failUnlessEqual(self.msg.toPostArgs(), {})
32
34 self.failUnlessEqual(self.msg.toArgs(), {})
35
38
41
43 base_url = 'http://base.url/'
44 self.failUnlessEqual(self.msg.toURL(base_url), base_url)
45
48
56
59
62
65
67 self.failUnlessEqual(self.msg.getKey('urn:nothing-significant', 'foo'),
68 None)
69
77
80
83
86
88 self.failUnlessEqual(self.msg.hasKey('urn:nothing-significant', 'foo'),
89 False)
90
96
101
109
110 test_getArgBARE = mkGetArgTest(message.BARE_NS, 'foo')
111 test_getArgNS1 = mkGetArgTest(message.OPENID1_NS, 'foo')
112 test_getArgNS2 = mkGetArgTest(message.OPENID2_NS, 'foo')
113 test_getArgNS3 = mkGetArgTest('urn:nothing-significant', 'foo')
114
122
125
128
131
133 self.failUnlessEqual(self.msg.getArgs('urn:nothing-significant'), {})
134
139
141 update_args = {
142 'Camper van Beethoven':'David Lowery',
143 'Magnolia Electric Co.':'Jason Molina',
144 }
145
146 self.failUnlessEqual(self.msg.getArgs(ns), {})
147 self.msg.updateArgs(ns, update_args)
148 self.failUnlessEqual(self.msg.getArgs(ns), update_args)
149
152
155
158
160 self._test_updateArgsNS('urn:nothing-significant')
161
166
168 key = 'Camper van Beethoven'
169 value = 'David Lowery'
170 self.failUnlessEqual(self.msg.getArg(ns, key), None)
171 self.msg.setArg(ns, key, value)
172 self.failUnlessEqual(self.msg.getArg(ns, key), value)
173
176
179
182
184 self._test_setArgNS('urn:nothing-significant')
185
189
198
200 key = 'Camper van Beethoven'
201 self.failUnlessRaises(KeyError, self.msg.delArg, ns, key)
202
205
208
211
213 self._test_delArgNS('urn:nothing-significant')
214
217
220
225
226 - def test_toPostArgs(self):
227 self.failUnlessEqual(self.msg.toPostArgs(),
228 {'openid.mode':'error',
229 'openid.error':'unit test'})
230
232 self.failUnlessEqual(self.msg.toArgs(), {'mode':'error',
233 'error':'unit test'})
234
238
240 self.failUnlessEqual(self.msg.toURLEncoded(),
241 'openid.error=unit+test&openid.mode=error')
242
244 base_url = 'http://base.url/'
245 actual = self.msg.toURL(base_url)
246 actual_base = actual[:len(base_url)]
247 self.failUnlessEqual(actual_base, base_url)
248 self.failUnlessEqual(actual[len(base_url)], '?')
249 query = actual[len(base_url) + 1:]
250 parsed = cgi.parse_qs(query)
251 self.failUnlessEqual(parsed, {'openid.mode':['error'],
252 'openid.error':['unit test']})
253
256
260
263
267
270
272 self.failUnlessEqual(
273 self.msg.getKey('urn:nothing-significant', 'mode'), None)
274
277
280
283
287
289 self.failUnlessEqual(
290 self.msg.hasKey('urn:nothing-significant', 'mode'), False)
291
292 test_getArgBARE = mkGetArgTest(message.BARE_NS, 'mode')
293 test_getArgNS = mkGetArgTest(message.OPENID_NS, 'mode', 'error')
294 test_getArgNS1 = mkGetArgTest(message.OPENID1_NS, 'mode', 'error')
295 test_getArgNS2 = mkGetArgTest(message.OPENID2_NS, 'mode')
296 test_getArgNS3 = mkGetArgTest('urn:nothing-significant', 'mode')
297
303
306
312
315
317 self.failUnlessEqual(self.msg.getArgs('urn:nothing-significant'), {})
318
320 if before is None:
321 before = {}
322 update_args = {
323 'Camper van Beethoven':'David Lowery',
324 'Magnolia Electric Co.':'Jason Molina',
325 }
326
327 self.failUnlessEqual(self.msg.getArgs(ns), before)
328 self.msg.updateArgs(ns, update_args)
329 after = dict(before)
330 after.update(update_args)
331 self.failUnlessEqual(self.msg.getArgs(ns), after)
332
334 self._test_updateArgsNS(message.OPENID_NS,
335 before={'mode':'error', 'error':'unit test'})
336
339
341 self._test_updateArgsNS(message.OPENID1_NS,
342 before={'mode':'error', 'error':'unit test'})
343
346
348 self._test_updateArgsNS('urn:nothing-significant')
349
351 key = 'Camper van Beethoven'
352 value = 'David Lowery'
353 self.failUnlessEqual(self.msg.getArg(ns, key), None)
354 self.msg.setArg(ns, key, value)
355 self.failUnlessEqual(self.msg.getArg(ns, key), value)
356
359
362
365
368
370 self._test_setArgNS('urn:nothing-significant')
371
373 key = 'Camper van Beethoven'
374 value = 'David Lowery'
375
376 self.failUnlessRaises(KeyError, self.msg.delArg, ns, key)
377 self.msg.setArg(ns, key, value)
378 self.failUnlessEqual(self.msg.getArg(ns, key), value)
379 self.msg.delArg(ns, key)
380 self.failUnlessEqual(self.msg.getArg(ns, key), None)
381
384
387
390
393
395 self._test_delArgNS('urn:nothing-significant')
396
397
400
403
410
411 - def test_toPostArgs(self):
412 self.failUnlessEqual(self.msg.toPostArgs(),
413 {'openid.mode':'error',
414 'openid.error':'unit test',
415 'openid.ns':message.OPENID1_NS
416 })
417
419 self.failUnlessEqual(self.msg.toArgs(), {'mode':'error',
420 'error':'unit test',
421 'ns':message.OPENID1_NS})
422
427
429 self.failUnlessEqual(self.msg.toURLEncoded(),
430 'openid.error=unit+test&openid.mode=error&openid.ns=http%3A%2F%2Fopenid.net%2Fsignon%2F1.0')
431
433 base_url = 'http://base.url/'
434 actual = self.msg.toURL(base_url)
435 actual_base = actual[:len(base_url)]
436 self.failUnlessEqual(actual_base, base_url)
437 self.failUnlessEqual(actual[len(base_url)], '?')
438 query = actual[len(base_url) + 1:]
439 parsed = cgi.parse_qs(query)
440 self.failUnlessEqual(parsed, {'openid.mode':['error'],
441 'openid.error':['unit test'],
442 'openid.ns':[message.OPENID1_NS]
443 })
444
447
448
456
457 - def test_toPostArgs(self):
458 self.failUnlessEqual(self.msg.toPostArgs(),
459 {'openid.mode':'error',
460 'openid.error':'unit test',
461 'openid.ns':message.OPENID2_NS,
462 'xey': 'value',
463 })
464
472
479
481 expected = ('openid.error=unit+test&openid.mode=error&'
482 'openid.ns=%s&xey=value' % (
483 urllib.quote(message.OPENID2_NS, ''),))
484 self.failUnlessEqual(s, expected)
485
486
489
491 base_url = 'http://base.url/'
492 actual = self.msg.toURL(base_url)
493 actual_base = actual[:len(base_url)]
494 self.failUnlessEqual(actual_base, base_url)
495 self.failUnlessEqual(actual[len(base_url)], '?')
496 query = actual[len(base_url) + 1:]
497 self._test_urlencoded(query)
498
501
505
508
512
516
518 self.failUnlessEqual(
519 self.msg.getKey('urn:nothing-significant', 'mode'), None)
520
523
526
530
534
536 self.failUnlessEqual(
537 self.msg.hasKey('urn:nothing-significant', 'mode'), False)
538
539 test_getArgBARE = mkGetArgTest(message.BARE_NS, 'mode')
540 test_getArgNS = mkGetArgTest(message.OPENID_NS, 'mode', 'error')
541 test_getArgNS1 = mkGetArgTest(message.OPENID1_NS, 'mode')
542 test_getArgNS2 = mkGetArgTest(message.OPENID2_NS, 'mode', 'error')
543 test_getArgNS3 = mkGetArgTest('urn:nothing-significant', 'mode')
544
550
554
557
563
565 self.failUnlessEqual(self.msg.getArgs('urn:nothing-significant'), {})
566
568 if before is None:
569 before = {}
570 update_args = {
571 'Camper van Beethoven':'David Lowery',
572 'Magnolia Electric Co.':'Jason Molina',
573 }
574
575 self.failUnlessEqual(self.msg.getArgs(ns), before)
576 self.msg.updateArgs(ns, update_args)
577 after = dict(before)
578 after.update(update_args)
579 self.failUnlessEqual(self.msg.getArgs(ns), after)
580
582 self._test_updateArgsNS(message.OPENID_NS,
583 before={'mode':'error', 'error':'unit test'})
584
586 self._test_updateArgsNS(message.BARE_NS,
587 before={'xey':'value'})
588
591
593 self._test_updateArgsNS(message.OPENID2_NS,
594 before={'mode':'error', 'error':'unit test'})
595
597 self._test_updateArgsNS('urn:nothing-significant')
598
600 key = 'Camper van Beethoven'
601 value = 'David Lowery'
602 self.failUnlessEqual(self.msg.getArg(ns, key), None)
603 self.msg.setArg(ns, key, value)
604 self.failUnlessEqual(self.msg.getArg(ns, key), value)
605
608
611
614
617
619 self._test_setArgNS('urn:nothing-significant')
620
622 """Make sure dotted aliases and OpenID protocol fields are not
623 allowed as namespace aliases."""
624
625 for f in message.OPENID_PROTOCOL_FIELDS + ['dotted.alias']:
626 args = {'openid.ns.%s' % f: 'blah',
627 'openid.%s.foo' % f: 'test'}
628
629
630
631
632 self.failUnlessRaises(AssertionError, self.msg.fromPostArgs,
633 args)
634
636 """A failing test for bug #112"""
637 openid_args = {
638 'assoc_handle': '{{HMAC-SHA256}{1211477242.29743}{v5cadg==}',
639 'claimed_id': 'http://nerdbank.org/OPAffirmative/AffirmativeIdentityWithSregNoAssoc.aspx',
640 'ns.sreg': 'http://openid.net/extensions/sreg/1.1',
641 'response_nonce': '2008-05-22T17:27:22ZUoW5.\\NV',
642 'signed': 'return_to,identity,claimed_id,op_endpoint,response_nonce,ns.sreg,sreg.email,sreg.nickname,assoc_handle',
643 'sig': 'e3eGZ10+TNRZitgq5kQlk5KmTKzFaCRI8OrRoXyoFa4=',
644 'mode': 'check_authentication',
645 'op_endpoint': 'http://nerdbank.org/OPAffirmative/ProviderNoAssoc.aspx',
646 'sreg.nickname': 'Andy',
647 'return_to': 'http://localhost.localdomain:8001/process?janrain_nonce=2008-05-22T17%3A27%3A21ZnxHULd',
648 'invalidate_handle': '{{HMAC-SHA1}{1211477241.92242}{H0akXw==}',
649 'identity': 'http://nerdbank.org/OPAffirmative/AffirmativeIdentityWithSregNoAssoc.aspx',
650 'sreg.email': 'a@b.com'
651 }
652 m = message.Message.fromOpenIDArgs(openid_args)
653
654 self.failUnless(('http://openid.net/extensions/sreg/1.1', 'sreg') in
655 list(m.namespaces.iteritems()))
656 missing = []
657 for k in openid_args['signed'].split(','):
658 if not ("openid."+k) in m.toPostArgs().keys():
659 missing.append(k)
660 self.assertEqual([], missing, missing)
661 self.assertEqual(openid_args, m.toArgs())
662 self.failUnless(m.isOpenID1())
663
665 args = {'openid.assoc_handle': 'fa1f5ff0-cde4-11dc-a183-3714bfd55ca8',
666 'openid.claimed_id': 'http://binkley.lan/user/test01',
667 'openid.identity': 'http://test01.binkley.lan/',
668 'openid.mode': 'id_res',
669 'openid.ns': 'http://specs.openid.net/auth/2.0',
670 'openid.ns.pape': 'http://specs.openid.net/extensions/pape/1.0',
671 'openid.op_endpoint': 'http://binkley.lan/server',
672 'openid.pape.auth_policies': 'none',
673 'openid.pape.auth_time': '2008-01-28T20:42:36Z',
674 'openid.pape.nist_auth_level': '0',
675 'openid.response_nonce': '2008-01-28T21:07:04Z99Q=',
676 'openid.return_to': 'http://binkley.lan:8001/process?janrain_nonce=2008-01-28T21%3A07%3A02Z0tMIKx',
677 'openid.sig': 'YJlWH4U6SroB1HoPkmEKx9AyGGg=',
678 'openid.signed': 'assoc_handle,identity,response_nonce,return_to,claimed_id,op_endpoint,pape.auth_time,ns.pape,pape.nist_auth_level,pape.auth_policies'
679 }
680 m = message.Message.fromPostArgs(args)
681 missing = []
682 for k in args['openid.signed'].split(','):
683 if not ("openid."+k) in m.toPostArgs().keys():
684 missing.append(k)
685 self.assertEqual([], missing, missing)
686 self.assertEqual(args, m.toPostArgs())
687 self.failUnless(m.isOpenID2())
688
699
701 key = 'Camper van Beethoven'
702 value = 'David Lowery'
703
704 self.failUnlessRaises(KeyError, self.msg.delArg, ns, key)
705 self.msg.setArg(ns, key, value)
706 self.failUnlessEqual(self.msg.getArg(ns, key), value)
707 self.msg.delArg(ns, key)
708 self.failUnlessEqual(self.msg.getArg(ns, key), None)
709
712
715
718
721
723 self._test_delArgNS('urn:nothing-significant')
724
726 ns = 'urn:unittest_extension'
727 key = 'mykey'
728 value_1 = 'value_1'
729 value_2 = 'value_2'
730
731 self.msg.setArg(ns, key, value_1)
732 self.failUnless(self.msg.getArg(ns, key) == value_1)
733 self.msg.setArg(ns, key, value_2)
734 self.failUnless(self.msg.getArg(ns, key) == value_2)
735
737 self.failUnlessRaises(TypeError, self.msg.fromPostArgs,
738 {'arg': [1, 2, 3]})
739
742
745
748 self.postargs = {
749 'openid.ns': message.OPENID2_NS,
750 'openid.mode': 'checkid_setup',
751 'openid.identity': 'http://bogus.example.invalid:port/',
752 'openid.assoc_handle': 'FLUB',
753 'openid.return_to': 'Neverland',
754 }
755
756 self.action_url = 'scheme://host:port/path?query'
757
758 self.form_tag_attrs = {
759 'company': 'janrain',
760 'class': 'fancyCSS',
761 }
762
763 self.submit_text = 'GO!'
764
765
766
767 self.required_form_attrs = {
768 'accept-charset':'UTF-8',
769 'enctype':'application/x-www-form-urlencoded',
770 'method': 'post',
771 }
772
841
848
850 """Be sure that caller cannot change form method to GET."""
851 m = message.Message.fromPostArgs(self.postargs)
852
853 tag_attrs = dict(self.form_tag_attrs)
854 tag_attrs['method'] = 'GET'
855
856 html = m.toFormMarkup(self.action_url, self.form_tag_attrs,
857 self.submit_text)
858 self._checkForm(html, m, self.action_url,
859 self.form_tag_attrs, self.submit_text)
860
862 """Be sure that caller CANNOT change the form charset for
863 encoding type."""
864 m = message.Message.fromPostArgs(self.postargs)
865
866 tag_attrs = dict(self.form_tag_attrs)
867 tag_attrs['accept-charset'] = 'UCS4'
868 tag_attrs['enctype'] = 'invalid/x-broken'
869
870 html = m.toFormMarkup(self.action_url, tag_attrs,
871 self.submit_text)
872 self._checkForm(html, m, self.action_url,
873 tag_attrs, self.submit_text)
874
875
877 m = message.Message()
878 invalid_things = [
879
880 '',
881
882 'http://openid.net/signon/2.0',
883
884 u'http://specs%\\\r2Eopenid.net/auth/2.0',
885
886 'http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0',
887
888 'http://specs.openid.net/auth/2.0/signon',
889 ]
890
891 for x in invalid_things:
892 self.failUnlessRaises(message.InvalidOpenIDNamespace,
893 m.setOpenIDNamespace, x, False)
894
895
910
917
922
927
928
936
938
939
940 query = {
941 u'openid.assoc_handle': u'',
942 u'openid.claimed_id': u'http://foobar.invalid/',
943 u'openid.identity': u'http://foobar.myopenid.com',
944 u'openid.mode': u'checkid_setup',
945 u'openid.ns': u'http://openid.net/signon/1.1',
946 u'openid.ns.sreg': u'http://openid.net/extensions/sreg/1.1',
947 u'openid.return_to': u'http://drupal.invalid/return_to',
948 u'openid.sreg.required': u'nickname,email',
949 u'openid.trust_root': u'http://drupal.invalid',
950 }
951 m = message.Message.fromPostArgs(query)
952 self.failUnless(m.isOpenID1())
953
954
955
964
966 nsm = message.NamespaceMap()
967 uripat = 'http://example.com/foo%r'
968
969 nsm.add(uripat%0)
970 for n in range(1,23):
971 self.failUnless(uripat%(n-1) in nsm)
972 self.failUnless(nsm.isDefined(uripat%(n-1)))
973 nsm.add(uripat%n)
974
975 for (uri, alias) in nsm.iteritems():
976 self.failUnless(uri[22:]==alias[3:])
977
978 i=0
979 it = nsm.iterAliases()
980 try:
981 while True:
982 it.next()
983 i += 1
984 except StopIteration:
985 self.failUnless(i == 23)
986
987 i=0
988 it = nsm.iterNamespaceURIs()
989 try:
990 while True:
991 it.next()
992 i += 1
993 except StopIteration:
994 self.failUnless(i == 23)
995
996
997 if __name__ == '__main__':
998 unittest.main()
999