[10340] | 1 | ## $Id: test_browser.py 10366 2013-06-23 20:00:56Z henrik $ |
---|
| 2 | ## |
---|
| 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """ |
---|
| 19 | Test the applicant-related UI components. |
---|
| 20 | """ |
---|
| 21 | |
---|
| 22 | import os |
---|
| 23 | import pytz |
---|
| 24 | import grok |
---|
| 25 | from datetime import datetime, date, timedelta |
---|
| 26 | from zope.event import notify |
---|
| 27 | from StringIO import StringIO |
---|
| 28 | from zope.component import createObject, getUtility |
---|
| 29 | from waeup.kofa.applicants.container import ApplicantsContainer |
---|
| 30 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
| 31 | from waeup.kofa.interfaces import ( |
---|
| 32 | IExtFileStore, IFileStoreNameChooser) |
---|
| 33 | |
---|
| 34 | from waeup.imostate.testing import FunctionalLayer |
---|
| 35 | |
---|
| 36 | PH_LEN = 15911 # Length of placeholder file |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | class ApplicantUITests(ApplicantsFullSetup): |
---|
| 40 | # Tests for uploading/browsing the passport image of appplicants |
---|
| 41 | |
---|
| 42 | layer = FunctionalLayer |
---|
| 43 | |
---|
| 44 | def test_upload_image_by_student(self): |
---|
| 45 | self.login() |
---|
| 46 | self.browser.open(self.edit_path) |
---|
| 47 | # Create a pseudo image file and select it to be uploaded in form |
---|
| 48 | photo_content = 'A' * 1024 * 21 # A string of 21 KB size |
---|
| 49 | pseudo_image = StringIO(photo_content) |
---|
| 50 | ctrl = self.browser.getControl(name='form.passport') |
---|
| 51 | file_ctrl = ctrl.mech_control |
---|
| 52 | file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') |
---|
| 53 | self.browser.getControl("Save").click() # submit form |
---|
| 54 | # There is a correct <img> link included |
---|
| 55 | self.assertTrue( |
---|
| 56 | '<img src="passport.jpg" height="180px" />' in self.browser.contents) |
---|
| 57 | # We get a warning message |
---|
| 58 | self.assertTrue( |
---|
| 59 | 'Uploaded image is too big' in self.browser.contents) |
---|
| 60 | # Browsing the image gives us the default image, not the |
---|
| 61 | # uploaded one. |
---|
| 62 | image_url = self.edit_path.replace('edit', 'passport.jpg') |
---|
| 63 | self.browser.open(image_url) |
---|
| 64 | self.assertEqual( |
---|
| 65 | self.browser.headers['content-type'], 'image/jpeg') |
---|
| 66 | self.assertEqual(len(self.browser.contents), PH_LEN) |
---|
| 67 | # There is really no file stored for the applicant |
---|
| 68 | img = getUtility(IExtFileStore).getFile( |
---|
| 69 | IFileStoreNameChooser(self.applicant).chooseName()) |
---|
| 70 | self.assertTrue(img is None) |
---|
| 71 | |
---|
| 72 | def test_upload_extraform_by_student(self): |
---|
| 73 | self.login() |
---|
| 74 | self.browser.open(self.edit_path) |
---|
| 75 | # Create a pseudo file and select it to be uploaded in form |
---|
| 76 | pdf_content = 'A' * 1024 * 600 # A string of 600 KB size |
---|
[10366] | 77 | pseudo_file = StringIO(pdf_content) |
---|
[10340] | 78 | ctrl = self.browser.getControl(name='form.extraform') |
---|
| 79 | file_ctrl = ctrl.mech_control |
---|
[10366] | 80 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 81 | self.browser.getControl("Save").click() # submit form |
---|
| 82 | # We get a warning message |
---|
| 83 | self.assertTrue( |
---|
| 84 | 'Uploaded file is too big' in self.browser.contents) |
---|
| 85 | # Create a pseudo file with acceptable size |
---|
| 86 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
---|
[10366] | 87 | pseudo_file = StringIO(pdf_content) |
---|
[10340] | 88 | ctrl = self.browser.getControl(name='form.extraform') |
---|
| 89 | file_ctrl = ctrl.mech_control |
---|
[10366] | 90 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 91 | self.browser.getControl("Save").click() # submit form |
---|
| 92 | # Even though the form could not be saved ... |
---|
| 93 | self.assertTrue('Required input is missing' in self.browser.contents) |
---|
| 94 | # ... the file has been successfully uploaded |
---|
| 95 | pdf_url = self.edit_path.replace('edit', 'extraform.pdf') |
---|
| 96 | self.browser.open(pdf_url) |
---|
| 97 | self.assertEqual( |
---|
| 98 | self.browser.headers['content-type'], 'application/pdf') |
---|
| 99 | self.assertEqual(len(self.browser.contents), 307200) |
---|
| 100 | # There is rally a file stored for the applicant |
---|
| 101 | storage = getUtility(IExtFileStore) |
---|
| 102 | file_id = IFileStoreNameChooser(self.applicant).chooseName( |
---|
| 103 | attr='extraform.pdf') |
---|
| 104 | # The stored file can be fetched |
---|
| 105 | fd = storage.getFile(file_id) |
---|
| 106 | file_len = len(fd.read()) |
---|
| 107 | self.assertEqual(file_len, 307200) |
---|
| 108 | # A file link is displayed on the edit view ... |
---|
| 109 | self.browser.open(self.edit_path) |
---|
| 110 | self.assertTrue('<a href="extraform.pdf">' in self.browser.contents) |
---|
| 111 | # ... and on the dislay view |
---|
| 112 | self.browser.open(self.view_path) |
---|
| 113 | self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>' |
---|
| 114 | in self.browser.contents) |
---|
| 115 | |
---|
| 116 | def test_upload_extraform_by_manager(self): |
---|
| 117 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 118 | self.browser.open(self.manage_path) |
---|
| 119 | # Create a pseudo file with acceptable size |
---|
| 120 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
---|
[10366] | 121 | pseudo_file = StringIO(pdf_content) |
---|
[10340] | 122 | ctrl = self.browser.getControl(name='form.extraform') |
---|
| 123 | file_ctrl = ctrl.mech_control |
---|
[10366] | 124 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 125 | self.browser.getControl("Save").click() # submit form |
---|
| 126 | # Even though the form could not be saved ... |
---|
| 127 | self.assertTrue('Required input is missing' in self.browser.contents) |
---|
| 128 | # ... the file has been successfully uploaded |
---|
| 129 | pdf_url = self.manage_path.replace('manage', 'extraform.pdf') |
---|
| 130 | self.browser.open(pdf_url) |
---|
| 131 | self.assertEqual( |
---|
| 132 | self.browser.headers['content-type'], 'application/pdf') |
---|
| 133 | self.assertEqual(len(self.browser.contents), 307200) |
---|
| 134 | # There is rally a file stored for the applicant |
---|
| 135 | storage = getUtility(IExtFileStore) |
---|
| 136 | file_id = IFileStoreNameChooser(self.applicant).chooseName( |
---|
| 137 | attr='extraform.pdf') |
---|
| 138 | # The stored file can be fetched |
---|
| 139 | fd = storage.getFile(file_id) |
---|
| 140 | file_len = len(fd.read()) |
---|
| 141 | self.assertEqual(file_len, 307200) |
---|
| 142 | # A file link is displayed on the edit view ... |
---|
| 143 | self.browser.open(self.manage_path) |
---|
| 144 | self.assertTrue('<a href="extraform.pdf">' in self.browser.contents) |
---|
| 145 | # ... and on the dislay view |
---|
| 146 | self.browser.open(self.view_path) |
---|
| 147 | self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>' |
---|
| 148 | in self.browser.contents) |
---|
| 149 | # Adding file is properly logged |
---|
| 150 | logfile = os.path.join( |
---|
| 151 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
| 152 | logcontent = open(logfile).read() |
---|
| 153 | self.assertTrue( |
---|
| 154 | 'zope.mgr - waeup.imostate.applicants.browser.CustomApplicantManageFormPage' |
---|
| 155 | ' - %s - saved: extraform' |
---|
| 156 | % (self.applicant.applicant_id) |
---|
| 157 | in logcontent) |
---|
| 158 | # When an applicant is removed, also the pdf files are gone. |
---|
| 159 | del self.app['applicants']['app2011'][self.applicant.application_number] |
---|
| 160 | fd = storage.getFile(file_id) |
---|
| 161 | self.assertTrue(fd is None) |
---|
| 162 | |
---|
| 163 | def test_upload_refereeform_by_student(self): |
---|
| 164 | self.login() |
---|
| 165 | self.browser.open(self.edit_path) |
---|
| 166 | # Create a pseudo file and select it to be uploaded in form |
---|
| 167 | pdf_content = 'A' * 1024 * 600 # A string of 600 KB size |
---|
[10366] | 168 | pseudo_file = StringIO(pdf_content) |
---|
[10340] | 169 | ctrl = self.browser.getControl(name='form.refereeform') |
---|
| 170 | file_ctrl = ctrl.mech_control |
---|
[10366] | 171 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 172 | self.browser.getControl("Save").click() # submit form |
---|
| 173 | # We get a warning message |
---|
| 174 | self.assertTrue( |
---|
| 175 | 'Uploaded file is too big' in self.browser.contents) |
---|
| 176 | # Create a pseudo file with acceptable size |
---|
| 177 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
---|
[10366] | 178 | pseudo_file = StringIO(pdf_content) |
---|
[10340] | 179 | ctrl = self.browser.getControl(name='form.refereeform') |
---|
| 180 | file_ctrl = ctrl.mech_control |
---|
[10366] | 181 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 182 | self.browser.getControl("Save").click() # submit form |
---|
| 183 | # Even though the form could not be saved ... |
---|
| 184 | self.assertTrue('Required input is missing' in self.browser.contents) |
---|
| 185 | # ... the file has been successfully uploaded |
---|
| 186 | pdf_url = self.edit_path.replace('edit', 'refereeform.pdf') |
---|
| 187 | self.browser.open(pdf_url) |
---|
| 188 | self.assertEqual( |
---|
| 189 | self.browser.headers['content-type'], 'application/pdf') |
---|
| 190 | self.assertEqual(len(self.browser.contents), 307200) |
---|
| 191 | # There is really a file stored for the applicant |
---|
| 192 | storage = getUtility(IExtFileStore) |
---|
| 193 | file_id = IFileStoreNameChooser(self.applicant).chooseName( |
---|
| 194 | attr='refereeform.pdf') |
---|
| 195 | # The stored file can be fetched |
---|
| 196 | fd = storage.getFile(file_id) |
---|
| 197 | file_len = len(fd.read()) |
---|
| 198 | self.assertEqual(file_len, 307200) |
---|
| 199 | # A file link is displayed on the edit view ... |
---|
| 200 | self.browser.open(self.edit_path) |
---|
| 201 | self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents) |
---|
| 202 | # ... and on the dislay view |
---|
| 203 | self.browser.open(self.view_path) |
---|
| 204 | self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>' |
---|
| 205 | in self.browser.contents) |
---|
| 206 | |
---|
| 207 | def test_upload_refereeform_by_manager(self): |
---|
| 208 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 209 | self.browser.open(self.manage_path) |
---|
| 210 | # Create a pseudo file with acceptable size |
---|
| 211 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
---|
[10366] | 212 | pseudo_file = StringIO(pdf_content) |
---|
[10340] | 213 | ctrl = self.browser.getControl(name='form.refereeform') |
---|
| 214 | file_ctrl = ctrl.mech_control |
---|
[10366] | 215 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 216 | self.browser.getControl("Save").click() # submit form |
---|
| 217 | # Even though the form could not be saved ... |
---|
| 218 | self.assertTrue('Required input is missing' in self.browser.contents) |
---|
| 219 | # ... the file has been successfully uploaded |
---|
| 220 | pdf_url = self.manage_path.replace('manage', 'refereeform.pdf') |
---|
| 221 | self.browser.open(pdf_url) |
---|
| 222 | self.assertEqual( |
---|
| 223 | self.browser.headers['content-type'], 'application/pdf') |
---|
| 224 | self.assertEqual(len(self.browser.contents), 307200) |
---|
| 225 | # There is rally a file stored for the applicant |
---|
| 226 | storage = getUtility(IExtFileStore) |
---|
| 227 | file_id = IFileStoreNameChooser(self.applicant).chooseName( |
---|
| 228 | attr='refereeform.pdf') |
---|
| 229 | # The stored file can be fetched |
---|
| 230 | fd = storage.getFile(file_id) |
---|
| 231 | file_len = len(fd.read()) |
---|
| 232 | self.assertEqual(file_len, 307200) |
---|
| 233 | # A file link is displayed on the edit view ... |
---|
| 234 | self.browser.open(self.manage_path) |
---|
| 235 | self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents) |
---|
| 236 | # ... and on the dislay view |
---|
| 237 | self.browser.open(self.view_path) |
---|
| 238 | self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>' |
---|
| 239 | in self.browser.contents) |
---|
| 240 | # Adding file is properly logged |
---|
| 241 | logfile = os.path.join( |
---|
| 242 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
| 243 | logcontent = open(logfile).read() |
---|
| 244 | self.assertTrue( |
---|
| 245 | 'zope.mgr - waeup.imostate.applicants.browser.CustomApplicantManageFormPage' |
---|
| 246 | ' - %s - saved: refereeform' |
---|
| 247 | % (self.applicant.applicant_id) |
---|
| 248 | in logcontent) |
---|
| 249 | # When an applicant is removed, also the pdf files are gone. |
---|
| 250 | del self.app['applicants']['app2011'][self.applicant.application_number] |
---|
| 251 | fd = storage.getFile(file_id) |
---|
| 252 | self.assertTrue(fd is None) |
---|
| 253 | |
---|
[10366] | 254 | def test_applicant_workflow(self): |
---|
| 255 | # Applicants can edit their record |
---|
| 256 | self.browser.open(self.login_path) |
---|
| 257 | self.login() |
---|
| 258 | self.assertTrue( |
---|
| 259 | 'You logged in.' in self.browser.contents) |
---|
| 260 | self.browser.open(self.edit_path) |
---|
| 261 | self.assertTrue(self.browser.url != self.login_path) |
---|
| 262 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
| 263 | self.fill_correct_values() |
---|
| 264 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
| 265 | self.browser.getControl("Save").click() |
---|
| 266 | self.assertTrue('Form has been saved' in self.browser.contents) |
---|
| 267 | self.assertTrue('started ' in self.browser.contents) |
---|
| 268 | |
---|
[10340] | 269 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
---|
[10366] | 270 | pseudo_file = StringIO(pdf_content) |
---|
| 271 | ctrl = self.browser.getControl(name='form.passport') |
---|
| 272 | file_ctrl = ctrl.mech_control |
---|
| 273 | file_ctrl.add_file(pseudo_file, filename='myphoto.jpg') |
---|
| 274 | ctrl = self.browser.getControl(name='form.extraform') |
---|
| 275 | file_ctrl = ctrl.mech_control |
---|
| 276 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
| 277 | ctrl = self.browser.getControl(name='form.refereeform') |
---|
| 278 | file_ctrl = ctrl.mech_control |
---|
| 279 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
---|
[10340] | 280 | |
---|
[10366] | 281 | self.browser.getControl(name="confirm_passport").value = True |
---|
| 282 | self.browser.getControl("Save").click() |
---|
| 283 | return |
---|