1 | ## $Id: test_applicantcopier.py 15036 2018-05-30 20:20:50Z 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 | Tests for the creation of student containers with data from admitted applicants. |
---|
20 | """ |
---|
21 | import os |
---|
22 | import grok |
---|
23 | from datetime import datetime |
---|
24 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
25 | from zope.event import notify |
---|
26 | from zope.component import getUtility |
---|
27 | from zope.i18n import translate |
---|
28 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
29 | from waeup.kofa.testing import FunctionalLayer |
---|
30 | from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser |
---|
31 | from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup |
---|
32 | from waeup.kofa.browser.tests.test_pdf import samples_dir |
---|
33 | |
---|
34 | session = datetime.now().year - 2 |
---|
35 | |
---|
36 | class ApplicantCopierFunctionalTests(ApplicantsFullSetup): |
---|
37 | |
---|
38 | layer = FunctionalLayer |
---|
39 | |
---|
40 | def setUp(self): |
---|
41 | super(ApplicantCopierFunctionalTests, self).setUp() |
---|
42 | return |
---|
43 | |
---|
44 | def prepare_applicant(self): |
---|
45 | self.browser.open(self.manage_path) |
---|
46 | self.fill_correct_values() |
---|
47 | self.browser.getControl("Save").click() |
---|
48 | # Upload a passport picture |
---|
49 | ctrl = self.browser.getControl(name='form.passport') |
---|
50 | file_obj = open( |
---|
51 | os.path.join(os.path.dirname(__file__), 'test_image.jpg'),'rb') |
---|
52 | file_ctrl = ctrl.mech_control |
---|
53 | file_ctrl.add_file(file_obj, filename='my_photo.jpg') |
---|
54 | self.browser.getControl("Save").click() # submit form |
---|
55 | return |
---|
56 | |
---|
57 | def test_copier(self): |
---|
58 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
59 | self.prepare_applicant() |
---|
60 | storage = getUtility(IExtFileStore) |
---|
61 | file_id = IFileStoreNameChooser(self.applicant).chooseName() |
---|
62 | # The stored image can be fetched |
---|
63 | fd = storage.getFile(file_id) |
---|
64 | file_len_orig = len(fd.read()) |
---|
65 | # Let's try to create the student |
---|
66 | (success, msg) = self.applicant.createStudent() |
---|
67 | self.assertTrue(msg == 'Applicant has not yet been admitted.') |
---|
68 | IWorkflowState(self.applicant).setState('admitted') |
---|
69 | (success, msg) = self.applicant.createStudent() |
---|
70 | self.assertTrue(msg == 'No course admitted provided.') |
---|
71 | self.browser.open(self.manage_path) |
---|
72 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
73 | self.browser.getControl("Save").click() |
---|
74 | # Maybe the certificate has meanwhile been removed |
---|
75 | del self.app['faculties']['fac1']['dep1'].certificates['CERT1'] |
---|
76 | (success, msg) = self.applicant.createStudent() |
---|
77 | self.assertFalse(success) |
---|
78 | self.assertTrue('ConstraintNotSatisfied: CERT1' in msg) |
---|
79 | # Ok, lets add the certificate and try again |
---|
80 | self.app['faculties']['fac1']['dep1'].certificates.addCertificate( |
---|
81 | self.certificate) |
---|
82 | # Managers are not allowed to trigger the create transition manually |
---|
83 | self.assertFalse('<option value="create">' in self.browser.contents) |
---|
84 | # Student can be created |
---|
85 | (success, msg) = self.applicant.createStudent() |
---|
86 | self.assertTrue('created' in msg) |
---|
87 | # The applicant is locked |
---|
88 | self.assertTrue(self.applicant.locked) |
---|
89 | student_id = translate(msg, 'waeup.kofa').split()[1] |
---|
90 | # View student container just created |
---|
91 | student = self.app['students'][student_id] |
---|
92 | student_path = 'http://localhost/app/students/%s' % student_id |
---|
93 | self.browser.open(student_path) |
---|
94 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
95 | self.assertEqual(self.browser.url, student_path) |
---|
96 | # Student is in state admitted |
---|
97 | self.assertEqual(student.state, 'admitted') |
---|
98 | # Attributes properly set? |
---|
99 | self.assertEqual(student.email, 'xx@yy.zz') |
---|
100 | self.assertEqual(student.firstname, 'John') |
---|
101 | self.assertEqual(student.middlename, 'Anthony') |
---|
102 | self.assertEqual(student.lastname, 'Tester') |
---|
103 | # student_id set in application record? |
---|
104 | self.assertEqual(self.applicant.student_id, student.student_id) |
---|
105 | # Check if passport image has been copied |
---|
106 | storage = getUtility(IExtFileStore) |
---|
107 | file_id = IFileStoreNameChooser( |
---|
108 | student).chooseName(attr='passport.jpg') |
---|
109 | fd = storage.getFile(file_id) |
---|
110 | file_len = len(fd.read()) |
---|
111 | self.assertEqual(file_len_orig, file_len) |
---|
112 | # Check if application slip exists and is a PDF file |
---|
113 | file_id = IFileStoreNameChooser( |
---|
114 | student).chooseName(attr='application_slip.pdf') |
---|
115 | pdf = storage.getFile(file_id).read() |
---|
116 | self.assertTrue(len(pdf) > 0) |
---|
117 | self.assertEqual(pdf[:8], '%PDF-1.4') |
---|
118 | # Copy te file to samples_dir |
---|
119 | path = os.path.join(samples_dir(), 'application_slip.pdf') |
---|
120 | open(path, 'wb').write(pdf) |
---|
121 | print "Sample PDF application_slip.pdf written to %s" % path |
---|
122 | # Check if there is an application slip link in UI |
---|
123 | self.assertTrue('Application Slip' in self.browser.contents) |
---|
124 | self.browser.getLink("Application Slip").click() |
---|
125 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
126 | self.assertEqual(self.browser.headers['Content-Type'], |
---|
127 | 'application/pdf') |
---|
128 | # Has the student been properly indexed? |
---|
129 | # Yes, we can find the student in department |
---|
130 | self.browser.open('http://localhost/app/students') |
---|
131 | self.browser.getControl(name="searchtype").value = ['depcode'] |
---|
132 | self.browser.getControl(name="searchterm").value = 'dep1' |
---|
133 | self.browser.getControl("Find student(s)").click() |
---|
134 | self.assertMatches('...John Anthony Tester...', self.browser.contents) |
---|
135 | # Has the student studycourse the correct attributes? |
---|
136 | self.assertEqual(student['studycourse'].certificate.code, 'CERT1') |
---|
137 | self.assertEqual(student['studycourse'].entry_session, session) |
---|
138 | self.assertEqual(student['studycourse'].entry_mode, 'ug_ft') |
---|
139 | self.assertEqual(student['studycourse'].current_session, session) |
---|
140 | self.assertEqual(student['studycourse'].current_level, 100) |
---|
141 | |
---|
142 | def test_batch_copying(self): |
---|
143 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
144 | self.prepare_applicant() |
---|
145 | IWorkflowState(self.applicant).setState('admitted') |
---|
146 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
147 | self.browser.getControl("Save").click() |
---|
148 | self.browser.open(self.manage_container_path) |
---|
149 | self.browser.getControl("Create students from selected", index=0).click() |
---|
150 | self.assertTrue('No applicant selected' in self.browser.contents) |
---|
151 | ctrl = self.browser.getControl(name='val_id') |
---|
152 | ctrl.getControl(value=self.applicant.application_number).selected = True |
---|
153 | self.browser.getControl("Create students from selected", index=0).click() |
---|
154 | self.assertTrue('1 students successfully created' in self.browser.contents) |
---|
155 | |
---|
156 | def test_hidden_batch_copying_container(self): |
---|
157 | logfile = os.path.join( |
---|
158 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
159 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
160 | self.prepare_applicant() |
---|
161 | self.browser.open(self.container_path + '/createallstudents') |
---|
162 | self.assertTrue('No record found' in self.browser.contents) |
---|
163 | self.assertFalse('Failed records' in self.browser.contents) |
---|
164 | self.assertFalse('Successfully created records' in self.browser.contents) |
---|
165 | IWorkflowState(self.applicant).setState('admitted') |
---|
166 | notify(grok.ObjectModifiedEvent(self.applicant)) |
---|
167 | self.browser.open(self.container_path + '/createallstudents') |
---|
168 | self.assertTrue('No course admitted provided' in self.browser.contents) |
---|
169 | self.assertFalse('Successfully created records' in self.browser.contents) |
---|
170 | self.assertTrue('Failed records' in self.browser.contents) |
---|
171 | self.browser.open(self.manage_path) |
---|
172 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
173 | self.browser.getControl("Save").click() |
---|
174 | # date_of_birth is not required for applicants but for students |
---|
175 | self.applicant.date_of_birth = None |
---|
176 | self.browser.open(self.container_path + '/createallstudents') |
---|
177 | self.assertTrue('RequiredMissing: date_of_birth' in self.browser.contents) |
---|
178 | self.assertTrue('Failed records' in self.browser.contents) |
---|
179 | self.assertFalse('Successfully created records' in self.browser.contents) |
---|
180 | self.browser.open(self.manage_path) |
---|
181 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
182 | self.browser.getControl("Save").click() |
---|
183 | self.browser.open(self.container_path + '/createallstudents') |
---|
184 | self.assertFalse('Failed records' in self.browser.contents) |
---|
185 | self.assertTrue('Successfully created records' in self.browser.contents) |
---|
186 | |
---|
187 | def test_hidden_batch_copying_all(self): |
---|
188 | logfile = os.path.join( |
---|
189 | self.app['datacenter'].storage, 'logs', 'applicants.log') |
---|
190 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
191 | self.prepare_applicant() |
---|
192 | self.browser.open(self.manage_path) |
---|
193 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
194 | #self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
195 | self.browser.getControl("Save").click() |
---|
196 | IWorkflowState(self.applicant).setState('admitted') |
---|
197 | notify(grok.ObjectModifiedEvent(self.applicant)) |
---|
198 | self.browser.open(self.root_path + '/createallstudents') |
---|
199 | self.assertTrue('No course admitted provided' in self.browser.contents) |
---|
200 | self.applicant.course_admitted = self.certificate |
---|
201 | self.browser.open(self.root_path + '/createallstudents') |
---|
202 | self.assertTrue('Successfully created records:' in self.browser.contents) |
---|
203 | |
---|
204 | def test_copier_wo_passport(self): |
---|
205 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
206 | self.browser.open(self.manage_path) |
---|
207 | self.fill_correct_values() |
---|
208 | # Let's try to create the student |
---|
209 | IWorkflowState(self.applicant).setState('admitted') |
---|
210 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
211 | self.browser.getControl("Save").click() |
---|
212 | (success, msg) = self.applicant.createStudent() |
---|
213 | self.assertTrue('created' in msg) |
---|
214 | |
---|
215 | def test_copier_with_defective_passport_image(self): |
---|
216 | IWorkflowState(self.applicant).setState('admitted') |
---|
217 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
218 | self.browser.open(self.manage_path) |
---|
219 | self.fill_correct_values() |
---|
220 | self.browser.getControl("Save").click() |
---|
221 | # Upload a passport picture |
---|
222 | ctrl = self.browser.getControl(name='form.passport') |
---|
223 | # This file has really been uploaded by KwaraPoly student |
---|
224 | # Until 4/1/15 these files resulted a traceback when opening |
---|
225 | # the createallstudents page. The traceback is now catched. |
---|
226 | file_obj = open( |
---|
227 | os.path.join(os.path.dirname(__file__), 'fake_image.jpg'),'rb') |
---|
228 | file_ctrl = ctrl.mech_control |
---|
229 | file_ctrl.add_file(file_obj, filename='my_photo.jpg') |
---|
230 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
231 | self.browser.getControl("Save").click() # submit form |
---|
232 | (success, msg) = self.applicant.createStudent() |
---|
233 | self.assertTrue(msg == 'IOError: Application Slip could not be created.') |
---|
234 | # The same in the UI |
---|
235 | self.browser.open(self.manage_container_path) |
---|
236 | ctrl = self.browser.getControl(name='val_id') |
---|
237 | ctrl.getControl(value=self.applicant.application_number).selected = True |
---|
238 | self.browser.getControl("Create students from selected", index=0).click() |
---|
239 | self.assertTrue('No student could be created.' in self.browser.contents) |
---|
240 | self.browser.open(self.container_path + '/createallstudents') |
---|
241 | self.assertTrue( |
---|
242 | 'IOError: Application Slip could not be created.' |
---|
243 | in self.browser.contents) |
---|
244 | |
---|
245 | def disabled_test_delay(self): |
---|
246 | # Create portalmanager manager |
---|
247 | self.app['users'].addUser('mrportalmanager', 'mrportalmanagersecret') |
---|
248 | self.app['users']['mrportalmanager'].email = 'mrportalmanager@foo.ng' |
---|
249 | self.app['users']['mrportalmanager'].title = 'Carlos Portales' |
---|
250 | prmglobal = IPrincipalRoleManager(self.app) |
---|
251 | prmglobal.assignRoleToPrincipal( |
---|
252 | 'waeup.PortalManager', 'mrportalmanager') |
---|
253 | # Login as portal manager |
---|
254 | self.browser.open(self.login_path) |
---|
255 | self.browser.getControl(name="form.login").value = 'mrportalmanager' |
---|
256 | self.browser.getControl(name="form.password").value = 'mrportalmanagersecret' |
---|
257 | self.browser.getControl("Login").click() |
---|
258 | self.prepare_applicant() |
---|
259 | self.browser.open(self.manage_path) |
---|
260 | self.browser.getControl(name="form.date_of_birth").value = '09/09/1988' |
---|
261 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
262 | self.browser.getControl("Save").click() |
---|
263 | IWorkflowState(self.applicant).setState('admitted') |
---|
264 | notify(grok.ObjectModifiedEvent(self.applicant)) |
---|
265 | start = datetime.now() |
---|
266 | self.browser.open(self.root_path + '/createallstudents') |
---|
267 | self.assertTrue('1 students successfully created' in self.browser.contents) |
---|
268 | self.assertTrue((datetime.now() - start).seconds >= 10) |
---|