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

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

Change notice and remove upload fields.

  • Property svn:keywords set to Id
File size: 8.5 KB
Line 
1## $Id: test_browser.py 10382 2013-06-25 12:46:38Z 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_file = StringIO(pdf_content)
78        ctrl = self.browser.getControl(name='form.extraform')
79        file_ctrl = ctrl.mech_control
80        file_ctrl.add_file(pseudo_file, 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_file = StringIO(pdf_content)
88        ctrl = self.browser.getControl(name='form.extraform')
89        file_ctrl = ctrl.mech_control
90        file_ctrl.add_file(pseudo_file, 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_file = StringIO(pdf_content)
122        ctrl = self.browser.getControl(name='form.extraform')
123        file_ctrl = ctrl.mech_control
124        file_ctrl.add_file(pseudo_file, 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_applicant_workflow(self):
164        # Applicants can edit their record
165        self.browser.open(self.login_path)
166        self.login()
167        self.assertTrue(
168            'You logged in.' in self.browser.contents)
169        self.browser.open(self.edit_path)
170        self.assertTrue(self.browser.url != self.login_path)
171        self.assertEqual(self.browser.headers['Status'], '200 Ok')
172        self.fill_correct_values()
173        self.browser.getControl(name="form.nationality").value = ['NG']
174        self.browser.getControl("Save").click()
175        self.assertTrue('Form has been saved' in self.browser.contents)
176        self.assertTrue('started ' in self.browser.contents)
177
178        pdf_content = 'A' * 1024 * 300  # A string of 300 KB size
179        pseudo_file = StringIO(pdf_content)
180        ctrl = self.browser.getControl(name='form.passport')
181        file_ctrl = ctrl.mech_control
182        file_ctrl.add_file(pseudo_file, filename='myphoto.jpg')
183        ctrl = self.browser.getControl(name='form.extraform')
184        file_ctrl = ctrl.mech_control
185        file_ctrl.add_file(pseudo_file, filename='myform.pdf')
186
187        self.browser.getControl(name="confirm_passport").value = True
188        self.browser.getControl("Save").click()
189        return
Note: See TracBrowser for help on using the repository browser.