| 1 | ## $Id: test_browser.py 11237 2014-02-22 14:11:26Z uli $ |
|---|
| 2 | ## |
|---|
| 3 | ## Copyright (C) 2014 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 ( |
|---|
| 31 | ApplicantsFullSetup, container_name_1, |
|---|
| 32 | ) |
|---|
| 33 | from waeup.kofa.interfaces import ( |
|---|
| 34 | IExtFileStore, IFileStoreNameChooser) |
|---|
| 35 | |
|---|
| 36 | from waeup.imostate.testing import FunctionalLayer |
|---|
| 37 | |
|---|
| 38 | PH_LEN = 15911 # Length of placeholder file |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | class ApplicantUITests(ApplicantsFullSetup): |
|---|
| 42 | # Tests for uploading/browsing the passport image of appplicants |
|---|
| 43 | |
|---|
| 44 | layer = FunctionalLayer |
|---|
| 45 | |
|---|
| 46 | def test_upload_image_by_student(self): |
|---|
| 47 | self.login() |
|---|
| 48 | self.browser.open(self.edit_path) |
|---|
| 49 | # Create a pseudo image file and select it to be uploaded in form |
|---|
| 50 | photo_content = 'A' * 1024 * 21 # A string of 21 KB size |
|---|
| 51 | pseudo_image = StringIO(photo_content) |
|---|
| 52 | ctrl = self.browser.getControl(name='form.passport') |
|---|
| 53 | file_ctrl = ctrl.mech_control |
|---|
| 54 | file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') |
|---|
| 55 | self.browser.getControl("Save").click() # submit form |
|---|
| 56 | # There is a correct <img> link included |
|---|
| 57 | self.assertTrue( |
|---|
| 58 | '<img src="passport.jpg" height="180px" />' in self.browser.contents) |
|---|
| 59 | # We get a warning message |
|---|
| 60 | self.assertTrue( |
|---|
| 61 | 'Uploaded image is too big' in self.browser.contents) |
|---|
| 62 | # Browsing the image gives us the default image, not the |
|---|
| 63 | # uploaded one. |
|---|
| 64 | image_url = self.edit_path.replace('edit', 'passport.jpg') |
|---|
| 65 | self.browser.open(image_url) |
|---|
| 66 | self.assertEqual( |
|---|
| 67 | self.browser.headers['content-type'], 'image/jpeg') |
|---|
| 68 | self.assertEqual(len(self.browser.contents), PH_LEN) |
|---|
| 69 | # There is really no file stored for the applicant |
|---|
| 70 | img = getUtility(IExtFileStore).getFile( |
|---|
| 71 | IFileStoreNameChooser(self.applicant).chooseName()) |
|---|
| 72 | self.assertTrue(img is None) |
|---|
| 73 | |
|---|
| 74 | def test_upload_extraform_by_student(self): |
|---|
| 75 | self.login() |
|---|
| 76 | self.browser.open(self.edit_path) |
|---|
| 77 | # Create a pseudo file and select it to be uploaded in form |
|---|
| 78 | pdf_content = 'A' * 1024 * 600 # A string of 600 KB size |
|---|
| 79 | pseudo_file = StringIO(pdf_content) |
|---|
| 80 | ctrl = self.browser.getControl(name='form.extraform') |
|---|
| 81 | file_ctrl = ctrl.mech_control |
|---|
| 82 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
|---|
| 83 | self.browser.getControl("Save").click() # submit form |
|---|
| 84 | # We get a warning message |
|---|
| 85 | self.assertTrue( |
|---|
| 86 | 'Uploaded file is too big' in self.browser.contents) |
|---|
| 87 | # Create a pseudo file with acceptable size |
|---|
| 88 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
|---|
| 89 | pseudo_file = StringIO(pdf_content) |
|---|
| 90 | ctrl = self.browser.getControl(name='form.extraform') |
|---|
| 91 | file_ctrl = ctrl.mech_control |
|---|
| 92 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
|---|
| 93 | self.browser.getControl("Save").click() # submit form |
|---|
| 94 | # Even though the form could not be saved ... |
|---|
| 95 | self.assertTrue('Required input is missing' in self.browser.contents) |
|---|
| 96 | # ... the file has been successfully uploaded |
|---|
| 97 | pdf_url = self.edit_path.replace('edit', 'extraform.pdf') |
|---|
| 98 | self.browser.open(pdf_url) |
|---|
| 99 | self.assertEqual( |
|---|
| 100 | self.browser.headers['content-type'], 'application/pdf') |
|---|
| 101 | self.assertEqual(len(self.browser.contents), 307200) |
|---|
| 102 | # There is rally a file stored for the applicant |
|---|
| 103 | storage = getUtility(IExtFileStore) |
|---|
| 104 | file_id = IFileStoreNameChooser(self.applicant).chooseName( |
|---|
| 105 | attr='extraform.pdf') |
|---|
| 106 | # The stored file can be fetched |
|---|
| 107 | fd = storage.getFile(file_id) |
|---|
| 108 | file_len = len(fd.read()) |
|---|
| 109 | self.assertEqual(file_len, 307200) |
|---|
| 110 | # A file link is displayed on the edit view ... |
|---|
| 111 | self.browser.open(self.edit_path) |
|---|
| 112 | self.assertTrue('<a href="extraform.pdf">' in self.browser.contents) |
|---|
| 113 | # ... and on the dislay view |
|---|
| 114 | self.browser.open(self.view_path) |
|---|
| 115 | self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>' |
|---|
| 116 | in self.browser.contents) |
|---|
| 117 | |
|---|
| 118 | def test_upload_extraform_by_manager(self): |
|---|
| 119 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
|---|
| 120 | self.browser.open(self.manage_path) |
|---|
| 121 | # Create a pseudo file with acceptable size |
|---|
| 122 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
|---|
| 123 | pseudo_file = StringIO(pdf_content) |
|---|
| 124 | ctrl = self.browser.getControl(name='form.extraform') |
|---|
| 125 | file_ctrl = ctrl.mech_control |
|---|
| 126 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
|---|
| 127 | self.browser.getControl("Save").click() # submit form |
|---|
| 128 | # Even though the form could not be saved ... |
|---|
| 129 | self.assertTrue('Required input is missing' in self.browser.contents) |
|---|
| 130 | # ... the file has been successfully uploaded |
|---|
| 131 | pdf_url = self.manage_path.replace('manage', 'extraform.pdf') |
|---|
| 132 | self.browser.open(pdf_url) |
|---|
| 133 | self.assertEqual( |
|---|
| 134 | self.browser.headers['content-type'], 'application/pdf') |
|---|
| 135 | self.assertEqual(len(self.browser.contents), 307200) |
|---|
| 136 | # There is rally a file stored for the applicant |
|---|
| 137 | storage = getUtility(IExtFileStore) |
|---|
| 138 | file_id = IFileStoreNameChooser(self.applicant).chooseName( |
|---|
| 139 | attr='extraform.pdf') |
|---|
| 140 | # The stored file can be fetched |
|---|
| 141 | fd = storage.getFile(file_id) |
|---|
| 142 | file_len = len(fd.read()) |
|---|
| 143 | self.assertEqual(file_len, 307200) |
|---|
| 144 | # A file link is displayed on the edit view ... |
|---|
| 145 | self.browser.open(self.manage_path) |
|---|
| 146 | self.assertTrue('<a href="extraform.pdf">' in self.browser.contents) |
|---|
| 147 | # ... and on the dislay view |
|---|
| 148 | self.browser.open(self.view_path) |
|---|
| 149 | self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>' |
|---|
| 150 | in self.browser.contents) |
|---|
| 151 | # Adding file is properly logged |
|---|
| 152 | logfile = os.path.join( |
|---|
| 153 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
|---|
| 154 | logcontent = open(logfile).read() |
|---|
| 155 | self.assertTrue( |
|---|
| 156 | 'zope.mgr - ' |
|---|
| 157 | 'waeup.imostate.applicants.browser.CustomApplicantManageFormPage' |
|---|
| 158 | ' - %s - saved: extraform' |
|---|
| 159 | % (self.applicant.applicant_id) |
|---|
| 160 | in logcontent) |
|---|
| 161 | # When an applicant is removed, also the pdf files are gone. |
|---|
| 162 | del self.app['applicants'][container_name_1][ |
|---|
| 163 | self.applicant.application_number] |
|---|
| 164 | fd = storage.getFile(file_id) |
|---|
| 165 | self.assertTrue(fd is None) |
|---|
| 166 | |
|---|
| 167 | def test_applicant_workflow(self): |
|---|
| 168 | # Applicants can edit their record |
|---|
| 169 | self.browser.open(self.login_path) |
|---|
| 170 | self.login() |
|---|
| 171 | self.assertTrue( |
|---|
| 172 | 'You logged in.' in self.browser.contents) |
|---|
| 173 | self.browser.open(self.edit_path) |
|---|
| 174 | self.assertTrue(self.browser.url != self.login_path) |
|---|
| 175 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
|---|
| 176 | self.fill_correct_values() |
|---|
| 177 | self.browser.getControl(name="form.nationality").value = ['NG'] |
|---|
| 178 | self.browser.getControl("Save").click() |
|---|
| 179 | self.assertTrue('Form has been saved' in self.browser.contents) |
|---|
| 180 | self.assertTrue('started ' in self.browser.contents) |
|---|
| 181 | |
|---|
| 182 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
|---|
| 183 | pseudo_file = StringIO(pdf_content) |
|---|
| 184 | ctrl = self.browser.getControl(name='form.passport') |
|---|
| 185 | file_ctrl = ctrl.mech_control |
|---|
| 186 | file_ctrl.add_file(pseudo_file, filename='myphoto.jpg') |
|---|
| 187 | ctrl = self.browser.getControl(name='form.extraform') |
|---|
| 188 | file_ctrl = ctrl.mech_control |
|---|
| 189 | file_ctrl.add_file(pseudo_file, filename='myform.pdf') |
|---|
| 190 | |
|---|
| 191 | self.browser.getControl(name="confirm_passport").value = True |
|---|
| 192 | self.browser.getControl("Save").click() |
|---|
| 193 | return |
|---|