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

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

If an applicant is removed also pdf files of this applicant have to be removed.

  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1## $Id: test_browser.py 10146 2013-05-02 20:01:17Z 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        # When an applicant is removed, also the pdf files are gone.
154        del self.app['applicants']['app2011'][self.applicant.application_number]
155        fd = storage.getFile(file_id)
156        self.assertTrue(fd is None)
157
158    def test_upload_refereeform_by_student(self):
159        self.login()
160        self.browser.open(self.edit_path)
161        # Create a pseudo file and select it to be uploaded in form
162        pdf_content = 'A' * 1024 * 600  # A string of 600 KB size
163        pseudo_pdf = StringIO(pdf_content)
164        ctrl = self.browser.getControl(name='form.refereeform')
165        file_ctrl = ctrl.mech_control
166        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
167        self.browser.getControl("Save").click() # submit form
168        # We get a warning message
169        self.assertTrue(
170            'Uploaded file is too big' in self.browser.contents)
171        # Create a pseudo file with acceptable size
172        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
173        pseudo_pdf = StringIO(pdf_content)
174        ctrl = self.browser.getControl(name='form.refereeform')
175        file_ctrl = ctrl.mech_control
176        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
177        self.browser.getControl("Save").click() # submit form
178        # Even though the form could not be saved ...
179        self.assertTrue('Required input is missing' in self.browser.contents)
180        # ... the file has been successfully uploaded
181        pdf_url = self.edit_path.replace('edit', 'refereeform.pdf')
182        self.browser.open(pdf_url)
183        self.assertEqual(
184            self.browser.headers['content-type'], 'application/pdf')
185        self.assertEqual(len(self.browser.contents), 307200)
186        # There is really a file stored for the applicant
187        storage = getUtility(IExtFileStore)
188        file_id = IFileStoreNameChooser(self.applicant).chooseName(
189            attr='refereeform.pdf')
190        # The stored file can be fetched
191        fd = storage.getFile(file_id)
192        file_len = len(fd.read())
193        self.assertEqual(file_len, 307200)
194        # A file link is displayed on the edit view ...
195        self.browser.open(self.edit_path)
196        self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents)
197        # ... and on the dislay view
198        self.browser.open(self.view_path)
199        self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>'
200            in self.browser.contents)
201
202    def test_upload_refereeform_by_manager(self):
203        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
204        self.browser.open(self.manage_path)
205        # Create a pseudo file with acceptable size
206        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
207        pseudo_pdf = StringIO(pdf_content)
208        ctrl = self.browser.getControl(name='form.refereeform')
209        file_ctrl = ctrl.mech_control
210        file_ctrl.add_file(pseudo_pdf, filename='myform.pdf')
211        self.browser.getControl("Save").click() # submit form
212        # Even though the form could not be saved ...
213        self.assertTrue('Required input is missing' in self.browser.contents)
214        # ... the file has been successfully uploaded
215        pdf_url = self.manage_path.replace('manage', 'refereeform.pdf')
216        self.browser.open(pdf_url)
217        self.assertEqual(
218            self.browser.headers['content-type'], 'application/pdf')
219        self.assertEqual(len(self.browser.contents), 307200)
220        # There is rally a file stored for the applicant
221        storage = getUtility(IExtFileStore)
222        file_id = IFileStoreNameChooser(self.applicant).chooseName(
223            attr='refereeform.pdf')
224        # The stored file can be fetched
225        fd = storage.getFile(file_id)
226        file_len = len(fd.read())
227        self.assertEqual(file_len, 307200)
228        # A file link is displayed on the edit view ...
229        self.browser.open(self.manage_path)
230        self.assertTrue('<a href="refereeform.pdf">' in self.browser.contents)
231        # ... and on the dislay view
232        self.browser.open(self.view_path)
233        self.assertTrue('<a href="refereeform.pdf">Referee\'s Form</a>'
234            in self.browser.contents)
235        # Adding file is properly logged
236        logfile = os.path.join(
237            self.app['datacenter'].storage, 'logs', 'applicants.log')
238        logcontent = open(logfile).read()
239        self.assertTrue(
240            'zope.mgr - waeup.futminna.applicants.browser.CustomApplicantManageFormPage'
241            ' - %s - saved: refereeform'
242            % (self.applicant.applicant_id)
243            in logcontent)
244        # When an applicant is removed, also the pdf files are gone.
245        del self.app['applicants']['app2011'][self.applicant.application_number]
246        fd = storage.getFile(file_id)
247        self.assertTrue(fd is None)
Note: See TracBrowser for help on using the repository browser.