source: main/waeup.fceokene/trunk/src/waeup/fceokene/applicants/tests.py @ 8599

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

See waeup.uniben.

  • Property svn:keywords set to Id
File size: 15.0 KB
RevLine 
[7866]1## $Id: tests.py 8587 2012-06-01 05:23:52Z 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##
18import os
19import shutil
20import tempfile
[8056]21import datetime
22from zope.intid.interfaces import IIntIds
[7866]23from zope.interface.verify import verifyClass, verifyObject
24from zope.component.hooks import setSite, clearSite
[8056]25from zope.component import createObject, getUtility
26from zope.catalog.interfaces import ICatalog
[8012]27from zope.testbrowser.testing import Browser
[7866]28from waeup.kofa.app import University
29from waeup.kofa.university.faculty import Faculty
30from waeup.kofa.university.department import Department
31from waeup.kofa.testing import FunctionalTestCase
[8528]32from waeup.kofa.configuration import SessionConfiguration
[8012]33from waeup.kofa.applicants.container import ApplicantsContainer
[8056]34from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
35from waeup.kofa.interfaces import IBatchProcessor
[8460]36from waeup.fceokene.testing import FunctionalLayer
37from waeup.fceokene.applicants.export import CustomApplicantsExporter
[8584]38from waeup.fceokene.applicants.batching import CustomApplicantProcessor
[7866]39
[8056]40
[8012]41class ApplicantUITest(FunctionalTestCase):
42    """Perform some browser tests.
[7866]43    """
44    layer = FunctionalLayer
45
46    def setUp(self):
[8012]47        super(ApplicantUITest, self).setUp()
[7866]48        # Setup a sample site for each test
49        app = University()
50        self.dc_root = tempfile.mkdtemp()
51        app['datacenter'].setStoragePath(self.dc_root)
52
53        # Prepopulate the ZODB...
54        self.getRootFolder()['app'] = app
55        # we add the site immediately after creation to the
56        # ZODB. Catalogs and other local utilities are not setup
57        # before that step.
58        self.app = self.getRootFolder()['app']
59        # Set site here. Some of the following setup code might need
60        # to access grok.getSite() and should get our new app then
61        setSite(app)
62
[8012]63        # Add an two different applicants containers
64        self.pgcontainer = ApplicantsContainer()
[8064]65        self.pgcontainer.code = u'pgft2011'
66        self.pgcontainer.prefix = u'pgft'
[8528]67        self.pgcontainer.year = 2011
68        self.pgcontainer.application_fee = 300.0
69        self.pgcontainer.title = u'This is the pgft2011 container'
[8064]70        self.app['applicants']['pgft2011'] = self.pgcontainer
[8012]71        self.ugcontainer = ApplicantsContainer()
[8528]72        self.ugcontainer.code = u'putme2011'
73        self.ugcontainer.prefix = u'putme'
74        self.ugcontainer.year = 2011
75        self.ugcontainer.application_fee = 200.0
76        self.ugcontainer.title = u'This is the app2011 container'
[8012]77        self.app['applicants']['app2011'] = self.ugcontainer
[7866]78
79        # Populate university
80        self.certificate = createObject('waeup.Certificate')
81        self.certificate.code = 'CERT1'
82        self.certificate.application_category = 'basic'
83        self.certificate.start_level = 100
84        self.certificate.end_level = 500
85        self.app['faculties']['fac1'] = Faculty()
86        self.app['faculties']['fac1']['dep1'] = Department()
87        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
88            self.certificate)
89
[8012]90        # Add (customized) applicants
91        pgapplicant = createObject(u'waeup.Applicant')
92        pgapplicant.firstname = u'Anna'
93        pgapplicant.lastname = u'Post'
[8064]94        self.app['applicants']['pgft2011'].addApplicant(pgapplicant)
[8012]95        self.pgapplication_number = pgapplicant.application_number
[8064]96        self.pgapplicant = self.app['applicants']['pgft2011'][
[8012]97            self.pgapplication_number]
[7866]98
[8012]99        ugapplicant = createObject(u'waeup.Applicant')
100        ugapplicant.firstname = u'Klaus'
101        ugapplicant.lastname = u'Under'
102        self.app['applicants']['app2011'].addApplicant(ugapplicant)
103        self.ugapplication_number = ugapplicant.application_number
104        self.ugapplicant = self.app['applicants']['app2011'][
105            self.ugapplication_number]
[8528]106        self.pgapplicant_path = ('http://localhost/app/applicants/pgft2011/%s'
107            % self.pgapplication_number)
108        self.ugapplicant_path = ('http://localhost/app/applicants/app2011/%s'
109            % self.ugapplication_number)
[8012]110
111        self.browser = Browser()
112        self.browser.handleErrors = False
113
[7866]114    def tearDown(self):
[8012]115        super(ApplicantUITest, self).tearDown()
[7866]116        shutil.rmtree(self.dc_root)
117        clearSite()
118        return
119
[8528]120    def fill_correct_values(self):
121        self.browser.getControl(name="form.reg_number").value = '1234'
122        self.browser.getControl(name="form.firstname").value = 'John'
123        self.browser.getControl(name="form.lastname").value = 'Tester'
124        self.browser.getControl(name="form.course1").value = ['CERT1']
125        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
126        self.browser.getControl(name="form.lga").value = ['foreigner']
127        self.browser.getControl(name="form.sex").value = ['m']
128        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
129
[8012]130    def test_manage_and_view_applicant(self):
131        # Managers can manage pg applicants
132        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
[8017]133        # The IPGApplicant interface is really used in all pages
[8528]134        self.browser.open(self.pgapplicant_path)
[8012]135        self.assertEqual(self.browser.headers['Status'], '200 Ok')
136        self.assertTrue('Employer' in self.browser.contents)
[8528]137        self.browser.open(self.pgapplicant_path + '/manage')
[8017]138        self.assertEqual(self.browser.headers['Status'], '200 Ok')
139        self.assertTrue('Employer' in self.browser.contents)
[8528]140        self.browser.open(self.pgapplicant_path + '/edit')
[8017]141        self.assertEqual(self.browser.headers['Status'], '200 Ok')
142        self.assertTrue('Employer' in self.browser.contents)
[8528]143        self.browser.open(self.pgapplicant_path + '/application_slip.pdf')
[8017]144        self.assertEqual(self.browser.headers['Status'], '200 Ok')
[8520]145        # If we view the applicant in the ug container,
[8017]146        # the employer field doesn't appear
[8528]147        self.browser.open(self.ugapplicant_path)
[8012]148        self.assertEqual(self.browser.headers['Status'], '200 Ok')
149        self.assertFalse('Employer' in self.browser.contents)
[8528]150        self.browser.open(self.ugapplicant_path + '/manage')
[8017]151        self.assertEqual(self.browser.headers['Status'], '200 Ok')
[8053]152        # We can save the applicant
[8528]153        self.fill_correct_values()
[8053]154        self.browser.getControl("Save").click()
155        self.assertMatches('...Form has been saved...', self.browser.contents)
[8017]156        self.assertFalse('Employer' in self.browser.contents)
[8528]157        self.browser.open(self.ugapplicant_path + '/edit')
[8017]158        self.assertEqual(self.browser.headers['Status'], '200 Ok')
159        self.assertFalse('Employer' in self.browser.contents)
[8528]160        self.browser.open(self.ugapplicant_path + '/application_slip.pdf')
[8017]161        self.assertEqual(self.browser.headers['Status'], '200 Ok')
[8056]162        return
163
[8528]164    def test_application_payment(self):
165        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
166
167        # UG (UTME) applicant
168        self.browser.open(self.ugapplicant_path)
169        self.browser.open(self.ugapplicant_path + '/manage')
170        self.assertEqual(self.browser.headers['Status'], '200 Ok')
171        self.fill_correct_values()
172        self.browser.getControl("Save").click()
173        self.assertMatches('...Form has been saved...', self.browser.contents)
174        self.fill_correct_values()
175        self.browser.getControl("Save").click()
176        self.browser.getControl("Add online payment ticket").click()
177        self.assertMatches('...Payment ticket created...',
178                           self.browser.contents)
179        self.assertMatches('...Amount Authorized...',
180                           self.browser.contents)
181        payment_id = self.ugapplicant.keys()[0]
182        payment = self.ugapplicant[payment_id]
183        self.assertEqual(payment.p_item,'This is the app2011 container')
184        self.assertEqual(payment.p_session,2011)
185        self.assertEqual(payment.p_category,'application')
186        self.assertEqual(payment.amount_auth, 200.0)
187
188        # PG applicants pay more
189        self.browser.open(self.pgapplicant_path)
190        self.browser.open(self.pgapplicant_path + '/manage')
191        self.assertEqual(self.browser.headers['Status'], '200 Ok')
192        self.fill_correct_values()
193        self.browser.getControl(name="form.reg_number").value = '2345'
194        self.browser.getControl(name="form.firstname").value = 'Anna'
195        self.browser.getControl("Save").click()
196        self.assertMatches('...Form has been saved...', self.browser.contents)
197        self.browser.getControl("Add online payment ticket").click()
198        self.assertMatches('...Payment ticket created...',
199                           self.browser.contents)
200        self.assertMatches('...Amount Authorized...',
201                           self.browser.contents)
202        payment_id = self.pgapplicant.keys()[0]
203        payment = self.pgapplicant[payment_id]
204        self.assertEqual(payment.p_item,'This is the pgft2011 container')
205        self.assertEqual(payment.p_session,2011)
206        self.assertEqual(payment.p_category,'application')
207        self.assertEqual(payment.amount_auth, 300.0)
208        return
209
[8056]210class ApplicantsExporterTest(ApplicantImportExportSetup):
211
212    layer = FunctionalLayer
213
214    def setUp(self):
215        super(ApplicantsExporterTest, self).setUp()
216        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
217        self.cat = getUtility(ICatalog, name='applicants_catalog')
218        self.intids = getUtility(IIntIds)
219        return
220
221    def setup_applicant(self, applicant):
222        # set predictable values for `applicant`
223        applicant.reg_number = u'123456'
224        applicant.applicant_id = u'dp2011_654321'
225        applicant.firstname = u'Anna'
226        applicant.lastname = u'Tester'
227        applicant.middlename = u'M.'
228        applicant.date_of_birth = datetime.date(1981, 2, 4)
229        applicant.sex = 'f'
230        applicant.email = 'anna@sample.com'
231        applicant.phone = u'+234-123-12345'
232        applicant.course1 = self.certificate
233        applicant.course2 = self.certificate
234        applicant.course_admitted = self.certificate
235        applicant.notice = u'Some notice\nin lines.'
236        applicant.screening_score = 98
237        applicant.screening_venue = u'Exam Room'
[8584]238        applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM'
[8056]239        applicant.password = 'any password'
240        return applicant
241
[8584]242    def test_export_reimport_all(self):
[8056]243        # we can export all applicants in a portal
244        # set values we can expect in export file
245        self.applicant = self.setup_applicant(self.applicant)
[8196]246        exporter = CustomApplicantsExporter()
[8056]247        exporter.export_all(self.app, self.outfile)
248        result = open(self.outfile, 'rb').read()
249        # The exported records do contain a real date in their
250        # history dict. We skip the date and split the comparison
251        # into two parts.
252        self.assertTrue(
253            'applicant_id,application_date,application_number,course1,course2,'
254            'course_admitted,date_of_birth,display_fullname,email,emp2_end,'
[8130]255            'emp2_position,emp2_reason,emp2_start,emp_end,emp_position,'
256            'emp_reason,emp_start,employer,employer2,firstname,history,'
257            'hq_degree,hq_disc,hq_matric_no,hq_school,hq_session,hq_type,'
[8537]258            'jamb_score,jamb_subjects,lastname,lga,locked,middlename,'
259            'nationality,notice,nysc_lga,'
[8130]260            'nysc_year,password,perm_address,phone,pp_school,presently_inst,'
[8584]261            'reg_number,screening_date,screening_score,screening_venue,sex,'
262            'state,student_id,'
[8531]263            'container_code'
[8056]264            in result)
265        self.assertTrue(
[8537]266            'Application initialized by system\'],,,,,,,,,Tester,,0,M.,,'
[8130]267            '"Some notice\nin lines.",,,any password,,+234-123-12345,,,'
[8584]268            '123456,"Saturday, 16th June 2012 2:00:00 PM",98,Exam Room,f,'
269            'initialized,,dp2011' in result)
270        # We can import the same file with if we ignore some columns.
271        # Since the applicants_catalog hasn't been notified, the same
272        # record with same reg_number can be imported twice.
273        processor = CustomApplicantProcessor()
274        result = processor.doImport(
275            self.outfile,
[8587]276            ['ignore_applicant_id','application_date','ignore_application_number',
277            'course1','course2',
278            'course_admitted','date_of_birth','ignore3','email','emp2_end',
279            'emp2_position','emp2_reason','emp2_start','emp_end','emp_position',
280            'emp_reason','emp_start','employer','employer2','firstname','ignore4',
281            'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type',
282            'jamb_score','jamb_subjects','lastname','lga','locked','middlename',
283            'nationality','notice','nysc_lga',
284            'nysc_year','password','perm_address','phone','pp_school','presently_inst',
285            'reg_number','screening_date','screening_score','screening_venue','sex',
[8584]286            'state','student_id','container_code'],
287            mode='create')
288        num_succ, num_fail, finished_path, failed_path = result
289        self.assertEqual(num_succ,1)
290        self.assertEqual(num_fail,0)
[8587]291        # Now we ignore also the container_code and import the same file
292        # in update mode which means that ICustomApplicantUpdateByRegNo
293        # is used for field conversion. applicant_id must be ignored
294        # too since the previous import has notified the applicants_catalog
295        # so that the portal 'knows' that reg_number is in use.
296        processor = CustomApplicantProcessor()
297        result = processor.doImport(
298            self.outfile,
299            ['ignore_applicant_id','application_date','ignore_application_number',
300            'course1','course2',
301            'course_admitted','date_of_birth','ignore3','email','emp2_end',
302            'emp2_position','emp2_reason','emp2_start','emp_end','emp_position',
303            'emp_reason','emp_start','employer','employer2','firstname','ignore4',
304            'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type',
305            'jamb_score','jamb_subjects','lastname','lga','locked','middlename',
306            'nationality','notice','nysc_lga',
307            'nysc_year','password','perm_address','phone','pp_school','presently_inst',
308            'reg_number','screening_date','screening_score','screening_venue','sex',
309            'state','student_id','ignore_container_code'],
310            mode='update')
311        num_succ, num_fail, finished_path, failed_path = result
312        self.assertEqual(num_succ,1)
313        self.assertEqual(num_fail,0)
[8012]314        return
Note: See TracBrowser for help on using the repository browser.