1 | :mod:`waeup.sirp.accesscodes.browser` -- UI components for access-codes |
---|
2 | *********************************************************************** |
---|
3 | |
---|
4 | .. module:: waeup.sirp.accesscodes.browser |
---|
5 | |
---|
6 | Here we visit the access-code related parts of a SIRP site using |
---|
7 | a virtual browser. |
---|
8 | |
---|
9 | :NOTTest-Layer: functional |
---|
10 | |
---|
11 | Preliminaries |
---|
12 | ============= |
---|
13 | |
---|
14 | Before we can do anything, we have to create a university site: |
---|
15 | |
---|
16 | >>> from zope.testbrowser.testing import Browser |
---|
17 | >>> browser = Browser() |
---|
18 | >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
19 | >>> browser.handleErrors = False |
---|
20 | >>> root = getRootFolder() |
---|
21 | |
---|
22 | >>> from waeup.sirp.app import University |
---|
23 | >>> u = University() |
---|
24 | >>> root['myuniversity'] = u |
---|
25 | |
---|
26 | We set a new datacenter storage path: |
---|
27 | |
---|
28 | >>> import os |
---|
29 | >>> browser.open('http://localhost/myuniversity') |
---|
30 | >>> browser.getLink('Data Center').click() |
---|
31 | >>> browser.getLink('Edit settings').click() |
---|
32 | >>> pathsetting = browser.getControl(name='newpath') |
---|
33 | |
---|
34 | >>> cwd = os.getcwd() |
---|
35 | >>> uploadpath = os.path.join(cwd, 'ac_testfiles') |
---|
36 | >>> os.mkdir(uploadpath) |
---|
37 | >>> pathsetting.value = uploadpath |
---|
38 | >>> browser.getControl(name='save').click() |
---|
39 | |
---|
40 | We remove any existing 'accesscodes' dir from datacenter dir: |
---|
41 | |
---|
42 | >>> import shutil |
---|
43 | >>> if os.path.exists(os.path.join(uploadpath, 'accesscodes')): |
---|
44 | ... shutil.rmtree(os.path.join(uploadpath, 'accesscodes')) |
---|
45 | |
---|
46 | |
---|
47 | Access-code management screen |
---|
48 | ============================= |
---|
49 | |
---|
50 | For users that have the right to manage access-code related stuff, the |
---|
51 | home page of a `University` instance provides a link to the |
---|
52 | access-code management screen. In the beginning, there are already |
---|
53 | three empty batches available which will be filled by online payments: |
---|
54 | |
---|
55 | >>> browser.open('http://localhost/myuniversity') |
---|
56 | >>> browser.getLink('Access Codes').click() |
---|
57 | >>> print browser.contents |
---|
58 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
59 | ... |
---|
60 | <h2>Access Code Batches</h2> |
---|
61 | ... |
---|
62 | ... The following batches are available: |
---|
63 | ...CLR... |
---|
64 | ...HOS... |
---|
65 | ...SFE... |
---|
66 | ... |
---|
67 | |
---|
68 | Adding batches |
---|
69 | ============== |
---|
70 | |
---|
71 | We can add a batch of access-codes using a button displayed in the |
---|
72 | action bar: |
---|
73 | |
---|
74 | >>> browser.getLink('Add Access Code Batch').click() |
---|
75 | |
---|
76 | The add screen shows a form where we have to enter a prefix, the |
---|
77 | number of access codes to be generated and the costs for each card. |
---|
78 | |
---|
79 | >>> browser.getControl(name='form.prefix').value = 'APP' |
---|
80 | >>> browser.getControl(name='form.entry_num').value = '5' |
---|
81 | >>> browser.getControl(name='form.cost').value = '12.12' |
---|
82 | |
---|
83 | If we click 'cancel' afterwards, the whole process will be cancelled |
---|
84 | and we'll be redirected to the management screen: |
---|
85 | |
---|
86 | >>> browser.getControl('Cancel').click() |
---|
87 | >>> browser.url |
---|
88 | 'http://localhost/myuniversity/accesscodes' |
---|
89 | |
---|
90 | >>> 'Batch creation cancelled' in browser.contents |
---|
91 | True |
---|
92 | |
---|
93 | Now let's try again and this time we finish the procedure by clicking |
---|
94 | 'Create batch' in the form: |
---|
95 | |
---|
96 | >>> browser.getLink('Add Access Code Batch').click() |
---|
97 | >>> browser.getControl(name='form.prefix').value = 'APP' |
---|
98 | >>> browser.getControl(name='form.entry_num').value = '5' |
---|
99 | >>> browser.getControl(name='form.cost').value = '12.12' |
---|
100 | >>> browser.getControl('Create batch').click() |
---|
101 | |
---|
102 | We're also redirected to the management screen, with a notice about |
---|
103 | the freshly created batch: |
---|
104 | |
---|
105 | >>> print browser.contents |
---|
106 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
107 | ... |
---|
108 | <h2>Access Code Batches</h2> |
---|
109 | ... |
---|
110 | ... The following batches are available: |
---|
111 | ...APP |
---|
112 | ...- |
---|
113 | ...1 |
---|
114 | ... |
---|
115 | ...5 |
---|
116 | .../ |
---|
117 | ...0 |
---|
118 | ... |
---|
119 | ...12.12... |
---|
120 | ...zope.mgr... |
---|
121 | ... |
---|
122 | |
---|
123 | which means: there exists a batch named ``APP-1`` with 5 entries of |
---|
124 | which 0 have been devalidated, each one costs ``12.12`` and the batch |
---|
125 | was created by ``zope.mgr``. |
---|
126 | |
---|
127 | We create a second batch to see whether searching and related stuff |
---|
128 | works: |
---|
129 | |
---|
130 | >>> browser.getLink('Add Access Code Batch').click() |
---|
131 | >>> browser.getControl(name='form.prefix').value = 'APP' |
---|
132 | >>> browser.getControl(name='form.entry_num').value = '5' |
---|
133 | >>> browser.getControl(name='form.cost').value = '10.12' |
---|
134 | >>> browser.getControl('Create batch').click() |
---|
135 | |
---|
136 | And a third one that can be deleted afterwards: |
---|
137 | |
---|
138 | >>> browser.getLink('Add Access Code Batch').click() |
---|
139 | >>> browser.getControl(name='form.prefix').value = 'BLA' |
---|
140 | >>> browser.getControl(name='form.entry_num').value = '3' |
---|
141 | >>> browser.getControl(name='form.cost').value = '19.12' |
---|
142 | >>> browser.getControl('Create batch').click() |
---|
143 | |
---|
144 | |
---|
145 | Creating Archive Files |
---|
146 | ====================== |
---|
147 | |
---|
148 | Once a batch is created, we can archive it. To do so we have to tick |
---|
149 | the respective checkbox and click on 'Archive': |
---|
150 | |
---|
151 | >>> ctrl = browser.getControl(name='batches') |
---|
152 | >>> ctrl.getControl(value='APP-2').selected = True |
---|
153 | >>> browser.getControl(name='archive').click() |
---|
154 | >>> print browser.contents |
---|
155 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
156 | ... |
---|
157 | <li ...>Archived APP-2 (APP-2_archive-...-zope.mgr.csv)</li> |
---|
158 | ... |
---|
159 | |
---|
160 | If we do not select a batch and try to archive or delete, the system |
---|
161 | will complain: |
---|
162 | |
---|
163 | >>> browser.getControl(name='archive').click() |
---|
164 | >>> print browser.contents |
---|
165 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
166 | ... |
---|
167 | <li ...>No batch selected.</li> |
---|
168 | ... |
---|
169 | |
---|
170 | Deleting Batches |
---|
171 | ================ |
---|
172 | |
---|
173 | We can delete batches. They are automatically archived when doing so: |
---|
174 | |
---|
175 | >>> ctrl = browser.getControl(name='batches') |
---|
176 | >>> ctrl.getControl(value='BLA-1').selected = True |
---|
177 | >>> browser.getControl('Archive and Delete').click() |
---|
178 | >>> print browser.contents |
---|
179 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
180 | ...Archived BLA-1 (BLA-1_archive-...-zope.mgr.csv)... |
---|
181 | ...Deleted batch BLA-1... |
---|
182 | ... |
---|
183 | |
---|
184 | |
---|
185 | Reimporting Batches |
---|
186 | =================== |
---|
187 | |
---|
188 | We can reimport batches using the log files written when a batch was |
---|
189 | created before. So one can reimport the freshly deleted BLA-1 |
---|
190 | batch. |
---|
191 | |
---|
192 | To do so we must copy the logfile into the ``imports`` dir of |
---|
193 | accesscodes inside the university's datacenter storage. Otherwisae the |
---|
194 | list of importable files is empty: |
---|
195 | |
---|
196 | >>> browser.getLink('Reimport Access Code Batch').click() |
---|
197 | >>> print browser.contents |
---|
198 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
199 | ... |
---|
200 | ...No import batches available... |
---|
201 | ... |
---|
202 | |
---|
203 | We can cancel that operation: |
---|
204 | |
---|
205 | >>> browser.getControl('Cancel').click() |
---|
206 | |
---|
207 | We copy the ``BLA-1`` archive batch file over to the ``imports`` directory: |
---|
208 | |
---|
209 | >>> ac_storage = os.path.join(uploadpath, 'accesscodes') |
---|
210 | >>> logfile2 = os.path.join(ac_storage, |
---|
211 | ... sorted(os.listdir(ac_storage))[-2]) |
---|
212 | >>> filename = os.path.basename(logfile2) |
---|
213 | >>> filename |
---|
214 | 'BLA-1_archive...-zope.mgr.csv' |
---|
215 | |
---|
216 | >>> import shutil |
---|
217 | >>> import_path = os.path.join(ac_storage, 'imports') |
---|
218 | >>> shutil.copy(logfile2, import_path) |
---|
219 | |
---|
220 | Now the file will be presented as import source: |
---|
221 | |
---|
222 | >>> browser.getLink('Reimport Access Code Batch').click() |
---|
223 | >>> filename in browser.contents |
---|
224 | True |
---|
225 | |
---|
226 | If we do not tick a filename to import and click 'Reimport', we will |
---|
227 | be warned: |
---|
228 | |
---|
229 | >>> browser.getControl('Reimport').click() |
---|
230 | >>> print browser.contents |
---|
231 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
232 | ...No file chosen. Action cancelled... |
---|
233 | |
---|
234 | Now let's really reimport the batch: |
---|
235 | |
---|
236 | >>> browser.getLink('Reimport Access Code Batch').click() |
---|
237 | >>> ctrl = browser.getControl(name='filenames') |
---|
238 | >>> ctrl.getControl(value=filename).selected = True |
---|
239 | >>> browser.getControl('Reimport').click() |
---|
240 | |
---|
241 | The batch does exist now again: |
---|
242 | |
---|
243 | >>> print browser.contents |
---|
244 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
245 | ...Successfully reimported: BLA-1_archive...-zope.mgr.csv... |
---|
246 | ... |
---|
247 | |
---|
248 | If we try to reimport an existing batch, that won't work: |
---|
249 | |
---|
250 | >>> browser.getLink('Reimport Access Code Batch').click() |
---|
251 | >>> ctrl = browser.getControl(name='filenames') |
---|
252 | >>> ctrl.getControl(value=filename).selected = True |
---|
253 | >>> browser.getControl('Reimport').click() |
---|
254 | >>> print browser.contents |
---|
255 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
256 | ...This batch already exists: BLA-1_archive...-zope.mgr.csv... |
---|
257 | ... |
---|
258 | |
---|
259 | Searching Access Codes |
---|
260 | ====================== |
---|
261 | |
---|
262 | We can search for access codes already created. |
---|
263 | |
---|
264 | First we pick some really existing access codes to look for: |
---|
265 | |
---|
266 | >>> codes = getRootFolder()['myuniversity']['accesscodes'] |
---|
267 | >>> app_1_codes = codes[u'APP-1'] |
---|
268 | >>> app_2_codes = codes[u'APP-2'] |
---|
269 | >>> bla_1_codes = codes[u'BLA-1'] |
---|
270 | |
---|
271 | >>> browser.open('http://localhost/myuniversity/accesscodes/search') |
---|
272 | >>> ctrl = browser.getControl(name='searchtype') |
---|
273 | >>> ctrl.getControl(value='code').selected = True |
---|
274 | >>> browser.getControl(name='searchterm').value = app_1_codes.keys()[0] |
---|
275 | >>> browser.getControl('Search').click() |
---|
276 | |
---|
277 | The first access code in the ``APP-1`` batch is displayed: |
---|
278 | |
---|
279 | >>> print browser.contents |
---|
280 | <!DOCTYPE html ... |
---|
281 | ... |
---|
282 | value="APP-1..." /></td> |
---|
283 | <td>...</td> |
---|
284 | <td>APP-1-...</td> |
---|
285 | <td>initialized</td> |
---|
286 | <td>... - AC initialized by zope.mgr</td> |
---|
287 | ... |
---|
288 | |
---|
289 | We can also search for batch serials (the number of an access code |
---|
290 | inside its batch). Looking for number ``1`` will display first access |
---|
291 | code of each batch ('APP-1', 'APP-2', and 'BLA-1') |
---|
292 | |
---|
293 | >>> browser.open('http://localhost/myuniversity/accesscodes/search') |
---|
294 | >>> ctrl = browser.getControl(name='searchtype') |
---|
295 | >>> ctrl.getControl(value='batch_serial').selected = True |
---|
296 | >>> browser.getControl(name='searchterm').value = '1' |
---|
297 | >>> browser.getControl('Search').click() |
---|
298 | >>> print browser.contents |
---|
299 | <!DOCTYPE html ... |
---|
300 | ... |
---|
301 | value="APP-1..." /></td> |
---|
302 | <td>1</td> |
---|
303 | <td>APP-1-...</td> |
---|
304 | <td>initialized</td> |
---|
305 | <td>... - AC initialized by zope.mgr</td> |
---|
306 | ... |
---|
307 | |
---|
308 | And we can search for text in history messages of access |
---|
309 | codes. Looking for the string ``AC initialized`` we get nearly all |
---|
310 | entries: |
---|
311 | |
---|
312 | >>> browser.open('http://localhost/myuniversity/accesscodes/search') |
---|
313 | >>> ctrl = browser.getControl(name='searchtype') |
---|
314 | >>> ctrl.getControl(value='history').selected = True |
---|
315 | >>> browser.getControl(name='searchterm').value = 'AC initialized' |
---|
316 | >>> browser.getControl('Search').click() |
---|
317 | >>> print browser.contents |
---|
318 | <!DOCTYPE html ... |
---|
319 | ... |
---|
320 | value="APP-1-<10-DIGITS>" /></td> |
---|
321 | <td>1</td> |
---|
322 | <td>APP-1-<10-DIGITS></td> |
---|
323 | <td>initialized</td> |
---|
324 | <td>... - AC initialized by zope.mgr</td> |
---|
325 | ... |
---|
326 | |
---|
327 | Enabling and Disabling Found Access Codes |
---|
328 | ----------------------------------------- |
---|
329 | |
---|
330 | If a search is successfull, we can enable or disable the found access |
---|
331 | codes: |
---|
332 | |
---|
333 | >>> browser.open('http://localhost/myuniversity/accesscodes/search') |
---|
334 | >>> ctrl = browser.getControl(name='searchtype') |
---|
335 | >>> ctrl.getControl(value='history').selected = True |
---|
336 | >>> browser.getControl(name='searchterm').value = 'AC initialized' |
---|
337 | >>> browser.getControl('Search').click() |
---|
338 | |
---|
339 | This lists all access codes. We now tick one to disable it and click |
---|
340 | on the appropriate button: |
---|
341 | |
---|
342 | >>> entries = browser.getControl(name='entries') |
---|
343 | >>> entries.controls[0].selected = True |
---|
344 | >>> browser.getControl('Disable ACs').click() |
---|
345 | >>> print browser.contents |
---|
346 | <!DOCTYPE html ... |
---|
347 | <li class="message">APP-1-<10-DIGITS> disabled.</li> |
---|
348 | ... |
---|
349 | |
---|
350 | We cannot disable the same entry again: |
---|
351 | |
---|
352 | >>> entries = browser.getControl(name='entries') |
---|
353 | >>> entries.controls[0].selected = True |
---|
354 | >>> browser.getControl('Disable ACs').click() |
---|
355 | >>> print browser.contents |
---|
356 | <!DOCTYPE html ... |
---|
357 | <li ...>APP-1-<10-DIGITS>: Disable transition not allowed.</li> |
---|
358 | ... |
---|
359 | |
---|
360 | But we can reenable the same entry: |
---|
361 | |
---|
362 | >>> entries = browser.getControl(name='entries') |
---|
363 | >>> entries.controls[0].selected = True |
---|
364 | >>> browser.getControl('Enable ACs').click() |
---|
365 | >>> print browser.contents |
---|
366 | <!DOCTYPE html ... |
---|
367 | <li class="message">APP-1-<10-DIGITS> (re-)enabled.</li> |
---|
368 | ... |
---|
369 | |
---|
370 | Enabling already enabled items gives a warning: |
---|
371 | |
---|
372 | >>> entries = browser.getControl(name='entries') |
---|
373 | >>> entries.controls[0].selected = True |
---|
374 | >>> browser.getControl('Enable ACs').click() |
---|
375 | >>> print browser.contents |
---|
376 | <!DOCTYPE html ... |
---|
377 | <li ...>APP-1-<10-DIGITS>: Re-enable transition not allowed.</li> |
---|
378 | ... |
---|
379 | |
---|
380 | Log- and Archive Files |
---|
381 | ====================== |
---|
382 | |
---|
383 | We store log- and archive-files inside the storage managed by the |
---|
384 | local datacenter. Access-code related files are placed inside an |
---|
385 | ``accesscode`` subdir: |
---|
386 | |
---|
387 | >>> ac_storage = os.path.join(uploadpath, 'accesscodes') |
---|
388 | |
---|
389 | Log files for access-code batches |
---|
390 | --------------------------------- |
---|
391 | |
---|
392 | Whenever a batch is created, there is also a log file with all entries |
---|
393 | created: |
---|
394 | |
---|
395 | >>> sorted(os.listdir(ac_storage)) |
---|
396 | ['APP-1-...-zope.mgr.csv', 'APP-2-...-zope.mgr.csv', ...] |
---|
397 | |
---|
398 | Each logfile name contains the prefix, batch number, date of creation |
---|
399 | and userid of creator. |
---|
400 | |
---|
401 | >>> logfile1 = os.path.join(ac_storage, |
---|
402 | ... sorted(os.listdir(ac_storage))[0]) |
---|
403 | >>> print open(logfile1, 'rb').read() |
---|
404 | "serial","ac","cost" |
---|
405 | "APP","1","12.12" |
---|
406 | "0","APP-1-<10-DIGITS>" |
---|
407 | "1","APP-1-<10-DIGITS>" |
---|
408 | "2","APP-1-<10-DIGITS>" |
---|
409 | "3","APP-1-<10-DIGITS>" |
---|
410 | "4","APP-1-<10-DIGITS>" |
---|
411 | |
---|
412 | |
---|
413 | |
---|
414 | Archive files |
---|
415 | ------------- |
---|
416 | |
---|
417 | We created an archive file above. An archive file name consists of the |
---|
418 | batch prefix, batch number, the string ``_archive``, creation datetime of |
---|
419 | the archive file and userid of batch creator: |
---|
420 | |
---|
421 | >>> sorted(os.listdir(ac_storage)) |
---|
422 | [..., 'BLA-1_archive-...-zope.mgr.csv', 'imports'] |
---|
423 | |
---|
424 | Archive files eventually also contain infos about invalidation dates |
---|
425 | and have a slightly different format therefore. |
---|
426 | |
---|
427 | >>> archive_file = os.path.join(ac_storage, |
---|
428 | ... sorted(os.listdir(ac_storage))[-2]) |
---|
429 | >>> print open(archive_file, 'rb').read() |
---|
430 | "prefix","serial","ac","state","history" |
---|
431 | "BLA","19.12","1","3" |
---|
432 | "BLA","0","BLA-1-<10-DIGITS>","initialized","..." |
---|
433 | "BLA","1","BLA-1-<10-DIGITS>","initialized","..." |
---|
434 | "BLA","2","BLA-1-<10-DIGITS>","initialized","..." |
---|
435 | |
---|
436 | Clean up: |
---|
437 | |
---|
438 | >>> shutil.rmtree(uploadpath) |
---|