source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py @ 7240

Last change on this file since 7240 was 7240, checked in by Henrik Bettermann, 13 years ago

Rebuild applicants package (1st part). Applicants now have an applicant_id and a password and can use the regular login page to enter the portal.

Add user_type attribute to SIRPPrincipal objects.

Add some permissions in students package.

Some tests are still missing and will be re-added soon.

  • Property svn:keywords set to Id
File size: 29.3 KB
Line 
1## $Id: test_browser.py 7240 2011-11-30 23:13:26Z 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"""
21import shutil
22import tempfile
23from StringIO import StringIO
24from datetime import datetime, date, timedelta
25from mechanize import LinkNotFoundError
26from zope.component import createObject, getUtility
27from zope.component.hooks import setSite, clearSite
28from zope.security.interfaces import Unauthorized
29from zope.testbrowser.testing import Browser
30from hurry.workflow.interfaces import IWorkflowInfo
31from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase
32from waeup.sirp.app import University
33from waeup.sirp.applicants.container import ApplicantsContainer
34from waeup.sirp.applicants.applicant import Applicant
35from waeup.sirp.interfaces import (
36    IExtFileStore, IFileStoreNameChooser, IUserAccount)
37from waeup.sirp.university.faculty import Faculty
38from waeup.sirp.university.department import Department
39
40PH_LEN = 2059  # Length of placeholder file
41
42class ApplicantsFullSetup(FunctionalTestCase):
43    # A test case that only contains a setup and teardown
44    #
45    # Complete setup for applicants handlings is rather complex and
46    # requires lots of things created before we can start. This is a
47    # setup that does all this, creates a university, creates PINs,
48    # etc.  so that we do not have to bother with that in different
49    # test cases.
50
51    layer = FunctionalLayer
52
53    def setUp(self):
54        super(ApplicantsFullSetup, self).setUp()
55
56        # Setup a sample site for each test
57        app = University()
58        self.dc_root = tempfile.mkdtemp()
59        app['datacenter'].setStoragePath(self.dc_root)
60
61        # Prepopulate the ZODB...
62        self.getRootFolder()['app'] = app
63        # we add the site immediately after creation to the
64        # ZODB. Catalogs and other local utilities are not setup
65        # before that step.
66        self.app = self.getRootFolder()['app']
67        # Set site here. Some of the following setup code might need
68        # to access grok.getSite() and should get our new app then
69        setSite(app)
70
71        self.login_path = 'http://localhost/app/login'
72        self.root_path = 'http://localhost/app/applicants'
73        self.manage_root_path = self.root_path + '/@@manage'
74        self.add_container_path = self.root_path + '/@@add'
75        self.container_path = 'http://localhost/app/applicants/app2009'
76        self.manage_container_path = self.container_path + '/@@manage'
77
78        # Add an applicants container
79        applicantscontainer = ApplicantsContainer()
80        applicantscontainer.ac_prefix = 'APP'
81        applicantscontainer.code = u'app2009'
82        applicantscontainer.prefix = 'app'
83        applicantscontainer.year = 2009
84        applicantscontainer.application_category = 'basic'
85        delta = timedelta(days=10)
86        applicantscontainer.startdate = date.today() - delta
87        applicantscontainer.enddate = date.today() + delta
88        self.app['applicants']['app2009'] = applicantscontainer
89        self.applicantscontainer = self.app['applicants']['app2009']
90
91        # Populate university
92        certificate = createObject('waeup.Certificate')
93        certificate.code = 'CERT1'
94        certificate.application_category = 'basic'
95        self.certificate = certificate
96        self.app['faculties']['fac1'] = Faculty()
97        self.app['faculties']['fac1']['dep1'] = Department()
98        self.department = self.app['faculties']['fac1']['dep1']
99        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
100            certificate)
101
102        # Put the prepopulated site into test ZODB and prepare test
103        # browser
104        self.browser = Browser()
105        self.browser.handleErrors = False
106
107        # Create 5 access codes with prefix'FOO' and cost 9.99 each
108        pin_container = self.app['accesscodes']
109        pin_container.createBatch(
110            datetime.now(), 'some_userid', 'APP', 9.99, 5)
111        pins = pin_container[pin_container.keys()[0]].values()
112        self.pins = [x.representation for x in pins]
113        self.existing_pin = self.pins[0]
114        parts = self.existing_pin.split('-')[1:]
115        self.existing_series, self.existing_number = parts
116
117        # Add an applicant
118        self.applicant = Applicant(container=applicantscontainer)
119        app['applicants']['app2009'][
120            self.applicant.application_number] = self.applicant
121        IUserAccount(
122            self.app['applicants']['app2009'][
123            self.applicant.application_number]).setPassword('apwd')
124        self.manage_path = 'http://localhost/app/applicants/%s/%s/%s' % (
125            'app2009', self.applicant.application_number, 'manage')
126        self.edit_path = 'http://localhost/app/applicants/%s/%s/%s' % (
127            'app2009', self.applicant.application_number, 'edit')
128        self.view_path = 'http://localhost/app/applicants/%s/%s' % (
129            'app2009', self.applicant.application_number)
130
131    def login(self):
132        # Perform an applicant login. This creates an applicant record.
133        #
134        # This helper also sets `self.applicant`, which is the
135        # applicant object created.
136        self.browser.open(self.login_path)
137        self.browser.getControl(name="form.login").value = self.applicant.applicant_id
138        self.browser.getControl(name="form.password").value = 'apwd'
139        self.browser.getControl("Login").click()
140
141    def fill_correct_values(self):
142        # Fill the edit form with suitable values
143        self.browser.getControl(name="form.firstname").value = 'John'
144        self.browser.getControl(name="form.lastname").value = 'Tester'
145        self.browser.getControl(name="form.course1").value = ['CERT1']
146        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
147        self.browser.getControl(name="form.lga").value = ['foreigner']
148        self.browser.getControl(name="form.sex").value = ['m']
149        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
150
151    def tearDown(self):
152        super(ApplicantsFullSetup, self).tearDown()
153        clearSite()
154        shutil.rmtree(self.dc_root)
155
156class ApplicantsRootUITests(ApplicantsFullSetup):
157    # Tests for ApplicantsRoot class
158
159    layer = FunctionalLayer
160
161    def test_anonymous_access(self):
162        # Anonymous users can access applicants root
163        self.browser.open(self.root_path)
164        self.assertEqual(self.browser.headers['Status'], '200 Ok')
165        self.assertFalse(
166            'Manage' in self.browser.contents)
167
168   
169    def test_anonymous_no_actions(self):
170        # Make sure anonymous users cannot access actions
171        self.browser.open(self.root_path)
172        self.assertRaises(
173            LookupError, self.browser.getControl, "Add local role")
174        # Manage screen neither linked nor accessible for anonymous
175        self.assertRaises(
176            LinkNotFoundError,
177            self.browser.getLink, 'Manage application section')
178        self.assertRaises(
179            Unauthorized, self.browser.open, self.manage_root_path)
180        # Add container screen not accessible for anonymous
181        self.assertRaises(
182            Unauthorized, self.browser.open, self.add_container_path)
183        return
184
185    def test_manage_access(self):
186        # Managers can access the manage pages of applicants root
187        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
188        self.browser.open(self.root_path)
189        self.assertTrue('Manage application section' in self.browser.contents)
190        # There is a manage link
191        link = self.browser.getLink('Manage application section')
192        link.click()
193        self.assertEqual(self.browser.headers['Status'], '200 Ok')
194        self.assertEqual(self.browser.url, self.manage_root_path)
195
196    def test_manage_actions_access(self):
197        # Managers can access the action on manage screen
198        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
199        self.browser.open(self.manage_root_path)
200        self.browser.getControl("Add local role").click()
201        self.assertTrue('No user selected' in self.browser.contents)
202        return
203
204    # We have no local roles yet
205    #def test_local_roles_add_delete(self):
206    #    # Managers can assign and delete local roles of applicants root
207    #    myusers = self.app['users']
208    #    myusers.addUser('bob', 'bobssecret')
209    #    self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
210    #    self.browser.open(self.manage_root_path)
211    #    self.browser.getControl(name="user").value = ['bob']
212    #    self.browser.getControl(name="local_role").value = [
213    #        'waeup.local.ApplicationsOfficer']
214    #    self.browser.getControl("Add local role").click()
215    #    self.assertTrue('<td>bob</td>' in self.browser.contents)
216    #    # Remove the role assigned
217    #    ctrl = self.browser.getControl(name='role_id')
218    #    ctrl.getControl(value='bob|waeup.ApplicationsOfficer').selected = True
219    #    self.browser.getControl("Remove selected local roles").click()
220    #    self.assertTrue('Successfully removed:' in self.browser.contents)
221    #    self.assertFalse('<td>bob</td>' in self.browser.contents)
222    #    return
223
224    def test_add_delete_container(self):
225        # Managers can add and delete applicants containers
226        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
227        self.browser.open(self.manage_root_path)
228        self.browser.getControl("Cancel").click()
229        self.assertEqual(self.browser.url, self.root_path)
230        self.browser.open(self.manage_root_path)
231        self.browser.getControl("Add applicants container").click()
232        self.assertEqual(self.browser.headers['Status'], '200 Ok')
233        self.assertEqual(self.browser.url, self.add_container_path)
234        self.browser.getControl(name="form.prefix").value = ['app']
235        self.browser.getControl("Add applicants container").click()
236        self.assertTrue(
237            'There were errors' in self.browser.contents)
238        self.browser.getControl(name="form.prefix").value = ['app']
239        self.browser.getControl(name="form.year").value = ['2010']
240        self.browser.getControl(name="form.provider").value = [
241            'waeup.sirp.applicants.ApplicantsContainer']
242        self.browser.getControl(name="form.ac_prefix").value = ['APP']
243        self.browser.getControl(name="form.application_category").value = ['basic']
244        self.browser.getControl("Add applicants container").click()
245        self.assertTrue('Added:' in self.browser.contents)
246        self.browser.open(self.add_container_path)
247        self.browser.getControl("Cancel").click()
248        self.assertEqual(self.browser.url, self.manage_root_path + '#tab-1')
249        self.browser.open(self.add_container_path)
250        self.browser.getControl(name="form.prefix").value = ['app']
251        self.browser.getControl(name="form.year").value = ['2010']
252        self.browser.getControl(name="form.provider").value = [
253            'waeup.sirp.applicants.ApplicantsContainer']
254        self.browser.getControl(name="form.ac_prefix").value = ['APP']
255        self.browser.getControl(name="form.application_category").value = ['basic']
256        self.browser.getControl("Add applicants container").click()
257        self.assertTrue('exists already in the database' in self.browser.contents)
258        self.browser.open(self.manage_root_path)
259        ctrl = self.browser.getControl(name='val_id')
260        ctrl.getControl(value='app2010').selected = True
261        self.browser.getControl("Remove selected", index=0).click()
262        self.assertTrue('Successfully removed:' in self.browser.contents)
263        self.browser.open(self.add_container_path)
264        self.browser.getControl(name="form.prefix").value = ['app']
265        self.browser.getControl(name="form.year").value = ['2010']
266        self.browser.getControl(name="form.provider").value = [
267            'waeup.sirp.applicants.ApplicantsContainer']
268        self.browser.getControl(name="form.ac_prefix").value = ['APP']
269        self.browser.getControl(name="form.application_category").value = ['basic']
270        self.browser.getControl("Add applicants container").click()
271        del self.app['applicants']['app2010']
272        ctrl = self.browser.getControl(name='val_id')
273        ctrl.getControl(value='app2010').selected = True
274        self.browser.getControl("Remove selected", index=0).click()
275        self.assertMatches('...Could not delete...', self.browser.contents)
276        return
277
278    def test_add_delete_applicants(self):
279        # Managers can add and delete applicants
280        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
281        self.add_applicant_path = self.container_path + '/addapplicant'
282        self.container_manage_path = self.container_path + '/@@manage'
283        self.browser.open(self.container_manage_path)
284        self.browser.getControl("Add applicant").click()
285        self.assertEqual(self.browser.headers['Status'], '200 Ok')
286        self.assertEqual(self.browser.url, self.add_applicant_path)
287        self.browser.getControl(name="form.firstname").value = 'Albert'
288        self.browser.getControl(name="form.lastname").value = 'Einstein'
289        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
290        self.browser.getControl("Create application record").click()
291        self.assertTrue('Application initialized' in self.browser.contents)
292        self.browser.open(self.container_manage_path)
293        self.assertEqual(self.browser.headers['Status'], '200 Ok')
294        ctrl = self.browser.getControl(name='val_id')
295        value = ctrl.options[0]
296        ctrl.getControl(value=value).selected = True
297        self.browser.getControl("Remove selected", index=0).click()
298        self.assertTrue('Successfully removed:' in self.browser.contents)
299        self.browser.open(self.add_applicant_path)
300        self.browser.getControl(name="form.firstname").value = 'Albert'
301        self.browser.getControl(name="form.lastname").value = 'Einstein'
302        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
303        self.browser.getControl("Create application record").click()
304        self.assertTrue('Application initialized' in self.browser.contents)
305        return
306
307    def test_manage_and_view_applicant(self):
308        # Managers can manage applicants
309        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
310        self.slip_path = self.view_path + '/application_slip.pdf'
311        self.browser.open(self.manage_path)
312        self.assertEqual(self.browser.headers['Status'], '200 Ok')
313        self.fill_correct_values()
314        self.browser.getControl(name="transition").value = ['start']
315        self.browser.getControl("Save").click()
316        self.assertMatches('...Form has been saved...', self.browser.contents)
317        self.assertMatches('...Application started by zope.mgr...', self.browser.contents)
318        self.browser.open(self.view_path)
319        self.assertEqual(self.browser.headers['Status'], '200 Ok')
320        self.browser.open(self.slip_path)
321        self.assertEqual(self.browser.headers['Status'], '200 Ok')
322        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
323        self.browser.open(self.manage_path)
324        self.browser.getControl(name="form.course_admitted").value = []
325        self.browser.getControl("Save").click()
326        self.browser.open(self.slip_path)
327        self.assertEqual(self.browser.headers['Status'], '200 Ok')
328        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
329        return
330
331    def test_passport_edit_view(self):
332        # We get a default image after login
333        self.browser.open(self.login_path)
334        self.login()
335        self.browser.open(self.browser.url + '/passport.jpg')
336        self.assertEqual(self.browser.headers['status'], '200 Ok')
337        self.assertEqual(self.browser.headers['content-type'], 'image/jpeg')
338        self.assertTrue('JFIF' in self.browser.contents)
339        self.assertEqual(
340            self.browser.headers['content-length'], str(PH_LEN))
341
342    def test_edit_applicant(self):
343        # Applicants can edit their record
344        self.browser.open(self.login_path)
345        self.login()
346        self.browser.open(self.edit_path)
347        self.assertTrue(self.browser.url != self.login_path)
348        self.assertEqual(self.browser.headers['Status'], '200 Ok')
349        self.fill_correct_values()
350        self.browser.getControl("Save").click()
351        self.assertMatches('...Form has been saved...', self.browser.contents)
352        return
353
354class ApplicantsContainerUITests(ApplicantsFullSetup):
355    # Tests for ApplicantsContainer class views and pages
356
357    layer = FunctionalLayer
358
359    def test_anonymous_access(self):
360        # Anonymous users can access applicants containers
361        self.browser.open(self.container_path)
362        self.assertEqual(self.browser.headers['Status'], '200 Ok')
363        self.assertFalse(
364            'Manage' in self.browser.contents)
365        return
366
367    def test_manage_access(self):
368        # Managers can access the manage pages of applicants
369        # containers and can perform actions
370        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
371        self.browser.open(self.manage_container_path)
372        self.assertEqual(self.browser.headers['Status'], '200 Ok')
373        self.assertEqual(self.browser.url, self.manage_container_path)
374        self.browser.getControl("Save").click()
375        self.assertTrue('Data saved' in self.browser.contents)
376        self.browser.getControl("Remove selected", index=0).click()
377        self.assertTrue('No applicant selected' in self.browser.contents)
378        self.browser.getControl("Add local role").click()
379        self.assertTrue('No user selected' in self.browser.contents)
380        self.browser.getControl("Cancel", index=0).click()
381        self.assertEqual(self.browser.url, self.container_path)
382        return
383
384class ApplicantsPassportTests(ApplicantsFullSetup):
385    # Tests for uploading/browsing the passport image of appplicants
386
387    layer = FunctionalLayer
388
389    def image_url(self, filename):
390        return self.edit_path.replace('edit', filename)
391
392    def test_after_login_default_browsable(self):
393        # After login we see the placeholder image in the edit view
394        #import pdb; pdb.set_trace()
395        self.login()
396        self.assertEqual(self.browser.url, self.view_path)
397        self.browser.open(self.edit_path)
398        # There is a correct <img> link included
399        self.assertTrue(
400              '<img src="passport.jpg" />' in self.browser.contents)
401        # Browsing the link shows a real image
402        self.browser.open(self.image_url('passport.jpg'))
403        self.assertEqual(
404            self.browser.headers['content-type'], 'image/jpeg')
405        self.assertEqual(len(self.browser.contents), PH_LEN)
406
407    def test_after_submit_default_browsable(self):
408        # After submitting an applicant form the default image is
409        # still visible
410        self.login()
411        self.browser.open(self.edit_path)
412        self.browser.getControl("Save").click() # submit form
413        # There is a correct <img> link included
414        self.assertTrue(
415            '<img src="passport.jpg" />' in self.browser.contents)
416        # Browsing the link shows a real image
417        self.browser.open(self.image_url('passport.jpg'))
418        self.assertEqual(
419            self.browser.headers['content-type'], 'image/jpeg')
420        self.assertEqual(len(self.browser.contents), PH_LEN)
421
422    def test_uploaded_image_respects_file_size_restriction(self):
423        # When we upload an image that is too big ( > 10 KB) we will
424        # get an error message
425        self.login()
426        self.browser.open(self.edit_path)
427        # Create a pseudo image file and select it to be uploaded in form
428        photo_content = 'A' * 1024 * 21  # A string of 21 KB size
429        pseudo_image = StringIO(photo_content)
430        ctrl = self.browser.getControl(name='form.passport')
431        file_ctrl = ctrl.mech_control
432        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
433        self.browser.getControl("Save").click() # submit form
434        # There is a correct <img> link included
435        self.assertTrue(
436            '<img src="passport.jpg" />' in self.browser.contents)
437        # We get a warning message
438        self.assertTrue(
439            'Uploaded image is too big' in self.browser.contents)
440        # Browsing the image gives us the default image, not the
441        # uploaded one.
442        self.browser.open(self.image_url('passport.jpg'))
443        self.assertEqual(
444            self.browser.headers['content-type'], 'image/jpeg')
445        self.assertEqual(len(self.browser.contents), PH_LEN)
446        # There is really no file stored for the applicant
447        img = getUtility(IExtFileStore).getFile(
448            IFileStoreNameChooser(self.applicant).chooseName())
449        self.assertTrue(img is None)
450
451    def test_uploaded_image_browsable_w_errors(self):
452        # We can upload a different image and browse it after submit,
453        # even if there are still errors in the form
454        self.login()
455        self.browser.open(self.edit_path)
456        # Create a pseudo image file and select it to be uploaded in form
457        photo_content = 'I pretend to be a graphics file'
458        pseudo_image = StringIO(photo_content)
459        ctrl = self.browser.getControl(name='form.passport')
460        file_ctrl = ctrl.mech_control
461        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
462        self.browser.getControl("Save").click() # submit form
463        # There is a correct <img> link included
464        self.assertTrue(
465            '<img src="passport.jpg" />' in self.browser.contents)
466        # Browsing the link shows a real image
467        self.browser.open(self.image_url('passport.jpg'))
468        self.assertEqual(
469            self.browser.headers['content-type'], 'image/jpeg')
470        self.assertEqual(self.browser.contents, photo_content)
471
472    def test_uploaded_image_stored_in_imagestorage_w_errors(self):
473        # After uploading a new passport pic the file is correctly
474        # stored in an imagestorage
475        self.login()
476        self.browser.open(self.edit_path)
477        # Create a pseudo image file and select it to be uploaded in form
478        pseudo_image = StringIO('I pretend to be a graphics file')
479        ctrl = self.browser.getControl(name='form.passport')
480        file_ctrl = ctrl.mech_control
481        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
482        self.browser.getControl("Save").click() # submit form
483        storage = getUtility(IExtFileStore)
484        file_id = IFileStoreNameChooser(self.applicant).chooseName()
485        pseudo_image.seek(0) # reset our file data source
486        self.assertEqual(
487            storage.getFile(file_id).read(), pseudo_image.read())
488        return
489
490    def test_uploaded_image_browsable_wo_errors(self):
491        # We can upload a different image and browse it after submit,
492        # if there are no errors in form
493        self.login()
494        self.browser.open(self.edit_path)
495        self.fill_correct_values() # fill other fields with correct values
496        # Create a pseudo image file and select it to be uploaded in form
497        pseudo_image = StringIO('I pretend to be a graphics file')
498        ctrl = self.browser.getControl(name='form.passport')
499        file_ctrl = ctrl.mech_control
500        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
501        self.browser.getControl("Save").click() # submit form
502        # There is a correct <img> link included
503        self.assertTrue(
504            '<img src="passport.jpg" />' in self.browser.contents)
505        # Browsing the link shows a real image
506        self.browser.open(self.image_url('passport.jpg'))
507        self.assertEqual(
508            self.browser.headers['content-type'], 'image/jpeg')
509        self.assertEqual(len(self.browser.contents), 31)
510
511    def test_uploaded_image_stored_in_imagestorage_wo_errors(self):
512        # After uploading a new passport pic the file is correctly
513        # stored in an imagestorage if form contains no errors
514        self.login()
515        self.browser.open(self.edit_path)
516        self.fill_correct_values() # fill other fields with correct values
517        # Create a pseudo image file and select it to be uploaded in form
518        pseudo_image = StringIO('I pretend to be a graphics file')
519        ctrl = self.browser.getControl(name='form.passport')
520        file_ctrl = ctrl.mech_control
521        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
522        self.browser.getControl("Save").click() # submit form
523        storage = getUtility(IExtFileStore)
524        file_id = IFileStoreNameChooser(self.applicant).chooseName()
525        # The stored image can be fetched
526        fd = storage.getFile(file_id)
527        file_len = len(fd.read())
528        self.assertEqual(file_len, 31)
529
530    def test_uploaded_images_equal(self):
531        # Make sure uploaded images do really differ if we eject a
532        # change notfication (and do not if we don't)
533        self.login()
534        self.browser.open(self.edit_path)
535        self.fill_correct_values() # fill other fields with correct values
536        self.browser.getControl("Save").click() # submit form
537        # Now go on as an officer
538        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
539        self.browser.open(self.manage_path)
540
541        # Create a pseudo image file and select it to be uploaded in form
542        pseudo_image = StringIO('I pretend to be a graphics file')
543        ctrl = self.browser.getControl(name='form.passport')
544        file_ctrl = ctrl.mech_control
545        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
546        file_id = IFileStoreNameChooser(self.applicant).chooseName()
547        setSite(self.app)
548        passport0 = getUtility(IExtFileStore).getFile(file_id)
549        self.browser.getControl("Save").click() # submit form with changed pic
550        passport1 = getUtility(IExtFileStore).getFile(file_id).read()
551        self.browser.getControl("Save").click() # submit form w/o changes
552        passport2 = getUtility(IExtFileStore).getFile(file_id).read()
553        self.assertTrue(passport0 is None)
554        self.assertTrue(passport0 != passport1)
555        self.assertTrue(passport1 == passport2)
556        return
557
558    def test_final_submit(self):
559        # Make sure that a correctly filled form with passport picture
560        # can be submitted
561        self.login()
562        self.browser.getLink("Edit application record").click()
563        self.fill_correct_values() # fill other fields with correct values
564        # Create a pseudo image file and select it to be uploaded in form
565        pseudo_image = StringIO('I pretend to be a graphics file')
566        ctrl = self.browser.getControl(name='form.passport')
567        file_ctrl = ctrl.mech_control
568        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
569        self.browser.getControl("Final Submit").click() # (finally) submit form
570        self.assertEqual(self.browser.headers['Status'], '200 Ok')
571        self.assertTrue(
572            'Passport confirmation box not ticked' in self.browser.contents)
573        self.browser.getControl(name="confirm_passport").value = True
574        IWorkflowInfo(self.applicant).fireTransition('submit')
575        self.browser.getControl("Final Submit").click() # submit form again
576        self.assertTrue(
577            'Wrong state' in self.browser.contents)
578        IWorkflowInfo(self.applicant).fireTransition('reset1')
579        # Now do the whole thing again but with correct state
580        self.login()
581        self.browser.open(self.edit_path)
582        self.fill_correct_values()
583        pseudo_image = StringIO('I pretend to be a graphics file')
584        ctrl = self.browser.getControl(name='form.passport')
585        file_ctrl = ctrl.mech_control
586        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
587        self.browser.getControl(name="confirm_passport").value = True
588        self.browser.getControl("Final Submit").click()
589        self.assertTrue(
590            '... submitted ...' in self.browser.contents)
591        self.browser.goBack(count=1)
592        self.browser.getControl("Save").click()
593        self.assertTrue(
594            'The requested form is locked' in self.browser.contents)
595        self.browser.goBack(count=1)
596        #import pdb; pdb.set_trace()
597        self.browser.getControl("Final Submit").click()
598        self.assertTrue(
599            'The requested form is locked' in self.browser.contents)
600        return
601
602    def test_locking(self):
603        # Make sure that locked forms can't be submitted
604        self.login()
605        self.browser.open(self.edit_path)
606        self.fill_correct_values() # fill other fields with correct values
607        # Create a pseudo image file and select it to be uploaded in form
608        pseudo_image = StringIO('I pretend to be a graphics file')
609        ctrl = self.browser.getControl(name='form.passport')
610        file_ctrl = ctrl.mech_control
611        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
612        self.browser.getControl("Save").click()
613        # Now we lock the form
614        self.applicant.locked = True
615        self.browser.open(self.edit_path)
616        self.assertEqual(self.browser.headers['Status'], '200 Ok')
617        #print self.browser.contents
618        self.assertTrue(
619            'The requested form is locked' in self.browser.contents)
620        return
Note: See TracBrowser for help on using the repository browser.