Ignore:
Timestamp:
23 Jul 2011, 03:00:24 (13 years ago)
Author:
uli
Message:
  • Reorganize browser tests: make it more readable, put often used things into own methods and so forth.
  • Add tests case for passport images.
  • Remove test_suite stuff at bottom of file. Zope testrunner does not need it, if there are no doctests.

There is however some major problem with uploaded images, when other
parts of the form have still errors: the uploaded picture, although it
is stored in the ZODB (a Blob), will not show up on the next
page. That's complicated to explain but comes down to the problem,
that the passport widget can generate a link to the uploaded picture,
but the picture file itself has to be delivered to the browser by some
traverser of the widget context (the applicant record) in which the
new image will not be stored until all data entered is correct. Until
then the applicant record cannot know where to find the uploaded
file. That's difficult to fix, therefore I check in what we got
already. The uploaded picture will be visible (and has to be uploaded
only once), when a user enters correct data.

The tests reflect the problem and give a failure for it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py

    r6487 r6540  
    2626import tempfile
    2727import unittest
     28from StringIO import StringIO
    2829from datetime import datetime
    2930from zope.app.testing.functional import HTTPCaller as http
     
    4142from waeup.sirp.university.certificatecontainer import CertificateContainer
    4243
    43 class ApplicantsUITests(FunctionalTestCase):
    44     # Tests for ApplicantsRoot class
     44PH_LEN = 2059  # Length of placeholder file
     45
     46class ApplicantsFullSetup(FunctionalTestCase):
     47    # A test case that only contains a setup and teardown
     48    #
     49    # Complete setup for applicants handlings is rather complex and
     50    # requires lots of things created before we can start. This is a
     51    # setup that does all this, creates a university, creates PINs,
     52    # etc.  so that we do not have to bother with that in different
     53    # test cases.
    4554
    4655    layer = FunctionalLayer
    4756
    4857    def setUp(self):
    49         super(ApplicantsUITests, self).setUp()
     58        super(ApplicantsFullSetup, self).setUp()
    5059
    5160        # Setup a sample site for each test
     
    107116
    108117    def tearDown(self):
    109         super(ApplicantsUITests, self).tearDown()
     118        super(ApplicantsFullSetup, self).tearDown()
    110119        clearSite()
    111120        shutil.rmtree(self.dc_root)
     121        #import pdb; pdb.set_trace()
     122
     123class ApplicantsUITests(ApplicantsFullSetup):
     124    # Tests for ApplicantsRoot class
     125
     126    layer = FunctionalLayer
    112127
    113128    def test_anonymous_access(self):
     
    218233        return
    219234
     235    def test_view_applicant(self):
     236        # Applicants can login and view their application
     237        self.login_path = 'http://localhost/app/applicants/app2009/login'
     238        self.browser.open(self.login_path)
     239        pin = self.pins[2]
     240        parts = pin.split('-')[1:]
     241        existing_series, existing_number = parts
     242        ac_series = self.browser.getControl(name="form.ac_series")
     243        ac_series.value = existing_series
     244        ac_number = self.browser.getControl(name="form.ac_number")
     245        ac_number.value = existing_number
     246        self.browser.getControl(name="SUBMIT").click()
     247        self.assertTrue(self.browser.url != self.login_path)
     248        self.assertEqual(self.browser.headers['Status'], '200 Ok')
     249        return
     250
     251    def test_passport_edit_view(self):
     252        # We get a default image after login
     253        login_path = 'http://localhost/app/applicants/app2009/login'
     254        self.browser.open(login_path)
     255        pin = self.pins[2]
     256        parts = pin.split('-')[1:]
     257        existing_series, existing_number = parts
     258        ac_series = self.browser.getControl(name="form.ac_series")
     259        ac_series.value = existing_series
     260        ac_number = self.browser.getControl(name="form.ac_number")
     261        ac_number.value = existing_number
     262        self.browser.getControl(name="SUBMIT").click()
     263
     264        pin = self.pins[2]
     265        appl = self.getRootFolder()['app']['applicants']['app2009']
     266        appl = appl[pin]
     267        passp = appl.passport
     268        #import pdb; pdb.set_trace()
     269        passp_len = len(passp.file.read())
     270        self.assertEqual(passp_len, PH_LEN)
     271
     272
     273        #image_url = "%s/%s" % (self.browser.url, 'placeholder.jpg')
     274        image_url = "%s/%s" % (self.browser.url, 'passport.jpg')
     275        #self.browser.open(image_url)
     276        self.browser.open('passport.jpg')
     277        self.assertEqual(self.browser.headers['status'], '200 Ok')
     278        self.assertEqual(self.browser.headers['content-type'], 'image/jpeg')
     279
     280
     281        self.assertTrue('JFIF' in self.browser.contents)
     282        self.assertEqual(
     283            self.browser.headers['content-length'], str(PH_LEN))
     284
     285
    220286    def test_edit_applicant(self):
    221287        # Applicants can edit their record
     
    386452        return
    387453
    388 
    389 def suite():
    390     suite = unittest.TestSuite()
    391     for testcase in [
    392         ApplicantsUITests,
    393         LoginTest,
    394         LoginTestWithPINs,
    395             ]:
    396         suite.addTests(unittest.makeSuite(testcase))
    397     return suite
    398 
    399 test_suite = suite
     454class ApplicantsPassportTests(ApplicantsFullSetup):
     455    # Tests for uploading/browsing the passport image of appplicants
     456
     457    layer = FunctionalLayer
     458
     459    def setUp(self):
     460        super(ApplicantsPassportTests, self).setUp()
     461        self.login_path = 'http://localhost/app/applicants/app2009/login'
     462        self.pin = self.pins[2]
     463        self.existing_series, self.existing_number = self.pin.split('-')[1:]
     464        self.edit_path = 'http://localhost/app/applicants/app2009/%s/edit' % (
     465            self.pin)
     466
     467    def tearDown(self):
     468        super(ApplicantsPassportTests, self).tearDown()
     469
     470    def login(self):
     471        # Perform an applicant login. This creates an applicant record.
     472        #
     473        # This helper also sets `self.applicant`, which is the
     474        # applicant object created.
     475        self.browser.open(self.login_path)
     476        ac_series = self.browser.getControl(name="form.ac_series")
     477        ac_series.value = self.existing_series
     478        ac_number = self.browser.getControl(name="form.ac_number")
     479        ac_number.value = self.existing_number
     480        self.browser.getControl(name="SUBMIT").click()
     481        self.applicant = self.app['applicants']['app2009'][self.pin]
     482
     483    def fill_correct_values(self):
     484        # Fill the edit form with suitable values
     485        self.browser.getControl(name="form.firstname").value = 'John'
     486        self.browser.getControl(name="form.lastname").value = 'Tester'
     487        self.browser.getControl(name="form.course1").value = ['CERT1']
     488        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
     489        self.browser.getControl(name="form.lga").value = ['foreigner']
     490        self.browser.getControl(name="form.sex").value = ['m']
     491
     492    def image_url(self, filename):
     493        return self.edit_path.replace('edit', filename)
     494
     495    def test_after_login_default_browsable(self):
     496        # After login we see the placeholder image in the edit view
     497        self.login()
     498        self.assertEqual(self.browser.url, self.edit_path)
     499        # There is a correct <img> link included
     500        self.assertTrue(
     501            '<img src="placeholder_m.jpg" />' in self.browser.contents)
     502        # Browsing the link shows a real image
     503        self.browser.open(self.image_url('placeholder_m.jpg'))
     504        self.assertEqual(
     505            self.browser.headers['content-type'], 'image/jpeg')
     506        self.assertEqual(len(self.browser.contents), PH_LEN)
     507
     508    def test_after_login_default_stored_in_imagestorage(self):
     509        # After login the applicants placeholder image is stored in
     510        # an imagestorage
     511        storage = self.app['images']
     512        # In the beginning, the storage is empty
     513        self.assertEqual([x for x in storage.keys()], [])
     514        self.login()
     515        # After login, it is filled
     516        self.assertEqual(
     517            [x for x in storage.keys()],
     518            [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])
     519        file_id = self.applicant.passport.data
     520        self.assertEqual(
     521            file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')
     522        # The stored image can be fetched
     523        fd = storage.retrieveFile(file_id)
     524        file_len = len(fd.read())
     525        self.assertEqual(file_len, PH_LEN)
     526
     527    def test_after_submit_default_browsable(self):
     528        # After submitting an applicant form the default image is
     529        # still visible
     530        self.login()
     531        self.browser.getControl("Save").click() # submit form
     532        # There is a correct <img> link included
     533        self.assertTrue(
     534            '<img src="placeholder_m.jpg" />' in self.browser.contents)
     535        # Browsing the link shows a real image
     536        self.browser.open(self.image_url('placeholder_m.jpg'))
     537        self.assertEqual(
     538            self.browser.headers['content-type'], 'image/jpeg')
     539        self.assertEqual(len(self.browser.contents), PH_LEN)
     540
     541    def test_after_submit_default_stored_in_imagestorage(self):
     542        # After submitting an applicant form the default image is
     543        # correctly stored in an imagestorage
     544        self.login()
     545        self.browser.getControl("Save").click() # submit form
     546        storage = self.app['images']
     547        self.assertEqual(
     548            [x for x in storage.keys()],
     549            [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])
     550        file_id = self.applicant.passport.data
     551        self.assertEqual(
     552            file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')
     553        # The stored image can be fetched
     554        fd = storage.retrieveFile(file_id)
     555        file_len = len(fd.read())
     556        self.assertEqual(file_len, PH_LEN)
     557
     558    # XXX: Make this test work again
     559    def DISABLEDtest_uploaded_image_browsable_w_errors(self):
     560        # We can upload a different image and browse it after submit,
     561        # even if there are still errors in the form
     562        self.login()
     563        # Create a pseudo image file and select it to be uploaded in form
     564        pseudo_image = StringIO('I pretend to be a graphics file')
     565        ctrl = self.browser.getControl(name='form.passport')
     566        file_ctrl = ctrl.mech_control
     567        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
     568        self.browser.getControl("Save").click() # submit form
     569        # There is a correct <img> link included
     570        self.assertTrue(
     571            '<img src="myphoto.jpg" />' in self.browser.contents)
     572        # Browsing the link shows a real image
     573        self.browser.open(self.image_url('myphoto.jpg'))
     574        self.assertEqual(
     575            self.browser.headers['content-type'], 'image/jpeg')
     576        self.assertEqual(len(self.browser.contents), PH_LEN)
     577
     578    def test_uploaded_image_stored_in_imagestorage_w_errors(self):
     579        # After uploading a new passport pic the file is correctly
     580        # stored in an imagestorage
     581        self.login()
     582        # Create a pseudo image file and select it to be uploaded in form
     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("Save").click() # submit form
     588        storage = self.app['images']
     589        self.assertTrue(
     590            u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())
     591        # The stored image can be fetched
     592        fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
     593        #fd = storage.retrieveFile(file_id)
     594        file_len = len(fd.read())
     595        self.assertEqual(file_len, 31)
     596        # The image uploaded belongs to the applicant
     597        # XXX: When there are errors in form, then the applicant is not
     598        #      changed. This (failing) test is here to remember us that
     599        #      we have to care for images when form submits fail.
     600        file_id = self.applicant.passport.data
     601        self.assertEqual(
     602            file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
     603
     604    def test_uploaded_image_browsable_wo_errors(self):
     605        # We can upload a different image and browse it after submit,
     606        # if there are no errors in form
     607        self.login()
     608        self.fill_correct_values() # fill other fields with correct values
     609        # Create a pseudo image file and select it to be uploaded in form
     610        pseudo_image = StringIO('I pretend to be a graphics file')
     611        ctrl = self.browser.getControl(name='form.passport')
     612        file_ctrl = ctrl.mech_control
     613        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
     614        self.browser.getControl("Save").click() # submit form
     615        # There is a correct <img> link included
     616        self.assertTrue(
     617            '<img src="myphoto.jpg" />' in self.browser.contents)
     618        # Browsing the link shows a real image
     619        self.browser.open(self.image_url('myphoto.jpg'))
     620        self.assertEqual(
     621            self.browser.headers['content-type'], 'image/jpeg')
     622        self.assertEqual(len(self.browser.contents), 31)
     623
     624    def test_uploaded_image_stored_in_imagestorage_wo_errors(self):
     625        # After uploading a new passport pic the file is correctly
     626        # stored in an imagestorage if form contains no errors
     627        self.login()
     628        self.fill_correct_values() # fill other fields with correct values
     629        # Create a pseudo image file and select it to be uploaded in form
     630        pseudo_image = StringIO('I pretend to be a graphics file')
     631        ctrl = self.browser.getControl(name='form.passport')
     632        file_ctrl = ctrl.mech_control
     633        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
     634        self.browser.getControl("Save").click() # submit form
     635        storage = self.app['images']
     636        self.assertTrue(
     637            u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())
     638        # The stored image can be fetched
     639        fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
     640        #fd = storage.retrieveFile(file_id)
     641        file_len = len(fd.read())
     642        self.assertEqual(file_len, 31)
     643        # The image uploaded belongs to the applicant
     644        file_id = self.applicant.passport.data
     645        self.assertEqual(
     646            file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
Note: See TracChangeset for help on using the changeset viewer.