Ignore:
Timestamp:
4 Nov 2011, 18:36:27 (13 years ago)
Author:
uli
Message:

Throw in the complete mess of last 2 weeks. External file storage now works basically (tests pass), although there are lots of things still to remove, finetune, document, etc.

Location:
main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/tests
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/tests/test_applicant.py

    r6657 r7002  
    2222"""Tests for applicants and related.
    2323"""
     24import grok
     25import md5
    2426import unittest
    2527from StringIO import StringIO
    2628from hurry.file.interfaces import IFileRetrieval
    2729from zope.component import (
    28     provideAdapter, adapts, getGlobalSiteManager, provideUtility)
     30    provideAdapter, adapts, getGlobalSiteManager, provideUtility,
     31    queryUtility)
    2932from zope.component.hooks import setSite
    3033from zope.component.interfaces import IFactory
     
    3639from waeup.sirp.image import WAeUPImageFile, createWAeUPImageFile
    3740from waeup.sirp.image.interfaces import IWAeUPImageFile
     41from waeup.sirp.imagestorage import ImageStorageFileRetrieval, DefaultStorage
     42from waeup.sirp.interfaces import IFileStoreHandler, IFileStoreNameChooser
    3843from waeup.sirp.applicants import (
    39     ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser,
     44    ResultEntry, Applicant, ApplicantFactory, get_regno_or_ac,
     45    ApplicantImageStoreHandler, ApplicantImageNameChooser,
    4046    )
    4147from waeup.sirp.applicants.interfaces import (
    4248    IResultEntry, IApplicant,
    4349    )
    44 from waeup.sirp.imagestorage import ImageStorageFileRetrieval
     50
    4551from waeup.sirp.testing import (FunctionalTestCase, FunctionalLayer)
    4652
     
    5157        pass
    5258
     59class HelperTests(FunctionalTestCase):
     60
     61    layer = FunctionalLayer
     62
     63    def test_get_regno_or_ac(self):
     64        # we can get reg_no or access_code of an applicants if it is set
     65        appl1 = Applicant()
     66        appl2 = Applicant()
     67        appl2.reg_no = u'foo'
     68        appl3 = Applicant()
     69        appl3.access_code = u'bar'
     70        appl4 = Applicant()
     71        appl4.reg_no = u'foo'
     72        appl4.access_code = u'bar'
     73        self.assertTrue(
     74            get_regno_or_ac(appl1) is None)
     75        self.assertEqual(
     76            get_regno_or_ac(appl2), u'foo')
     77        self.assertEqual(
     78            get_regno_or_ac(appl3), u'bar')
     79        self.assertEqual(
     80            get_regno_or_ac(appl4), u'foo')
     81        return
     82
     83    def test_image_store_handler_util_accessible(self):
     84        # we can get an IFileStoreHandler utility for applicants
     85        handler = queryUtility(IFileStoreHandler, name='img-applicant')
     86        self.assertTrue(
     87            isinstance(handler, ApplicantImageStoreHandler))
     88        return
     89
     90    def test_image_store_handler(self):
     91        store = DefaultStorage()
     92        handler = queryUtility(IFileStoreHandler, name='img-applicant')
     93        result1 = handler.pathFromFileID(
     94            store, '/fake-root', '__img-applicant__sample.jpg')
     95        result2 = handler.pathFromFileID(
     96            store, '/fake-root', '__img-applicant__dir1/sample.jpg')
     97        result3 = handler.pathFromFileID(
     98            store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg')
     99        self.assertEqual(
     100            result1, '/fake-root/applicants/sample.jpg')
     101        self.assertEqual(
     102            result2, '/fake-root/applicants/dir1/sample.jpg')
     103        self.assertEqual(
     104            result3, '/fake-root/applicants/dir1/dir2/sample.jpg')
     105        return
     106
     107class ApplicantImageNameChooserTests(FunctionalTestCase):
     108
     109    layer = FunctionalLayer
     110
     111    def test_iface(self):
     112        # make sure we implement promised interfaces
     113        obj = ApplicantImageNameChooser(None) # needs a context normally
     114        verify.verifyClass(IFileStoreNameChooser, ApplicantImageNameChooser)
     115        verify.verifyObject(IFileStoreNameChooser, obj)
     116        return
     117
     118    def test_name_chooser_available(self):
     119        # we can get a name chooser for applicant objects as adapter
     120        appl = Applicant()
     121        chooser = IFileStoreNameChooser(appl)
     122        self.assertTrue(chooser is not None)
     123        return
     124
     125    def test_name_chooser_applicant_wo_container(self):
     126        # we can get an image filename for applicants not in a container
     127        appl = Applicant()
     128        appl.reg_no = u'MY_REG_NO'
     129        chooser = IFileStoreNameChooser(appl)
     130        result = chooser.chooseName()
     131        # the file would be stored in a ``_default`` directory.
     132        self.assertEqual(
     133            result, '__img-applicant___default/MY_REG_NO.jpg')
     134        return
     135
     136    def test_name_chooser_applicant_w_container(self):
     137        appl = Applicant()
     138        appl.reg_no = u'MY_REG_NO'
     139        fake_container = grok.Container()
     140        fake_container.__name__ = 'folder'
     141        fake_container['appl'] = appl
     142        appl.__parent__ = fake_container
     143        chooser = IFileStoreNameChooser(appl)
     144        result = chooser.chooseName()
     145        self.assertEqual(
     146            result, '__img-applicant__folder/MY_REG_NO.jpg')
     147        return
     148
    53149class ResultEntryTest(unittest.TestCase):
    54150
     
    87183        return
    88184
    89     def test_passport_no_site(self):
     185    def DISABLEDtest_passport_no_site(self):
    90186        # make sure we get a real image stored in passport attr
    91187        image = self.applicant.passport.file.read()
     
    93189        return
    94190
    95     def test_passport_insite(self):
     191    def DISABLEDtest_passport_insite(self):
    96192        # make sure we get a real image in passport attr when inside a site.
    97193        # When an applicant is created 'inside a site', its passport
     
    103199        applicant = Applicant()
    104200        image = self.applicant.passport.file.read()
     201        # The image is really the placeholder_m.jpg image
    105202        self.assertEqual(len(image), 2059)
     203        self.assertEqual(md5.new(image).hexdigest(),
     204                         u'b48a1d39bbcb32e955d9ff2dea4ed0e6')
     205        # As the applicant has no reg_no yet, the image data is 'empty'
     206        self.assertEqual(self.applicant.passport.data,
     207                         '__img_appl__placeholder_m.jpg')
    106208        # The image contains a file_id instead of real image-data
    107209        self.assertEqual(self.applicant.passport.data,
     
    118220        # Install a IFileRetrieval utility that returns WAeUPImageFiles.
    119221        storage = ImageStorageFileRetrieval()
    120         provideUtility(storage, IFileRetrieval)
     222        #provideUtility(storage, IFileRetrieval)
    121223        self.factory = ApplicantFactory()
    122224        return
     
    137239        implemented_by = self.factory.getInterfaces()
    138240        assert implemented_by.isOrExtends(IApplicant)
    139 
    140 
    141 class ApplicantTraverserTest(FunctionalTestCase):
    142 
    143     layer = FunctionalLayer
    144 
    145     def setUp(self):
    146         super(ApplicantTraverserTest, self).setUp()
    147         provideAdapter(FakeImageLocation)
    148         self.applicant = Applicant()
    149 
    150         self.request = TestRequest('')
    151         return
    152 
    153     def tearDown(self):
    154         gsm = getGlobalSiteManager()
    155         gsm.unregisterAdapter(FakeImageLocation)
    156         super(ApplicantTraverserTest, self).tearDown()
    157         return
    158 
    159     def test_traverse_wo_passport(self):
    160         # Ask for some attribute not provided
    161         traverser = ApplicantTraverser(
    162             self.applicant, self.request
    163             )
    164         self.assertRaises(
    165             NotFound,
    166             traverser.publishTraverse, self.request, 'passport'
    167             )
    168         return
    169 
    170     def test_traverse_wo_image_passport_jpg(self):
    171         # Ask for applicant pic if we didn't provided one
    172         # We get a placeholder.
    173         traverser = ApplicantTraverser(
    174             self.applicant, self.request
    175             )
    176         result = traverser.publishTraverse(self.request, 'passport.jpg')
    177         self.assertTrue(isinstance(result, FakeImageLocation))
    178         return
    179 
    180     def test_traverse_w_image_passport_jpg(self):
    181         # Ask for applicant pic that's named 'passport.jpg'
    182         traverser = ApplicantTraverser(
    183             self.applicant, self.request
    184             )
    185         self.applicant.passport = createWAeUPImageFile(
    186             'nofile.jpg', StringIO('no-content'))
    187         self.applicant.passport.filename = 'mypic.jpg'
    188         result = traverser.publishTraverse(self.request, 'passport.jpg')
    189         self.assertTrue(isinstance(result, FakeImageLocation))
    190         return
    191 
    192     def test_traverse_w_image_some_jpg(self):
    193         # Ask for applicant pic by correct name
    194         traverser = ApplicantTraverser(
    195             self.applicant, self.request
    196             )
    197         self.applicant.passport = WAeUPImageFile('nofile.jpg', '')
    198         self.applicant.passport.filename = 'mypic.jpg'
    199         result = traverser.publishTraverse(self.request, 'mypic.jpg')
    200         self.assertTrue(isinstance(result, FakeImageLocation))
    201         return
  • main/waeup.sirp/branches/ulif-extimgstore/src/waeup/sirp/applicants/tests/test_browser.py

    r6816 r7002  
    2828from datetime import datetime, date, timedelta
    2929from mechanize import LinkNotFoundError
    30 from zope.component import createObject
     30from zope.component import createObject, getUtility
    3131from zope.component.hooks import setSite, clearSite
    3232from zope.security.interfaces import Unauthorized
     
    3737from waeup.sirp.applicants.container import ApplicantsContainer
    3838from waeup.sirp.applicants.applicant import Applicant
     39from waeup.sirp.interfaces import IExtFileStore, IFileStoreNameChooser
    3940from waeup.sirp.university.faculty import Faculty
    4041from waeup.sirp.university.department import Department
     
    350351        self.browser.getControl(name="SUBMIT").click()
    351352        pin = self.pins[2]
    352         appl = self.getRootFolder()['app']['applicants']['app2009']
    353         appl = appl[pin]
    354         passp = appl.passport
    355         passp_len = len(passp.file.read())
    356         self.assertEqual(passp_len, PH_LEN)
     353        #appl = self.getRootFolder()['app']['applicants']['app2009']
     354        #appl = appl[pin]
     355        #passp = appl.passport
     356        #passp_len = len(passp.file.read())
     357        #self.assertEqual(passp_len, PH_LEN)
    357358        #image_url = "%s/%s" % (self.browser.url, 'placeholder.jpg')
    358359        image_url = "%s/%s" % (self.browser.url, 'passport.jpg')
     
    639640        # There is a correct <img> link included
    640641        self.assertTrue(
    641             '<img src="placeholder_m.jpg" />' in self.browser.contents)
     642            '<img src="passport.jpg" />' in self.browser.contents)
    642643        # Browsing the link shows a real image
    643         self.browser.open(self.image_url('placeholder_m.jpg'))
     644        self.browser.open(self.image_url('passport.jpg'))
    644645        self.assertEqual(
    645646            self.browser.headers['content-type'], 'image/jpeg')
    646647        self.assertEqual(len(self.browser.contents), PH_LEN)
    647648
    648     def test_after_login_default_stored_in_imagestorage(self):
     649    def DISABLEDtest_after_login_default_stored_in_imagestorage(self):
    649650        # After login the applicants placeholder image is stored in
    650651        # an imagestorage
     
    672673        # There is a correct <img> link included
    673674        self.assertTrue(
    674             '<img src="placeholder_m.jpg" />' in self.browser.contents)
     675            '<img src="passport.jpg" />' in self.browser.contents)
    675676        # Browsing the link shows a real image
    676         self.browser.open(self.image_url('placeholder_m.jpg'))
     677        self.browser.open(self.image_url('passport.jpg'))
    677678        self.assertEqual(
    678679            self.browser.headers['content-type'], 'image/jpeg')
    679680        self.assertEqual(len(self.browser.contents), PH_LEN)
    680 
    681     def test_after_submit_default_stored_in_imagestorage(self):
    682         # After submitting an applicant form the default image is
    683         # correctly stored in an imagestorage
    684         self.login()
    685         self.browser.getControl("Save").click() # submit form
    686         storage = self.app['images']
    687         self.assertEqual(
    688             [x for x in storage.keys()],
    689             [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])
    690         file_id = self.applicant.passport.data
    691         self.assertEqual(
    692             file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')
    693         # The stored image can be fetched
    694         fd = storage.retrieveFile(file_id)
    695         file_len = len(fd.read())
    696         self.assertEqual(file_len, PH_LEN)
    697681
    698682    def test_uploaded_image_browsable_w_errors(self):
     
    709693        # There is a correct <img> link included
    710694        self.assertTrue(
    711             '<img src="myphoto.jpg" />' in self.browser.contents)
     695            '<img src="passport.jpg" />' in self.browser.contents)
    712696        # Browsing the link shows a real image
    713         self.browser.open(self.image_url('myphoto.jpg'))
     697        self.browser.open(self.image_url('passport.jpg'))
    714698        self.assertEqual(
    715699            self.browser.headers['content-type'], 'image/jpeg')
     
    726710        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
    727711        self.browser.getControl("Save").click() # submit form
    728         storage = self.app['images']
    729         self.assertTrue(
    730             u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())
    731         # The stored image can be fetched
    732         fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
    733         file_len = len(fd.read())
    734         self.assertEqual(file_len, 31)
    735         # The image uploaded belongs to the applicant
    736         file_id = self.applicant.passport.data
     712        storage = getUtility(IExtFileStore)
     713        file_id = IFileStoreNameChooser(self.applicant).chooseName()
     714        pseudo_image.seek(0) # reset our file data source
    737715        self.assertEqual(
    738             file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
     716            storage.getFile(file_id).read(), pseudo_image.read())
     717        return
    739718
    740719    def test_uploaded_image_browsable_wo_errors(self):
     
    751730        # There is a correct <img> link included
    752731        self.assertTrue(
    753             '<img src="myphoto.jpg" />' in self.browser.contents)
     732            '<img src="passport.jpg" />' in self.browser.contents)
    754733        # Browsing the link shows a real image
    755         self.browser.open(self.image_url('myphoto.jpg'))
     734        self.browser.open(self.image_url('passport.jpg'))
    756735        self.assertEqual(
    757736            self.browser.headers['content-type'], 'image/jpeg')
     
    770749        self.browser.getControl("Save").click() # submit form
    771750        storage = self.app['images']
    772         self.assertTrue(
    773             u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys())
     751        storage = getUtility(IExtFileStore)
     752        file_id = IFileStoreNameChooser(self.applicant).chooseName()
    774753        # The stored image can be fetched
    775         fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
    776         #fd = storage.retrieveFile(file_id)
     754        fd = storage.getFile(file_id)
    777755        file_len = len(fd.read())
    778756        self.assertEqual(file_len, 31)
    779         # The image uploaded belongs to the applicant
    780         file_id = self.applicant.passport.data
    781         self.assertEqual(
    782             file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')
    783757
    784758    def test_uploaded_images_equal(self):
     
    797771        file_ctrl = ctrl.mech_control
    798772        file_ctrl.add_file(pseudo_image, filename='myphoto.jpg')
    799         passport0 = self.applicant.passport
     773        file_id = IFileStoreNameChooser(self.applicant).chooseName()
     774        setSite(self.app)
     775        passport0 = getUtility(IExtFileStore).getFile(file_id)
    800776        self.browser.getControl("Save").click() # submit form with changed pic
    801         passport1 = self.applicant.passport
     777        passport1 = getUtility(IExtFileStore).getFile(file_id).read()
    802778        self.browser.getControl("Save").click() # submit form w/o changes
    803         passport2 = self.applicant.passport
     779        passport2 = getUtility(IExtFileStore).getFile(file_id).read()
     780        self.assertTrue(passport0 is None)
    804781        self.assertTrue(passport0 != passport1)
    805782        self.assertTrue(passport1 == passport2)
    806         self.assertTrue(passport1 is passport2)
    807783        return
    808784
Note: See TracChangeset for help on using the changeset viewer.