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

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

Make provision for uploading the referee's form. Rename form extension.

  • Property svn:keywords set to Id
File size: 11.1 KB
Line 
1## $Id: test_browser.py 10105 2013-04-29 08:18:41Z 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
23from StringIO import StringIO
24from zope.component import createObject, getUtility
25from waeup.kofa.applicants.tests.test_browser import ApplicantsFullSetup
26from waeup.kofa.interfaces import (
27    IExtFileStore, IFileStoreNameChooser)
28
29from waeup.futminna.testing import FunctionalLayer
30
31PH_LEN = 15911  # Length of placeholder file
32
33
34class ApplicantUITests(ApplicantsFullSetup):
35    # Tests for uploading/browsing the passport image of appplicants
36
37    layer = FunctionalLayer
38
39    def test_upload_image_by_student(self):
40        self.login()
41        self.browser.open(self.edit_path)
42        # Create a pseudo image file and select it to be uploaded in form
43        photo_content = 'A' * 1024 * 21  # A string of 21 KB size
44        pseudo_image = StringIO(photo_content)
45        ctrl = self.browser.getControl(name='form.passport')
46        file_ctrl = ctrl.mech_control
47        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
48        self.browser.getControl("Save").click() # submit form
49        # There is a correct <img> link included
50        self.assertTrue(
51            '<img src="passport.jpg" height="180px" />' in self.browser.contents)
52        # We get a warning message
53        self.assertTrue(
54            'Uploaded image is too big' in self.browser.contents)
55        # Browsing the image gives us the default image, not the
56        # uploaded one.
57        image_url = self.edit_path.replace('edit', 'passport.jpg')
58        self.browser.open(image_url)
59        self.assertEqual(
60            self.browser.headers['content-type'], 'image/jpeg')
61        self.assertEqual(len(self.browser.contents), PH_LEN)
62        # There is really no file stored for the applicant
63        img = getUtility(IExtFileStore).getFile(
64            IFileStoreNameChooser(self.applicant).chooseName())
65        self.assertTrue(img is None)
66
67    def test_upload_extraform_by_student(self):
68        self.login()
69        self.browser.open(self.edit_path)
70        # Create a pseudo file and select it to be uploaded in form
71        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
72        pseudo_pdf = StringIO(pdf_content)
73        ctrl = self.browser.getControl(name='form.extraform')
74        file_ctrl = ctrl.mech_control
75        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
76        self.browser.getControl("Save").click() # submit form
77        # We get a warning message
78        self.assertTrue(
79            'Uploaded file is too big' in self.browser.contents)
80        # Create a pseudo file with acceptable size
81        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
82        pseudo_pdf = StringIO(pdf_content)
83        ctrl = self.browser.getControl(name='form.extraform')
84        file_ctrl = ctrl.mech_control
85        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
86        self.browser.getControl("Save").click() # submit form
87        # Even though the form could not be saved ...
88        self.assertTrue('Required input is missing' in self.browser.contents)
89        # ... the file has been successfully uploaded
90        pdf_url = self.edit_path.replace('edit', 'extraform.pdf')
91        self.browser.open(pdf_url)
92        self.assertEqual(
93            self.browser.headers['content-type'], 'application/pdf')
94        self.assertEqual(len(self.browser.contents), 307200)
95        # There is rally a file stored for the applicant
96        storage = getUtility(IExtFileStore)
97        file_id = IFileStoreNameChooser(self.applicant).chooseName(
98            attr='extraform.pdf')
99        # The stored file can be fetched
100        fd = storage.getFile(file_id)
101        file_len = len(fd.read())
102        self.assertEqual(file_len, 307200)
103        # A file link is displayed on the edit view ...
104        self.browser.open(self.edit_path)
105        self.assertTrue('<a href="extraform.pdf">' in self.browser.contents)
106        # ... and on the dislay view
107        self.browser.open(self.view_path)
108        self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>'
109            in self.browser.contents)
110
111    def test_upload_extraform_by_manager(self):
112        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
113        self.browser.open(self.manage_path)
114        # Create a pseudo file with acceptable size
115        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
116        pseudo_pdf = StringIO(pdf_content)
117        ctrl = self.browser.getControl(name='form.extraform')
118        file_ctrl = ctrl.mech_control
119        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
120        self.browser.getControl("Save").click() # submit form
121        # Even though the form could not be saved ...
122        self.assertTrue('Required input is missing' in self.browser.contents)
123        # ... the file has been successfully uploaded
124        pdf_url = self.manage_path.replace('manage', 'extraform.pdf')
125        self.browser.open(pdf_url)
126        self.assertEqual(
127            self.browser.headers['content-type'], 'application/pdf')
128        self.assertEqual(len(self.browser.contents), 307200)
129        # There is rally a file stored for the applicant
130        storage = getUtility(IExtFileStore)
131        file_id = IFileStoreNameChooser(self.applicant).chooseName(
132            attr='extraform.pdf')
133        # The stored file can be fetched
134        fd = storage.getFile(file_id)
135        file_len = len(fd.read())
136        self.assertEqual(file_len, 307200)
137        # A file link is displayed on the edit view ...
138        self.browser.open(self.manage_path)
139        self.assertTrue('<a href="extraform.pdf">' in self.browser.contents)
140        # ... and on the dislay view
141        self.browser.open(self.view_path)
142        self.assertTrue('<a href="extraform.pdf">Extra Applicant Information Form</a>'
143            in self.browser.contents)
144        # Adding file is properly logged
145        logfile = os.path.join(
146            self.app['datacenter'].storage, 'logs', 'applicants.log')
147        logcontent = open(logfile).read()
148        self.assertTrue(
149            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
150            ' - %s - saved: extraform'
151            % (self.applicant.applicant_id)
152            in logcontent)
153
154    def test_upload_refereeform_by_student(self):
155        self.login()
156        self.browser.open(self.edit_path)
157        # Create a pseudo file and select it to be uploaded in form
158        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
159        pseudo_pdf = StringIO(pdf_content)
160        ctrl = self.browser.getControl(name='form.refereeform')
161        file_ctrl = ctrl.mech_control
162        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
163        self.browser.getControl("Save").click() # submit form
164        # We get a warning message
165        self.assertTrue(
166            'Uploaded file is too big' in self.browser.contents)
167        # Create a pseudo file with acceptable size
168        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
169        pseudo_pdf = StringIO(pdf_content)
170        ctrl = self.browser.getControl(name='form.refereeform')
171        file_ctrl = ctrl.mech_control
172        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
173        self.browser.getControl("Save").click() # submit form
174        # Even though the form could not be saved ...
175        self.assertTrue('Required input is missing' in self.browser.contents)
176        # ... the file has been successfully uploaded
177        pdf_url = self.edit_path.replace('edit', 'refereeform.pdf')
178        self.browser.open(pdf_url)
179        self.assertEqual(
180            self.browser.headers['content-type'], 'application/pdf')
181        self.assertEqual(len(self.browser.contents), 307200)
182        # There is really a file stored for the applicant
183        storage = getUtility(IExtFileStore)
184        file_id = IFileStoreNameChooser(self.applicant).chooseName(
185            attr='refereeform.pdf')
186        # The stored file can be fetched
187        fd = storage.getFile(file_id)
188        file_len = len(fd.read())
189        self.assertEqual(file_len, 307200)
190        # A file link is displayed on the edit view ...
191        self.browser.open(self.edit_path)
192        self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents)
193        # ... and on the dislay view
194        self.browser.open(self.view_path)
195        self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>'
196            in self.browser.contents)
197
198    def test_upload_refereeform_by_manager(self):
199        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
200        self.browser.open(self.manage_path)
201        # Create a pseudo file with acceptable size
202        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
203        pseudo_pdf = StringIO(pdf_content)
204        ctrl = self.browser.getControl(name='form.refereeform')
205        file_ctrl = ctrl.mech_control
206        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
207        self.browser.getControl("Save").click() # submit form
208        # Even though the form could not be saved ...
209        self.assertTrue('Required input is missing' in self.browser.contents)
210        # ... the file has been successfully uploaded
211        pdf_url = self.manage_path.replace('manage', 'refereeform.pdf')
212        self.browser.open(pdf_url)
213        self.assertEqual(
214            self.browser.headers['content-type'], 'application/pdf')
215        self.assertEqual(len(self.browser.contents), 307200)
216        # There is rally a file stored for the applicant
217        storage = getUtility(IExtFileStore)
218        file_id = IFileStoreNameChooser(self.applicant).chooseName(
219            attr='refereeform.pdf')
220        # The stored file can be fetched
221        fd = storage.getFile(file_id)
222        file_len = len(fd.read())
223        self.assertEqual(file_len, 307200)
224        # A file link is displayed on the edit view ...
225        self.browser.open(self.manage_path)
226        self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents)
227        # ... and on the dislay view
228        self.browser.open(self.view_path)
229        self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>'
230            in self.browser.contents)
231        # Adding file is properly logged
232        logfile = os.path.join(
233            self.app['datacenter'].storage, 'logs', 'applicants.log')
234        logcontent = open(logfile).read()
235        self.assertTrue(
236            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
237            ' - %s - saved: refereeform'
238            % (self.applicant.applicant_id)
239            in logcontent)
Note: See TracBrowser for help on using the repository browser.