source: main/waeup.futminna/trunk/src/waeup/futminna/applicants/tests/test_browser.py @ 10221

Last change on this file since 10221 was 10220, checked in by Henrik Bettermann, 12 years ago

Add credential form upload facilities.

  • Property svn:keywords set to Id
File size: 18.1 KB
Line 
1## $Id: test_browser.py 10220 2013-05-24 06:51:01Z 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"""
19Test the applicant-related UI components.
20"""
21
22import os
23import grok
24from zope.event import notify
25from StringIO import StringIO
26from zope.component import createObject, getUtility
27from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
28from waeup.kofa.interfaces import (
29    IExtFileStore, IFileStoreNameChooser)
30
31from waeup.futminna.testing import FunctionalLayer
32
33PH_LEN = 15911  # Length of placeholder file
34
35
36class ApplicantUITests(ApplicantsFullSetup):
37    # Tests for uploading/browsing the passport image of appplicants
38
39    layer = FunctionalLayer
40
41    def test_upload_image_by_student(self):
42        self.login()
43        self.browser.open(self.edit_path)
44        # Create a pseudo image file and select it to be uploaded in form
45        photo_content = 'A' * 1024 * 21  # A string of 21 KB size
46        pseudo_image = StringIO(photo_content)
47        ctrl = self.browser.getControl(name='form.passport')
48        file_ctrl = ctrl.mech_control
49        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
50        self.browser.getControl("Save").click() # submit form
51        # There is a correct <img> link included
52        self.assertTrue(
53            '<img src="passport.jpg" height="180px" />' in self.browser.contents)
54        # We get a warning message
55        self.assertTrue(
56            'Uploaded image is too big' in self.browser.contents)
57        # Browsing the image gives us the default image, not the
58        # uploaded one.
59        image_url = self.edit_path.replace('edit', 'passport.jpg')
60        self.browser.open(image_url)
61        self.assertEqual(
62            self.browser.headers['content-type'], 'image/jpeg')
63        self.assertEqual(len(self.browser.contents), PH_LEN)
64        # There is really no file stored for the applicant
65        img = getUtility(IExtFileStore).getFile(
66            IFileStoreNameChooser(self.applicant).chooseName())
67        self.assertTrue(img is None)
68
69    def test_upload_extraform_by_student(self):
70        self.login()
71        self.browser.open(self.edit_path)
72        # Create a pseudo file and select it to be uploaded in form
73        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
74        pseudo_pdf = StringIO(pdf_content)
75        ctrl = self.browser.getControl(name='form.extraform')
76        file_ctrl = ctrl.mech_control
77        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
78        self.browser.getControl("Save").click() # submit form
79        # We get a warning message
80        self.assertTrue(
81            'Uploaded file is too big' in self.browser.contents)
82        # Create a pseudo file with acceptable size
83        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
84        pseudo_pdf = StringIO(pdf_content)
85        ctrl = self.browser.getControl(name='form.extraform')
86        file_ctrl = ctrl.mech_control
87        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
88        self.browser.getControl("Save").click() # submit form
89        # Even though the form could not be saved ...
90        self.assertTrue('Required input is missing' in self.browser.contents)
91        # ... the file has been successfully uploaded
92        pdf_url = self.edit_path.replace('edit', 'extraform.pdf')
93        self.browser.open(pdf_url)
94        self.assertEqual(
95            self.browser.headers['content-type'], 'application/pdf')
96        self.assertEqual(len(self.browser.contents), 307200)
97        # There is rally a file stored for the applicant
98        storage = getUtility(IExtFileStore)
99        file_id = IFileStoreNameChooser(self.applicant).chooseName(
100            attr='extraform.pdf')
101        # The stored file can be fetched
102        fd = storage.getFile(file_id)
103        file_len = len(fd.read())
104        self.assertEqual(file_len, 307200)
105        # A file link is displayed on the edit view ...
106        self.browser.open(self.edit_path)
107        self.assertTrue('<a href="extraform.pdf">' in self.browser.contents)
108        # ... and on the dislay view
109        self.browser.open(self.view_path)
110        self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>'
111            in self.browser.contents)
112
113    def test_upload_extraform_by_manager(self):
114        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
115        self.browser.open(self.manage_path)
116        # Create a pseudo file with acceptable size
117        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
118        pseudo_pdf = StringIO(pdf_content)
119        ctrl = self.browser.getControl(name='form.extraform')
120        file_ctrl = ctrl.mech_control
121        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
122        self.browser.getControl("Save").click() # submit form
123        # Even though the form could not be saved ...
124        self.assertTrue('Required input is missing' in self.browser.contents)
125        # ... the file has been successfully uploaded
126        pdf_url = self.manage_path.replace('manage', 'extraform.pdf')
127        self.browser.open(pdf_url)
128        self.assertEqual(
129            self.browser.headers['content-type'], 'application/pdf')
130        self.assertEqual(len(self.browser.contents), 307200)
131        # There is rally a file stored for the applicant
132        storage = getUtility(IExtFileStore)
133        file_id = IFileStoreNameChooser(self.applicant).chooseName(
134            attr='extraform.pdf')
135        # The stored file can be fetched
136        fd = storage.getFile(file_id)
137        file_len = len(fd.read())
138        self.assertEqual(file_len, 307200)
139        # A file link is displayed on the edit view ...
140        self.browser.open(self.manage_path)
141        self.assertTrue('<a href="extraform.pdf">' in self.browser.contents)
142        # ... and on the dislay view
143        self.browser.open(self.view_path)
144        self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>'
145            in self.browser.contents)
146        # Adding file is properly logged
147        logfile = os.path.join(
148            self.app['datacenter'].storage, 'logs', 'applicants.log')
149        logcontent = open(logfile).read()
150        self.assertTrue(
151            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
152            ' - %s - saved: extraform'
153            % (self.applicant.applicant_id)
154            in logcontent)
155        # When an applicant is removed, also the pdf files are gone.
156        del self.app['applicants']['app2011'][self.applicant.application_number]
157        fd = storage.getFile(file_id)
158        self.assertTrue(fd is None)
159
160    def test_upload_refereeform_by_student(self):
161        self.login()
162        self.browser.open(self.edit_path)
163        # Create a pseudo file and select it to be uploaded in form
164        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
165        pseudo_pdf = StringIO(pdf_content)
166        ctrl = self.browser.getControl(name='form.refereeform')
167        file_ctrl = ctrl.mech_control
168        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
169        self.browser.getControl("Save").click() # submit form
170        # We get a warning message
171        self.assertTrue(
172            'Uploaded file is too big' in self.browser.contents)
173        # Create a pseudo file with acceptable size
174        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
175        pseudo_pdf = StringIO(pdf_content)
176        ctrl = self.browser.getControl(name='form.refereeform')
177        file_ctrl = ctrl.mech_control
178        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
179        self.browser.getControl("Save").click() # submit form
180        # Even though the form could not be saved ...
181        self.assertTrue('Required input is missing' in self.browser.contents)
182        # ... the file has been successfully uploaded
183        pdf_url = self.edit_path.replace('edit', 'refereeform.pdf')
184        self.browser.open(pdf_url)
185        self.assertEqual(
186            self.browser.headers['content-type'], 'application/pdf')
187        self.assertEqual(len(self.browser.contents), 307200)
188        # There is really a file stored for the applicant
189        storage = getUtility(IExtFileStore)
190        file_id = IFileStoreNameChooser(self.applicant).chooseName(
191            attr='refereeform.pdf')
192        # The stored file can be fetched
193        fd = storage.getFile(file_id)
194        file_len = len(fd.read())
195        self.assertEqual(file_len, 307200)
196        # A file link is displayed on the edit view ...
197        self.browser.open(self.edit_path)
198        self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents)
199        # ... and on the dislay view
200        self.browser.open(self.view_path)
201        self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>'
202            in self.browser.contents)
203
204    def test_upload_refereeform_by_manager(self):
205        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
206        self.browser.open(self.manage_path)
207        # Create a pseudo file with acceptable size
208        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
209        pseudo_pdf = StringIO(pdf_content)
210        ctrl = self.browser.getControl(name='form.refereeform')
211        file_ctrl = ctrl.mech_control
212        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
213        self.browser.getControl("Save").click() # submit form
214        # Even though the form could not be saved ...
215        self.assertTrue('Required input is missing' in self.browser.contents)
216        # ... the file has been successfully uploaded
217        pdf_url = self.manage_path.replace('manage', 'refereeform.pdf')
218        self.browser.open(pdf_url)
219        self.assertEqual(
220            self.browser.headers['content-type'], 'application/pdf')
221        self.assertEqual(len(self.browser.contents), 307200)
222        # There is rally a file stored for the applicant
223        storage = getUtility(IExtFileStore)
224        file_id = IFileStoreNameChooser(self.applicant).chooseName(
225            attr='refereeform.pdf')
226        # The stored file can be fetched
227        fd = storage.getFile(file_id)
228        file_len = len(fd.read())
229        self.assertEqual(file_len, 307200)
230        # A file link is displayed on the edit view ...
231        self.browser.open(self.manage_path)
232        self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents)
233        # ... and on the dislay view
234        self.browser.open(self.view_path)
235        self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>'
236            in self.browser.contents)
237        # Adding file is properly logged
238        logfile = os.path.join(
239            self.app['datacenter'].storage, 'logs', 'applicants.log')
240        logcontent = open(logfile).read()
241        self.assertTrue(
242            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
243            ' - %s - saved: refereeform'
244            % (self.applicant.applicant_id)
245            in logcontent)
246        # When an applicant is removed, also the pdf files are gone.
247        del self.app['applicants']['app2011'][self.applicant.application_number]
248        fd = storage.getFile(file_id)
249        self.assertTrue(fd is None)
250
251    def test_upload_credentialform_by_student(self):
252        self.login()
253        self.browser.open(self.edit_path)
254        # Create a pseudo file and select it to be uploaded in form
255        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
256        pseudo_pdf = StringIO(pdf_content)
257        ctrl = self.browser.getControl(name='form.credentialform')
258        file_ctrl = ctrl.mech_control
259        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
260        self.browser.getControl("Save").click() # submit form
261        # We get a warning message
262        self.assertTrue(
263            'Uploaded file is too big' in self.browser.contents)
264        # Create a pseudo file with acceptable size
265        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
266        pseudo_pdf = StringIO(pdf_content)
267        ctrl = self.browser.getControl(name='form.credentialform')
268        file_ctrl = ctrl.mech_control
269        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
270        self.browser.getControl("Save").click() # submit form
271        # Even though the form could not be saved ...
272        self.assertTrue('Required input is missing' in self.browser.contents)
273        # ... the file has been successfully uploaded
274        pdf_url = self.edit_path.replace('edit', 'credentialform.pdf')
275        self.browser.open(pdf_url)
276        self.assertEqual(
277            self.browser.headers['content-type'], 'application/pdf')
278        self.assertEqual(len(self.browser.contents), 307200)
279        # There is really a file stored for the applicant
280        storage = getUtility(IExtFileStore)
281        file_id = IFileStoreNameChooser(self.applicant).chooseName(
282            attr='credentialform.pdf')
283        # The stored file can be fetched
284        fd = storage.getFile(file_id)
285        file_len = len(fd.read())
286        self.assertEqual(file_len, 307200)
287        # A file link is displayed on the edit view ...
288        self.browser.open(self.edit_path)
289        self.assertTrue('<a href="credentialform.pdf">' in self.browser.contents)
290        # ... and on the dislay view
291        self.browser.open(self.view_path)
292        self.assertTrue('<a href="credentialform.pdf">Credential Form</a>'
293            in self.browser.contents)
294
295    def test_upload_credentialform_by_manager(self):
296        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
297        self.browser.open(self.manage_path)
298        # Create a pseudo file with acceptable size
299        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
300        pseudo_pdf = StringIO(pdf_content)
301        ctrl = self.browser.getControl(name='form.credentialform')
302        file_ctrl = ctrl.mech_control
303        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
304        self.browser.getControl("Save").click() # submit form
305        # Even though the form could not be saved ...
306        self.assertTrue('Required input is missing' in self.browser.contents)
307        # ... the file has been successfully uploaded
308        pdf_url = self.manage_path.replace('manage', 'credentialform.pdf')
309        self.browser.open(pdf_url)
310        self.assertEqual(
311            self.browser.headers['content-type'], 'application/pdf')
312        self.assertEqual(len(self.browser.contents), 307200)
313        # There is rally a file stored for the applicant
314        storage = getUtility(IExtFileStore)
315        file_id = IFileStoreNameChooser(self.applicant).chooseName(
316            attr='credentialform.pdf')
317        # The stored file can be fetched
318        fd = storage.getFile(file_id)
319        file_len = len(fd.read())
320        self.assertEqual(file_len, 307200)
321        # A file link is displayed on the edit view ...
322        self.browser.open(self.manage_path)
323        self.assertTrue('<a href="credentialform.pdf">' in self.browser.contents)
324        # ... and on the dislay view
325        self.browser.open(self.view_path)
326        self.assertTrue('<a href="credentialform.pdf">Credential Form</a>'
327            in self.browser.contents)
328        # Adding file is properly logged
329        logfile = os.path.join(
330            self.app['datacenter'].storage, 'logs', 'applicants.log')
331        logcontent = open(logfile).read()
332        self.assertTrue(
333            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
334            ' - %s - saved: credentialform'
335            % (self.applicant.applicant_id)
336            in logcontent)
337        # When an applicant is removed, also the pdf files are gone.
338        del self.app['applicants']['app2011'][self.applicant.application_number]
339        fd = storage.getFile(file_id)
340        self.assertTrue(fd is None)
341
342    def test_certificate_source(self):
343        # We add a second 'alternative' certificate
344        certificate = createObject('waeup.Certificate')
345        certificate.code = 'CERT2'
346        certificate.title = 'New Certificate'
347        certificate.application_category = 'basic'
348        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
349            certificate)
350        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
351        self.browser.open(self.edit_path)
352        self.assertFalse('course2' in self.browser.contents)
353        self.certificate.custom_textline_1 = u'Maths, Physics'
354        self.certificate.custom_float_1 = 50.0
355        certificate.custom_textline_1 = u'German, Physics'
356        certificate.custom_float_1 = 30.0
357        notify(grok.ObjectModifiedEvent(certificate))
358        self.browser.open(self.edit_path)
359        # Score must be set and below the limit
360        # of course1 to see course2 field
361        self.applicant.jamb_score = 20
362        self.browser.open(self.edit_path)
363        self.assertTrue('course2' in self.browser.contents)
364        # But 20 is not enough to select the alternative CERT2
365        self.assertFalse(
366            'value="CERT2">CERT2 - New Certificate'
367            in self.browser.contents)
368        self.applicant.jamb_score = 30
369        self.browser.open(self.edit_path)
370        # 30 is enough
371        self.assertTrue('course2' in self.browser.contents)
372        self.assertTrue(
373            'value="CERT2">CERT2 - New Certificate [German, Physics, 30]</option>'
374            in self.browser.contents)
375
376    def test_low_jamb_score(self):
377        self.login()
378        self.assertFalse('<select id="form.course2" name="form.course2" size="1" >'
379            in self.browser.contents)
380        self.certificate.custom_textline_1 = u'Maths, Physics'
381        self.certificate.custom_float_1 = 50.0
382        self.applicant.jamb_score = 40
383        self.browser.open(self.edit_path)
384        self.assertTrue('<select id="form.course2" name="form.course2" size="1" >'
385            in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.