source: main/waeup.uniben/trunk/src/waeup/uniben/applicants/tests/test_browser.py @ 16182

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

Enable file upload and change prefixes.

  • Property svn:keywords set to Id
File size: 18.2 KB
Line 
1# -*- coding: utf-8 -*-
2## $Id: test_browser.py 16164 2020-07-13 07:17:03Z henrik $
3##
4## Copyright (C) 2013 Uli Fouquet & Henrik Bettermann
5## This program is free software; you can redistribute it and/or modify
6## it under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 2 of the License, or
8## (at your option) any later version.
9##
10## This program is distributed in the hope that it will be useful,
11## but WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13## GNU General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with this program; if not, write to the Free Software
17## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18##
19"""
20Test the applicant-related UI components.
21"""
22import os
23import datetime
24import pytz
25from StringIO import StringIO
26from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
27from zope.securitypolicy.interfaces import IPrincipalRoleManager
28from zope.component import createObject, getUtility
29from waeup.kofa.interfaces import IUserAccount
30from waeup.kofa.browser.tests.test_pdf import samples_dir
31from waeup.kofa.applicants.container import ApplicantsContainer
32from waeup.uniben.testing import FunctionalLayer
33from waeup.uniben.configuration import CustomSessionConfiguration
34from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup, PH_LEN
35
36session_1 = datetime.datetime.now().year - 2
37SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
38
39class CustomApplicantUITests(ApplicantsFullSetup):
40    # Tests for uploading/browsing the passport image of appplicants
41
42    layer = FunctionalLayer
43
44    #def setUp(self):
45    #    super(CustomApplicantUITests, self).setUp()
46    #    return
47
48    def test_applicant_access(self):
49        # Anonymous users can't see the application fee.
50        self.browser.open(self.container_path)
51        self.assertFalse('Application Fee' in self.browser.contents)
52        # Applicants can edit their record
53        self.browser.open(self.login_path)
54        self.login()
55        self.assertTrue(
56            'You logged in.' in self.browser.contents)
57        self.browser.open(self.edit_path)
58        self.assertTrue(self.browser.url != self.login_path)
59        self.assertEqual(self.browser.headers['Status'], '200 Ok')
60        self.fill_correct_values()
61        self.browser.getControl("Save").click()
62        self.assertMatches('...Form has been saved...', self.browser.contents)
63        # Applicants can't see the application fee.
64        self.browser.open(self.container_path)
65        self.assertFalse('Application Fee' in self.browser.contents)
66        return
67       
68    def image_url(self, filename):
69        return self.edit_path.replace('edit', filename)
70
71    def test_upload_passport_wo_permission(self):
72        # Create CRPU officer
73        self.app['users'].addUser('mrcrpu', 'mrCrpusecret1')
74        self.app['users']['mrcrpu'].email = 'mrcrpu@foo.ng'
75        self.app['users']['mrcrpu'].title = 'Carlo Pitter'
76        prmglobal = IPrincipalRoleManager(self.app)
77        prmglobal.assignRoleToPrincipal('waeup.CCOfficer', 'mrcrpu')
78        # Login as CRPU officer
79        self.browser.open(self.login_path)
80        self.browser.getControl(name="form.login").value = 'mrcrpu'
81        self.browser.getControl(name="form.password").value = 'mrCrpusecret1'
82        self.browser.getControl("Login").click()
83        self.assertMatches('...You logged in...', self.browser.contents)
84        # Let's try to change the passport image
85        self.browser.open(self.manage_path)
86        self.fill_correct_values()
87        # Create a pseudo image file and select it to be uploaded in form
88        pseudo_image = StringIO('I pretend to be a graphics file')
89        ctrl = self.browser.getControl(name='form.passport')
90        file_ctrl = ctrl.mech_control
91        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
92        self.browser.getControl("Save").click()
93        self.assertMatches('...You are not entitled to upload passport pictures...',
94            self.browser.contents)
95        # The officer still sees the placeholder passport image
96        self.browser.open(self.image_url('passport.jpg'))
97        self.assertEqual(
98            self.browser.headers['content-type'], 'image/jpeg')
99        self.assertEqual(len(self.browser.contents), PH_LEN)
100        # After adding the additional role ...
101        prmglobal.assignRoleToPrincipal('waeup.PassportPictureManager', 'mrcrpu')
102        # ... passport pictures can be uploaded
103        self.browser.open(self.manage_path)
104        self.fill_correct_values()
105        pseudo_image = StringIO('I pretend to be a graphics file')
106        ctrl = self.browser.getControl(name='form.passport')
107        file_ctrl = ctrl.mech_control
108        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
109        self.browser.getControl("Save").click()
110        self.assertMatches('...Form has been saved...', self.browser.contents)
111        # There is a correct <img> link included
112        self.assertTrue(
113            '<img src="passport.jpg" height="180px" />' in self.browser.contents)
114        # Browsing the link shows a real image
115        self.browser.open(self.image_url('passport.jpg'))
116        self.assertEqual(
117            self.browser.headers['content-type'], 'image/jpeg')
118        self.assertEqual(len(self.browser.contents), 31)
119
120    def test_pay_admission_checking_fee(self):
121        IWorkflowState(self.applicant).setState('admitted')
122        self.applicant.screening_score = 55
123        self.applicant.course_admitted = self.certificate
124        self.login()
125        # SessionConfiguration is not set, thus admission checking payment
126        # is not necessary. Screening results and course admitted are visible.
127        self.assertFalse(
128            'Add admission checking payment ticket' in self.browser.contents)
129        self.assertTrue('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
130        self.assertTrue('<span>55</span>' in self.browser.contents)
131        configuration = CustomSessionConfiguration()
132        configuration.academic_session = datetime.datetime.now().year - 2
133        self.app['configuration'].addSessionConfiguration(configuration)
134        # Admission checking fee is 0, thus admission checking payment
135        # is not necessary. Screening results and course admitted are visible.
136        self.browser.open(self.view_path)
137        self.assertFalse(
138            'Add admission checking payment ticket' in self.browser.contents)
139        self.assertTrue('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
140        self.assertTrue('<span>55</span>' in self.browser.contents)
141        configuration.admchecking_fee = 22.0
142        # Admission checking payment button is now visible, but screening results
143        # and course admitted are not.
144        self.browser.open(self.view_path)
145        self.assertTrue(
146            'Add admission checking payment ticket' in self.browser.contents)
147        self.assertFalse('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
148        self.assertFalse('<span>55</span>' in self.browser.contents)
149        # Application slip can't be downloaded
150        self.assertFalse('Download application slip' in self.browser.contents)
151        slip_path = self.view_path + '/application_slip.pdf'
152        self.browser.open(slip_path)
153        self.assertTrue(
154            'Please pay admission checking fee before trying to download'
155            in self.browser.contents)
156        # Pay admission checking fee.
157        self.browser.getControl("Add admission checking").click()
158        p_id = self.applicant.keys()[0]
159        self.applicant[p_id].p_state = 'paid'
160        # Screening results and course admitted are visible after payment.
161        self.browser.open(self.view_path)
162        self.assertFalse(
163            'Add admission checking payment ticket' in self.browser.contents)
164        self.assertTrue('<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">CERT1 - Unnamed Certificate</a>' in self.browser.contents)
165        self.assertTrue('<span>55</span>' in self.browser.contents)
166        # Application slip can be downloaded again.
167        self.browser.getLink("Download application slip").click()
168        self.assertEqual(self.browser.headers['Status'], '200 Ok')
169        self.assertEqual(self.browser.headers['Content-Type'],
170                         'application/pdf')
171        return
172
173    def test_application_slips(self):
174
175        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
176        self.slip_path = self.view_path + '/application_slip.pdf'
177        self.browser.open(self.manage_path)
178        self.assertEqual(self.browser.headers['Status'], '200 Ok')
179        self.fill_correct_values()
180        self.browser.getControl("Save").click()
181        IWorkflowState(self.applicant).setState('submitted')
182        self.browser.open(self.manage_path)
183        self.browser.getLink("Download application slip").click()
184        self.assertEqual(self.browser.headers['Status'], '200 Ok')
185        self.assertEqual(self.browser.headers['Content-Type'],
186                         'application/pdf')
187        path = os.path.join(samples_dir(), 'application_slip.pdf')
188        open(path, 'wb').write(self.browser.contents)
189        print "Sample application_slip.pdf written to %s" % path
190        # Screening invitation letter is not yet available
191        self.browser.open(self.view_path)
192        self.assertFalse('invitation slip' in self.browser.contents)
193        self.browser.open(self.view_path + '/screening_invitation_slip.pdf')
194        self.assertTrue('Forbidden' in self.browser.contents)
195        self.applicant.screening_date = u'any date'
196        self.applicant.screening_venue = u'MAIN AUDITORIUM'
197        self.applicantscontainer.application_slip_notice = u'This is an additional notice.'
198        # The invitation letter can be printed
199        self.browser.open(self.view_path)
200        self.browser.getLink("invitation").click()
201        self.assertEqual(self.browser.headers['Status'], '200 Ok')
202        self.assertEqual(self.browser.headers['Content-Type'],
203                         'application/pdf')
204        path = os.path.join(samples_dir(), 'screening_invitation_slip.pdf')
205        open(path, 'wb').write(self.browser.contents)
206        print "Sample screening_invitation_slip.pdf written to %s" % path
207
208    def test_akoka_application_slip(self):
209
210        # Remove required FieldProperty attribute first ...
211        delattr(ApplicantsContainer, 'prefix')
212        # ... and replace by akoka
213        self.applicantscontainer.prefix = 'akj'
214        self.applicantscontainer.title = u'FCET Akoka JUPEB Pre-Degree (Foundation) Studies 2016/2017'
215        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
216        self.slip_path = self.view_path + '/application_slip.pdf'
217        self.browser.open(self.manage_path)
218        self.assertEqual(self.browser.headers['Status'], '200 Ok')
219        self.fill_correct_values()
220        self.browser.getControl("Save").click()
221        IWorkflowState(self.applicant).setState('submitted')
222        self.browser.open(self.manage_path)
223        self.browser.getLink("Download application slip").click()
224        self.assertEqual(self.browser.headers['Status'], '200 Ok')
225        self.assertEqual(self.browser.headers['Content-Type'],
226                         'application/pdf')
227        path = os.path.join(samples_dir(), 'akoka_application_slip.pdf')
228        open(path, 'wb').write(self.browser.contents)
229        print "Sample akoka_application_slip.pdf written to %s" % path
230
231    def test_nils_application_slip(self):
232
233        # Remove required FieldProperty attribute first ...
234        #delattr(ApplicantsContainer, 'prefix')
235        # ... and replace by akoka
236        self.applicantscontainer.prefix = 'pgn'
237        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
238        self.slip_path = self.view_path + '/application_slip.pdf'
239        self.browser.open(self.manage_path)
240        self.assertEqual(self.browser.headers['Status'], '200 Ok')
241        self.fill_correct_values()
242        self.browser.getControl("Save").click()
243        IWorkflowState(self.applicant).setState('submitted')
244        self.browser.open(self.manage_path)
245        self.browser.getLink("Download application slip").click()
246        self.assertEqual(self.browser.headers['Status'], '200 Ok')
247        self.assertEqual(self.browser.headers['Content-Type'],
248                         'application/pdf')
249        path = os.path.join(samples_dir(), 'nils_application_slip.pdf')
250        open(path, 'wb').write(self.browser.contents)
251        print "Sample nils_application_slip.pdf written to %s" % path
252
253    def test_ictwk_application(self):
254
255        # Remove required FieldProperty attribute first ...
256        #delattr(ApplicantsContainer, 'prefix')
257        # ... and replace by ictw
258        self.applicantscontainer.prefix = 'ictwk'
259        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
260        self.slip_path = self.view_path + '/application_slip.pdf'
261        self.browser.open(self.manage_path)
262        self.assertEqual(self.browser.headers['Status'], '200 Ok')
263        self.browser.getControl(name="form.firstname").value = 'John'
264        self.browser.getControl(name="form.middlename").value = 'Anthony'
265        self.browser.getControl(name="form.lastname").value = 'Tester'
266        #self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
267        self.browser.getControl(name="form.sex").value = ['m']
268        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
269        image = open(SAMPLE_IMAGE, 'rb')
270        ctrl = self.browser.getControl(name='form.passport')
271        file_ctrl = ctrl.mech_control
272        file_ctrl.add_file(image, filename='myphoto.jpg')
273        self.browser.getControl("Save").click()
274        self.applicant.registration_cats = ['group', 'corporate']
275        IWorkflowState(self.applicant).setState('started')
276        configuration = CustomSessionConfiguration()
277        configuration.academic_session = session_1
278        configuration.application_fee = 0.0
279        self.app['configuration'].addSessionConfiguration(configuration)
280        self.browser.getControl("Add online payment ticket").click()
281        self.assertMatches('...Payment ticket created...',
282                           self.browser.contents)
283        self.assertEqual(self.applicant.values()[0].amount_auth, 450000)
284        IWorkflowState(self.applicant).setState('submitted')
285        self.browser.open(self.view_path)
286        self.assertTrue('Group Registration @ ₦ 200000' in self.browser.contents)
287        self.browser.open(self.manage_path)
288        self.browser.getLink("Download application slip").click()
289        self.assertEqual(self.browser.headers['Status'], '200 Ok')
290        self.assertEqual(self.browser.headers['Content-Type'],
291                         'application/pdf')
292        path = os.path.join(samples_dir(), 'ictwk_application_slip.pdf')
293        open(path, 'wb').write(self.browser.contents)
294        print "Sample ictwk_application_slip.pdf written to %s" % path
295
296    def test_transcript_payment(self):
297        configuration = CustomSessionConfiguration()
298        configuration.academic_session = session_1
299        self.app['configuration'].addSessionConfiguration(configuration)
300        # Add special application container
301        applicantscontainer = ApplicantsContainer()
302        applicantscontainer.year = session_1
303        applicantscontainer.application_fee = 1.0 # Must be set but is not used.
304        applicantscontainer.code = u'trans1234'
305        applicantscontainer.prefix = 'tscf'
306        applicantscontainer.title = u'This is a trans container'
307        applicantscontainer.application_category = 'no'
308        applicantscontainer.mode = 'create'
309        applicantscontainer.strict_deadline = True
310        applicantscontainer.with_picture = False
311        delta = datetime.timedelta(days=10)
312        applicantscontainer.startdate = datetime.datetime.now(pytz.utc) - delta
313        applicantscontainer.enddate = datetime.datetime.now(pytz.utc) + delta
314        self.app['applicants']['trans1234'] = applicantscontainer
315        # Add an applicant
316        applicant = createObject('waeup.Applicant')
317        # reg_number is the only field which has to be preset here
318        # because managers are allowed to edit this required field
319        applicant.reg_number = u'12345'
320        self.app['applicants']['trans1234'].addApplicant(applicant)
321        IUserAccount(
322            self.app['applicants']['trans1234'][
323            applicant.application_number]).setPassword('apwd')
324        # Login
325        self.browser.open(self.login_path)
326        self.browser.getControl(
327            name="form.login").value = applicant.applicant_id
328        self.browser.getControl(name="form.password").value = 'apwd'
329        self.browser.getControl("Login").click()
330        self.browser.getLink("Application record").click()
331        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
332        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
333        self.browser.getControl(name="form.firstname").value = 'Angela'
334        self.browser.getControl(name="form.lastname").value = 'Merkel'
335        self.browser.getControl(name="form.sex").value = ['f']
336        self.browser.getControl(name="form.no_copies").value = ['2']
337        self.browser.getControl(name="form.course_studied").value = ['CERT1']
338        self.browser.getControl(name="form.matric_number").value = '234'
339        self.browser.getControl(name="form.dispatch_address").value = 'Kuensche'
340        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
341        self.browser.getControl(name="form.entry_session").value = ['2014']
342        self.browser.getControl(name="form.end_session").value = ['2015']
343        self.browser.getControl(name="form.phone.country").value = ['+234']
344        self.browser.getControl(name="form.phone.ext").value = '5678'
345        self.browser.getControl(name="form.charge").value = ['nigeria']
346        self.browser.getControl("Save").click()
347        self.browser.getControl("Add online payment ticket").click()
348        self.assertTrue('Payment ticket created' in self.browser.contents)
349        self.assertTrue('<span>40000.0</span>' in self.browser.contents)
350        self.assertEqual(applicant.values()[0].amount_auth, 40000.0)
351        return
Note: See TracBrowser for help on using the repository browser.