[5125] | 1 | :mod:`waeup.sirp.accesscodes.browser` -- UI components for access-codes |
---|
| 2 | *********************************************************************** |
---|
[5105] | 3 | |
---|
[5125] | 4 | .. module:: waeup.sirp.accesscodes.browser |
---|
| 5 | |
---|
[5105] | 6 | Here we visit the access-code related parts of a WAeUP SIRP site using |
---|
| 7 | a virtual browser. |
---|
| 8 | |
---|
| 9 | :Test-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 | |
---|
[5115] | 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 | |
---|
[5105] | 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 naturally |
---|
| 53 | no batches available: |
---|
| 54 | |
---|
| 55 | >>> browser.open('http://localhost/myuniversity') |
---|
| 56 | >>> browser.getLink('Manage 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 | ...No batches yet... |
---|
| 64 | ... |
---|
| 65 | |
---|
| 66 | Adding batches |
---|
| 67 | ============== |
---|
| 68 | |
---|
| 69 | We can add a batch of access-codes using a button displayed in the |
---|
| 70 | action bar: |
---|
| 71 | |
---|
| 72 | >>> browser.getLink('Add Scratch Card Batch').click() |
---|
| 73 | |
---|
| 74 | The add screen shows a form where we have to enter a prefix, the |
---|
| 75 | number of access codes to be generated and the costs for each card. |
---|
| 76 | |
---|
| 77 | >>> browser.getControl(name='form.batch_prefix').value = 'APP' |
---|
| 78 | >>> browser.getControl(name='form.entry_num').value = '5' |
---|
| 79 | >>> browser.getControl(name='form.cost').value = '12.12' |
---|
| 80 | |
---|
| 81 | If we click 'cancel' afterwards, the whole process will be cancelled |
---|
| 82 | and we'll be redirected to the management screen: |
---|
| 83 | |
---|
| 84 | >>> browser.getControl('Cancel').click() |
---|
| 85 | >>> browser.url |
---|
| 86 | 'http://localhost/myuniversity/accesscodes' |
---|
| 87 | |
---|
| 88 | >>> 'Batch creation cancelled' in browser.contents |
---|
| 89 | True |
---|
| 90 | |
---|
| 91 | Now let's try again and this time we finish the procedure by clicking |
---|
| 92 | 'Create batch' in the form: |
---|
| 93 | |
---|
| 94 | >>> browser.getLink('Add Scratch Card Batch').click() |
---|
| 95 | >>> browser.getControl(name='form.batch_prefix').value = 'APP' |
---|
| 96 | >>> browser.getControl(name='form.entry_num').value = '5' |
---|
| 97 | >>> browser.getControl(name='form.cost').value = '12.12' |
---|
| 98 | >>> browser.getControl('Create batch').click() |
---|
| 99 | |
---|
| 100 | We're also redirected to the management screen, with a notice about |
---|
| 101 | the freshly created batch: |
---|
| 102 | |
---|
| 103 | >>> print browser.contents |
---|
| 104 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
| 105 | ... |
---|
| 106 | <h2>Access Code Batches</h2> |
---|
| 107 | ... |
---|
| 108 | ... The following batches are available: |
---|
| 109 | ...APP |
---|
| 110 | ...- |
---|
| 111 | ...1 |
---|
| 112 | ... |
---|
| 113 | ...5 |
---|
| 114 | .../ |
---|
| 115 | ...0 |
---|
| 116 | ... |
---|
| 117 | ...12.12... |
---|
| 118 | ...zope.mgr... |
---|
| 119 | ... |
---|
| 120 | |
---|
| 121 | which means: there exists a batch named ``APP-1`` with 5 entries of |
---|
| 122 | which 0 have been devalidated, each one costs ``12.12`` and the batch |
---|
| 123 | was created by ``zope.mgr``. |
---|
[5108] | 124 | |
---|
| 125 | We create a second batch to see whether searching and related stuff |
---|
| 126 | works: |
---|
| 127 | |
---|
| 128 | >>> browser.getLink('Add Scratch Card Batch').click() |
---|
| 129 | >>> browser.getControl(name='form.batch_prefix').value = 'APP' |
---|
| 130 | >>> browser.getControl(name='form.entry_num').value = '5' |
---|
| 131 | >>> browser.getControl(name='form.cost').value = '10.12' |
---|
| 132 | >>> browser.getControl('Create batch').click() |
---|
[5115] | 133 | |
---|
[5125] | 134 | And a third one that can be deleted afterwards: |
---|
| 135 | |
---|
| 136 | >>> browser.getLink('Add Scratch Card Batch').click() |
---|
| 137 | >>> browser.getControl(name='form.batch_prefix').value = 'BLA' |
---|
| 138 | >>> browser.getControl(name='form.entry_num').value = '3' |
---|
| 139 | >>> browser.getControl(name='form.cost').value = '19.12' |
---|
| 140 | >>> browser.getControl('Create batch').click() |
---|
| 141 | |
---|
[5128] | 142 | We also invalidate one entry: |
---|
| 143 | |
---|
| 144 | >>> batch = getRootFolder()['myuniversity']['accesscodes']['BLA-1'] |
---|
| 145 | >>> ac = list(batch.entries())[0] |
---|
| 146 | >>> batch.invalidate(ac.representation) |
---|
| 147 | |
---|
[5125] | 148 | Creating Archive Files |
---|
| 149 | ====================== |
---|
| 150 | |
---|
| 151 | Once a batch is created, we can archive it. To do so we have to tick |
---|
| 152 | the respective checkbox and click on 'Archive': |
---|
| 153 | |
---|
| 154 | >>> ctrl = browser.getControl(name='batches') |
---|
| 155 | >>> ctrl.getControl(value='APP-2').selected = True |
---|
| 156 | >>> browser.getControl(name='archive').click() |
---|
| 157 | >>> print browser.contents |
---|
| 158 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
| 159 | ... |
---|
| 160 | <li ...>Archived APP-2 (APP-2_archive-...-zope.mgr.csv)</li> |
---|
| 161 | ... |
---|
| 162 | |
---|
| 163 | If we do not select a batch and try to archive or delete, the system |
---|
| 164 | will complain: |
---|
| 165 | |
---|
| 166 | >>> browser.getControl(name='archive').click() |
---|
| 167 | >>> print browser.contents |
---|
| 168 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
| 169 | ... |
---|
| 170 | <li ...>No batch selected.</li> |
---|
| 171 | ... |
---|
| 172 | |
---|
| 173 | Deleting Batches |
---|
| 174 | ================ |
---|
| 175 | |
---|
| 176 | We can delete batches. They are automatically archived when doing so: |
---|
| 177 | |
---|
| 178 | >>> ctrl = browser.getControl(name='batches') |
---|
| 179 | >>> ctrl.getControl(value='BLA-1').selected = True |
---|
| 180 | >>> browser.getControl('Archive and Delete').click() |
---|
| 181 | >>> print browser.contents |
---|
| 182 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
---|
| 183 | ...Archived BLA-1 (BLA-1_archive-...-zope.mgr.csv)... |
---|
| 184 | ...Deleted batch BLA-1... |
---|
| 185 | ... |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | Log- and Archive Files |
---|
| 189 | ====================== |
---|
| 190 | |
---|
| 191 | We store log- and archive-files inside the storage managed by the |
---|
| 192 | local datacenter. Access-code related files are placed inside an |
---|
| 193 | ``accesscode`` subdir: |
---|
| 194 | |
---|
| 195 | >>> ac_storage = os.path.join(uploadpath, 'accesscodes') |
---|
| 196 | |
---|
| 197 | Log files for access-code batches |
---|
| 198 | --------------------------------- |
---|
| 199 | |
---|
| 200 | Whenever a batch is created, there is also a log file with all entries |
---|
| 201 | created: |
---|
| 202 | |
---|
| 203 | >>> sorted(os.listdir(ac_storage)) |
---|
| 204 | ['APP-1-...-zope.mgr.csv', 'APP-2-...-zope.mgr.csv', ...] |
---|
| 205 | |
---|
| 206 | Each logfile name contains the prefix, batch number, date of creation |
---|
| 207 | and userid of creator. |
---|
| 208 | |
---|
| 209 | >>> logfile1 = os.path.join(ac_storage, |
---|
| 210 | ... sorted(os.listdir(ac_storage))[0]) |
---|
| 211 | >>> print open(logfile1, 'rb').read() |
---|
| 212 | "serial","ac","cost" |
---|
| 213 | "APP","1","12.12" |
---|
| 214 | "0","APP-1-<10-DIGITS>" |
---|
| 215 | "1","APP-1-<10-DIGITS>" |
---|
| 216 | "2","APP-1-<10-DIGITS>" |
---|
| 217 | "3","APP-1-<10-DIGITS>" |
---|
| 218 | "4","APP-1-<10-DIGITS>" |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | Archive files |
---|
| 222 | ------------- |
---|
| 223 | |
---|
| 224 | We created an archive file above. An archive file name consists of the |
---|
| 225 | batch prefix, batch number, the string ``_archive``, creation datetime of |
---|
| 226 | the archive file and userid of batch creator: |
---|
| 227 | |
---|
| 228 | >>> sorted(os.listdir(ac_storage)) |
---|
| 229 | [..., 'BLA-1_archive-...-zope.mgr.csv'] |
---|
| 230 | |
---|
| 231 | Archive files eventually also contain infos about invalidation dates |
---|
| 232 | and have a slightly different format therefore. |
---|
| 233 | |
---|
| 234 | >>> archive_file = os.path.join(ac_storage, os.listdir(ac_storage)[-1]) |
---|
| 235 | >>> print open(archive_file, 'rb').read() |
---|
| 236 | "prefix","serial","ac","student","date" |
---|
| 237 | "BLA","19.12","1","3" |
---|
[5128] | 238 | "BLA","0","BLA-1-<10-DIGITS>","","..." |
---|
[5125] | 239 | "BLA","1","BLA-1-<10-DIGITS>","","" |
---|
| 240 | "BLA","2","BLA-1-<10-DIGITS>","","" |
---|
| 241 | |
---|
[5115] | 242 | Clean up: |
---|
| 243 | |
---|
| 244 | >>> shutil.rmtree(uploadpath) |
---|