source: main/waeup.uniben/trunk/src/waeup/uniben/applicants/tests.py @ 8686

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

Copy also customized fields from applicant to student.

Fix typos.

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