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.hooks import setSite |
---|
25 | from zope.site import LocalSiteManager |
---|
26 | from zope.site.folder import Folder |
---|
27 | from zope.site.testing import siteSetUp, siteTearDown |
---|
28 | from waeup.sirp.jambtables.authentication import ApplicantsAuthenticatorPlugin |
---|
29 | |
---|
30 | class FakeBatch(dict): |
---|
31 | def getAccessCode(self, id): |
---|
32 | return self.get(id) |
---|
33 | |
---|
34 | class FakeAccessCode(object): |
---|
35 | def __init__(self, repr, inv_date=None): |
---|
36 | self.invalidation_date = inv_date |
---|
37 | self.representation = repr |
---|
38 | |
---|
39 | class FakeInvAccessCode(object): |
---|
40 | invalidation_date = True |
---|
41 | |
---|
42 | class FakeApplication(object): |
---|
43 | def __init__(self, ac=None): |
---|
44 | self.access_code = ac |
---|
45 | |
---|
46 | class FakeSite(grok.Application, grok.Container): |
---|
47 | def __init__(self): |
---|
48 | super(FakeSite, self).__init__() |
---|
49 | self['applications'] = { |
---|
50 | 'APP-12345': FakeApplication('APP-12345'), |
---|
51 | 'APP-54321': FakeApplication('APP-54321'), |
---|
52 | 'APP-22222': FakeApplication('APP-OTHER'), |
---|
53 | 'APP-44444': FakeApplication(), |
---|
54 | 'APP-55555': FakeApplication('APP-OTHER'), |
---|
55 | 'JAMB1': FakeApplication(), |
---|
56 | 'JAMB2': FakeApplication('APP-12345'), |
---|
57 | 'JAMB3': FakeApplication('APP-54321'), |
---|
58 | 'JAMB4': FakeApplication('APP-44444'), |
---|
59 | } |
---|
60 | self['accesscodes'] = { |
---|
61 | 'APP': FakeBatch({ |
---|
62 | 'APP-12345': FakeAccessCode('APP-12345'), |
---|
63 | 'APP-54321': FakeAccessCode('APP-54321', True), |
---|
64 | 'APP-11111': FakeAccessCode('APP-11111'), |
---|
65 | 'APP-22222': FakeAccessCode('APP-22222'), |
---|
66 | 'APP-33333': FakeAccessCode('APP-33333', True), |
---|
67 | 'APP-44444': FakeAccessCode('APP-44444', True), |
---|
68 | 'APP-55555': FakeAccessCode('APP-55555', True), |
---|
69 | }) |
---|
70 | } |
---|
71 | |
---|
72 | class AuthenticatorPluginTest(unittest.TestCase): |
---|
73 | |
---|
74 | def setUp(self): |
---|
75 | self.root = Folder() |
---|
76 | siteSetUp(self.root) |
---|
77 | self.app = FakeSite() #grok.Application() |
---|
78 | self.root['app'] = self.app |
---|
79 | self.app.setSiteManager(LocalSiteManager(self.app)) |
---|
80 | self.plugin = ApplicantsAuthenticatorPlugin() |
---|
81 | setSite(self.app) |
---|
82 | return |
---|
83 | |
---|
84 | def tearDown(self): |
---|
85 | siteTearDown() |
---|
86 | return |
---|
87 | |
---|
88 | def test_invalid_credentials(self): |
---|
89 | result = self.plugin.authenticateCredentials('not-a-dict') |
---|
90 | assert result is None |
---|
91 | |
---|
92 | result = self.plugin.authenticateCredentials( |
---|
93 | dict(accesscode=None, foo='blah')) |
---|
94 | assert result is None |
---|
95 | |
---|
96 | result = self.plugin.authenticateCredentials( |
---|
97 | dict(jambregno=None, foo='blah')) |
---|
98 | assert result is None |
---|
99 | |
---|
100 | result = self.plugin.authenticateCredentials( |
---|
101 | dict(accesscode=None, jambregno=None)) |
---|
102 | assert result is None |
---|
103 | |
---|
104 | result = self.plugin.authenticateCredentials( |
---|
105 | dict(accesscode='Nonsense', jambregno='Nonsense')) |
---|
106 | assert result is None |
---|
107 | |
---|
108 | # The nine possible cases, where formal correct authentication |
---|
109 | # data is not valid: |
---|
110 | result = self.plugin.authenticateCredentials( |
---|
111 | dict(accesscode='APP-22222')) |
---|
112 | assert result is None |
---|
113 | |
---|
114 | result = self.plugin.authenticateCredentials( |
---|
115 | dict(accesscode='APP-33333')) |
---|
116 | assert result is None |
---|
117 | |
---|
118 | result = self.plugin.authenticateCredentials( |
---|
119 | dict(accesscode='APP-44444')) |
---|
120 | assert result is None |
---|
121 | |
---|
122 | result = self.plugin.authenticateCredentials( |
---|
123 | dict(accesscode='APP-55555')) |
---|
124 | assert result is None |
---|
125 | |
---|
126 | result = self.plugin.authenticateCredentials( |
---|
127 | dict(accesscode='APP-12345', jambregno='JAMB-NOT-EXISTENT')) |
---|
128 | assert result is None |
---|
129 | |
---|
130 | result = self.plugin.authenticateCredentials( |
---|
131 | dict(accesscode='APP-12345', jambregno='JAMB3')) |
---|
132 | assert result is None |
---|
133 | |
---|
134 | result = self.plugin.authenticateCredentials( |
---|
135 | dict(accesscode='APP-54321', jambregno='JAMB-NOT-EXISTENT')) |
---|
136 | assert result is None |
---|
137 | |
---|
138 | result = self.plugin.authenticateCredentials( |
---|
139 | dict(accesscode='APP-54321', jambregno='JAMB1')) |
---|
140 | assert result is None |
---|
141 | |
---|
142 | result = self.plugin.authenticateCredentials( |
---|
143 | dict(accesscode='APP-54321', jambregno='JAMB2')) |
---|
144 | assert result is None |
---|
145 | |
---|
146 | return |
---|
147 | |
---|
148 | def test_valid_credentials(self): |
---|
149 | """The six different cases where we allow login. |
---|
150 | |
---|
151 | All other combinations should be forbidden. |
---|
152 | """ |
---|
153 | result = self.plugin.authenticateCredentials( |
---|
154 | dict(accesscode='APP-11111')) |
---|
155 | assert result is not None |
---|
156 | |
---|
157 | result = self.plugin.authenticateCredentials( |
---|
158 | dict(accesscode='APP-12345')) |
---|
159 | assert result is not None |
---|
160 | |
---|
161 | result = self.plugin.authenticateCredentials( |
---|
162 | dict(accesscode='APP-54321')) |
---|
163 | assert result is not None |
---|
164 | |
---|
165 | result = self.plugin.authenticateCredentials( |
---|
166 | dict(accesscode='APP-12345', jambregno='JAMB1')) |
---|
167 | |
---|
168 | result = self.plugin.authenticateCredentials( |
---|
169 | dict(accesscode='APP-12345', jambregno='JAMB2')) |
---|
170 | assert result is not None |
---|
171 | |
---|
172 | result = self.plugin.authenticateCredentials( |
---|
173 | dict(accesscode='APP-54321', jambregno='JAMB3')) |
---|
174 | assert result is not None |
---|
175 | return |
---|
176 | |
---|
177 | def test_suite(): |
---|
178 | suite = unittest.TestSuite() |
---|
179 | for testcase in [ |
---|
180 | AuthenticatorPluginTest, |
---|
181 | ]: |
---|
182 | suite.addTest(unittest.TestLoader().loadTestsFromTestCase( |
---|
183 | testcase |
---|
184 | ) |
---|
185 | ) |
---|
186 | return suite |
---|