[7866] | 1 | ## $Id: tests.py 8671 2012-06-11 07:09:06Z 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 | ## |
---|
| 18 | import os |
---|
| 19 | import shutil |
---|
| 20 | import tempfile |
---|
[8056] | 21 | import datetime |
---|
[8671] | 22 | import grok |
---|
| 23 | import pytz |
---|
| 24 | from zope.event import notify |
---|
[8056] | 25 | from zope.intid.interfaces import IIntIds |
---|
[7866] | 26 | from zope.interface.verify import verifyClass, verifyObject |
---|
| 27 | from zope.component.hooks import setSite, clearSite |
---|
[8056] | 28 | from zope.component import createObject, getUtility |
---|
| 29 | from zope.catalog.interfaces import ICatalog |
---|
[8012] | 30 | from zope.testbrowser.testing import Browser |
---|
[8671] | 31 | from hurry.workflow.interfaces import IWorkflowState |
---|
[7866] | 32 | from waeup.kofa.app import University |
---|
| 33 | from waeup.kofa.university.faculty import Faculty |
---|
| 34 | from waeup.kofa.university.department import Department |
---|
| 35 | from waeup.kofa.testing import FunctionalTestCase |
---|
[8528] | 36 | from waeup.kofa.configuration import SessionConfiguration |
---|
[8012] | 37 | from waeup.kofa.applicants.container import ApplicantsContainer |
---|
[8056] | 38 | from waeup.kofa.applicants.tests.test_batching import ApplicantImportExportSetup |
---|
| 39 | from waeup.kofa.interfaces import IBatchProcessor |
---|
[8460] | 40 | from waeup.fceokene.testing import FunctionalLayer |
---|
| 41 | from waeup.fceokene.applicants.export import CustomApplicantsExporter |
---|
[8584] | 42 | from waeup.fceokene.applicants.batching import CustomApplicantProcessor |
---|
[7866] | 43 | |
---|
[8056] | 44 | |
---|
[8012] | 45 | class 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 | |
---|
[8671] | 67 | # Add an applicants containers |
---|
[8012] | 68 | self.ugcontainer = ApplicantsContainer() |
---|
[8528] | 69 | self.ugcontainer.code = u'putme2011' |
---|
| 70 | self.ugcontainer.prefix = u'putme' |
---|
[8624] | 71 | self.ugcontainer.application_category = u'basic' |
---|
[8528] | 72 | self.ugcontainer.year = 2011 |
---|
| 73 | self.ugcontainer.application_fee = 200.0 |
---|
| 74 | self.ugcontainer.title = u'This is the app2011 container' |
---|
[8012] | 75 | self.app['applicants']['app2011'] = self.ugcontainer |
---|
[7866] | 76 | |
---|
[8671] | 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 | |
---|
[7866] | 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 |
---|
[8671] | 88 | self.certificate.study_mode = u'ug_ft' |
---|
[7866] | 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 | |
---|
[8671] | 94 | # Add (customized) applicant |
---|
[8012] | 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] |
---|
[8528] | 102 | self.ugapplicant_path = ('http://localhost/app/applicants/app2011/%s' |
---|
| 103 | % self.ugapplication_number) |
---|
[8012] | 104 | |
---|
| 105 | self.browser = Browser() |
---|
| 106 | self.browser.handleErrors = False |
---|
| 107 | |
---|
[7866] | 108 | def tearDown(self): |
---|
[8012] | 109 | super(ApplicantUITest, self).tearDown() |
---|
[7866] | 110 | shutil.rmtree(self.dc_root) |
---|
| 111 | clearSite() |
---|
| 112 | return |
---|
| 113 | |
---|
[8528] | 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'] |
---|
[8671] | 120 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
[8528] | 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() |
---|
[8624] | 132 | self.browser.getControl(name="form.course1").value = ['CERT1'] |
---|
[8528] | 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 | |
---|
[8671] | 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) |
---|
[8528] | 155 | self.fill_correct_values() |
---|
| 156 | self.browser.getControl("Save").click() |
---|
[8671] | 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') |
---|
[8528] | 172 | return |
---|
| 173 | |
---|
[8056] | 174 | class 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' |
---|
[8584] | 202 | applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM' |
---|
[8056] | 203 | applicant.password = 'any password' |
---|
| 204 | return applicant |
---|
| 205 | |
---|
[8584] | 206 | def test_export_reimport_all(self): |
---|
[8056] | 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) |
---|
[8196] | 210 | exporter = CustomApplicantsExporter() |
---|
[8056] | 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 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
| 218 | 'course_admitted,date_of_birth,display_fullname,email,emp2_end,' |
---|
[8130] | 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,' |
---|
[8537] | 222 | 'jamb_score,jamb_subjects,lastname,lga,locked,middlename,' |
---|
| 223 | 'nationality,notice,nysc_lga,' |
---|
[8130] | 224 | 'nysc_year,password,perm_address,phone,pp_school,presently_inst,' |
---|
[8584] | 225 | 'reg_number,screening_date,screening_score,screening_venue,sex,' |
---|
| 226 | 'state,student_id,' |
---|
[8531] | 227 | 'container_code' |
---|
[8056] | 228 | in result) |
---|
| 229 | self.assertTrue( |
---|
[8537] | 230 | 'Application initialized by system\'],,,,,,,,,Tester,,0,M.,,' |
---|
[8130] | 231 | '"Some notice\nin lines.",,,any password,,+234-123-12345,,,' |
---|
[8584] | 232 | '123456,"Saturday, 16th June 2012 2:00:00 PM",98,Exam Room,f,' |
---|
| 233 | 'initialized,,dp2011' in result) |
---|
| 234 | # We can import the same file with if we ignore some columns. |
---|
| 235 | # Since the applicants_catalog hasn't been notified, the same |
---|
| 236 | # record with same reg_number can be imported twice. |
---|
| 237 | processor = CustomApplicantProcessor() |
---|
| 238 | result = processor.doImport( |
---|
| 239 | self.outfile, |
---|
[8587] | 240 | ['ignore_applicant_id','application_date','ignore_application_number', |
---|
| 241 | 'course1','course2', |
---|
| 242 | 'course_admitted','date_of_birth','ignore3','email','emp2_end', |
---|
| 243 | 'emp2_position','emp2_reason','emp2_start','emp_end','emp_position', |
---|
| 244 | 'emp_reason','emp_start','employer','employer2','firstname','ignore4', |
---|
| 245 | 'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type', |
---|
| 246 | 'jamb_score','jamb_subjects','lastname','lga','locked','middlename', |
---|
| 247 | 'nationality','notice','nysc_lga', |
---|
| 248 | 'nysc_year','password','perm_address','phone','pp_school','presently_inst', |
---|
| 249 | 'reg_number','screening_date','screening_score','screening_venue','sex', |
---|
[8584] | 250 | 'state','student_id','container_code'], |
---|
| 251 | mode='create') |
---|
| 252 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 253 | self.assertEqual(num_succ,1) |
---|
| 254 | self.assertEqual(num_fail,0) |
---|
[8587] | 255 | # Now we ignore also the container_code and import the same file |
---|
| 256 | # in update mode which means that ICustomApplicantUpdateByRegNo |
---|
| 257 | # is used for field conversion. applicant_id must be ignored |
---|
| 258 | # too since the previous import has notified the applicants_catalog |
---|
| 259 | # so that the portal 'knows' that reg_number is in use. |
---|
| 260 | processor = CustomApplicantProcessor() |
---|
| 261 | result = processor.doImport( |
---|
| 262 | self.outfile, |
---|
| 263 | ['ignore_applicant_id','application_date','ignore_application_number', |
---|
| 264 | 'course1','course2', |
---|
| 265 | 'course_admitted','date_of_birth','ignore3','email','emp2_end', |
---|
| 266 | 'emp2_position','emp2_reason','emp2_start','emp_end','emp_position', |
---|
| 267 | 'emp_reason','emp_start','employer','employer2','firstname','ignore4', |
---|
| 268 | 'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type', |
---|
| 269 | 'jamb_score','jamb_subjects','lastname','lga','locked','middlename', |
---|
| 270 | 'nationality','notice','nysc_lga', |
---|
| 271 | 'nysc_year','password','perm_address','phone','pp_school','presently_inst', |
---|
| 272 | 'reg_number','screening_date','screening_score','screening_venue','sex', |
---|
| 273 | 'state','student_id','ignore_container_code'], |
---|
| 274 | mode='update') |
---|
| 275 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 276 | self.assertEqual(num_succ,1) |
---|
| 277 | self.assertEqual(num_fail,0) |
---|
[8671] | 278 | return |
---|