1 | :mod:`waeup.kofa.accesscodes.browser` -- UI components for access-codes |
---|
2 | *********************************************************************** |
---|
3 | |
---|
4 | .. module:: waeup.kofa.accesscodes.browser |
---|
5 | |
---|
6 | Here we visit the access-code related parts of a Kofa 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.kofa.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 | ...<h1>Access Code Batches</h1> |
---|
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 | ...<h1>Access Code Batches</h1> |
---|
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 | <div ...>Archived APP-2 (APP-2_archive-...-zope.mgr.csv)</div> |
---|
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 | <div ...>No batch selected.</div> |
---|
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>... - initialized by Manager</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>... - initialized by Manager</td> |
---|
306 | ... |
---|
307 | |
---|
308 | Searching for non-integer values does not result in an exception: |
---|
309 | |
---|
310 | >>> ctrl = browser.getControl(name='searchtype') |
---|
311 | >>> ctrl.getControl(value='batch_serial').selected = True |
---|
312 | >>> browser.getControl(name='searchterm').value = 'xyz' |
---|
313 | >>> browser.getControl('Search').click() |
---|
314 | >>> print browser.contents |
---|
315 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
316 | ... |
---|
317 | |
---|
318 | And we can search for text in history messages of access |
---|
319 | codes. Looking for the string ``initialized`` we get nearly all |
---|
320 | entries: |
---|
321 | |
---|
322 | >>> browser.open('http://localhost/myuniversity/accesscodes/search') |
---|
323 | >>> ctrl = browser.getControl(name='searchtype') |
---|
324 | >>> ctrl.getControl(value='history').selected = True |
---|
325 | >>> browser.getControl(name='searchterm').value = 'initialized' |
---|
326 | >>> browser.getControl('Search').click() |
---|
327 | >>> print browser.contents |
---|
328 | <!DOCTYPE html ... |
---|
329 | ... |
---|
330 | value="APP-1-<10-DIGITS>" /></td> |
---|
331 | <td>1</td> |
---|
332 | <td>APP-1-<10-DIGITS></td> |
---|
333 | <td>initialized</td> |
---|
334 | <td>... - initialized by Manager</td> |
---|
335 | ... |
---|
336 | |
---|
337 | Enabling and Disabling Found Access Codes |
---|
338 | ----------------------------------------- |
---|
339 | |
---|
340 | If a search is successfull, we can enable or disable the found access |
---|
341 | codes: |
---|
342 | |
---|
343 | >>> browser.open('http://localhost/myuniversity/accesscodes/search') |
---|
344 | >>> ctrl = browser.getControl(name='searchtype') |
---|
345 | >>> ctrl.getControl(value='history').selected = True |
---|
346 | >>> browser.getControl(name='searchterm').value = 'initialized' |
---|
347 | >>> browser.getControl('Search').click() |
---|
348 | |
---|
349 | This lists all access codes. We now tick one to disable it and click |
---|
350 | on the appropriate button: |
---|
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 | ...APP-1-<10-DIGITS> disabled.</div> |
---|
358 | ... |
---|
359 | |
---|
360 | We cannot disable the same entry again: |
---|
361 | |
---|
362 | >>> entries = browser.getControl(name='entries') |
---|
363 | >>> entries.controls[0].selected = True |
---|
364 | >>> browser.getControl('Disable ACs').click() |
---|
365 | >>> print browser.contents |
---|
366 | <!DOCTYPE html ... |
---|
367 | <div ...>APP-1-<10-DIGITS>: Disable transition not allowed.</div> |
---|
368 | ... |
---|
369 | |
---|
370 | But we can reenable the same entry: |
---|
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 | ...APP-1-<10-DIGITS> (re-)enabled.</div> |
---|
378 | ... |
---|
379 | |
---|
380 | Enabling already enabled items gives a warning: |
---|
381 | |
---|
382 | >>> entries = browser.getControl(name='entries') |
---|
383 | >>> entries.controls[0].selected = True |
---|
384 | >>> browser.getControl('Enable ACs').click() |
---|
385 | >>> print browser.contents |
---|
386 | <!DOCTYPE html ... |
---|
387 | <div ...>APP-1-<10-DIGITS>: Re-enable transition not allowed.</div> |
---|
388 | ... |
---|
389 | |
---|
390 | Log- and Archive Files |
---|
391 | ====================== |
---|
392 | |
---|
393 | We store log- and archive-files inside the storage managed by the |
---|
394 | local datacenter. Access-code related files are placed inside an |
---|
395 | ``accesscode`` subdir: |
---|
396 | |
---|
397 | >>> ac_storage = os.path.join(uploadpath, 'accesscodes') |
---|
398 | |
---|
399 | Log files for access-code batches |
---|
400 | --------------------------------- |
---|
401 | |
---|
402 | Whenever a batch is created, there is also a log file with all entries |
---|
403 | created: |
---|
404 | |
---|
405 | >>> sorted(os.listdir(ac_storage)) |
---|
406 | ['APP-1-...-zope.mgr.csv', 'APP-2-...-zope.mgr.csv', ...] |
---|
407 | |
---|
408 | Each logfile name contains the prefix, batch number, date of creation |
---|
409 | and userid of creator. |
---|
410 | |
---|
411 | >>> logfile1 = os.path.join(ac_storage, |
---|
412 | ... sorted(os.listdir(ac_storage))[0]) |
---|
413 | >>> print open(logfile1, 'rb').read() |
---|
414 | "serial","ac","cost" |
---|
415 | "APP","1","12.12" |
---|
416 | "0","APP-1-<10-DIGITS>" |
---|
417 | "1","APP-1-<10-DIGITS>" |
---|
418 | "2","APP-1-<10-DIGITS>" |
---|
419 | "3","APP-1-<10-DIGITS>" |
---|
420 | "4","APP-1-<10-DIGITS>" |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | Archive files |
---|
425 | ------------- |
---|
426 | |
---|
427 | We created an archive file above. An archive file name consists of the |
---|
428 | batch prefix, batch number, the string ``_archive``, creation datetime of |
---|
429 | the archive file and userid of batch creator: |
---|
430 | |
---|
431 | >>> sorted(os.listdir(ac_storage)) |
---|
432 | [..., 'BLA-1_archive-...-zope.mgr.csv', 'imports'] |
---|
433 | |
---|
434 | Archive files eventually also contain infos about invalidation dates |
---|
435 | and have a slightly different format therefore. |
---|
436 | |
---|
437 | >>> archive_file = os.path.join(ac_storage, |
---|
438 | ... sorted(os.listdir(ac_storage))[-2]) |
---|
439 | >>> print open(archive_file, 'rb').read() |
---|
440 | "prefix","serial","ac","state","history" |
---|
441 | "BLA","19.12","1","3" |
---|
442 | "BLA","0","BLA-1-<10-DIGITS>","initialized","..." |
---|
443 | "BLA","1","BLA-1-<10-DIGITS>","initialized","..." |
---|
444 | "BLA","2","BLA-1-<10-DIGITS>","initialized","..." |
---|
445 | |
---|
446 | Clean up: |
---|
447 | |
---|
448 | >>> shutil.rmtree(uploadpath) |
---|