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

Last change on this file since 13394 was 12565, checked in by Henrik Bettermann, 10 years ago

Application slip must be created again to correspond to the final slip version.

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