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