[7866] | 1 | ## $Id: tests.py 8669 2012-06-11 07:06:40Z 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 |
---|
[8669] | 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 |
---|
[8669] | 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 |
---|
[8527] | 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 |
---|
[8444] | 40 | from waeup.aaue.testing import FunctionalLayer |
---|
| 41 | from waeup.aaue.applicants.export import CustomApplicantsExporter |
---|
[8583] | 42 | from waeup.aaue.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 | |
---|
[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' |
---|
[8623] | 71 | self.pgcontainer.application_category = u'pg_ft' |
---|
[8527] | 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() |
---|
[8527] | 77 | self.ugcontainer.code = u'putme2011' |
---|
| 78 | self.ugcontainer.prefix = u'putme' |
---|
[8623] | 79 | self.ugcontainer.application_category = u'basic' |
---|
[8527] | 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 | |
---|
[8669] | 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 |
---|
[8669] | 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) |
---|
[8623] | 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 |
---|
[8669] | 106 | self.certificate.study_mode = u'pg_ft' |
---|
[8623] | 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] |
---|
[8527] | 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 | |
---|
[8527] | 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'] |
---|
[8669] | 146 | self.browser.getControl(name="form.nationality").value = ['NG'] |
---|
[8527] | 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 |
---|
[8527] | 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) |
---|
[8527] | 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) |
---|
[8527] | 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) |
---|
[8527] | 163 | self.browser.open(self.pgapplicant_path + '/application_slip.pdf') |
---|
[8017] | 164 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
[8521] | 165 | # If we view the applicant in the ug container, |
---|
[8017] | 166 | # the employer field doesn't appear |
---|
[8527] | 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) |
---|
[8527] | 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 |
---|
[8527] | 173 | self.fill_correct_values() |
---|
[8623] | 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) |
---|
[8527] | 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) |
---|
[8527] | 181 | self.browser.open(self.ugapplicant_path + '/application_slip.pdf') |
---|
[8017] | 182 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
[8056] | 183 | return |
---|
| 184 | |
---|
[8527] | 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() |
---|
[8623] | 193 | self.browser.getControl(name="form.course1").value = ['CERT1'] |
---|
[8527] | 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() |
---|
[8623] | 214 | self.browser.getControl(name="form.course1").value = ['CERT2'] |
---|
[8527] | 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 | |
---|
[8669] | 232 | def test_create_ugstudent(self): |
---|
| 233 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
| 234 | manage_path = 'http://localhost/app/applicants/%s/%s/%s' % ( |
---|
| 235 | 'app2011', self.ugapplicant.application_number, 'manage') |
---|
| 236 | self.browser.open(manage_path) |
---|
| 237 | self.fill_correct_values() |
---|
| 238 | self.browser.getControl("Save").click() |
---|
| 239 | IWorkflowState(self.ugapplicant).setState('admitted') |
---|
| 240 | self.browser.getControl(name="form.course1").value = ['CERT1'] |
---|
| 241 | self.browser.getControl(name="form.course_admitted").value = ['CERT1'] |
---|
| 242 | self.browser.getControl("Save").click() |
---|
| 243 | self.browser.getLink("Create student").click() |
---|
| 244 | student_id = self.app['students'].keys()[0] |
---|
| 245 | self.assertTrue(('Student %s created' % student_id) |
---|
| 246 | in self.browser.contents) |
---|
| 247 | student = self.app['students'][student_id] |
---|
| 248 | self.assertEqual(student.email, 'xx@yy.zz') |
---|
| 249 | self.assertEqual(student.firstname, 'John') |
---|
| 250 | self.assertEqual(student.lastname, 'Tester') |
---|
| 251 | # Also additional attributes have been copied. |
---|
| 252 | self.assertEqual(student.lga, 'foreigner') |
---|
| 253 | self.assertEqual(student.nationality, 'NG') |
---|
| 254 | return |
---|
| 255 | |
---|
[8056] | 256 | class ApplicantsExporterTest(ApplicantImportExportSetup): |
---|
| 257 | |
---|
| 258 | layer = FunctionalLayer |
---|
| 259 | |
---|
| 260 | def setUp(self): |
---|
| 261 | super(ApplicantsExporterTest, self).setUp() |
---|
| 262 | self.outfile = os.path.join(self.workdir, 'myoutput.csv') |
---|
| 263 | self.cat = getUtility(ICatalog, name='applicants_catalog') |
---|
| 264 | self.intids = getUtility(IIntIds) |
---|
| 265 | return |
---|
| 266 | |
---|
| 267 | def setup_applicant(self, applicant): |
---|
| 268 | # set predictable values for `applicant` |
---|
| 269 | applicant.reg_number = u'123456' |
---|
| 270 | applicant.applicant_id = u'dp2011_654321' |
---|
| 271 | applicant.firstname = u'Anna' |
---|
| 272 | applicant.lastname = u'Tester' |
---|
| 273 | applicant.middlename = u'M.' |
---|
| 274 | applicant.date_of_birth = datetime.date(1981, 2, 4) |
---|
| 275 | applicant.sex = 'f' |
---|
| 276 | applicant.email = 'anna@sample.com' |
---|
| 277 | applicant.phone = u'+234-123-12345' |
---|
| 278 | applicant.course1 = self.certificate |
---|
| 279 | applicant.course2 = self.certificate |
---|
| 280 | applicant.course_admitted = self.certificate |
---|
| 281 | applicant.notice = u'Some notice\nin lines.' |
---|
| 282 | applicant.screening_score = 98 |
---|
| 283 | applicant.screening_venue = u'Exam Room' |
---|
[8583] | 284 | applicant.screening_date = u'Saturday, 16th June 2012 2:00:00 PM' |
---|
[8056] | 285 | applicant.password = 'any password' |
---|
| 286 | return applicant |
---|
| 287 | |
---|
[8583] | 288 | def test_export_reimport_all(self): |
---|
[8056] | 289 | # we can export all applicants in a portal |
---|
| 290 | # set values we can expect in export file |
---|
| 291 | self.applicant = self.setup_applicant(self.applicant) |
---|
[8196] | 292 | exporter = CustomApplicantsExporter() |
---|
[8056] | 293 | exporter.export_all(self.app, self.outfile) |
---|
| 294 | result = open(self.outfile, 'rb').read() |
---|
| 295 | # The exported records do contain a real date in their |
---|
| 296 | # history dict. We skip the date and split the comparison |
---|
| 297 | # into two parts. |
---|
| 298 | self.assertTrue( |
---|
| 299 | 'applicant_id,application_date,application_number,course1,course2,' |
---|
| 300 | 'course_admitted,date_of_birth,display_fullname,email,emp2_end,' |
---|
[8130] | 301 | 'emp2_position,emp2_reason,emp2_start,emp_end,emp_position,' |
---|
| 302 | 'emp_reason,emp_start,employer,employer2,firstname,history,' |
---|
| 303 | 'hq_degree,hq_disc,hq_matric_no,hq_school,hq_session,hq_type,' |
---|
[8536] | 304 | 'jamb_score,jamb_subjects,lastname,lga,locked,middlename,' |
---|
| 305 | 'nationality,notice,nysc_lga,' |
---|
[8130] | 306 | 'nysc_year,password,perm_address,phone,pp_school,presently_inst,' |
---|
[8583] | 307 | 'reg_number,screening_date,screening_score,screening_venue,sex,' |
---|
| 308 | 'state,student_id,' |
---|
[8532] | 309 | 'container_code' |
---|
[8056] | 310 | in result) |
---|
| 311 | self.assertTrue( |
---|
[8536] | 312 | 'Application initialized by system\'],,,,,,,,,Tester,,0,M.,,' |
---|
[8130] | 313 | '"Some notice\nin lines.",,,any password,,+234-123-12345,,,' |
---|
[8583] | 314 | '123456,"Saturday, 16th June 2012 2:00:00 PM",98,Exam Room,f,' |
---|
| 315 | 'initialized,,dp2011' in result) |
---|
| 316 | # We can import the same file with if we ignore some columns. |
---|
| 317 | # Since the applicants_catalog hasn't been notified, the same |
---|
| 318 | # record with same reg_number can be imported twice. |
---|
| 319 | processor = CustomApplicantProcessor() |
---|
| 320 | result = processor.doImport( |
---|
| 321 | self.outfile, |
---|
[8588] | 322 | ['ignore_applicant_id','application_date','ignore_application_number', |
---|
| 323 | 'course1','course2', |
---|
| 324 | 'course_admitted','date_of_birth','ignore3','email','emp2_end', |
---|
| 325 | 'emp2_position','emp2_reason','emp2_start','emp_end','emp_position', |
---|
| 326 | 'emp_reason','emp_start','employer','employer2','firstname','ignore4', |
---|
| 327 | 'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type', |
---|
| 328 | 'jamb_score','jamb_subjects','lastname','lga','locked','middlename', |
---|
| 329 | 'nationality','notice','nysc_lga', |
---|
| 330 | 'nysc_year','password','perm_address','phone','pp_school','presently_inst', |
---|
| 331 | 'reg_number','screening_date','screening_score','screening_venue','sex', |
---|
[8583] | 332 | 'state','student_id','container_code'], |
---|
| 333 | mode='create') |
---|
| 334 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 335 | self.assertEqual(num_succ,1) |
---|
| 336 | self.assertEqual(num_fail,0) |
---|
[8588] | 337 | # Now we ignore also the container_code and import the same file |
---|
| 338 | # in update mode which means that ICustomApplicantUpdateByRegNo |
---|
| 339 | # is used for field conversion. applicant_id must be ignored |
---|
| 340 | # too since the previous import has notified the applicants_catalog |
---|
| 341 | # so that the portal 'knows' that reg_number is in use. |
---|
| 342 | processor = CustomApplicantProcessor() |
---|
| 343 | result = processor.doImport( |
---|
| 344 | self.outfile, |
---|
| 345 | ['ignore_applicant_id','application_date','ignore_application_number', |
---|
| 346 | 'course1','course2', |
---|
| 347 | 'course_admitted','date_of_birth','ignore3','email','emp2_end', |
---|
| 348 | 'emp2_position','emp2_reason','emp2_start','emp_end','emp_position', |
---|
| 349 | 'emp_reason','emp_start','employer','employer2','firstname','ignore4', |
---|
| 350 | 'hq_degree','hq_disc','hq_matric_no','hq_school','hq_session','hq_type', |
---|
| 351 | 'jamb_score','jamb_subjects','lastname','lga','locked','middlename', |
---|
| 352 | 'nationality','notice','nysc_lga', |
---|
| 353 | 'nysc_year','password','perm_address','phone','pp_school','presently_inst', |
---|
| 354 | 'reg_number','screening_date','screening_score','screening_venue','sex', |
---|
| 355 | 'state','student_id','ignore_container_code'], |
---|
| 356 | mode='update') |
---|
| 357 | num_succ, num_fail, finished_path, failed_path = result |
---|
| 358 | self.assertEqual(num_succ,1) |
---|
| 359 | self.assertEqual(num_fail,0) |
---|
[8669] | 360 | return |
---|