source: main/waeup.kofa/branches/uli-py3/src/waeup/kofa/applicants/tests/test_applicantcopier.py @ 17650

Last change on this file since 17650 was 15941, checked in by Henrik Bettermann, 5 years ago

Copy also files from applicants to students section.

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