1 | ## $Id: test_browser.py 16292 2020-10-30 12:36:49Z henrik $ |
---|
2 | ## |
---|
3 | ## Copyright (C) 2013 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 | import grok |
---|
22 | import datetime |
---|
23 | import pytz |
---|
24 | import os |
---|
25 | from StringIO import StringIO |
---|
26 | from zope.event import notify |
---|
27 | from zope.component import createObject, getUtility |
---|
28 | from zope.catalog.interfaces import ICatalog |
---|
29 | from zope.intid.interfaces import IIntIds |
---|
30 | from hurry.workflow.interfaces import IWorkflowState |
---|
31 | from zope.component import createObject, getUtility |
---|
32 | from waeup.kofa.configuration import SessionConfiguration |
---|
33 | from waeup.kofa.interfaces import ( |
---|
34 | IUserAccount, IExtFileStore, IFileStoreNameChooser) |
---|
35 | from kofacustom.iuokada.testing import FunctionalLayer |
---|
36 | from waeup.kofa.applicants.container import ApplicantsContainer |
---|
37 | from waeup.kofa.browser.tests.test_pdf import samples_dir |
---|
38 | from waeup.kofa.applicants.tests.test_browser import (ApplicantsFullSetup, |
---|
39 | ApplicantsFullSetup, container_name_1, session_1) |
---|
40 | from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup |
---|
41 | from kofacustom.iuokada.applicants.export import CustomApplicantExporter |
---|
42 | from kofacustom.iuokada.applicants.batching import CustomApplicantProcessor |
---|
43 | |
---|
44 | class CustomApplicantUITests(ApplicantsFullSetup): |
---|
45 | # Tests for uploading/browsing the passport image of appplicants |
---|
46 | |
---|
47 | layer = FunctionalLayer |
---|
48 | |
---|
49 | class ApplicantExporterTest(ApplicantImportExportSetup): |
---|
50 | |
---|
51 | layer = FunctionalLayer |
---|
52 | |
---|
53 | def setUp(self): |
---|
54 | super(ApplicantExporterTest, self).setUp() |
---|
55 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
56 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
57 | self.intids = getUtility(IIntIds) |
---|
58 | return |
---|
59 | |
---|
60 | def setup_applicant(self, applicant): |
---|
61 | # set predictable values for `applicant` |
---|
62 | applicant.reg_number = u'123456' |
---|
63 | applicant.applicant_id = u'dp2011_654321' |
---|
64 | applicant.firstname = u'Anna' |
---|
65 | applicant.lastname = u'Tester' |
---|
66 | applicant.middlename = u'M.' |
---|
67 | applicant.nationality = u'NG' |
---|
68 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
69 | applicant.sex = 'f' |
---|
70 | applicant.email = 'anna@sample.com' |
---|
71 | applicant.phone = u'+234-123-12345' |
---|
72 | applicant.course1 = self.certificate |
---|
73 | applicant.course2 = self.certificate |
---|
74 | applicant.course_admitted = self.certificate |
---|
75 | applicant.notice = u'Some notice\nin lines.' |
---|
76 | applicant.screening_score = 98 |
---|
77 | applicant.screening_venue = u'Exam Room' |
---|
78 | applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM' |
---|
79 | applicant.password = 'any password' |
---|
80 | return applicant |
---|
81 | |
---|
82 | class ApplicantsContainerUITests(ApplicantsFullSetup): |
---|
83 | # Tests for ApplicantsContainer class views and pages |
---|
84 | |
---|
85 | layer = FunctionalLayer |
---|
86 | |
---|
87 | def fill_correct_values(self): |
---|
88 | # Fill the edit form with suitable values |
---|
89 | self.browser.getControl(name="form.firstname").value = 'John' |
---|
90 | self.browser.getControl(name="form.middlename").value = 'Anthony' |
---|
91 | self.browser.getControl(name="form.lastname").value = 'Tester' |
---|
92 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
93 | self.browser.getControl(name="form.sex").value = ['m'] |
---|
94 | self.browser.getControl(name="form.email").value = 'xx@yy.zz' |
---|
95 | self.browser.getControl(name="form.perm_address").value = 'Adresse' |
---|
96 | |
---|
97 | def test_manage_application(self): |
---|
98 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
99 | self.slip_path = self.view_path + '/application_slip.pdf' |
---|
100 | self.browser.open(self.manage_path) |
---|
101 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
102 | self.fill_correct_values() |
---|
103 | self.browser.getControl(name="custom.course1").value = [''] |
---|
104 | self.browser.getControl("Save").click() |
---|
105 | self.assertTrue('Please select your 1st Choice Course of Study.' |
---|
106 | in self.browser.contents) |
---|
107 | self.browser.getControl(name="custom.course1").value = ['CERT1'] |
---|
108 | self.browser.getControl("Save").click() |
---|
109 | self.assertEqual(self.applicant.course1, self.certificate) |
---|
110 | self.assertEqual(self.applicant.course2, None) |
---|
111 | IWorkflowState(self.applicant).setState('submitted') |
---|
112 | self.browser.open(self.view_path) |
---|
113 | self.browser.getLink("Download application slip").click() |
---|
114 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
115 | self.assertEqual(self.browser.headers['Content-Type'], |
---|
116 | 'application/pdf') |
---|
117 | path = os.path.join(samples_dir(), 'application_slip.pdf') |
---|
118 | open(path, 'wb').write(self.browser.contents) |
---|
119 | print "Sample application_slip.pdf written to %s" % path |
---|
120 | # Log entries have been added (course1 is missing because it was already stored) |
---|
121 | logfile = os.path.join( |
---|
122 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
123 | logcontent = open(logfile).read() |
---|
124 | self.assertTrue( |
---|
125 | 'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage - ' |
---|
126 | '%s - saved: firstname + middlename + lastname + date_of_birth ' |
---|
127 | '+ sex + email' |
---|
128 | % (self.applicant.applicant_id) |
---|
129 | in logcontent) |
---|
130 | |
---|
131 | def test_upload_res_stat_by_manager(self): |
---|
132 | # Add trans applicants container |
---|
133 | self.transcontainer = ApplicantsContainer() |
---|
134 | self.transcontainer.mode = 'create' |
---|
135 | self.transcontainer.code = u'ug%s' % session_1 |
---|
136 | self.transcontainer.prefix = u'ug' |
---|
137 | self.transcontainer.application_category = u'no' |
---|
138 | self.transcontainer.year = session_1 |
---|
139 | self.transcontainer.application_fee = 300.0 |
---|
140 | self.transcontainer.title = u'This is the ug%s container' % session_1 |
---|
141 | self.app['applicants'][self.transcontainer.code] = self.transcontainer |
---|
142 | delta = datetime.timedelta(days=10) |
---|
143 | self.transcontainer.startdate = datetime.datetime.now(pytz.utc) - delta |
---|
144 | self.transcontainer.enddate = datetime.datetime.now(pytz.utc) + delta |
---|
145 | # Add applicant |
---|
146 | transapplicant = createObject(u'waeup.Applicant') |
---|
147 | transapplicant.firstname = u'Anna' |
---|
148 | transapplicant.lastname = u'Post' |
---|
149 | transapplicant.subtype = 'transfer' |
---|
150 | self.app['applicants'][self.transcontainer.code].addApplicant(transapplicant) |
---|
151 | self.transapplicant = self.app['applicants'][self.transcontainer.code][ |
---|
152 | transapplicant.application_number] |
---|
153 | self.transapplicant_view_path = ('http://localhost/app/applicants/ug%s/%s' |
---|
154 | % (session_1, transapplicant.application_number)) |
---|
155 | self.transapplicant_manage_path = ('http://localhost/app/applicants/ug%s/%s/manage' |
---|
156 | % (session_1, transapplicant.application_number)) |
---|
157 | # Login |
---|
158 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
159 | self.browser.open(self.transapplicant_manage_path) |
---|
160 | # Create a pseudo file with acceptable size |
---|
161 | pdf_content = 'A' * 1024 * 300 # A string of 300 KB size |
---|
162 | pseudo_pdf = StringIO(pdf_content) |
---|
163 | ctrl = self.browser.getControl(name='res_stat.pdf') |
---|
164 | file_ctrl = ctrl.mech_control |
---|
165 | file_ctrl.add_file(pseudo_pdf, filename='myform.pdf') |
---|
166 | self.browser.getControl("Save").click() # submit form |
---|
167 | # Even though the form could not be saved ... |
---|
168 | self.assertTrue('Required input is missing' in self.browser.contents) |
---|
169 | # ... the file has been successfully uploaded |
---|
170 | pdf_url = self.transapplicant_manage_path.replace('manage', 'res_stat.pdf') |
---|
171 | self.browser.open(pdf_url) |
---|
172 | self.assertEqual( |
---|
173 | self.browser.headers['content-type'], 'application/pdf') |
---|
174 | self.assertEqual(len(self.browser.contents), 307200) |
---|
175 | # There is really a file stored for the applicant |
---|
176 | storage = getUtility(IExtFileStore) |
---|
177 | file_id = IFileStoreNameChooser(self.transapplicant).chooseName( |
---|
178 | attr='res_stat.pdf') |
---|
179 | # The stored file can be fetched |
---|
180 | fd = storage.getFile(file_id) |
---|
181 | file_len = len(fd.read()) |
---|
182 | self.assertEqual(file_len, 307200) |
---|
183 | # A file link is displayed on the edit view ... |
---|
184 | self.browser.open(self.transapplicant_manage_path) |
---|
185 | self.assertTrue('<a href="res_stat.pdf">' in self.browser.contents) |
---|
186 | # ... and on the dislay view |
---|
187 | self.browser.open(self.transapplicant_view_path) |
---|
188 | self.assertTrue('res_stat.pdf">Statement of Result</a>' |
---|
189 | in self.browser.contents) |
---|
190 | # Adding file is properly logged |
---|
191 | logfile = os.path.join( |
---|
192 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
193 | logcontent = open(logfile).read() |
---|
194 | self.assertTrue( |
---|
195 | 'zope.mgr - kofacustom.iuokada.applicants.browser.CustomApplicantManageFormPage' |
---|
196 | ' - %s - saved: res_stat.pdf' |
---|
197 | % (self.transapplicant.applicant_id) |
---|
198 | in logcontent) |
---|
199 | # When an applicant is removed, also the pdf files are gone. |
---|
200 | del self.app['applicants'][self.transcontainer.code][self.transapplicant.application_number] |
---|
201 | fd = storage.getFile(file_id) |
---|
202 | self.assertTrue(fd is None) |
---|