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

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

Merged with waeup.uniben 8802:HEAD.

  • Property svn:keywords set to Id
File size: 13.0 KB
Line 
1## $Id: tests.py 8807 2012-06-26 07:46:53Z 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
21import datetime
22import grok
23import pytz
24from zope.event import notify
25from zope.intid.interfaces import IIntIds
26from zope.interface.verify import verifyClass, verifyObject
27from zope.component.hooks import setSite, clearSite
28from zope.component import createObject, getUtility
29from zope.catalog.interfaces import ICatalog
30from zope.testbrowser.testing import Browser
31from hurry.workflow.interfaces import IWorkflowState
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
36from waeup.kofa.configuration import SessionConfiguration
37from waeup.kofa.applicants.container import ApplicantsContainer
38from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup
39from waeup.kofa.interfaces import IBatchProcessor
40from waeup.fceokene.testing import FunctionalLayer
41from waeup.fceokene.applicants.export import CustomApplicantsExporter
42from waeup.fceokene.applicants.batching import CustomApplicantProcessor
43
44
45class ApplicantUITest(FunctionalTestCase):
46    """Perform some browser tests.
47    """
48    layer = FunctionalLayer
49
50    def setUp(self):
51        super(ApplicantUITest, self).setUp()
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
67        # Add an applicants containers
68        self.ugcontainer = ApplicantsContainer()
69        self.ugcontainer.code = u'putme2011'
70        self.ugcontainer.prefix = u'putme'
71        self.ugcontainer.application_category = u'basic'
72        self.ugcontainer.year = 2011
73        self.ugcontainer.application_fee = 200.0
74        self.ugcontainer.title = u'This is the app2011 container'
75        self.app['applicants']['app2011'] = self.ugcontainer
76
77        self.ugcontainer.mode = 'update'
78        delta = datetime.timedelta(days=10)
79        self.ugcontainer.startdate = datetime.datetime.now(pytz.utc) - delta
80        self.ugcontainer.enddate = datetime.datetime.now(pytz.utc) + delta
81
82        # Populate university
83        self.certificate = createObject('waeup.Certificate')
84        self.certificate.code = 'CERT1'
85        self.certificate.application_category = 'basic'
86        self.certificate.start_level = 100
87        self.certificate.end_level = 500
88        self.certificate.study_mode = u'ug_ft'
89        self.app['faculties']['fac1'] = Faculty()
90        self.app['faculties']['fac1']['dep1'] = Department()
91        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
92            self.certificate)
93
94        # Add (customized) applicant
95        ugapplicant = createObject(u'waeup.Applicant')
96        ugapplicant.firstname = u'Klaus'
97        ugapplicant.lastname = u'Under'
98        self.app['applicants']['app2011'].addApplicant(ugapplicant)
99        self.ugapplication_number = ugapplicant.application_number
100        self.ugapplicant = self.app['applicants']['app2011'][
101            self.ugapplication_number]
102        self.ugapplicant_path = ('http://localhost/app/applicants/app2011/%s'
103            % self.ugapplication_number)
104
105        self.browser = Browser()
106        self.browser.handleErrors = False
107
108    def tearDown(self):
109        super(ApplicantUITest, self).tearDown()
110        shutil.rmtree(self.dc_root)
111        clearSite()
112        return
113
114    def fill_correct_values(self):
115        self.browser.getControl(name="form.reg_number").value = '1234'
116        self.browser.getControl(name="form.firstname").value = 'John'
117        self.browser.getControl(name="form.lastname").value = 'Tester'
118        self.browser.getControl(name="form.date_of_birth").value = '09/09/1988'
119        self.browser.getControl(name="form.lga").value = ['foreigner']
120        self.browser.getControl(name="form.nationality").value = ['NG']
121        self.browser.getControl(name="form.sex").value = ['m']
122        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
123
124    def test_application_payment(self):
125        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
126
127        # UG (UTME) applicant
128        self.browser.open(self.ugapplicant_path)
129        self.browser.open(self.ugapplicant_path + '/manage')
130        self.assertEqual(self.browser.headers['Status'], '200 Ok')
131        self.fill_correct_values()
132        self.browser.getControl(name="form.course1").value = ['CERT1']
133        self.browser.getControl("Save").click()
134        self.assertMatches('...Form has been saved...', self.browser.contents)
135        self.browser.getControl("Save").click()
136        self.browser.getControl("Add online payment ticket").click()
137        self.assertMatches('...Payment ticket created...',
138                           self.browser.contents)
139        self.assertMatches('...Amount Authorized...',
140                           self.browser.contents)
141        payment_id = self.ugapplicant.keys()[0]
142        payment = self.ugapplicant[payment_id]
143        self.assertEqual(payment.p_item,'This is the app2011 container')
144        self.assertEqual(payment.p_session,2011)
145        self.assertEqual(payment.p_category,'application')
146        self.assertEqual(payment.amount_auth, 200.0)
147
148        return
149
150    def test_create_ugstudent(self):
151        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
152        manage_path = 'http://localhost/app/applicants/%s/%s/%s' % (
153            'app2011', self.ugapplicant.application_number, 'manage')
154        self.browser.open(manage_path)
155        self.fill_correct_values()
156        self.browser.getControl("Save").click()
157        IWorkflowState(self.ugapplicant).setState('admitted')
158        self.browser.getControl(name="form.course1").value = ['CERT1']
159        self.browser.getControl(name="form.course_admitted").value = ['CERT1']
160        self.browser.getControl("Save").click()
161        self.browser.getLink("Create student").click()
162        student_id =  self.app['students'].keys()[0]
163        self.assertTrue(('Student %s created' % student_id)
164            in self.browser.contents)
165        student = self.app['students'][student_id]
166        self.assertEqual(student.email, 'xx@yy.zz')
167        self.assertEqual(student.firstname, 'John')
168        self.assertEqual(student.lastname, 'Tester')
169        # Also additional attributes have been copied.
170        self.assertEqual(student.lga, 'foreigner')
171        self.assertEqual(student.nationality, 'NG')
172        return
173
174class ApplicantsExporterTest(ApplicantImportExportSetup):
175
176    layer = FunctionalLayer
177
178    def setUp(self):
179        super(ApplicantsExporterTest, self).setUp()
180        self.outfile = os.path.join(self.workdir, 'myoutput.csv')
181        self.cat = getUtility(ICatalog, name='applicants_catalog')
182        self.intids = getUtility(IIntIds)
183        return
184
185    def setup_applicant(self, applicant):
186        # set predictable values for `applicant`
187        applicant.reg_number = u'123456'
188        applicant.applicant_id = u'dp2011_654321'
189        applicant.firstname = u'Anna'
190        applicant.lastname = u'Tester'
191        applicant.middlename = u'M.'
192        applicant.date_of_birth = datetime.date(1981, 2, 4)
193        applicant.sex = 'f'
194        applicant.email = 'anna@sample.com'
195        applicant.phone = u'+234-123-12345'
196        applicant.course1 = self.certificate
197        applicant.course2 = self.certificate
198        applicant.course_admitted = self.certificate
199        applicant.notice = u'Some notice\nin lines.'
200        applicant.screening_score = 98
201        applicant.screening_venue = u'Exam Room'
202        applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM'
203        applicant.password = 'any password'
204        return applicant
205
206    def test_export_reimport_all(self):
207        # we can export all applicants in a portal
208        # set values we can expect in export file
209        self.applicant = self.setup_applicant(self.applicant)
210        exporter = CustomApplicantsExporter()
211        exporter.export_all(self.app, self.outfile)
212        result = open(self.outfile, 'rb').read()
213        # The exported records do contain a real date in their
214        # history dict. We skip the date and split the comparison
215        # into two parts.
216        self.assertTrue(
217            'aggregate,applicant_id,application_date,application_number,course1,course2,'
218            'course_admitted,date_of_birth,display_fullname,email,emp2_end,'
219            'emp2_position,emp2_reason,emp2_start,emp_end,emp_position,'
220            'emp_reason,emp_start,employer,employer2,firstname,history,'
221            'hq_degree,hq_disc,hq_matric_no,hq_school,hq_session,hq_type,'
222            'jamb_score,jamb_subjects,lastname,lga,locked,middlename,'
223            'nationality,notice,nysc_lga,'
224            'nysc_year,password,perm_address,phone,pp_school,presently_inst,'
225            'reg_number,result_uploaded,screening_date,screening_score,screening_venue,sex,'
226            'state,student_id,'
227            'container_code'
228            in result)
229        self.assertTrue(
230            'Application initialized by system\'],,,,,,,,,Tester,,0,M.,,'
231            '"Some notice\nin lines.",,,any password,,+234-123-12345,,,'
232            '123456,,"Saturday, 16th June 2012 2:00:00 PM",98,Exam Room,f,'
233            'initialized,,dp2011'
234            in result)
235        # We can import the same file if we ignore some columns.
236        # Since the applicants_catalog hasn't been notified, the same
237        # record with same reg_number can be imported twice.
238        processor = CustomApplicantProcessor()
239        result = processor.doImport(
240            self.outfile,
241            ['aggreagate','ignore_applicant_id','application_date',
242            'ignore_application_number','course1','course2',
243            'course_admitted','date_of_birth','ignore3','email','emp2_end',
244            'emp2_position','emp2_reason','emp2_start','emp_end','emp_position',
245            'emp_reason','emp_start','employer','employer2','firstname','ignore4',
246            'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type',
247            'jamb_score','jamb_subjects','lastname','lga','locked','middlename',
248            'nationality','notice','nysc_lga',
249            'nysc_year','password','perm_address','phone','pp_school','presently_inst',
250            'reg_number','result_uploaded',
251            'screening_date','screening_score','screening_venue','sex',
252            'state','student_id','container_code'],
253            mode='create')
254        num_succ, num_fail, finished_path, failed_path = result
255        self.assertEqual(num_succ,1)
256        self.assertEqual(num_fail,0)
257        # Now we ignore also the container_code and import the same file
258        # in update mode which means that ICustomApplicantUpdateByRegNo
259        # is used for field conversion. applicant_id must be ignored
260        # too since the previous import has notified the applicants_catalog
261        # so that the portal 'knows' that reg_number is in use.
262        processor = CustomApplicantProcessor()
263        result = processor.doImport(
264            self.outfile,
265            ['aggregate','ignore_applicant_id','application_date',
266            'ignore_application_number','course1','course2',
267            'course_admitted','date_of_birth','ignore3','email','emp2_end',
268            'emp2_position','emp2_reason','emp2_start','emp_end','emp_position',
269            'emp_reason','emp_start','employer','employer2','firstname','ignore4',
270            'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type',
271            'jamb_score','jamb_subjects','lastname','lga','locked','middlename',
272            'nationality','notice','nysc_lga',
273            'nysc_year','password','perm_address','phone','pp_school','presently_inst',
274            'reg_number','result_uploaded','screening_date','screening_score',
275            'screening_venue','sex',
276            'state','student_id','ignore_container_code'],
277            mode='update')
278        num_succ, num_fail, finished_path, failed_path = result
279        self.assertEqual(num_succ,1)
280        self.assertEqual(num_fail,0)
281        return
Note: See TracBrowser for help on using the repository browser.