Changeset 7063 for main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests
- Timestamp:
- 9 Nov 2011, 15:42:45 (13 years ago)
- Location:
- main/waeup.sirp/trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
main/waeup.sirp/trunk
- Property svn:mergeinfo changed
/main/waeup.sirp/branches/ulif-extimgstore (added) merged: 7001-7002,7010-7011,7016,7031-7041,7043-7044,7046-7055
- Property svn:mergeinfo changed
-
main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_applicant.py
r6657 r7063 22 22 """Tests for applicants and related. 23 23 """ 24 import grok 24 25 import unittest 25 from StringIO import StringIO 26 from hurry.file.interfaces import IFileRetrieval 27 from zope.component import ( 28 provideAdapter, adapts, getGlobalSiteManager, provideUtility) 29 from zope.component.hooks import setSite 26 from zope.component import adapts, queryUtility 30 27 from zope.component.interfaces import IFactory 31 28 from zope.interface import verify, implements 32 29 from zope.location.interfaces import ILocation 33 from zope.publisher.base import TestRequest34 from zope.publisher.interfaces import NotFound35 from waeup.sirp.app import University36 from waeup.sirp.image import WAeUPImageFile, createWAeUPImageFile37 30 from waeup.sirp.image.interfaces import IWAeUPImageFile 31 from waeup.sirp.imagestorage import DefaultStorage 32 from waeup.sirp.interfaces import IFileStoreHandler, IFileStoreNameChooser 38 33 from waeup.sirp.applicants import ( 39 ResultEntry, Applicant, ApplicantFactory, ApplicantTraverser, 34 ResultEntry, Applicant, ApplicantFactory, get_regno_or_ac, 35 ApplicantImageStoreHandler, ApplicantImageNameChooser, 40 36 ) 41 from waeup.sirp.applicants.interfaces import ( 42 IResultEntry, IApplicant, 43 ) 44 from waeup.sirp.imagestorage import ImageStorageFileRetrieval 45 from waeup.sirp.testing import (FunctionalTestCase, FunctionalLayer) 37 from waeup.sirp.applicants.interfaces import IResultEntry, IApplicant 38 from waeup.sirp.testing import FunctionalTestCase, FunctionalLayer 46 39 47 40 class FakeImageLocation(object): … … 51 44 pass 52 45 46 class HelperTests(FunctionalTestCase): 47 48 layer = FunctionalLayer 49 50 def test_get_regno_or_ac(self): 51 # we can get reg_no or access_code of an applicants if it is set 52 appl1 = Applicant() 53 appl2 = Applicant() 54 appl2.reg_no = u'foo' 55 appl3 = Applicant() 56 appl3.access_code = u'bar' 57 appl4 = Applicant() 58 appl4.reg_no = u'foo' 59 appl4.access_code = u'bar' 60 self.assertTrue( 61 get_regno_or_ac(appl1) is None) 62 self.assertEqual( 63 get_regno_or_ac(appl2), u'foo') 64 self.assertEqual( 65 get_regno_or_ac(appl3), u'bar') 66 self.assertEqual( 67 get_regno_or_ac(appl4), u'foo') 68 return 69 70 def test_image_store_handler_util_accessible(self): 71 # we can get an IFileStoreHandler utility for applicants 72 handler = queryUtility(IFileStoreHandler, name='img-applicant') 73 self.assertTrue( 74 isinstance(handler, ApplicantImageStoreHandler)) 75 return 76 77 def test_image_store_handler(self): 78 store = DefaultStorage() 79 handler = queryUtility(IFileStoreHandler, name='img-applicant') 80 result1 = handler.pathFromFileID( 81 store, '/fake-root', '__img-applicant__sample.jpg') 82 result2 = handler.pathFromFileID( 83 store, '/fake-root', '__img-applicant__dir1/sample.jpg') 84 result3 = handler.pathFromFileID( 85 store, '/fake-root', '__img-applicant__dir1/dir2/sample.jpg') 86 self.assertEqual( 87 result1, '/fake-root/applicants/sample.jpg') 88 self.assertEqual( 89 result2, '/fake-root/applicants/dir1/sample.jpg') 90 self.assertEqual( 91 result3, '/fake-root/applicants/dir1/dir2/sample.jpg') 92 return 93 94 class ApplicantImageNameChooserTests(FunctionalTestCase): 95 96 layer = FunctionalLayer 97 98 def test_iface(self): 99 # make sure we implement promised interfaces 100 obj = ApplicantImageNameChooser(None) # needs a context normally 101 verify.verifyClass(IFileStoreNameChooser, ApplicantImageNameChooser) 102 verify.verifyObject(IFileStoreNameChooser, obj) 103 return 104 105 def test_name_chooser_available(self): 106 # we can get a name chooser for applicant objects as adapter 107 appl = Applicant() 108 chooser = IFileStoreNameChooser(appl) 109 self.assertTrue(chooser is not None) 110 return 111 112 def test_name_chooser_applicant_wo_container(self): 113 # we can get an image filename for applicants not in a container 114 appl = Applicant() 115 appl.reg_no = u'MY_REG_NO' 116 chooser = IFileStoreNameChooser(appl) 117 result = chooser.chooseName() 118 # the file would be stored in a ``_default`` directory. 119 self.assertEqual( 120 result, '__img-applicant___default/MY_REG_NO.jpg') 121 return 122 123 def test_name_chooser_applicant_w_container(self): 124 appl = Applicant() 125 appl.reg_no = u'MY_REG_NO' 126 fake_container = grok.Container() 127 fake_container.__name__ = 'folder' 128 fake_container['appl'] = appl 129 appl.__parent__ = fake_container 130 chooser = IFileStoreNameChooser(appl) 131 result = chooser.chooseName() 132 self.assertEqual( 133 result, '__img-applicant__folder/MY_REG_NO.jpg') 134 return 135 136 def test_name_chooser_check_name(self): 137 # we can check file ids for applicants 138 appl = Applicant() 139 appl.reg_no = u'MY_REG_NO' 140 fake_container = grok.Container() 141 fake_container.__name__ = 'folder' 142 fake_container['appl'] = appl 143 appl.__parent__ = fake_container 144 chooser = IFileStoreNameChooser(appl) 145 result1 = chooser.checkName('foo') 146 result2 = chooser.checkName('__img-applicant__folder/MY_REG_NO.jpg') 147 self.assertEqual(result1, False) 148 self.assertEqual(result2, True) 149 return 150 53 151 class ResultEntryTest(unittest.TestCase): 54 152 … … 87 185 return 88 186 89 def test_passport_no_site(self):90 # make sure we get a real image stored in passport attr91 image = self.applicant.passport.file.read()92 self.assertEqual(len(image), 2059)93 return94 95 def test_passport_insite(self):96 # make sure we get a real image in passport attr when inside a site.97 # When an applicant is created 'inside a site', its passport98 # photograph should be put inside the images folder of the99 # site, even if it is only a placeholder.100 self.getRootFolder()['app'] = University()101 app = self.getRootFolder()['app']102 setSite(app)103 applicant = Applicant()104 image = self.applicant.passport.file.read()105 self.assertEqual(len(image), 2059)106 # The image contains a file_id instead of real image-data107 self.assertEqual(self.applicant.passport.data,108 u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')109 assert u'b48a1d39bbcb32e955d9ff2dea4ed0e6' in app['images'].keys()110 return111 112 187 class ApplicantFactoryTest(FunctionalTestCase): 113 188 … … 116 191 def setUp(self): 117 192 super(ApplicantFactoryTest, self).setUp() 118 # Install a IFileRetrieval utility that returns WAeUPImageFiles.119 storage = ImageStorageFileRetrieval()120 provideUtility(storage, IFileRetrieval)121 193 self.factory = ApplicantFactory() 122 194 return … … 137 209 implemented_by = self.factory.getInterfaces() 138 210 assert implemented_by.isOrExtends(IApplicant) 139 140 141 class ApplicantTraverserTest(FunctionalTestCase):142 143 layer = FunctionalLayer144 145 def setUp(self):146 super(ApplicantTraverserTest, self).setUp()147 provideAdapter(FakeImageLocation)148 self.applicant = Applicant()149 150 self.request = TestRequest('')151 return152 153 def tearDown(self):154 gsm = getGlobalSiteManager()155 gsm.unregisterAdapter(FakeImageLocation)156 super(ApplicantTraverserTest, self).tearDown()157 return158 159 def test_traverse_wo_passport(self):160 # Ask for some attribute not provided161 traverser = ApplicantTraverser(162 self.applicant, self.request163 )164 self.assertRaises(165 NotFound,166 traverser.publishTraverse, self.request, 'passport'167 )168 return169 170 def test_traverse_wo_image_passport_jpg(self):171 # Ask for applicant pic if we didn't provided one172 # We get a placeholder.173 traverser = ApplicantTraverser(174 self.applicant, self.request175 )176 result = traverser.publishTraverse(self.request, 'passport.jpg')177 self.assertTrue(isinstance(result, FakeImageLocation))178 return179 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.request184 )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 return191 192 def test_traverse_w_image_some_jpg(self):193 # Ask for applicant pic by correct name194 traverser = ApplicantTraverser(195 self.applicant, self.request196 )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/trunk/src/waeup/sirp/applicants/tests/test_authentication.py
r6730 r7063 24 24 import unittest 25 25 from zope.authentication.interfaces import IAuthentication 26 from zope.component import provideAdapter, getUtility 26 from zope.component import provideAdapter, getUtility, queryUtility 27 27 from zope.component.hooks import setSite 28 28 from zope.interface import verify … … 35 35 from waeup.sirp.testing import FunctionalLayer, FunctionalTestCase 36 36 from waeup.sirp.app import University 37 from waeup.sirp.interfaces import IAuthPluginUtility 37 38 from waeup.sirp.applicants import ApplicantsContainer, Applicant 38 39 from waeup.sirp.applicants.authentication import ( 39 40 ApplicantsAuthenticatorPlugin, WAeUPApplicantCredentialsPlugin, 40 41 ApplicantCredentials, AuthenticatedApplicantPrincipalFactory, 41 ApplicantPrincipalInfo, ApplicantPrincipal, )42 ApplicantPrincipalInfo, ApplicantPrincipal, ApplicantsAuthUtility) 42 43 43 44 … … 367 368 return 368 369 369 def test_suite(): 370 suite = unittest.TestSuite() 371 for testcase in [ 372 AuthenticatorPluginTest, CredentialsPluginTest, 373 ApplicantCredentialsTest, PrincipalFactoryTest, 374 PAUSetupTest, 375 ]: 376 suite.addTest(unittest.TestLoader().loadTestsFromTestCase( 377 testcase 378 ) 379 ) 380 return suite 370 class FakePAU(object): 371 credentialsPlugins = () 372 authenticatorPlugins = () 373 374 class ApplicantsAuthUtilityTests(FunctionalTestCase): 375 376 layer = FunctionalLayer 377 378 def test_ifaces(self): 379 # make sure we fullfill the interface promises 380 obj = ApplicantsAuthUtility() 381 verify.verifyClass(IAuthPluginUtility, ApplicantsAuthUtility) 382 verify.verifyObject(IAuthPluginUtility, obj) 383 return 384 385 def test_utility_available(self): 386 # we can get an applicant auth utility by lookup 387 util = queryUtility(IAuthPluginUtility, 388 name='applicants_auth_setup') 389 self.assertTrue(util is not None) 390 return 391 392 def test_register(self): 393 # make sure we can register additional components 394 pau = FakePAU() 395 result = ApplicantsAuthUtility().register(pau) 396 self.assertEqual( 397 result.credentialsPlugins, ('applicant_credentials',)) 398 self.assertEqual( 399 result.authenticatorPlugins, ('applicants',)) 400 return 401 402 def test_unregister(self): 403 # make sure we can unregister applicant auth components 404 pau = FakePAU() 405 util = ApplicantsAuthUtility() 406 pau = util.register(pau) 407 result = util.unregister(pau) 408 self.assertEqual( 409 result.credentialsPlugins, ()) 410 self.assertEqual( 411 result.authenticatorPlugins, ()) 412 return -
main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_browser.py
r6816 r7063 28 28 from datetime import datetime, date, timedelta 29 29 from mechanize import LinkNotFoundError 30 from zope.component import createObject 30 from zope.component import createObject, getUtility 31 31 from zope.component.hooks import setSite, clearSite 32 32 from zope.security.interfaces import Unauthorized … … 37 37 from waeup.sirp.applicants.container import ApplicantsContainer 38 38 from waeup.sirp.applicants.applicant import Applicant 39 from waeup.sirp.interfaces import IExtFileStore, IFileStoreNameChooser 39 40 from waeup.sirp.university.faculty import Faculty 40 41 from waeup.sirp.university.department import Department … … 350 351 self.browser.getControl(name="SUBMIT").click() 351 352 pin = self.pins[2] 352 appl = self.getRootFolder()['app']['applicants']['app2009']353 appl = appl[pin]354 passp = appl.passport355 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) 357 358 #image_url = "%s/%s" % (self.browser.url, 'placeholder.jpg') 358 359 image_url = "%s/%s" % (self.browser.url, 'passport.jpg') … … 639 640 # There is a correct <img> link included 640 641 self.assertTrue( 641 '<img src="p laceholder_m.jpg" />' in self.browser.contents)642 '<img src="passport.jpg" />' in self.browser.contents) 642 643 # Browsing the link shows a real image 643 self.browser.open(self.image_url('p laceholder_m.jpg'))644 self.browser.open(self.image_url('passport.jpg')) 644 645 self.assertEqual( 645 646 self.browser.headers['content-type'], 'image/jpeg') 646 647 self.assertEqual(len(self.browser.contents), PH_LEN) 647 648 648 def test_after_login_default_stored_in_imagestorage(self):649 def DISABLEDtest_after_login_default_stored_in_imagestorage(self): 649 650 # After login the applicants placeholder image is stored in 650 651 # an imagestorage … … 672 673 # There is a correct <img> link included 673 674 self.assertTrue( 674 '<img src="p laceholder_m.jpg" />' in self.browser.contents)675 '<img src="passport.jpg" />' in self.browser.contents) 675 676 # Browsing the link shows a real image 676 self.browser.open(self.image_url('p laceholder_m.jpg'))677 self.browser.open(self.image_url('passport.jpg')) 677 678 self.assertEqual( 678 679 self.browser.headers['content-type'], 'image/jpeg') 679 680 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 is683 # correctly stored in an imagestorage684 self.login()685 self.browser.getControl("Save").click() # submit form686 storage = self.app['images']687 self.assertEqual(688 [x for x in storage.keys()],689 [u'b48a1d39bbcb32e955d9ff2dea4ed0e6'])690 file_id = self.applicant.passport.data691 self.assertEqual(692 file_id, u'b48a1d39bbcb32e955d9ff2dea4ed0e6-1')693 # The stored image can be fetched694 fd = storage.retrieveFile(file_id)695 file_len = len(fd.read())696 self.assertEqual(file_len, PH_LEN)697 681 698 682 def test_uploaded_image_browsable_w_errors(self): … … 709 693 # There is a correct <img> link included 710 694 self.assertTrue( 711 '<img src=" myphoto.jpg" />' in self.browser.contents)695 '<img src="passport.jpg" />' in self.browser.contents) 712 696 # 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')) 714 698 self.assertEqual( 715 699 self.browser.headers['content-type'], 'image/jpeg') … … 726 710 file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') 727 711 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 737 715 self.assertEqual( 738 file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1') 716 storage.getFile(file_id).read(), pseudo_image.read()) 717 return 739 718 740 719 def test_uploaded_image_browsable_wo_errors(self): … … 751 730 # There is a correct <img> link included 752 731 self.assertTrue( 753 '<img src=" myphoto.jpg" />' in self.browser.contents)732 '<img src="passport.jpg" />' in self.browser.contents) 754 733 # 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')) 756 735 self.assertEqual( 757 736 self.browser.headers['content-type'], 'image/jpeg') … … 769 748 file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') 770 749 self.browser.getControl("Save").click() # submit form 771 storage = self.app['images'] 772 self.assertTrue( 773 u'18e57c7eac6ca7fb15b54b5b2bd4106d' in storage.keys()) 750 storage = getUtility(IExtFileStore) 751 file_id = IFileStoreNameChooser(self.applicant).chooseName() 774 752 # The stored image can be fetched 775 fd = storage.retrieveFile(u'18e57c7eac6ca7fb15b54b5b2bd4106d-1') 776 #fd = storage.retrieveFile(file_id) 753 fd = storage.getFile(file_id) 777 754 file_len = len(fd.read()) 778 755 self.assertEqual(file_len, 31) 779 # The image uploaded belongs to the applicant780 file_id = self.applicant.passport.data781 self.assertEqual(782 file_id, u'18e57c7eac6ca7fb15b54b5b2bd4106d-1')783 756 784 757 def test_uploaded_images_equal(self): … … 797 770 file_ctrl = ctrl.mech_control 798 771 file_ctrl.add_file(pseudo_image, filename='myphoto.jpg') 799 passport0 = self.applicant.passport 772 file_id = IFileStoreNameChooser(self.applicant).chooseName() 773 setSite(self.app) 774 passport0 = getUtility(IExtFileStore).getFile(file_id) 800 775 self.browser.getControl("Save").click() # submit form with changed pic 801 passport1 = self.applicant.passport776 passport1 = getUtility(IExtFileStore).getFile(file_id).read() 802 777 self.browser.getControl("Save").click() # submit form w/o changes 803 passport2 = self.applicant.passport 778 passport2 = getUtility(IExtFileStore).getFile(file_id).read() 779 self.assertTrue(passport0 is None) 804 780 self.assertTrue(passport0 != passport1) 805 781 self.assertTrue(passport1 == passport2) 806 self.assertTrue(passport1 is passport2)807 782 return 808 783 -
main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_interfaces.py
r6340 r7063 25 25 import unittest 26 26 from zc.sourcefactory.browser.source import FactoredTerms 27 from zope.browser.interfaces import ITerms28 from zope.component import getMultiAdapter29 from zope.interface.verify import verifyClass, verifyObject30 27 from zope.publisher.browser import TestRequest 31 from waeup.sirp.applicants import interfaces 32 from waeup.sirp.applicants.vocabularies import ( 33 APPLICATION_TYPES, application_types_vocab, GenderSource, 34 ) 28 from waeup.sirp.applicants.vocabularies import application_types_vocab 29 from waeup.sirp.students.vocabularies import GenderSource 35 30 36 31 class ApplicationCategoriesTestCase(unittest.TestCase): -
main/waeup.sirp/trunk/src/waeup/sirp/applicants/tests/test_root.py
r6659 r7063 39 39 ) 40 40 from waeup.sirp.testing import ( 41 FunctionalLayer, FunctionalTestCase, get_all_loggers, remove_new_loggers, 42 remove_logger) 41 FunctionalLayer, FunctionalTestCase, remove_logger) 43 42 44 43
Note: See TracChangeset for help on using the changeset viewer.