Index: /main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py
===================================================================
--- /main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py	(revision 6539)
+++ /main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py	(revision 6540)
@@ -26,4 +26,5 @@
 import tempfile
 import unittest
+from StringIO import StringIO
 from datetime import datetime
 from zope.app.testing.functional import HTTPCaller as http
@@ -41,11 +42,19 @@
 from waeup.sirp.university.certificatecontainer import CertificateContainer
 
-class ApplicantsUITests(FunctionalTestCase):
-    # Tests for ApplicantsRoot class
+PH_LEN = 2059  # Length of placeholder file
+
+class ApplicantsFullSetup(FunctionalTestCase):
+    # A test case that only contains a setup and teardown
+    #
+    # Complete setup for applicants handlings is rather complex and
+    # requires lots of things created before we can start. This is a
+    # setup that does all this, creates a university, creates PINs,
+    # etc.  so that we do not have to bother with that in different
+    # test cases.
 
     layer = FunctionalLayer
 
     def setUp(self):
-        super(ApplicantsUITests, self).setUp()
+        super(ApplicantsFullSetup, self).setUp()
 
         # Setup a sample site for each test
@@ -107,7 +116,13 @@
 
     def tearDown(self):
-        super(ApplicantsUITests, self).tearDown()
+        super(ApplicantsFullSetup, self).tearDown()
         clearSite()
         shutil.rmtree(self.dc_root)
+        #import pdb; pdb.set_trace()
+
+class ApplicantsUITests(ApplicantsFullSetup):
+    # Tests for ApplicantsRoot class
+
+    layer = FunctionalLayer
 
     def test_anonymous_access(self):
@@ -218,4 +233,55 @@
         return
 
+    def test_view_applicant(self):
+        # Applicants can login and view their application
+        self.login_path = 'http://localhost/app/applicants/app2009/login'
+        self.browser.open(self.login_path)
+        pin = self.pins[2]
+        parts = pin.split('-')[1:]
+        existing_series, existing_number = parts
+        ac_series = self.browser.getControl(name="form.ac_series")
+        ac_series.value = existing_series
+        ac_number = self.browser.getControl(name="form.ac_number")
+        ac_number.value = existing_number
+        self.browser.getControl(name="SUBMIT").click()
+        self.assertTrue(self.browser.url != self.login_path)
+        self.assertEqual(self.browser.headers['Status'], '200 Ok')
+        return
+
+    def test_passport_edit_view(self):
+        # We get a default image after login
+        login_path = 'http://localhost/app/applicants/app2009/login'
+        self.browser.open(login_path)
+        pin = self.pins[2]
+        parts = pin.split('-')[1:]
+        existing_series, existing_number = parts
+        ac_series = self.browser.getControl(name="form.ac_series")
+        ac_series.value = existing_series
+        ac_number = self.browser.getControl(name="form.ac_number")
+        ac_number.value = existing_number
+        self.browser.getControl(name="SUBMIT").click()
+
+        pin = self.pins[2]
+        appl = self.getRootFolder()['app']['applicants']['app2009']
+        appl = appl[pin]
+        passp = appl.passport
+        #import pdb; pdb.set_trace()
+        passp_len = len(passp.file.read())
+        self.assertEqual(passp_len, PH_LEN)
+
+
+        #image_url = "%s/%s" % (self.browser.url, 'placeholder.jpg')
+        image_url = "%s/%s" % (self.browser.url, 'passport.jpg')
+        #self.browser.open(image_url)
+        self.browser.open('passport.jpg')
+        self.assertEqual(self.browser.headers['status'], '200 Ok')
+        self.assertEqual(self.browser.headers['content-type'], 'image/jpeg')
+
+
+        self.assertTrue('JFIF' in self.browser.contents)
+        self.assertEqual(
+            self.browser.headers['content-length'], str(PH_LEN))
+
+
     def test_edit_applicant(self):
         # Applicants can edit their record
@@ -386,14 +452,195 @@
         return
 
-
-def suite():
-    suite = unittest.TestSuite()
-    for testcase in [
-        ApplicantsUITests,
-        LoginTest,
-        LoginTestWithPINs,
-            ]:
-        suite.addTests(unittest.makeSuite(testcase))
-    return suite
-
-test_suite = suite
+class ApplicantsPassportTests(ApplicantsFullSetup):
+    # Tests for uploading/browsing the passport image of appplicants
+
+    layer = FunctionalLayer
+
+    def setUp(self):
+        super(ApplicantsPassportTests, self).setUp()
+        self.login_path = 'http://localhost/app/applicants/app2009/login'
+        self.pin = self.pins[2]
+        self.existing_series, self.existing_number = self.pin.split('-')[1:]
+        self.edit_path = 'http://localhost/app/applicants/app2009/%s/edit' % (
+            self.pin)
+
+    def tearDown(self):
+        super(ApplicantsPassportTests, self).tearDown()
+
+    def login(self):
+        # Perform an applicant login. This creates an applicant record.
+        #
+        # This helper also sets `self.applicant`, which is the
+        # applicant object created.
+        self.browser.open(self.login_path)
+        ac_series = self.browser.getControl(name="form.ac_series")
+        ac_series.value = self.existing_series
+        ac_number = self.browser.getControl(name="form.ac_number")
+        ac_number.value = self.existing_number
+        self.browser.getControl(name="SUBMIT").click()
+        self.applicant = self.app['applicants']['app2009'][self.pin]
+
+    def fill_correct_values(self):
+        # Fill the edit form with suitable values
+        self.browser.getControl(name="form.firstname").value = 'John'
+        self.browser.getControl(name="form.lastname").value = 'Tester'
+        self.browser.getControl(name="form.course1").value = ['CERT1']
+        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
+        self.browser.getControl(name="form.lga").value = ['foreigner']
+        self.browser.getControl(name="form.sex").value = ['m']
+
+    def image_url(self, filename):
+        return self.edit_path.replace('edit', filename)
+
+    def test_after_login_default_browsable(self):
+        # After login we see the placeholder image in the edit view
+        self.login()
+        self.assertEqual(self.browser.url, self.edit_path)
+        # There is a correct <img> link included
+        self.assertTrue(
+            '<img src="placeholder_m.jpg" />' in self.browser.contents)
+        # Browsing the link shows a real image
+        self.browser.open(self.image_url('placeholder_m.jpg'))
+        self.assertEqual(
+            self.browser.headers['content-type'], 'image/jpeg')
+        self.assertEqual(len(self.browser.contents), PH_LEN)
+
+    def test_after_login_default_stored_in_imagestorage(self):
+        # After login the applicants placeholder image is stored in
+        # an imagestorage
+        storage = self.app['images']
+        # In the beginning, the storage is empty
+        self.assertEqual([x for x in storage.keys()], [])
+        self.login()
+        # After login, it is filled
+        self.assertEqual(
+            [x for x in storage.keys()],
+            [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])
+        file_id = self.applicant.passport.data
+        self.assertEqual(
+            file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')
+        # The stored image can be fetched
+        fd = storage.retrieveFile(file_id)
+        file_len = len(fd.read())
+        self.assertEqual(file_len, PH_LEN)
+
+    def test_after_submit_default_browsable(self):
+        # After submitting an applicant form the default image is
+        # still visible
+        self.login()
+        self.browser.getControl("Save").click() # submit form
+        # There is a correct <img> link included
+        self.assertTrue(
+            '<img src="placeholder_m.jpg" />' in self.browser.contents)
+        # Browsing the link shows a real image
+        self.browser.open(self.image_url('placeholder_m.jpg'))
+        self.assertEqual(
+            self.browser.headers['content-type'], 'image/jpeg')
+        self.assertEqual(len(self.browser.contents), PH_LEN)
+
+    def test_after_submit_default_stored_in_imagestorage(self):
+        # After submitting an applicant form the default image is
+        # correctly stored in an imagestorage
+        self.login()
+        self.browser.getControl("Save").click() # submit form
+        storage = self.app['images']
+        self.assertEqual(
+            [x for x in storage.keys()],
+            [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])
+        file_id = self.applicant.passport.data
+        self.assertEqual(
+            file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')
+        # The stored image can be fetched
+        fd = storage.retrieveFile(file_id)
+        file_len = len(fd.read())
+        self.assertEqual(file_len, PH_LEN)
+
+    # XXX: Make this test work again
+    def DISABLEDtest_uploaded_image_browsable_w_errors(self):
+        # We can upload a different image and browse it after submit,
+        # even if there are still errors in the form
+        self.login()
+        # Create a pseudo image file and select it to be uploaded in form
+        pseudo_image = StringIO('I pretend to be a graphics file')
+        ctrl = self.browser.getControl(name='form.passport')
+        file_ctrl = ctrl.mech_control
+        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
+        self.browser.getControl("Save").click() # submit form
+        # There is a correct <img> link included
+        self.assertTrue(
+            '<img src="myphoto.jpg" />' in self.browser.contents)
+        # Browsing the link shows a real image
+        self.browser.open(self.image_url('myphoto.jpg'))
+        self.assertEqual(
+            self.browser.headers['content-type'], 'image/jpeg')
+        self.assertEqual(len(self.browser.contents), PH_LEN)
+
+    def test_uploaded_image_stored_in_imagestorage_w_errors(self):
+        # After uploading a new passport pic the file is correctly
+        # stored in an imagestorage
+        self.login()
+        # Create a pseudo image file and select it to be uploaded in form
+        pseudo_image = StringIO('I pretend to be a graphics file')
+        ctrl = self.browser.getControl(name='form.passport')
+        file_ctrl = ctrl.mech_control
+        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
+        self.browser.getControl("Save").click() # submit form
+        storage = self.app['images']
+        self.assertTrue(
+            u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())
+        # The stored image can be fetched
+        fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
+        #fd = storage.retrieveFile(file_id)
+        file_len = len(fd.read())
+        self.assertEqual(file_len, 31)
+        # The image uploaded belongs to the applicant
+        # XXX: When there are errors in form, then the applicant is not
+        #      changed. This (failing) test is here to remember us that
+        #      we have to care for images when form submits fail.
+        file_id = self.applicant.passport.data
+        self.assertEqual(
+            file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
+
+    def test_uploaded_image_browsable_wo_errors(self):
+        # We can upload a different image and browse it after submit,
+        # if there are no errors in form
+        self.login()
+        self.fill_correct_values() # fill other fields with correct values
+        # Create a pseudo image file and select it to be uploaded in form
+        pseudo_image = StringIO('I pretend to be a graphics file')
+        ctrl = self.browser.getControl(name='form.passport')
+        file_ctrl = ctrl.mech_control
+        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
+        self.browser.getControl("Save").click() # submit form
+        # There is a correct <img> link included
+        self.assertTrue(
+            '<img src="myphoto.jpg" />' in self.browser.contents)
+        # Browsing the link shows a real image
+        self.browser.open(self.image_url('myphoto.jpg'))
+        self.assertEqual(
+            self.browser.headers['content-type'], 'image/jpeg')
+        self.assertEqual(len(self.browser.contents), 31)
+
+    def test_uploaded_image_stored_in_imagestorage_wo_errors(self):
+        # After uploading a new passport pic the file is correctly
+        # stored in an imagestorage if form contains no errors
+        self.login()
+        self.fill_correct_values() # fill other fields with correct values
+        # Create a pseudo image file and select it to be uploaded in form
+        pseudo_image = StringIO('I pretend to be a graphics file')
+        ctrl = self.browser.getControl(name='form.passport')
+        file_ctrl = ctrl.mech_control
+        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
+        self.browser.getControl("Save").click() # submit form
+        storage = self.app['images']
+        self.assertTrue(
+            u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())
+        # The stored image can be fetched
+        fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
+        #fd = storage.retrieveFile(file_id)
+        file_len = len(fd.read())
+        self.assertEqual(file_len, 31)
+        # The image uploaded belongs to the applicant
+        file_id = self.applicant.passport.data
+        self.assertEqual(
+            file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
