Attachment 'test_oidauth.py'
Download 1 from unittest import TestCase
2 from MoinMoin._tests import TestConfig
3 from MoinMoin import auth, user, userform
4 from MoinMoin import oidauth
5 import unittest
6
7 # TEST: openid login with known user
8 # TEST: openid login with user not seen before
9 # TEST: openid login that fails at beginAuth
10 # TEST: openid login that fails at completeAuth
11
12 class DummyLoginFormHandler(object):
13 called = 0
14 identityURL = None
15
16 def authmethod(self, request, name=None, password=None, login=None,
17 logout=None, _consumer=None, **kw):
18 self.called = self.called + 1
19 self.identityURL = name
20
21 class TestUserForm(TestCase):
22 def setUp(self):
23 self.config = TestConfig(self.request,
24 auth=[oidauth.openid, auth.moin_cookie],
25 user_form_fields=
26 oidauth.ConfigMixin.user_form_fields)
27 # self.request.initTheme()
28
29 def tearDown(self):
30 del self.config
31
32 def test_openIDField(self):
33 self.request.form['login'] = ['on']
34 self.request.user = user.User(self.request, "JimUnitTest")
35 self.request.user.valid = True
36 loginform = userform.UserSettings(self.request).asHTML()
37 # Does there exist a field named 'openid_url'?
38 # (as per http://openid.net/specs.bml#submitclaim )
39 # This is a pretty weak test:
40 self.failUnless('openid_url' in loginform, loginform)
41
42 def test_openIDSet(self):
43 self.request.request_method = 'POST'
44 f = {'save': ['on'],
45 'name': ['JoeUnitTest'],
46 'openid_url': ['http://unittest7.myopenid.com/'],
47 'email': ['joe.ut@localhost'],
48 'password': ['secret'],
49 'password2': ['secret'],
50 }
51 self.request.form.update(f)
52 self.request.user = user.User(self.request, "JoeUnitTest")
53 result = userform.UserSettingsHandler(self.request).handleData()
54 self.failUnlessEqual(result, "User preferences saved!")
55 self.failUnlessEqual(self.request.user.openid_url,
56 'http://unittest7.myopenid.com/')
57
58
59 class MockRequest(object):
60 def __init__(self, orig):
61 self.orig = orig
62 self.cfg = orig.cfg
63 self.calls = []
64 self.request_uri = orig.request_uri
65 self.query_string = orig.query_string
66
67 def getBaseURL(self, *a):
68 return self.orig.getBaseURL(*a)
69
70 def getQualifiedURL(self, *a):
71 return self.orig.getQualifiedURL(*a)
72
73 def log(self, msg):
74 self.calls.append(('log', [msg]))
75
76 def http_redirect(self, url):
77 self.calls.append(('http_redirect', [url]))
78
79 def finish(self):
80 self.calls.append(('finish', []))
81
82 class DummyBeginAuth(object):
83 def __init__(self, result, username):
84 from openid.consumer import consumer
85 auth_req = lambda : None
86 auth_req.token = "a_shiny_silver_token"
87 self.result = {
88 'good': (consumer.SUCCESS, auth_req),
89 'cancelled': (consumer.SUCCESS, None),
90 'failed': (consumer.FAILURE, None)}[result]
91
92 def beginAuth(self, identityURL):
93 return self.result
94
95 def constructRedirect(self, foo, bar, baz):
96 return "http://blah.blah.blah"
97
98 class TestLoginForm(TestCase):
99 def setUp(self):
100 self.config = TestConfig(self.request,
101 auth=[auth.moin_cookie, oidauth.openid],
102 openid_assoc_dir = './openid/',
103 user_form_fields=
104 oidauth.ConfigMixin.user_form_fields)
105 self.request.initTheme()
106
107 def tearDown(self):
108 del self.config
109
110 def test_redirect(self):
111 identityURL = "http://openidenabled.com/test.bob"
112 request = MockRequest(self.request)
113 retval = oidauth.openid(request, name=identityURL, login=True,
114 _consumer=DummyBeginAuth('good', identityURL))
115 self.failUnlessEqual(len(request.calls), 2,
116 "Request received wrong number of messages: %s"
117 % (request.calls,))
118 self.failUnlessEqual(request.calls[0][0], 'http_redirect')
119 self.failUnlessEqual(request.calls[-1][0], 'finish')
120
121
122 class DummyCompleteAuth(object):
123 def __init__(self, result, username):
124 from openid.consumer import consumer
125 self.result = {
126 'good': (consumer.SUCCESS, username),
127 'cancelled': (consumer.SUCCESS, None),
128 'failed': (consumer.FAILURE, None)}[result]
129
130 def completeAuth(self, token, query):
131 return self.result
132
133
134 class TestCheckIDAuth(TestCase):
135 def test_idResponse(self):
136 self.request.form['openid.mode'] = ['id_res']
137 self.request.form['moidtoken'] = ['shiny']
138 consumer = DummyCompleteAuth('good', "http://frankie.myopenid.com/")
139 r = oidauth.openid(self.request, _consumer=consumer)
140 self.failUnlessEqual(r[1], False)
141 self.failUnless(isinstance(r[0], user.User),
142 "This is not user: %r" % (r[0],))
143
144 def test_cancelled(self):
145 self.request.form['openid.mode'] = ['cancel']
146 self.request.form['moidtoken'] = ['penny']
147 consumer = DummyCompleteAuth('cancelled',
148 "http://lousy.myopenid.com/")
149 r = oidauth.openid(self.request, _consumer=consumer)
150 self.failUnlessEqual(r, (None, False))
151 # FIXME: Test for user interface. Should *tell* you that you tried
152 # to log in and failed.
153
154 def test_failure(self):
155 self.request.form['openid.mode'] = ['id_res']
156 self.request.form['moidtoken'] = ['copper']
157 consumer = DummyCompleteAuth('failed', "http://bugsy.myopenid.com/")
158 r = oidauth.openid(self.request, _consumer=consumer)
159 self.failUnlessEqual(r, (None, False))
160 # FIXME: Test for user interface. Should *tell* you that you tried
161 # to log in and failed.
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.