1 | ## |
---|
2 | ## test_accesscodes.py |
---|
3 | ## Login : <uli@pu.smp.net> |
---|
4 | ## Started on Sun Jun 12 13:07:58 2011 Uli Fouquet |
---|
5 | ## $Id$ |
---|
6 | ## |
---|
7 | ## Copyright (C) 2011 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 shutil |
---|
23 | import tempfile |
---|
24 | import unittest |
---|
25 | from datetime import datetime |
---|
26 | from hurry.workflow.interfaces import InvalidTransitionError, IWorkflowState |
---|
27 | from zope.app.testing.functional import FunctionalTestCase |
---|
28 | from zope.component.hooks import setSite, clearSite |
---|
29 | from zope.interface.verify import verifyObject, verifyClass |
---|
30 | from waeup.sirp.app import University |
---|
31 | from waeup.sirp.testing import FunctionalLayer |
---|
32 | from waeup.sirp.accesscodes.accesscodes import ( |
---|
33 | AccessCodeBatch, get_access_code, invalidate_accesscode, AccessCode, |
---|
34 | disable_accesscode, reenable_accesscode, fire_transition, |
---|
35 | AccessCodeBatchContainer,) |
---|
36 | from waeup.sirp.accesscodes.interfaces import ( |
---|
37 | IAccessCode, IAccessCodeBatch, IAccessCodeBatchContainer,) |
---|
38 | from waeup.sirp.accesscodes.workflow import INITIALIZED, USED, DISABLED |
---|
39 | |
---|
40 | class AccessCodeHelpersTests(FunctionalTestCase): |
---|
41 | # Tests for helpers like get_access_code, disable_accesscode, ... |
---|
42 | |
---|
43 | layer = FunctionalLayer |
---|
44 | |
---|
45 | def setUp(self): |
---|
46 | super(AccessCodeHelpersTests, self).setUp() |
---|
47 | |
---|
48 | # Prepopulate ZODB |
---|
49 | app = University() |
---|
50 | self.dc_root = tempfile.mkdtemp() |
---|
51 | app['datacenter'].setStoragePath(self.dc_root) |
---|
52 | |
---|
53 | # Prepopulate the ZODB... |
---|
54 | self.getRootFolder()['app'] = app |
---|
55 | self.app = self.getRootFolder()['app'] |
---|
56 | |
---|
57 | # Create batch |
---|
58 | batch = AccessCodeBatch('now', 'manfred', 'APP', 6.6, 0) |
---|
59 | self.app['accesscodes'].addBatch(batch) |
---|
60 | |
---|
61 | # Fill batch with accesscodes |
---|
62 | batch.addAccessCode(0, '11111111') |
---|
63 | batch.addAccessCode(1, '22222222') |
---|
64 | batch.addAccessCode(2, '33333333') |
---|
65 | self.ac1 = batch.getAccessCode('APP-1-11111111') |
---|
66 | self.ac2 = batch.getAccessCode('APP-1-22222222') |
---|
67 | self.ac3 = batch.getAccessCode('APP-1-33333333') |
---|
68 | |
---|
69 | setSite(self.app) |
---|
70 | return |
---|
71 | |
---|
72 | def tearDown(self): |
---|
73 | shutil.rmtree(self.dc_root) |
---|
74 | super(AccessCodeHelpersTests, self).tearDown() |
---|
75 | return |
---|
76 | |
---|
77 | def test_get_access_code(self): |
---|
78 | ac = get_access_code('APP-1-11111111') |
---|
79 | assert ac is self.ac1 |
---|
80 | |
---|
81 | def test_get_access_code_not_string(self): |
---|
82 | ac = get_access_code(object()) |
---|
83 | assert ac is None |
---|
84 | |
---|
85 | def test_get_access_code_no_proper_pin(self): |
---|
86 | ac = get_access_code('APP-without_pin') |
---|
87 | assert ac is None |
---|
88 | |
---|
89 | def test_get_access_code_invalid_batch_num(self): |
---|
90 | ac = get_access_code('APP-invalid-11111111') |
---|
91 | assert ac is None |
---|
92 | |
---|
93 | def test_get_access_code_invalid_pin(self): |
---|
94 | ac = get_access_code('APP-1-notexistent') |
---|
95 | assert ac is None |
---|
96 | |
---|
97 | def test_invalidate_accesscode(self): |
---|
98 | assert self.ac1.used is False |
---|
99 | result = invalidate_accesscode('APP-1-11111111') |
---|
100 | assert self.ac1.used is True |
---|
101 | assert result is True |
---|
102 | |
---|
103 | def test_disable_accesscode_unused(self): |
---|
104 | # we can disable initialized acs |
---|
105 | assert self.ac1.disabled is False |
---|
106 | disable_accesscode('APP-1-11111111') |
---|
107 | assert self.ac1.disabled is True |
---|
108 | |
---|
109 | def test_disable_accesscode_used(self): |
---|
110 | # we can disable already used acs |
---|
111 | assert self.ac1.disabled is False |
---|
112 | invalidate_accesscode('APP-1-11111111') |
---|
113 | disable_accesscode('APP-1-11111111') |
---|
114 | assert self.ac1.disabled is True |
---|
115 | |
---|
116 | def test_reenable_accesscode(self): |
---|
117 | # we can reenable disabled acs |
---|
118 | disable_accesscode('APP-1-11111111') |
---|
119 | result = reenable_accesscode('APP-1-11111111') |
---|
120 | assert result is True |
---|
121 | assert self.ac1.disabled is False |
---|
122 | |
---|
123 | def test_fire_transition(self): |
---|
124 | # we can fire transitions generally |
---|
125 | fire_transition('APP-1-11111111', 'use') |
---|
126 | assert IWorkflowState(self.ac1).getState() is USED |
---|
127 | |
---|
128 | def test_fire_transition_toward(self): |
---|
129 | # the `toward` keyword is respected |
---|
130 | fire_transition('APP-1-11111111', DISABLED, toward=True) |
---|
131 | assert IWorkflowState(self.ac1).getState() is DISABLED |
---|
132 | |
---|
133 | def test_fire_transition_no_site(self): |
---|
134 | # when no site is available, we will get a TypeError |
---|
135 | clearSite() |
---|
136 | self.assertRaises( |
---|
137 | KeyError, |
---|
138 | fire_transition, 'APP-1-11111111', 'use') |
---|
139 | |
---|
140 | def test_fire_transition_broken_ac_id(self): |
---|
141 | # if we get an invalid access code id (of wrong format) we get |
---|
142 | # ValueErrors |
---|
143 | self.assertRaises( |
---|
144 | ValueError, |
---|
145 | fire_transition, '11111111', 'use') |
---|
146 | |
---|
147 | def test_fire_transition_invalid_batch_id(self): |
---|
148 | # if we request a non-existent batch_id, we'll get a KeyError |
---|
149 | self.assertRaises( |
---|
150 | KeyError, |
---|
151 | fire_transition, 'FOO-1-11111111', 'use') |
---|
152 | |
---|
153 | def test_fire_transition_invalid_ac(self): |
---|
154 | # if we request a non-exitent access-code, we'll get a KeyError |
---|
155 | self.assertRaises( |
---|
156 | KeyError, |
---|
157 | fire_transition, 'APP-1-NONSENSE', 'use') |
---|
158 | |
---|
159 | def test_fire_transition_undef_trans_id(self): |
---|
160 | # asking for undefined transition id means a KeyError |
---|
161 | self.assertRaises( |
---|
162 | KeyError, |
---|
163 | fire_transition, 'APP-1-11111111', 'nonsense') |
---|
164 | |
---|
165 | def test_fire_transition_invalid_transition(self): |
---|
166 | # asking for a forbidden transition will result in |
---|
167 | # InvalidTransitionError |
---|
168 | self.assertRaises( |
---|
169 | InvalidTransitionError, |
---|
170 | fire_transition, 'APP-1-11111111', 'init') # already initialized |
---|
171 | |
---|
172 | |
---|
173 | class AccessCodeTests(FunctionalTestCase): |
---|
174 | # Tests for AccessCode class |
---|
175 | |
---|
176 | layer = FunctionalLayer |
---|
177 | |
---|
178 | def setUp(self): |
---|
179 | super(AccessCodeTests, self).setUp() |
---|
180 | |
---|
181 | # Prepopulate ZODB |
---|
182 | app = University() |
---|
183 | self.dc_root = tempfile.mkdtemp() |
---|
184 | app['datacenter'].setStoragePath(self.dc_root) |
---|
185 | |
---|
186 | # Prepopulate the ZODB... |
---|
187 | self.getRootFolder()['app'] = app |
---|
188 | self.app = self.getRootFolder()['app'] |
---|
189 | |
---|
190 | setSite(self.app) |
---|
191 | return |
---|
192 | |
---|
193 | def tearDown(self): |
---|
194 | shutil.rmtree(self.dc_root) |
---|
195 | super(AccessCodeTests, self).tearDown() |
---|
196 | return |
---|
197 | |
---|
198 | def test_iface(self): |
---|
199 | ac = AccessCode('1', '12345678') |
---|
200 | assert verifyObject(IAccessCode, ac) |
---|
201 | assert verifyClass(IAccessCode, AccessCode) |
---|
202 | |
---|
203 | class AccessCodeBatchTests(FunctionalTestCase): |
---|
204 | # Tests for AccessCodeBatch class |
---|
205 | |
---|
206 | layer = FunctionalLayer |
---|
207 | |
---|
208 | def setUp(self): |
---|
209 | super(AccessCodeBatchTests, self).setUp() |
---|
210 | |
---|
211 | # Prepopulate ZODB |
---|
212 | app = University() |
---|
213 | self.dc_root = tempfile.mkdtemp() |
---|
214 | app['datacenter'].setStoragePath(self.dc_root) |
---|
215 | |
---|
216 | # Prepopulate the ZODB... |
---|
217 | self.getRootFolder()['app'] = app |
---|
218 | self.app = self.getRootFolder()['app'] |
---|
219 | |
---|
220 | setSite(self.app) |
---|
221 | return |
---|
222 | |
---|
223 | def tearDown(self): |
---|
224 | shutil.rmtree(self.dc_root) |
---|
225 | super(AccessCodeBatchTests, self).tearDown() |
---|
226 | return |
---|
227 | |
---|
228 | def test_iface(self): |
---|
229 | batch = AccessCodeBatch( |
---|
230 | datetime(2009, 12, 23), 'Fred','APP', 12.12, 3, num=10) |
---|
231 | assert verifyObject(IAccessCodeBatch, batch) |
---|
232 | assert verifyClass(IAccessCodeBatch, AccessCodeBatch) |
---|
233 | |
---|
234 | class AccessCodeBatchContainerTests(FunctionalTestCase): |
---|
235 | # Tests for AccessCodeContainer class |
---|
236 | |
---|
237 | layer = FunctionalLayer |
---|
238 | |
---|
239 | def setUp(self): |
---|
240 | super(AccessCodeBatchContainerTests, self).setUp() |
---|
241 | |
---|
242 | # Prepopulate ZODB |
---|
243 | app = University() |
---|
244 | self.dc_root = tempfile.mkdtemp() |
---|
245 | app['datacenter'].setStoragePath(self.dc_root) |
---|
246 | |
---|
247 | # Prepopulate the ZODB... |
---|
248 | self.getRootFolder()['app'] = app |
---|
249 | self.app = self.getRootFolder()['app'] |
---|
250 | |
---|
251 | setSite(self.app) |
---|
252 | return |
---|
253 | |
---|
254 | def tearDown(self): |
---|
255 | shutil.rmtree(self.dc_root) |
---|
256 | super(AccessCodeBatchContainerTests, self).tearDown() |
---|
257 | return |
---|
258 | |
---|
259 | def test_iface(self): |
---|
260 | accesscodes = AccessCodeBatchContainer() |
---|
261 | assert verifyObject(IAccessCodeBatchContainer, accesscodes) |
---|
262 | assert verifyClass(IAccessCodeBatchContainer, AccessCodeBatchContainer) |
---|