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

Last change on this file since 17387 was 11488, checked in by Henrik Bettermann, 11 years ago

Fix test.

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