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

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

Fix test.

Replace default value of verdicts.

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