## $Id: test_browser.py 11054 2014-02-05 07:00:02Z 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 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 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.students.batching import StudentProcessor
from waeup.kofa.students.interfaces import IStudentsUtils
from kofacustom.nigeria.students.batching import NigeriaStudentProcessor
from kofacustom.nigeria.testing import FunctionalLayer
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):
# We have an empty column 'date_of_birth' in the import file.
# The original processor will fail because 'date_of_birth' is required
# in the base package.
num, num_warns, fin_file, fail_file = self.processor_base.doImport(
self.csv_file, STUDENT_HEADER_FIELDS)
self.assertEqual(num_warns,3)
assert len(self.app['students'].keys()) == 0
# The customized processor does not complain since 'date_of_birth' is
# not required in the custom package.
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_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(
''
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)