1 | ## |
---|
2 | ## test_authentication.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Fri Aug 20 08:18:58 2010 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2010 Uli Fouquet |
---|
8 | ## This program is free software; you can redistribute it and/or modify |
---|
9 | ## it under the terms of the GNU General Public License as published by |
---|
10 | ## the Free Software Foundation; either version 2 of the License, or |
---|
11 | ## (at your option) any later version. |
---|
12 | ## |
---|
13 | ## This program is distributed in the hope that it will be useful, |
---|
14 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | ## GNU General Public License for more details. |
---|
17 | ## |
---|
18 | ## You should have received a copy of the GNU General Public License |
---|
19 | ## along with this program; if not, write to the Free Software |
---|
20 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
21 | ## |
---|
22 | import grok |
---|
23 | import unittest |
---|
24 | from zope.component import provideAdapter |
---|
25 | from zope.component.hooks import setSite |
---|
26 | from zope.interface import verify |
---|
27 | from zope.pluggableauth.interfaces import IAuthenticatedPrincipalFactory |
---|
28 | from zope.publisher.browser import TestRequest |
---|
29 | from zope.publisher.interfaces import IRequest |
---|
30 | from zope.session.session import RAMSessionDataContainer |
---|
31 | from zope.session.interfaces import ISession |
---|
32 | from zope.site import LocalSiteManager |
---|
33 | from zope.site.folder import Folder |
---|
34 | from zope.site.testing import siteSetUp, siteTearDown |
---|
35 | from zope.testing import cleanup |
---|
36 | from waeup.sirp.applicants.authentication import ( |
---|
37 | ApplicantsAuthenticatorPlugin, WAeUPApplicantCredentialsPlugin, |
---|
38 | ApplicantCredentials, AuthenticatedApplicantPrincipalFactory, |
---|
39 | ApplicantPrincipalInfo, ApplicantPrincipal) |
---|
40 | from waeup.sirp.applicants.interfaces import( |
---|
41 | IJAMBApplicantSessionCredentials,) |
---|
42 | |
---|
43 | class FakeBatch(dict): |
---|
44 | def getAccessCode(self, id): |
---|
45 | return self.get(id) |
---|
46 | |
---|
47 | class FakeAccessCode(object): |
---|
48 | def __init__(self, repr, inv_date=None): |
---|
49 | self.invalidation_date = inv_date |
---|
50 | self.representation = repr |
---|
51 | |
---|
52 | class FakeInvAccessCode(object): |
---|
53 | invalidation_date = True |
---|
54 | |
---|
55 | class FakeApplication(object): |
---|
56 | def __init__(self, ac=None): |
---|
57 | self.access_code = ac |
---|
58 | |
---|
59 | class FakeSite(grok.Application, grok.Container): |
---|
60 | def __init__(self): |
---|
61 | super(FakeSite, self).__init__() |
---|
62 | self['applications'] = { |
---|
63 | 'APP-12345': FakeApplication('APP-12345'), |
---|
64 | 'APP-54321': FakeApplication('APP-54321'), |
---|
65 | 'APP-22222': FakeApplication('APP-OTHER'), |
---|
66 | 'APP-44444': FakeApplication(), |
---|
67 | 'APP-55555': FakeApplication('APP-OTHER'), |
---|
68 | 'JAMB1': FakeApplication(), |
---|
69 | 'JAMB2': FakeApplication('APP-12345'), |
---|
70 | 'JAMB3': FakeApplication('APP-54321'), |
---|
71 | 'JAMB4': FakeApplication('APP-44444'), |
---|
72 | } |
---|
73 | self['accesscodes'] = { |
---|
74 | 'APP': FakeBatch({ |
---|
75 | 'APP-12345': FakeAccessCode('APP-12345'), |
---|
76 | 'APP-54321': FakeAccessCode('APP-54321', True), |
---|
77 | 'APP-11111': FakeAccessCode('APP-11111'), |
---|
78 | 'APP-22222': FakeAccessCode('APP-22222'), |
---|
79 | 'APP-33333': FakeAccessCode('APP-33333', True), |
---|
80 | 'APP-44444': FakeAccessCode('APP-44444', True), |
---|
81 | 'APP-55555': FakeAccessCode('APP-55555', True), |
---|
82 | }) |
---|
83 | } |
---|
84 | |
---|
85 | class AuthenticatorPluginTest(unittest.TestCase): |
---|
86 | |
---|
87 | def setUp(self): |
---|
88 | self.root = Folder() |
---|
89 | siteSetUp(self.root) |
---|
90 | self.app = FakeSite() #grok.Application() |
---|
91 | self.root['app'] = self.app |
---|
92 | self.app.setSiteManager(LocalSiteManager(self.app)) |
---|
93 | self.plugin = ApplicantsAuthenticatorPlugin() |
---|
94 | setSite(self.app) |
---|
95 | return |
---|
96 | |
---|
97 | def tearDown(self): |
---|
98 | siteTearDown() |
---|
99 | return |
---|
100 | |
---|
101 | def test_invalid_credentials(self): |
---|
102 | result = self.plugin.authenticateCredentials('not-a-dict') |
---|
103 | assert result is None |
---|
104 | |
---|
105 | result = self.plugin.authenticateCredentials( |
---|
106 | dict(accesscode=None, foo='blah')) |
---|
107 | assert result is None |
---|
108 | |
---|
109 | result = self.plugin.authenticateCredentials( |
---|
110 | dict(jambregno=None, foo='blah')) |
---|
111 | assert result is None |
---|
112 | |
---|
113 | result = self.plugin.authenticateCredentials( |
---|
114 | dict(accesscode=None, jambregno=None)) |
---|
115 | assert result is None |
---|
116 | |
---|
117 | result = self.plugin.authenticateCredentials( |
---|
118 | dict(accesscode='Nonsense', jambregno='Nonsense')) |
---|
119 | assert result is None |
---|
120 | |
---|
121 | # The nine possible cases, where formal correct authentication |
---|
122 | # data is not valid: |
---|
123 | result = self.plugin.authenticateCredentials( |
---|
124 | dict(accesscode='APP-22222')) |
---|
125 | assert result is None |
---|
126 | |
---|
127 | result = self.plugin.authenticateCredentials( |
---|
128 | dict(accesscode='APP-33333')) |
---|
129 | assert result is None |
---|
130 | |
---|
131 | result = self.plugin.authenticateCredentials( |
---|
132 | dict(accesscode='APP-44444')) |
---|
133 | assert result is None |
---|
134 | |
---|
135 | result = self.plugin.authenticateCredentials( |
---|
136 | dict(accesscode='APP-55555')) |
---|
137 | assert result is None |
---|
138 | |
---|
139 | result = self.plugin.authenticateCredentials( |
---|
140 | dict(accesscode='APP-12345', jambregno='JAMB-NOT-EXISTENT')) |
---|
141 | assert result is None |
---|
142 | |
---|
143 | result = self.plugin.authenticateCredentials( |
---|
144 | dict(accesscode='APP-12345', jambregno='JAMB3')) |
---|
145 | assert result is None |
---|
146 | |
---|
147 | result = self.plugin.authenticateCredentials( |
---|
148 | dict(accesscode='APP-54321', jambregno='JAMB-NOT-EXISTENT')) |
---|
149 | assert result is None |
---|
150 | |
---|
151 | result = self.plugin.authenticateCredentials( |
---|
152 | dict(accesscode='APP-54321', jambregno='JAMB1')) |
---|
153 | assert result is None |
---|
154 | |
---|
155 | result = self.plugin.authenticateCredentials( |
---|
156 | dict(accesscode='APP-54321', jambregno='JAMB2')) |
---|
157 | assert result is None |
---|
158 | |
---|
159 | return |
---|
160 | |
---|
161 | def test_valid_credentials(self): |
---|
162 | """The six different cases where we allow login. |
---|
163 | |
---|
164 | All other combinations should be forbidden. |
---|
165 | """ |
---|
166 | result = self.plugin.authenticateCredentials( |
---|
167 | dict(accesscode='APP-11111')) |
---|
168 | assert result is not None |
---|
169 | |
---|
170 | result = self.plugin.authenticateCredentials( |
---|
171 | dict(accesscode='APP-12345')) |
---|
172 | assert result is not None |
---|
173 | |
---|
174 | result = self.plugin.authenticateCredentials( |
---|
175 | dict(accesscode='APP-54321')) |
---|
176 | assert result is not None |
---|
177 | |
---|
178 | result = self.plugin.authenticateCredentials( |
---|
179 | dict(accesscode='APP-12345', jambregno='JAMB1')) |
---|
180 | |
---|
181 | result = self.plugin.authenticateCredentials( |
---|
182 | dict(accesscode='APP-12345', jambregno='JAMB2')) |
---|
183 | assert result is not None |
---|
184 | |
---|
185 | result = self.plugin.authenticateCredentials( |
---|
186 | dict(accesscode='APP-54321', jambregno='JAMB3')) |
---|
187 | assert result is not None |
---|
188 | |
---|
189 | # check the `principalInfo` method of authenticator |
---|
190 | # plugin. This is only here to satisfy the coverage report. |
---|
191 | assert self.plugin.principalInfo('not-an-id') is None |
---|
192 | return |
---|
193 | |
---|
194 | session_data = { |
---|
195 | 'zope.pluggableauth.browserplugins': {} |
---|
196 | } |
---|
197 | |
---|
198 | class FakeSession(dict): |
---|
199 | def __init__(self, request): |
---|
200 | pass |
---|
201 | |
---|
202 | def get(self, key, default=None): |
---|
203 | return self.__getitem__(key, default) |
---|
204 | |
---|
205 | def __getitem__(self, key, default=None): |
---|
206 | return session_data.get(key, default) |
---|
207 | |
---|
208 | def __setitem__(self, key, value): |
---|
209 | session_data[key] = value |
---|
210 | return |
---|
211 | |
---|
212 | class CredentialsPluginTest(unittest.TestCase): |
---|
213 | |
---|
214 | def setUp(self): |
---|
215 | self.request = TestRequest() |
---|
216 | provideAdapter(FakeSession, (IRequest,), ISession) |
---|
217 | self.plugin = WAeUPApplicantCredentialsPlugin() |
---|
218 | self.full_request = TestRequest() |
---|
219 | session_data['zope.pluggableauth.browserplugins'] = {} |
---|
220 | return |
---|
221 | |
---|
222 | def tearDown(self): |
---|
223 | cleanup.tearDown() |
---|
224 | return |
---|
225 | |
---|
226 | def filled_request(self, form_dict): |
---|
227 | request = TestRequest() |
---|
228 | for key, value in form_dict.items(): |
---|
229 | request.form[key] = value |
---|
230 | return request |
---|
231 | |
---|
232 | def test_extractCredentials_invalid(self): |
---|
233 | result = self.plugin.extractCredentials('not-a-request') |
---|
234 | assert result is None |
---|
235 | return |
---|
236 | |
---|
237 | def test_extractCredentials_empty(self): |
---|
238 | result = self.plugin.extractCredentials(self.request) |
---|
239 | assert result is None |
---|
240 | return |
---|
241 | |
---|
242 | def test_extractCredentials_full_set(self): |
---|
243 | request = self.filled_request({ |
---|
244 | 'form.prefix': 'APP', |
---|
245 | 'form.ac_series': '1', |
---|
246 | 'form.ac_number': '1234567890', |
---|
247 | 'form.jamb_reg_no': 'JAMB_NUMBER', |
---|
248 | }) |
---|
249 | result = self.plugin.extractCredentials(request) |
---|
250 | self.assertEqual(result, {'jambregno': 'JAMB_NUMBER', |
---|
251 | 'accesscode': 'APP-1-1234567890'}) |
---|
252 | return |
---|
253 | |
---|
254 | def test_extractCredentials_accesscode_only(self): |
---|
255 | request = self.filled_request({ |
---|
256 | 'form.prefix': 'APP', |
---|
257 | 'form.ac_series': '1', |
---|
258 | 'form.ac_number': '1234567890', |
---|
259 | }) |
---|
260 | result = self.plugin.extractCredentials(request) |
---|
261 | self.assertEqual(result, {'accesscode': 'APP-1-1234567890'}) |
---|
262 | return |
---|
263 | |
---|
264 | def test_extractCredentials_from_empty_session(self): |
---|
265 | session_data['zope.pluggableauth.browserplugins']['credentials'] = None |
---|
266 | result = self.plugin.extractCredentials(self.request) |
---|
267 | assert result is None |
---|
268 | return |
---|
269 | |
---|
270 | def test_extractCredentials_from_nonempty_session(self): |
---|
271 | credentials = ApplicantCredentials('APP-1-12345') |
---|
272 | session_data['zope.pluggableauth.browserplugins'][ |
---|
273 | 'credentials'] = credentials |
---|
274 | result = self.plugin.extractCredentials(self.request) |
---|
275 | self.assertEqual(result, {'accesscode': 'APP-1-12345'}) |
---|
276 | return |
---|
277 | |
---|
278 | |
---|
279 | class ApplicantCredentialsTest(unittest.TestCase): |
---|
280 | |
---|
281 | def setUp(self): |
---|
282 | self.credentials = ApplicantCredentials('SOME_ACCESSCODE') |
---|
283 | return |
---|
284 | |
---|
285 | def tearDown(self): |
---|
286 | return |
---|
287 | |
---|
288 | def test_methods(self): |
---|
289 | self.assertEqual(self.credentials.getAccessCode(), 'SOME_ACCESSCODE') |
---|
290 | assert self.credentials.getLogin() is None |
---|
291 | assert self.credentials.getPassword() is None |
---|
292 | return |
---|
293 | |
---|
294 | class FakePluggableAuth(object): |
---|
295 | prefix = 'foo' |
---|
296 | |
---|
297 | class PrincipalFactoryTest(unittest.TestCase): |
---|
298 | |
---|
299 | def setUp(self): |
---|
300 | self.info = ApplicantPrincipalInfo('APP-1-1234567890') |
---|
301 | return |
---|
302 | |
---|
303 | def tearDown(self): |
---|
304 | pass |
---|
305 | |
---|
306 | def test_principalFactory_interface(self): |
---|
307 | verify.verifyClass(IAuthenticatedPrincipalFactory, |
---|
308 | AuthenticatedApplicantPrincipalFactory |
---|
309 | ) |
---|
310 | return |
---|
311 | |
---|
312 | def test_principalFactory_create(self): |
---|
313 | factory = AuthenticatedApplicantPrincipalFactory(self.info, None) |
---|
314 | |
---|
315 | assert factory.info is self.info |
---|
316 | assert factory.request is None |
---|
317 | return |
---|
318 | |
---|
319 | def test_principalFactory_call_w_prefix(self): |
---|
320 | factory = AuthenticatedApplicantPrincipalFactory(self.info, None) |
---|
321 | principal = factory(FakePluggableAuth()) |
---|
322 | |
---|
323 | assert isinstance(principal, ApplicantPrincipal) |
---|
324 | self.assertEqual(principal.__repr__(), |
---|
325 | "ApplicantPrincipal('foo.APP-1-1234567890')") |
---|
326 | self.assertEqual(principal.id, 'foo.APP-1-1234567890') |
---|
327 | return |
---|
328 | |
---|
329 | def test_principalFactory_call_wo_prefix(self): |
---|
330 | factory = AuthenticatedApplicantPrincipalFactory(self.info, None) |
---|
331 | fake_auth = FakePluggableAuth() |
---|
332 | fake_auth.prefix = None |
---|
333 | principal = factory(fake_auth) |
---|
334 | self.assertEqual(principal.id, 'APP-1-1234567890') |
---|
335 | return |
---|
336 | |
---|
337 | def test_suite(): |
---|
338 | suite = unittest.TestSuite() |
---|
339 | for testcase in [ |
---|
340 | AuthenticatorPluginTest, CredentialsPluginTest, |
---|
341 | ApplicantCredentialsTest, PrincipalFactoryTest, |
---|
342 | ]: |
---|
343 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
344 | testcase |
---|
345 | ) |
---|
346 | ) |
---|
347 | return suite |
---|