source: main/waeup.imostate/src/waeup/imostate/applicants/tests/test_browser.py @ 10350

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

Remove futminna customizations which are not applicable here.

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