## $Id: test_browser.py 17866 2024-08-01 11:02:51Z henrik $ ## ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## import os import shutil import tempfile from time import time from datetime import datetime, timedelta from StringIO import StringIO from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo from zope.component.hooks import setSite, clearSite from zope.component import getUtility, createObject from zope.interface import verify from zope.securitypolicy.interfaces import IPrincipalRoleManager from waeup.kofa.app import University from waeup.kofa.students.tests.test_browser import ( StudentsFullSetup, SAMPLE_IMAGE) from waeup.kofa.testing import FunctionalTestCase from waeup.kofa.interfaces import ( IExtFileStore, IFileStoreNameChooser) from waeup.kofa.schoolgrades import ResultEntry from waeup.kofa.students.batching import StudentProcessor from waeup.kofa.students.interfaces import IStudentsUtils from waeup.kofa.browser.tests.test_pdf import samples_dir from waeup.kofa.tests.test_authentication import SECRET from kofacustom.nigeria.students.batching import NigeriaStudentProcessor from kofacustom.nigeria.testing import FunctionalLayer from kofacustom.nigeria.utils.utils import NigeriaKofaUtils from kofacustom.nigeria.students.interfaces import ( INigeriaStudentStudyCourse, INigeriaStudent, INigeriaStudentStudyLevel, INigeriaCourseTicket) STUDENT_SAMPLE_DATA = open( os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'), 'rb').read() STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split( '\n')[0].split(',') class StudentProcessorTest(FunctionalTestCase): """Perform some batching tests. """ layer = FunctionalLayer def setUp(self): super(StudentProcessorTest, self).setUp() # Setup a sample site for each test app = University() self.dc_root = tempfile.mkdtemp() app['datacenter'].setStoragePath(self.dc_root) # Prepopulate the ZODB... self.getRootFolder()['app'] = app # we add the site immediately after creation to the # ZODB. Catalogs and other local utilities are not setup # before that step. self.app = self.getRootFolder()['app'] # Set site here. Some of the following setup code might need # to access grok.getSite() and should get our new app then setSite(app) self.processor_base = StudentProcessor() self.processor = NigeriaStudentProcessor() self.workdir = tempfile.mkdtemp() self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv') open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA) def tearDown(self): super(StudentProcessorTest, self).tearDown() shutil.rmtree(self.workdir) shutil.rmtree(self.dc_root) clearSite() return def test_import(self): num, num_warns, fin_file, fail_file = self.processor.doImport( self.csv_file, STUDENT_HEADER_FIELDS) #print open(fail_file).read() self.assertEqual(num_warns,0) assert len(self.app['students'].keys()) == 3 # Also fst_sit_results have been properly imported (tested only here!) self.assertEqual( self.app['students']['K1000000'].fst_sit_results[0].__dict__, {'grade': 'A1', 'subject': 'visual_art'}) self.assertEqual( self.app['students']['K1000000'].fst_sit_results[1].__dict__, {'grade': 'C6', 'subject': 'applied_electricity'}) shutil.rmtree(os.path.dirname(fin_file)) class StudentUITests(StudentsFullSetup): """Tests for customized student class views and pages """ layer = FunctionalLayer def setUp(self): super(StudentUITests, self).setUp() def test_classes(self): # Let's see if objects created in the customized # portal really implement the customized interfaces verify.verifyObject(INigeriaStudent, self.student) verify.verifyObject( INigeriaStudentStudyCourse, self.student['studycourse']) studylevel = createObject(u'waeup.StudentStudyLevel') verify.verifyObject(INigeriaStudentStudyLevel, studylevel) ticket = createObject(u'waeup.CourseTicket') verify.verifyObject(INigeriaCourseTicket, ticket) IWorkflowState(self.student).setState('returning') # Let's see if next_session_allowed works as expected # A, ug_ft, 100 self.assertTrue(self.student['studycourse'].next_session_allowed) # Zero, ug_ft, 100 self.student['studycourse'].current_verdict = '0' # Zero! self.assertTrue(self.student['studycourse'].next_session_allowed) # Zero, ug_ft, 200 self.student['studycourse'].current_level = 200 self.assertFalse(self.student['studycourse'].next_session_allowed) # Zero, de_ft, 200 self.student['studycourse'].certificate.study_mode = 'de_ft' self.assertTrue(self.student['studycourse'].next_session_allowed) # Zero, ph_ft, 300 self.student['studycourse'].certificate.study_mode = 'ph_ft' self.student['studycourse'].current_level = 300 self.assertTrue(self.student['studycourse'].next_session_allowed) # Zero, ph_ft, 400 self.student['studycourse'].current_level = 400 self.assertFalse(self.student['studycourse'].next_session_allowed) # Now we convert the certificate into a postgraduate certificate IWorkflowState(self.student).setState('school fee paid') self.certificate.study_mode = 'pg_ft' # ... and voila next session registration is allowed self.assertTrue(self.student['studycourse'].next_session_allowed) def test_manage_access(self): self.student.nationality = u'DE' # Managers can access the pages of students # and can perform actions self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') # The student created in the base package is an ug student self.browser.open(self.student_path) self.browser.getLink("Clearance Data").click() self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.url, self.clearance_path) self.browser.getLink("Manage").click() self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.url, self.manage_clearance_path) self.browser.getControl(name="form.date_of_birth").value = '09/10/1961' self.browser.getControl("Save").click() self.assertMatches('...Form has been saved...', self.browser.contents) self.assertMatches('...First Sitting Record...', self.browser.contents) # Managers can open clearance slip of ug students self.browser.open(self.student_path + '/clearance_slip.pdf') self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') # There is no pg field in the clearance form self.assertFalse('Second Higher Education Record' in self.browser.contents) # Now we change the study mode ... self.certificate.study_mode = 'pg_ft' self.browser.open(self.clearance_path) # ... and additional pg clearance fields appear self.assertMatches('...Second Higher Education Record...', self.browser.contents) # But also fields from the ug form are displayed self.assertMatches('...First Sitting Record...', self.browser.contents) # Managers can open clearance slip of pg students self.browser.open(self.student_path + '/clearance_slip.pdf') self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') # Managers can edit personal data. No fields are required. self.browser.open(self.manage_personal_path) self.browser.getControl("Save").click() self.assertMatches('...Form has been saved...', self.browser.contents) def test_logging(self): self.student.nationality = u'DE' valid_subj = NigeriaKofaUtils().EXAM_SUBJECTS_DICT.keys()[0] valid_grade = NigeriaKofaUtils().EXAM_GRADES[0][0] result_entry = ResultEntry(valid_subj, valid_grade) self.student.fst_sit_results = [result_entry,] # Managers can access the pages of students # and can perform actions self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') # The student created in the base package is an ug student self.browser.open(self.student_path) self.browser.getLink("Clearance Data").click() self.browser.getLink("Manage").click() self.browser.getControl("Save").click() logfile = os.path.join( self.app['datacenter'].storage, 'logs', 'students.log') logcontent = open(logfile).read() self.assertFalse('saved: fst_sit_results' in logcontent) def test_student_access(self): # Students can edit clearance data IWorkflowState(self.student).setState('cleared') self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = self.student_id self.browser.getControl(name="form.password").value = 'spwd' self.browser.getControl("Login").click() # Even in state admitted students can't change the portait if # application slip exists. IWorkflowState(self.student).setState('admitted') self.browser.open(self.student_path) self.assertTrue('Change portrait' in self.browser.contents) file_store = getUtility(IExtFileStore) applicant_slip = 'My application slip' file_id = IFileStoreNameChooser(self.student).chooseName( attr="application_slip.pdf") file_store.createFile(file_id, StringIO(applicant_slip)) self.browser.open(self.student_path) self.assertFalse('Change portrait' in self.browser.contents) self.browser.open(self.student_path + '/change_portrait') self.assertTrue('The requested form is locked' in self.browser.contents) # Student can view and edit clearance data if clearance has started ... IWorkflowInfo(self.student).fireTransition('start_clearance') self.student.officer_comment = u'Fill properly' self.browser.getLink("Clearance Data").click() self.assertTrue("Officer's Comment" in self.browser.contents) # Students can't edit officer's comment self.browser.getLink("Edit").click() self.assertFalse("Officer's Comment" in self.browser.contents) self.assertTrue('Save' in self.browser.contents) # ... and request clearance if nationality field has been filled. self.browser.getControl("Save and request clearance").click() self.assertMatches('...Required input is missing...', self.browser.contents) self.student.nationality = u'DE' self.browser.open(self.edit_clearance_path) self.browser.getControl("Save and request clearance").click() self.assertMatches('...Clearance has been requested...', self.browser.contents) # Students can edit personal data. Some fields are required. self.browser.open(self.personal_path) self.assertTrue('Updated' in self.browser.contents) self.browser.getLink("Edit").click() self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.url, self.edit_personal_path) self.browser.getControl("Save").click() self.assertMatches('...Required input is missing...', self.browser.contents) self.browser.getControl(name="form.perm_address").value = 'My address!' self.browser.getControl("Save").click() self.assertMatches('...Required input is missing...', self.browser.contents) # Ok, let's give up and fill the rest. self.browser.getControl(name="form.next_kin_name").value = 'My Mutti' self.browser.getControl(name="form.next_kin_relation").value = 'mother' self.browser.getControl(name="form.next_kin_address").value = 'sweet home' self.browser.getControl(name="form.next_kin_phone.country").value = ['+234'] self.browser.getControl(name="form.next_kin_phone.ext").value = '45678' self.browser.getControl("Save").click() self.assertMatches('...Form has been saved...', self.browser.contents) def test_manage_upload_file(self): # Managers can upload a file via the StudentClearanceManageFormPage # The image is stored even if form has errors self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') self.browser.open(self.manage_clearance_path) # Managers can add and delete a file self.browser.open(self.manage_clearance_path) image = open(SAMPLE_IMAGE, 'rb') ctrl = self.browser.getControl(name='birthcertificateupload') file_ctrl = ctrl.mech_control file_ctrl.add_file(image, filename='my_acceptance_letter.jpg') self.browser.getControl( name='upload_acceptanceletterupload').click() # Uups, we used the wrong 'Browse' field self.assertFalse( '' in self.browser.contents) ctrl = self.browser.getControl(name='acceptanceletterupload') file_ctrl = ctrl.mech_control image.seek(0) file_ctrl.add_file(image, filename='my_acceptance_letter.jpg') self.browser.getControl( name='upload_acceptanceletterupload').click() self.assertTrue( 'http://localhost/app/students/K1000000/acc_let' in self.browser.contents) self.browser.getControl( name='delete_acceptanceletterupload').click() self.assertTrue( 'acc_let deleted' in self.browser.contents) def test_student_expired_personal_data(self): # Login IWorkflowState(self.student).setState('school fee paid') delta = timedelta(days=180) self.student.personal_updated = datetime.utcnow() - delta self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = self.student_id self.browser.getControl(name="form.password").value = 'spwd' self.browser.getControl("Login").click() self.assertEqual(self.browser.url, self.student_path) self.assertTrue( 'You logged in' in self.browser.contents) # Students don't see personal_updated field in edit form self.browser.open(self.edit_personal_path) self.assertFalse('Updated' in self.browser.contents) self.browser.open(self.personal_path) self.assertTrue('Updated' in self.browser.contents) self.browser.getLink("Logout").click() delta = timedelta(days=181) self.student.personal_updated = datetime.utcnow() - delta self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = self.student_id self.browser.getControl(name="form.password").value = 'spwd' self.browser.getControl("Login").click() self.assertEqual(self.browser.url, self.edit_personal_path) self.assertTrue( 'Your personal data record is outdated.' in self.browser.contents) def test_lga_nationality(self): self.student.nationality = u'DE' self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') self.browser.open(self.manage_clearance_path) self.browser.getControl(name="form.lga").value = ['abia_aba_north'] self.browser.getControl("Save").click() self.assertTrue( 'Nationalty and LGA are contradictory.' in self.browser.contents) self.browser.getControl(name="form.nationality").value = ['NG'] self.browser.getControl("Save").click() self.assertTrue( 'Form has been saved' in self.browser.contents) def test_financial_clearance(self): self.app['users'].addUser('mrbursary', SECRET) self.app['users']['mrbursary'].email = 'mrbursary@foo.ng' self.app['users']['mrbursary'].title = u'Carlo Pitter' # Clearance officers needs to get # the StudentsOfficer site role prmglobal = IPrincipalRoleManager(self.app) prmglobal.assignRoleToPrincipal('waeup.StudentsOfficer', 'mrbursary') # Assign BursaryOfficer role prmglobal.assignRoleToPrincipal('waeup.BursaryOfficer', 'mrbursary') # and FinancialClearanceOfficer role prmglobal.assignRoleToPrincipal( 'waeup.FinancialClearanceOfficer', 'mrbursary') # Login self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = 'mrbursary' self.browser.getControl(name="form.password").value = SECRET self.browser.getControl("Login").click() # BO can see his roles self.browser.getLink("My Roles").click() self.assertMatches( '...
Bursary Officer
...', self.browser.contents) # BO can view student record self.browser.open(self.student_path) # BO can see clearance button ... self.assertTrue( 'Clear student financially' in self.browser.contents) # ... but not withdraw button self.assertFalse( 'Withdraw financial clearance' in self.browser.contents) # BO can clear student self.browser.getLink("Clear student financially").click() self.assertTrue( 'Student has been financially cleared' in self.browser.contents) # Name of BO and date have been stored self.assertEqual(self.student.financially_cleared_by, 'Carlo Pitter') self.assertMatches( '', self.student.financial_clearance_date.strftime( "%Y-%m-%d %H:%M:%S")) # BO can't see clearance button ... self.assertFalse( 'Clear student financially' in self.browser.contents) # ... but withdraw button and can withdraw clearance self.browser.getLink("Withdraw financial clearance").click() self.assertTrue( 'Financial clearance withdrawn' in self.browser.contents) # Name of BO and date have been deleted self.assertEqual(self.student.financially_cleared_by, None) self.assertEqual(self.student.financial_clearance_date, None) # Clearance is logged logfile = os.path.join( self.app['datacenter'].storage, 'logs', 'students.log') logcontent = open(logfile).read() self.assertTrue( 'mrbursary - kofacustom.nigeria.students.browser.ClearStudentFinancially' ' - K1000000 - financially cleared' in logcontent) self.assertTrue( 'mrbursary - kofacustom.nigeria.students.browser.WithdrawFinancialClearance' ' - K1000000 - financial clearance withdrawn' in logcontent) # Clearance is also stored in the history self.browser.open(self.history_path) self.assertMatches( '...2016-01-16 15:50:48 WAT - Financially cleared by Carlo Pitter...', self.browser.contents) self.assertMatches( '...2016-01-16 15:50:48 WAT - Financial clearance withdrawn by Carlo Pitter...', self.browser.contents) def test_fiancial_clearance_pdf_slip(self): payment1 = createObject(u'waeup.StudentOnlinePayment') timestamp = ("%d" % int(time()*10000))[1:] payment1.p_id = "LSCNEW-2-4153206270" # the longest possible p_id payment1.p_category = 'schoolfee' payment1.p_item = u'My School Fee' payment1.p_session = 2015 payment1.p_level = 100 payment1.p_current = True payment1.amount_auth = 23456.9 payment1.approve() payment2 = createObject(u'waeup.StudentOnlinePayment') timestamp = ("%d" % int(time()*10000))[1:] payment2.p_id = "p%s" % timestamp payment2.p_category = 'clearance' payment2.p_item = u'My Clearance Fee' payment2.p_session = 2015 payment2.p_level = 100 payment2.p_current = True payment2.amount_auth = 5678.6 payment2.approve() self.student['payments'][payment1.p_id] = payment1 self.student['payments'][payment2.p_id] = payment2 self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') self.browser.open(self.student_path + '/clear_financially') self.browser.getLink("Download fee payment history").click() self.assertEqual(self.browser.headers['Status'], '200 Ok') self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') path = os.path.join(samples_dir(), 'fee_payment_history.pdf') open(path, 'wb').write(self.browser.contents) print "Sample PDF fee_payment_history.pdf written to %s" % path return def test_final_clearance(self): self.app['users'].addUser('mrbursary', SECRET) self.app['users']['mrbursary'].email = 'mrbursary@foo.ng' self.app['users']['mrbursary'].title = u'Carl Pitter' # Clearance officers needs to get # the StudentsOfficer site role prmglobal = IPrincipalRoleManager(self.app) prmglobal.assignRoleToPrincipal('waeup.StudentsOfficer', 'mrbursary') # Assign BursaryOfficer role prmglobal.assignRoleToPrincipal('waeup.BursaryOfficer', 'mrbursary') # and FinalClearanceOfficer role prmglobal.assignRoleToPrincipal( 'waeup.FinalClearanceOfficer', 'mrbursary') # Login self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = 'mrbursary' self.browser.getControl(name="form.password").value = SECRET self.browser.getControl("Login").click() # BO can see his roles self.browser.getLink("My Roles").click() self.assertMatches( '...
Bursary Officer
...', self.browser.contents) # BO can view student record self.browser.open(self.student_path) # BO can see clearance button ... self.assertTrue( 'Clear student finally' in self.browser.contents) # ... but not withdraw button self.assertFalse( 'Withdraw final clearance' in self.browser.contents) # BO can clear student self.browser.getLink("Clear student finally").click() self.assertTrue( 'Student has been finally cleared' in self.browser.contents) # Name of BO and date have been stored self.assertEqual(self.student.finally_cleared_by, 'Carl Pitter') self.assertMatches( '', self.student.final_clearance_date.strftime( "%Y-%m-%d %H:%M:%S")) # BO can't see clearance button ... self.assertFalse( 'Clear student finally' in self.browser.contents) # ... but withdraw button and can withdraw clearance self.browser.getLink("Withdraw final clearance").click() self.assertTrue( 'Final clearance withdrawn' in self.browser.contents) # Name of BO and date have been deleted self.assertEqual(self.student.finally_cleared_by, None) self.assertEqual(self.student.final_clearance_date, None) # Clearance is logged logfile = os.path.join( self.app['datacenter'].storage, 'logs', 'students.log') logcontent = open(logfile).read() self.assertTrue( 'mrbursary - kofacustom.nigeria.students.browser.ClearStudentFinally' ' - K1000000 - finally cleared' in logcontent) self.assertTrue( 'mrbursary - kofacustom.nigeria.students.browser.WithdrawFinalClearance' ' - K1000000 - final clearance withdrawn' in logcontent) # Clearance is also stored in the history self.browser.open(self.history_path) self.assertMatches( '...2016-01-16 15:50:48 WAT - Finally cleared by Carl Pitter...', self.browser.contents) self.assertMatches( '...2016-01-16 15:50:48 WAT - Final clearance withdrawn by Carl Pitter...', self.browser.contents) def test_provisionally_cleared(self): # Students can edit clearance data IWorkflowState(self.student).setState('school fee paid') self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = self.student_id self.browser.getControl(name="form.password").value = 'spwd' self.browser.getControl("Login").click() # Student can't access the clearance form self.browser.open(self.student_path) self.browser.getLink("Clearance Data").click() # Student can't open clearance edit form before starting clearance self.browser.open(self.student_path + '/cedit') self.assertMatches('...The requested form is locked...', self.browser.contents) self.assertFalse( '

Edit clearance data

' in self.browser.contents) # If provisionally_cleared is set they can self.student.provisionally_cleared = True self.browser.getLink("Clearance Data").click() self.browser.getLink("Edit").click() self.assertTrue( '

Edit clearance data

' in self.browser.contents) def test_student_redirect_camefrom(self): payment1 = createObject(u'waeup.StudentOnlinePayment') payment1.p_id = "p4153206270" payment1.p_category = 'schoolfee' payment1.p_item = u'My School Fee' payment1.p_session = 2015 payment1.p_level = 100 payment1.p_current = True payment1.amount_auth = 23456.9 payment1.approve() self.student['payments'][payment1.p_id] = payment1 self.browser.open(self.login_path) self.browser.getControl(name="form.login").value = self.student_id self.browser.getControl(name="form.password").value = 'spwd' self.browser.getControl("Login").click() self.assertMatches( '...You logged in...', self.browser.contents) camefrom = '/students/K1000000/studycourse' self.browser.open(self.login_path + '?camefrom=%s' % camefrom) # nothing happens self.assertEqual(self.browser.url, self.login_path + '?camefrom=%s' % camefrom) camefrom = '/students/K1000000/payments/%s/request_webservice' % payment1.p_id # Kofa redirects to request_webservice and then returns to payment ticket index view self.browser.open(self.login_path + '?camefrom=%s' % camefrom) self.assertEqual( self.browser.url, 'http://localhost/app/students/K1000000/payments/p4153206270/@@index')