source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/tests/test_applicantcopier.py @ 14939

Last change on this file since 14939 was 14934, checked in by Henrik Bettermann, 7 years ago

Do no longer hide the 'Create students' buttons but switch the portal to
maintenance mode to ensure that nobody can enter the portal during
record creation except for user 'admin'. This guarantees that record
creation is only started once.

  • Property svn:keywords set to Id
File size: 13.7 KB
Line 
1## $Id: test_applicantcopier.py 14934 2018-01-10 14:44:49Z 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 datetime import datetime
24from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
25from zope.event import notify
26from zope.component import getUtility
27from zope.i18n import translate
28from zope.securitypolicy.interfaces import IPrincipalRoleManager
29from waeup.kofa.testing import FunctionalLayer
30from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
31from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
32from waeup.kofa.browser.tests.test_pdf import samples_dir
33
34session = datetime.now().year - 2
35
36class 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(), 'aplication_slip.pdf')
120        open(path, 'wb').write(pdf)
121        print "Sample PDF aplication_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 student could be created' in self.browser.contents)
163        IWorkflowState(self.applicant).setState('admitted')
164        notify(grok.ObjectModifiedEvent(self.applicant))
165        self.browser.open(self.container_path + '/createallstudents')
166        self.assertTrue('No student could be created' in self.browser.contents)
167        logcontent = open(logfile).read()
168        self.assertTrue('No course admitted provided' in logcontent)
169        self.browser.open(self.manage_path)
170        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
171        self.browser.getControl("Save").click()
172        # date_of_birth is not required for applicants but for students
173        self.applicant.date_of_birth = None
174        self.browser.open(self.container_path + '/createallstudents')
175        self.assertTrue('No student could be created' in self.browser.contents)
176        logcontent = open(logfile).read()
177        self.assertTrue('RequiredMissing: date_of_birth' in logcontent)
178        self.browser.open(self.manage_path)
179        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
180        self.browser.getControl("Save").click()
181        self.browser.open(self.container_path + '/createallstudents')
182        self.assertTrue('1 students successfully created' in self.browser.contents)
183
184    def test_hidden_batch_copying_all(self):
185        logfile = os.path.join(
186            self.app['datacenter'].storage, 'logs', 'applicants.log')
187        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
188        self.prepare_applicant()
189        self.browser.open(self.manage_path)
190        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
191        #self.browser.getControl(name="form.course_admitted").value = ['CERT1']
192        self.browser.getControl("Save").click()
193        IWorkflowState(self.applicant).setState('admitted')
194        notify(grok.ObjectModifiedEvent(self.applicant))
195        self.browser.open(self.root_path + '/createallstudents')
196        self.assertTrue('No student could be created' in self.browser.contents)
197        logcontent = open(logfile).read()
198        self.assertTrue('No course admitted provided' in logcontent)
199        self.applicant.course_admitted = self.certificate
200        self.browser.open(self.root_path + '/createallstudents')
201        self.assertTrue('1 students successfully created' in self.browser.contents)
202
203    def test_copier_wo_passport(self):
204        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
205        self.browser.open(self.manage_path)
206        self.fill_correct_values()
207        # Let's try to create the student
208        IWorkflowState(self.applicant).setState('admitted')
209        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
210        self.browser.getControl("Save").click()
211        (success, msg) = self.applicant.createStudent()
212        self.assertTrue('created' in msg)
213
214    def test_copier_with_defective_passport_image(self):
215        IWorkflowState(self.applicant).setState('admitted')
216        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
217        self.browser.open(self.manage_path)
218        self.fill_correct_values()
219        self.browser.getControl("Save").click()
220        # Upload a passport picture
221        ctrl = self.browser.getControl(name='form.passport')
222        # This file has really been uploaded by KwaraPoly student
223        # Until 4/1/15 these files resulted a traceback when opening
224        # the createallstudents page. The traceback is now catched.
225        file_obj = open(
226            os.path.join(os.path.dirname(__file__), 'fake_image.jpg'),'rb')
227        file_ctrl = ctrl.mech_control
228        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
229        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
230        self.browser.getControl("Save").click() # submit form
231        (success, msg) = self.applicant.createStudent()
232        self.assertTrue(msg == 'IOError: Application Slip could not be created.')
233        # The same in the UI
234        self.browser.open(self.manage_container_path)
235        ctrl = self.browser.getControl(name='val_id')
236        ctrl.getControl(value=self.applicant.application_number).selected = True
237        self.browser.getControl("Create students from selected", index=0).click()
238        self.assertTrue('No student could be created.' in self.browser.contents)
239        self.browser.open(self.container_path + '/createallstudents')
240        self.assertTrue('No student could be created' in self.browser.contents)
241        logfile = os.path.join(self.app['datacenter'].storage, 'logs', 'applicants.log')
242        logcontent = open(logfile).read()
243        self.assertTrue('IOError: Application Slip could not be created.' in logcontent)
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)
Note: See TracBrowser for help on using the repository browser.