source: main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_browser.py @ 16105

Last change on this file since 16105 was 16086, checked in by Henrik Bettermann, 5 years ago

Implement ExportPDFBaseDataPlusSlip (without button in the base package)

  • Property svn:keywords set to Id
File size: 251.0 KB
Line 
1# -*- coding: utf-8 -*-
2## $Id: test_browser.py 16086 2020-05-06 13:39:48Z henrik $
3##
4## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
5## This program is free software; you can redistribute it and/or modify
6## it under the terms of the GNU General Public License as published by
7## the Free Software Foundation; either version 2 of the License, or
8## (at your option) any later version.
9##
10## This program is distributed in the hope that it will be useful,
11## but WITHOUT ANY WARRANTY; without even the implied warranty of
12## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13## GNU General Public License for more details.
14##
15## You should have received a copy of the GNU General Public License
16## along with this program; if not, write to the Free Software
17## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18##
19"""
20Test the student-related UI components.
21"""
22import shutil
23import tempfile
24import pytz
25import base64
26from datetime import datetime, timedelta, date
27from StringIO import StringIO
28import os
29import grok
30from zc.async.testing import wait_for_result
31from zope.event import notify
32from zope.component import createObject, queryUtility, getUtility
33from zope.component.hooks import setSite, clearSite
34from zope.catalog.interfaces import ICatalog
35from zope.security.interfaces import Unauthorized
36from zope.securitypolicy.interfaces import IPrincipalRoleManager
37from zope.testbrowser.testing import Browser
38from zope.interface import implementedBy
39from zope.schema.fieldproperty import FieldProperty
40from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
41from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
42from waeup.kofa.app import University
43from waeup.kofa.interfaces import IFileStoreNameChooser, IExtFileStore
44from waeup.kofa.payments.interfaces import IPayer
45from waeup.kofa.students.payments import StudentOnlinePayment
46from waeup.kofa.students.student import Student
47from waeup.kofa.students.studylevel import StudentStudyLevel
48from waeup.kofa.university.faculty import Faculty
49from waeup.kofa.university.department import Department
50from waeup.kofa.interfaces import IUserAccount, IJobManager, VALIDATED, CREATED
51from waeup.kofa.authentication import LocalRoleSetEvent
52from waeup.kofa.hostels.hostel import Hostel, Bed, NOT_OCCUPIED
53from waeup.kofa.tests.test_async import FunctionalAsyncTestCase
54from waeup.kofa.browser.tests.test_pdf import samples_dir
55from waeup.kofa.tests.test_authentication import SECRET
56
57PH_LEN = 15911  # Length of placeholder file
58
59SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
60SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp')
61URL_LECTURER_LANDING = 'http://localhost/app/my_courses'
62
63curr_year = datetime.now().year
64
65def lookup_submit_value(name, value, browser):
66    """Find a button with a certain value."""
67    for num in range(0, 100):
68        try:
69            button = browser.getControl(name=name, index=num)
70            if button.value.endswith(value):
71                return button
72        except IndexError:
73            break
74    return None
75
76
77class StudentsFullSetup(FunctionalTestCase):
78    # A test case that only contains a setup and teardown
79    #
80    # Complete setup for students handlings is rather complex and
81    # requires lots of things created before we can start. This is a
82    # setup that does all this, creates a university, creates PINs,
83    # etc.  so that we do not have to bother with that in different
84    # test cases.
85
86    layer = FunctionalLayer
87
88    def setUp(self):
89        super(StudentsFullSetup, self).setUp()
90
91        # Setup a sample site for each test
92        app = University()
93        self.dc_root = tempfile.mkdtemp()
94        app['datacenter'].setStoragePath(self.dc_root)
95
96        # Prepopulate the ZODB...
97        self.getRootFolder()['app'] = app
98        # we add the site immediately after creation to the
99        # ZODB. Catalogs and other local utilities are not setup
100        # before that step.
101        self.app = self.getRootFolder()['app']
102        # Set site here. Some of the following setup code might need
103        # to access grok.getSite() and should get our new app then
104        setSite(app)
105
106        # Add student with subobjects
107        student = createObject('waeup.Student')
108        student.firstname = u'Anna'
109        student.lastname = u'Tester'
110        student.reg_number = u'123'
111        student.matric_number = u'234'
112        student.sex = u'm'
113        student.email = 'aa@aa.ng'
114        student.phone = u'1234'
115        student.date_of_birth = date(1981, 2, 4)
116        self.app['students'].addStudent(student)
117        self.student_id = student.student_id
118        self.student = self.app['students'][self.student_id]
119
120        # Set password
121        IUserAccount(
122            self.app['students'][self.student_id]).setPassword('spwd')
123
124        self.login_path = 'http://localhost/app/login'
125        self.container_path = 'http://localhost/app/students'
126        self.manage_container_path = self.container_path + '/@@manage'
127        self.add_student_path = self.container_path + '/addstudent'
128        self.student_path = self.container_path + '/' + self.student_id
129        self.manage_student_path = self.student_path + '/manage_base'
130        self.trigtrans_path = self.student_path + '/trigtrans'
131        self.clearance_path = self.student_path + '/view_clearance'
132        self.personal_path = self.student_path + '/view_personal'
133        self.edit_clearance_path = self.student_path + '/cedit'
134        self.manage_clearance_path = self.student_path + '/manage_clearance'
135        self.edit_personal_path = self.student_path + '/edit_personal'
136        self.manage_personal_path = self.student_path + '/manage_personal'
137        self.studycourse_path = self.student_path + '/studycourse'
138        self.payments_path = self.student_path + '/payments'
139        self.acco_path = self.student_path + '/accommodation'
140        self.history_path = self.student_path + '/history'
141
142        # Create 5 access codes with prefix'PWD'
143        pin_container = self.app['accesscodes']
144        pin_container.createBatch(
145            datetime.utcnow(), 'some_userid', 'PWD', 9.99, 5)
146        pins = pin_container['PWD-1'].values()
147        self.pwdpins = [x.representation for x in pins]
148        self.existing_pwdpin = self.pwdpins[0]
149        parts = self.existing_pwdpin.split('-')[1:]
150        self.existing_pwdseries, self.existing_pwdnumber = parts
151        # Create 5 access codes with prefix 'CLR'
152        pin_container.createBatch(
153            datetime.now(), 'some_userid', 'CLR', 9.99, 5)
154        pins = pin_container['CLR-1'].values()
155        pins[0].owner = u'Hans Wurst'
156        self.existing_clrac = pins[0]
157        self.existing_clrpin = pins[0].representation
158        parts = self.existing_clrpin.split('-')[1:]
159        self.existing_clrseries, self.existing_clrnumber = parts
160        # Create 2 access codes with prefix 'HOS'
161        pin_container.createBatch(
162            datetime.now(), 'some_userid', 'HOS', 9.99, 2)
163        pins = pin_container['HOS-1'].values()
164        self.existing_hosac = pins[0]
165        self.existing_hospin = pins[0].representation
166        parts = self.existing_hospin.split('-')[1:]
167        self.existing_hosseries, self.existing_hosnumber = parts
168
169        # Populate university
170        self.certificate = createObject('waeup.Certificate')
171        self.certificate.code = u'CERT1'
172        self.certificate.application_category = 'basic'
173        self.certificate.study_mode = 'ug_ft'
174        self.certificate.start_level = 100
175        self.certificate.end_level = 500
176        self.certificate.school_fee_1 = 40000.0
177        self.certificate.school_fee_2 = 20000.0
178        self.app['faculties']['fac1'] = Faculty(code=u'fac1')
179        self.app['faculties']['fac1']['dep1'] = Department(code=u'dep1')
180        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
181            self.certificate)
182        self.course = createObject('waeup.Course')
183        self.course.code = 'COURSE1'
184        self.course.semester = 1
185        self.course.credits = 10
186        self.course.passmark = 40
187        self.app['faculties']['fac1']['dep1'].courses.addCourse(
188            self.course)
189        self.app['faculties']['fac1']['dep1'].certificates[
190            'CERT1'].addCertCourse(self.course, level=100)
191
192        # Configure university and hostels
193        self.app['hostels'].accommodation_states = ['admitted']
194        self.app['hostels'].accommodation_session = 2004
195        delta = timedelta(days=10)
196        self.app['hostels'].startdate = datetime.now(pytz.utc) - delta
197        self.app['hostels'].enddate = datetime.now(pytz.utc) + delta
198        self.app['configuration'].carry_over = True
199        configuration = createObject('waeup.SessionConfiguration')
200        configuration.academic_session = 2004
201        configuration.clearance_fee = 3456.0
202        configuration.transcript_fee = 4567.0
203        configuration.booking_fee = 123.4
204        configuration.maint_fee = 987.0
205        configuration.transfer_fee = 456.0
206        configuration.late_registration_fee = 345.0
207        self.app['configuration'].addSessionConfiguration(configuration)
208
209        # Create a hostel with two beds
210        hostel = Hostel()
211        hostel.hostel_id = u'hall-1'
212        hostel.hostel_name = u'Hall 1'
213        hostel.maint_fee = 876.0
214        self.app['hostels'].addHostel(hostel)
215        bed = Bed()
216        bed.bed_id = u'hall-1_A_101_A'
217        bed.bed_number = 1
218        bed.owner = NOT_OCCUPIED
219        bed.bed_type = u'regular_male_fr'
220        self.app['hostels'][hostel.hostel_id].addBed(bed)
221        bed = Bed()
222        bed.bed_id = u'hall-1_A_101_B'
223        bed.bed_number = 2
224        bed.owner = NOT_OCCUPIED
225        bed.bed_type = u'regular_female_fr'
226        self.app['hostels'][hostel.hostel_id].addBed(bed)
227
228        # Set study course attributes of test student
229        self.student['studycourse'].certificate = self.certificate
230        self.student['studycourse'].current_session = 2004
231        self.student['studycourse'].entry_session = 2004
232        self.student['studycourse'].current_verdict = 'A'
233        self.student['studycourse'].current_level = 100
234        # Update the catalog
235        notify(grok.ObjectModifiedEvent(self.student))
236
237        # Put the prepopulated site into test ZODB and prepare test
238        # browser
239        self.browser = Browser()
240        self.browser.handleErrors = False
241
242    def tearDown(self):
243        super(StudentsFullSetup, self).tearDown()
244        clearSite()
245        shutil.rmtree(self.dc_root)
246
247
248class StudentsContainerUITests(StudentsFullSetup):
249    # Tests for StudentsContainer class views and pages
250
251    layer = FunctionalLayer
252
253    def test_anonymous_access(self):
254        # Anonymous users can't access students containers
255        self.assertRaises(
256            Unauthorized, self.browser.open, self.container_path)
257        self.assertRaises(
258            Unauthorized, self.browser.open, self.manage_container_path)
259        return
260
261    def test_manage_access(self):
262        # Managers can access the view page of students
263        # containers and can perform actions
264        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
265        self.browser.open(self.container_path)
266        self.assertEqual(self.browser.headers['Status'], '200 Ok')
267        self.assertEqual(self.browser.url, self.container_path)
268        self.browser.getLink("Manage students section").click()
269        self.assertEqual(self.browser.headers['Status'], '200 Ok')
270        self.assertEqual(self.browser.url, self.manage_container_path)
271        return
272
273    def test_add_search_delete_students(self):
274        # Managers can add search and remove students
275        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
276        self.browser.open(self.manage_container_path)
277        self.browser.getLink("Add student").click()
278        self.assertEqual(self.browser.headers['Status'], '200 Ok')
279        self.assertEqual(self.browser.url, self.add_student_path)
280        self.browser.getControl(name="form.firstname").value = 'Bob'
281        self.browser.getControl(name="form.lastname").value = 'Tester'
282        self.browser.getControl(name="form.reg_number").value = '123'
283        self.browser.getControl("Create student").click()
284        self.assertTrue(
285            'Registration number exists already' in self.browser.contents)
286        self.browser.getControl(name="form.reg_number").value = '1234'
287        self.browser.getControl("Create student").click()
288        self.assertTrue('Student record created' in self.browser.contents)
289
290        # Registration and matric numbers must be unique
291        self.browser.getLink("Manage").click()
292        self.browser.getControl(name="form.reg_number").value = '123'
293        self.browser.getControl("Save").click()
294        self.assertMatches('...Registration number exists...',
295                           self.browser.contents)
296        self.browser.getControl(name="form.reg_number").value = '789'
297        self.browser.getControl(name="form.matric_number").value = '234'
298        self.browser.getControl("Save").click()
299        self.assertMatches('...Matriculation number exists...',
300                           self.browser.contents)
301
302        # We can find a student with a certain student_id
303        self.browser.open(self.container_path)
304        self.browser.getControl("Find student(s)").click()
305        self.assertTrue('Empty search string' in self.browser.contents)
306        self.browser.getControl(name="searchtype").value = ['student_id']
307        self.browser.getControl(name="searchterm").value = self.student_id
308        self.browser.getControl("Find student(s)").click()
309        self.assertTrue('Anna Tester' in self.browser.contents)
310
311        # We can find a student in a certain session
312        self.browser.open(self.container_path)
313        self.browser.getControl(name="searchtype").value = ['current_session']
314        self.browser.getControl(name="searchterm").value = '2004'
315        self.browser.getControl("Find student(s)").click()
316        self.assertTrue('Anna Tester' in self.browser.contents)
317        # Session fileds require integer values
318        self.browser.open(self.container_path)
319        self.browser.getControl(name="searchtype").value = ['current_session']
320        self.browser.getControl(name="searchterm").value = '2004/2005'
321        self.browser.getControl("Find student(s)").click()
322        self.assertTrue('Only year dates allowed' in self.browser.contents)
323        self.browser.open(self.manage_container_path)
324        self.browser.getControl(name="searchtype").value = ['current_session']
325        self.browser.getControl(name="searchterm").value = '2004/2005'
326        self.browser.getControl("Find student(s)").click()
327        self.assertTrue('Only year dates allowed' in self.browser.contents)
328
329        # We can find a student in a certain study_mode
330        self.browser.open(self.container_path)
331        self.browser.getControl(name="searchtype").value = ['current_mode']
332        self.browser.getControl(name="searchterm").value = 'ug_ft'
333        self.browser.getControl("Find student(s)").click()
334        self.assertTrue('Anna Tester' in self.browser.contents)
335
336        # We can find a student in a certain department
337        self.browser.open(self.container_path)
338        self.browser.getControl(name="searchtype").value = ['depcode']
339        self.browser.getControl(name="searchterm").value = 'dep1'
340        self.browser.getControl("Find student(s)").click()
341        self.assertTrue('Anna Tester' in self.browser.contents)
342
343        # We can find a student by searching for all kind of name parts
344        self.browser.open(self.manage_container_path)
345        self.browser.getControl("Find student(s)").click()
346        self.assertTrue('Empty search string' in self.browser.contents)
347        self.browser.getControl(name="searchtype").value = ['fullname']
348        self.browser.getControl(name="searchterm").value = 'Anna Tester'
349        self.browser.getControl("Find student(s)").click()
350        self.assertTrue('Anna Tester' in self.browser.contents)
351        self.browser.open(self.manage_container_path)
352        self.browser.getControl(name="searchtype").value = ['fullname']
353        self.browser.getControl(name="searchterm").value = 'Anna'
354        self.browser.getControl("Find student(s)").click()
355        self.assertTrue('Anna Tester' in self.browser.contents)
356        self.browser.open(self.manage_container_path)
357        self.browser.getControl(name="searchtype").value = ['fullname']
358        self.browser.getControl(name="searchterm").value = 'Tester'
359        self.browser.getControl("Find student(s)").click()
360        self.assertTrue('Anna Tester' in self.browser.contents)
361        self.browser.open(self.manage_container_path)
362        self.browser.getControl(name="searchtype").value = ['fullname']
363        self.browser.getControl(name="searchterm").value = 'An'
364        self.browser.getControl("Find student(s)").click()
365        self.assertFalse('Anna Tester' in self.browser.contents)
366        self.browser.open(self.manage_container_path)
367        self.browser.getControl(name="searchtype").value = ['fullname']
368        self.browser.getControl(name="searchterm").value = 'An*'
369        self.browser.getControl("Find student(s)").click()
370        self.assertTrue('Anna Tester' in self.browser.contents)
371        self.browser.open(self.manage_container_path)
372        self.browser.getControl(name="searchtype").value = ['fullname']
373        self.browser.getControl(name="searchterm").value = 'tester'
374        self.browser.getControl("Find student(s)").click()
375        self.assertTrue('Anna Tester' in self.browser.contents)
376        self.browser.open(self.manage_container_path)
377        self.browser.getControl(name="searchtype").value = ['fullname']
378        self.browser.getControl(name="searchterm").value = 'Tester Ana'
379        self.browser.getControl("Find student(s)").click()
380        self.assertFalse('Anna Tester' in self.browser.contents)
381        self.browser.open(self.manage_container_path)
382        self.browser.getControl(name="searchtype").value = ['fullname']
383        self.browser.getControl(name="searchterm").value = 'Tester Anna'
384        self.browser.getControl("Find student(s)").click()
385        self.assertTrue('Anna Tester' in self.browser.contents)
386        # The old searchterm will be used again
387        self.browser.getControl("Find student(s)").click()
388        self.assertTrue('Anna Tester' in self.browser.contents)
389
390        # We can find suspended students
391        self.student.suspended = True
392        notify(grok.ObjectModifiedEvent(self.student))
393        self.browser.open(self.manage_container_path)
394        self.browser.getControl(name="searchtype").value = ['suspended']
395        self.browser.getControl("Find student(s)").click()
396        self.assertTrue('Anna Tester' in self.browser.contents)
397        self.browser.open(self.container_path)
398        self.browser.getControl(name="searchtype").value = ['suspended']
399        self.browser.getControl("Find student(s)").click()
400        self.assertTrue('Anna Tester' in self.browser.contents)
401
402        # The catalog is informed when studycourse objects have been
403        # edited
404        self.browser.open(self.studycourse_path + '/manage')
405        self.browser.getControl(name="form.current_session").value = ['2010']
406        self.browser.getControl(name="form.entry_session").value = ['2010']
407        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
408        self.browser.getControl("Save").click()
409
410        # We can find the student in the new session
411        self.browser.open(self.manage_container_path)
412        self.browser.getControl(name="searchtype").value = ['current_session']
413        self.browser.getControl(name="searchterm").value = '2010'
414        self.browser.getControl("Find student(s)").click()
415        self.assertTrue('Anna Tester' in self.browser.contents)
416
417        ctrl = self.browser.getControl(name='entries')
418        ctrl.getControl(value=self.student_id).selected = True
419        self.browser.getControl("Remove selected", index=0).click()
420        self.assertTrue('Successfully removed' in self.browser.contents)
421        self.browser.getControl(name="searchtype").value = ['student_id']
422        self.browser.getControl(name="searchterm").value = self.student_id
423        self.browser.getControl("Find student(s)").click()
424        self.assertTrue('No student found' in self.browser.contents)
425
426        self.browser.open(self.container_path)
427        self.browser.getControl(name="searchtype").value = ['student_id']
428        self.browser.getControl(name="searchterm").value = self.student_id
429        self.browser.getControl("Find student(s)").click()
430        self.assertTrue('No student found' in self.browser.contents)
431        return
432
433    def test_add_graduated_students(self):
434        # Managers can add search and remove students
435        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
436        self.browser.open(self.manage_container_path)
437        self.browser.getLink("Add student").click()
438        self.assertEqual(self.browser.headers['Status'], '200 Ok')
439        self.assertEqual(self.browser.url, self.add_student_path)
440        self.browser.getControl(name="form.firstname").value = 'Bob'
441        self.browser.getControl(name="form.lastname").value = 'Tester'
442        self.browser.getControl(name="form.reg_number").value = '1234'
443        self.browser.getControl("Create graduated student").click()
444        self.assertTrue('Graduated student record created' in self.browser.contents)
445        self.assertEqual(self.app['students']['K1000001'].state, 'graduated')
446        self.browser.open("http://localhost/app/students/K1000001/history")
447        self.assertTrue("State 'graduated' set by Manager" in self.browser.contents)
448        return
449
450
451class OfficerUITests(StudentsFullSetup):
452    # Tests for Student class views and pages
453
454    def test_student_properties(self):
455        self.student['studycourse'].current_level = 100
456        self.assertEqual(self.student.current_level, 100)
457        self.student['studycourse'].current_session = 2011
458        self.assertEqual(self.student.current_session, 2011)
459        self.student['studycourse'].current_verdict = 'A'
460        self.assertEqual(self.student.current_verdict, 'A')
461        return
462
463    def test_studylevelmanagepage(self):
464        studylevel = StudentStudyLevel()
465        studylevel.level = 100
466        cert = self.app['faculties']['fac1']['dep1'].certificates['CERT1']
467        self.student['studycourse'].addStudentStudyLevel(cert, studylevel)
468        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
469        self.browser.open(self.studycourse_path + '/100/manage')
470        self.assertEqual(
471            self.browser.url, self.studycourse_path + '/100/manage')
472        self.assertEqual(self.browser.headers['Status'], '200 Ok')
473
474    def test_basic_auth(self):
475        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
476        self.browser.open('http://localhost/app')
477        self.browser.getLink("Logout").click()
478        self.assertTrue('You have been logged out' in self.browser.contents)
479        # But we are still logged in since we've used basic
480        # authentication here.  Wikipedia says: Existing browsers
481        # retain authentication information until the tab or browser
482        # is closed or the user clears the history.  HTTP does not
483        # provide a method for a server to direct clients to discard
484        # these cached credentials. This means that there is no
485        # effective way for a server to "log out" the user without
486        # closing the browser. This is a significant defect that
487        # requires browser manufacturers to support a "logout" user
488        # interface element ...
489        self.assertTrue('Manager' in self.browser.contents)
490
491    def test_basic_auth_base64(self):
492        auth_token = base64.b64encode('mgr:mgrpw')
493        self.browser.addHeader('Authorization', 'Basic %s' % auth_token)
494        self.browser.open(self.manage_container_path)
495        self.assertEqual(self.browser.headers['Status'], '200 Ok')
496
497    def test_manage_access(self):
498        # Managers can access the pages of students
499        # and can perform actions
500        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
501        self.browser.open(self.student_path)
502        self.assertEqual(self.browser.headers['Status'], '200 Ok')
503        self.assertEqual(self.browser.url, self.student_path)
504        self.browser.getLink("Trigger").click()
505        self.assertEqual(self.browser.headers['Status'], '200 Ok')
506        # Managers can trigger transitions
507        self.browser.getControl(name="transition").value = ['admit']
508        self.browser.getControl("Save").click()
509        # Managers can edit base
510        self.browser.open(self.student_path)
511        self.browser.getLink("Manage").click()
512        self.assertEqual(self.browser.url, self.manage_student_path)
513        self.assertEqual(self.browser.headers['Status'], '200 Ok')
514        self.browser.getControl(name="form.firstname").value = 'John'
515        self.browser.getControl(name="form.lastname").value = 'Tester'
516        self.browser.getControl(name="form.reg_number").value = '345'
517        self.browser.getControl(name="password").value = 'secret'
518        self.browser.getControl(name="control_password").value = 'secret'
519        self.browser.getControl("Save").click()
520        self.assertMatches('...Form has been saved...',
521                           self.browser.contents)
522        self.browser.open(self.student_path)
523        self.browser.getLink("Clearance Data").click()
524        self.assertEqual(self.browser.headers['Status'], '200 Ok')
525        self.assertEqual(self.browser.url, self.clearance_path)
526        self.browser.getLink("Manage").click()
527        self.assertEqual(self.browser.headers['Status'], '200 Ok')
528        self.assertEqual(self.browser.url, self.manage_clearance_path)
529        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
530        self.browser.getControl("Save").click()
531        self.assertMatches('...Form has been saved...',
532                           self.browser.contents)
533
534        self.browser.open(self.student_path)
535        self.browser.getLink("Personal Data").click()
536        self.assertEqual(self.browser.headers['Status'], '200 Ok')
537        self.assertEqual(self.browser.url, self.personal_path)
538        self.browser.getLink("Manage").click()
539        self.assertEqual(self.browser.headers['Status'], '200 Ok')
540        self.assertEqual(self.browser.url, self.manage_personal_path)
541        self.browser.open(self.personal_path)
542        self.assertTrue('Updated' in self.browser.contents)
543        self.browser.getLink("Edit").click()
544        self.assertEqual(self.browser.headers['Status'], '200 Ok')
545        self.assertEqual(self.browser.url, self.edit_personal_path)
546        self.browser.getControl("Save").click()
547        # perm_address is required in IStudentPersonalEdit
548        self.assertMatches('...Required input is missing...',
549                           self.browser.contents)
550        self.browser.getControl(name="form.perm_address").value = 'My address!'
551        self.browser.getControl("Save").click()
552        self.assertMatches('...Form has been saved...',
553                           self.browser.contents)
554
555        # Managers can browse all subobjects
556        self.browser.open(self.student_path)
557        self.browser.getLink("Payments").click()
558        self.assertEqual(self.browser.headers['Status'], '200 Ok')
559        self.assertEqual(self.browser.url, self.payments_path)
560        self.browser.open(self.student_path)
561        self.browser.getLink("Accommodation").click()
562        self.assertEqual(self.browser.headers['Status'], '200 Ok')
563        # We have been redirected to the manage page
564        self.assertEqual(self.browser.url, self.acco_path + '/manage')
565        self.browser.open(self.student_path)
566        self.browser.getLink("History").click()
567        self.assertEqual(self.browser.headers['Status'], '200 Ok')
568        self.assertEqual(self.browser.url, self.history_path)
569        self.assertMatches('...Admitted by Manager...',
570                           self.browser.contents)
571        # Only the Application Slip does not exist
572        self.assertFalse('Application Slip' in self.browser.contents)
573        return
574
575    def test_flash_notice(self):
576        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
577        self.browser.open(self.student_path)
578        self.assertFalse('alert alert-warning' in self.browser.contents)
579        self.student.flash_notice = u'Happy Birthday!'
580        self.browser.open(self.student_path)
581        self.assertTrue(
582            '<div><div class="alert alert-warning">Happy Birthday!</div>'
583            in self.browser.contents)
584        return
585
586    def test_manage_contact_student(self):
587        # Managers can contact student
588        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
589        # Remove required FieldProperty attribute first ...
590        delattr(Student, 'email')
591        # ... and replace by None
592        self.student.email = None
593        # Now we have to add  the FieldProperty attribute again. Otherwise
594        # many other tests below will fail.
595        iface = list(implementedBy(Student))[0]
596        field_property = FieldProperty(iface['email'])
597        setattr(Student, 'email', field_property)
598        self.browser.open(self.student_path)
599        self.browser.getLink("Send email").click()
600        self.browser.getControl(
601            name="form.subject").value = 'Important subject'
602        self.browser.getControl(name="form.body").value = 'Hello!'
603        self.browser.getControl("Send message now").click()
604        self.assertTrue(
605            'An smtp server error occurred' in self.browser.contents)
606        self.student.email = 'xx@yy.zz'
607        self.browser.getControl("Send message now").click()
608        self.assertTrue('Your message has been sent' in self.browser.contents)
609        return
610
611    def test_manage_remove_department(self):
612        # Lazy student is studying CERT1
613        lazystudent = Student()
614        lazystudent.firstname = u'Lazy'
615        lazystudent.lastname = u'Student'
616        self.app['students'].addStudent(lazystudent)
617        student_id = lazystudent.student_id
618        student_path = self.container_path + '/' + student_id
619        lazystudent['studycourse'].certificate = self.certificate
620        notify(grok.ObjectModifiedEvent(lazystudent))
621        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
622        self.browser.open(student_path + '/studycourse')
623        self.assertTrue('CERT1' in self.browser.contents)
624        # After some years the department is removed
625        del self.app['faculties']['fac1']['dep1']
626        # So CERT1 does no longer exist and lazy student's
627        # certificate reference is removed too
628        self.browser.open(student_path + '/studycourse')
629        self.assertEqual(self.browser.headers['Status'], '200 Ok')
630        self.assertEqual(self.browser.url, student_path + '/studycourse')
631        self.assertFalse('CERT1' in self.browser.contents)
632        self.assertMatches('...<div>--</div>...',
633                           self.browser.contents)
634
635    def test_manage_upload_file(self):
636        # Managers can upload a file via the StudentClearanceManageFormPage
637        # The image is stored even if form has errors
638        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
639        self.browser.open(self.manage_clearance_path)
640        # No birth certificate has been uploaded yet
641        # Browsing the link shows a placerholder image
642        self.browser.open('birth_certificate')
643        self.assertEqual(
644            self.browser.headers['content-type'], 'image/jpeg')
645        self.assertEqual(len(self.browser.contents), PH_LEN)
646        # Create a pseudo image file and select it to be uploaded in form
647        # as birth certificate
648        self.browser.open(self.manage_clearance_path)
649        image = open(SAMPLE_IMAGE, 'rb')
650        ctrl = self.browser.getControl(name='birthcertificateupload')
651        file_ctrl = ctrl.mech_control
652        file_ctrl.add_file(image, filename='my_birth_certificate.jpg')
653        # The Save action does not upload files
654        self.browser.getControl("Save").click()  # submit form
655        self.assertFalse(
656            '<a target="image" href="birth_certificate">'
657            in self.browser.contents)
658        # ... but the correct upload submit button does
659        image = open(SAMPLE_IMAGE)
660        ctrl = self.browser.getControl(name='birthcertificateupload')
661        file_ctrl = ctrl.mech_control
662        file_ctrl.add_file(image, filename='my_birth_certificate.jpg')
663        self.browser.getControl(
664            name='upload_birthcertificateupload').click()
665        # There is a correct <img> link included
666        self.assertTrue(
667            'href="http://localhost/app/students/K1000000/birth_certificate"'
668            in self.browser.contents)
669        # Browsing the link shows a real image
670        self.browser.open('birth_certificate')
671        self.assertEqual(
672            self.browser.headers['content-type'], 'image/jpeg')
673        self.assertEqual(len(self.browser.contents), 2787)
674        # We can't reupload a file. The existing file must be deleted first.
675        self.browser.open(self.manage_clearance_path)
676        self.assertFalse(
677            'upload_birthcertificateupload' in self.browser.contents)
678        # File must be deleted first
679        self.browser.getControl(name='delete_birthcertificateupload').click()
680        self.assertTrue(
681            'birth_certificate deleted' in self.browser.contents)
682        # Uploading a file which is bigger than 250k will raise an error
683        big_image = StringIO(open(SAMPLE_IMAGE, 'rb').read() * 100)
684        ctrl = self.browser.getControl(name='birthcertificateupload')
685        file_ctrl = ctrl.mech_control
686        file_ctrl.add_file(big_image, filename='my_birth_certificate.jpg')
687        self.browser.getControl(
688            name='upload_birthcertificateupload').click()
689        self.assertTrue(
690            'Uploaded file is too big' in self.browser.contents)
691        # we do not rely on filename extensions given by uploaders
692        image = open(SAMPLE_IMAGE, 'rb')  # a jpg-file
693        ctrl = self.browser.getControl(name='birthcertificateupload')
694        file_ctrl = ctrl.mech_control
695        # tell uploaded file is bmp
696        file_ctrl.add_file(image, filename='my_birth_certificate.bmp')
697        self.browser.getControl(
698            name='upload_birthcertificateupload').click()
699        self.assertTrue(
700            # jpg file was recognized
701            'File birth_certificate.jpg uploaded.' in self.browser.contents)
702        # Delete file again
703        self.browser.getControl(name='delete_birthcertificateupload').click()
704        self.assertTrue(
705            'birth_certificate deleted' in self.browser.contents)
706        # File names must meet several conditions
707        bmp_image = open(SAMPLE_IMAGE_BMP, 'rb')
708        ctrl = self.browser.getControl(name='birthcertificateupload')
709        file_ctrl = ctrl.mech_control
710        file_ctrl.add_file(bmp_image, filename='my_birth_certificate.bmp')
711        self.browser.getControl(
712            name='upload_birthcertificateupload').click()
713        self.assertTrue(
714            'Only the following extensions are allowed'
715            in self.browser.contents)
716
717        # Managers can upload a file via the StudentBaseManageFormPage
718        self.browser.open(self.manage_student_path)
719        image = open(SAMPLE_IMAGE_BMP, 'rb')
720        ctrl = self.browser.getControl(name='passportuploadmanage')
721        file_ctrl = ctrl.mech_control
722        file_ctrl.add_file(image, filename='my_photo.bmp')
723        self.browser.getControl(
724            name='upload_passportuploadmanage').click()
725        self.assertTrue(
726            'jpg file format expected' in self.browser.contents)
727        ctrl = self.browser.getControl(name='passportuploadmanage')
728        file_ctrl = ctrl.mech_control
729        image = open(SAMPLE_IMAGE, 'rb')
730        file_ctrl.add_file(image, filename='my_photo.jpg')
731        self.browser.getControl(
732            name='upload_passportuploadmanage').click()
733        self.assertTrue(
734            'src="http://localhost/app/students/K1000000/passport.jpg"'
735            in self.browser.contents)
736        # We remove the passport file again
737        self.browser.open(self.manage_student_path)
738        self.browser.getControl('Delete').click()
739        self.browser.open(self.student_path + '/clearance_slip.pdf')
740        self.assertEqual(self.browser.headers['Status'], '200 Ok')
741        self.assertEqual(self.browser.headers['Content-Type'],
742                         'application/pdf')
743        # We want to see the signature fields.
744        IWorkflowState(self.student).setState('cleared')
745        self.browser.open(self.student_path + '/clearance_slip.pdf')
746        self.assertEqual(self.browser.headers['Status'], '200 Ok')
747        self.assertEqual(self.browser.headers['Content-Type'],
748                         'application/pdf')
749        path = os.path.join(samples_dir(), 'clearance_slip.pdf')
750        open(path, 'wb').write(self.browser.contents)
751        print "Sample PDF clearance_slip.pdf written to %s" % path
752
753    def test_manage_course_lists(self):
754        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
755        self.browser.open(self.student_path)
756        self.browser.getLink("Study Course").click()
757        self.assertEqual(self.browser.headers['Status'], '200 Ok')
758        self.assertEqual(self.browser.url, self.studycourse_path)
759        self.assertTrue('Undergraduate Full-Time' in self.browser.contents)
760        self.browser.getLink("Manage").click()
761        self.assertTrue('Manage study course' in self.browser.contents)
762        # Before we can select a level, the certificate must
763        # be selected and saved
764        self.browser.getControl(name="form.certificate").value = ['CERT1']
765        self.browser.getControl(name="form.current_session").value = ['2004']
766        self.browser.getControl(name="form.current_verdict").value = ['A']
767        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
768        self.browser.getControl("Save").click()
769        # Now we can save also the current level which depends on start and end
770        # level of the certificate
771        self.browser.getControl(name="form.current_level").value = ['100']
772        self.browser.getControl("Save").click()
773        # Managers can add and remove any study level (course list)
774        self.browser.getControl(name="addlevel").value = ['100']
775        self.browser.getControl("Add study level").click()
776        self.assertMatches(
777            '...You must select a session...', self.browser.contents)
778        self.browser.getControl(name="addlevel").value = ['100']
779        self.browser.getControl(name="level_session").value = ['2004']
780        self.browser.getControl("Add study level").click()
781        self.assertMatches('...<span>100</span>...', self.browser.contents)
782        self.assertEqual(self.student['studycourse']['100'].level, 100)
783        self.assertEqual(
784            self.student['studycourse']['100'].level_session, 2004)
785        self.browser.getControl(name="addlevel").value = ['100']
786        self.browser.getControl(name="level_session").value = ['2004']
787        self.browser.getControl("Add study level").click()
788        self.assertMatches('...This level exists...', self.browser.contents)
789        self.browser.getControl("Remove selected").click()
790        self.assertMatches(
791            '...No study level selected...', self.browser.contents)
792        self.browser.getControl(name="val_id").value = ['100']
793        self.browser.getControl(name="level_session").value = ['2004']
794        self.browser.getControl("Remove selected").click()
795        self.assertMatches('...Successfully removed...', self.browser.contents)
796        # Removing levels is properly logged
797        logfile = os.path.join(
798            self.app['datacenter'].storage, 'logs', 'students.log')
799        logcontent = open(logfile).read()
800        self.assertTrue(
801            'zope.mgr - students.browser.StudyCourseManageFormPage '
802            '- K1000000 - removed: 100' in logcontent)
803        # Add level again
804        self.browser.getControl(name="addlevel").value = ['100']
805        self.browser.getControl(name="level_session").value = ['2004']
806        self.browser.getControl("Add study level").click()
807
808        # Managers can view and manage course lists
809        self.browser.getLink("100").click()
810        self.assertMatches(
811            '...: 100 (Year 1)...', self.browser.contents)
812        self.browser.getLink("Manage").click()
813        self.browser.getControl(name="form.level_session").value = ['2002']
814        self.browser.getControl("Save").click()
815        self.browser.getControl("Remove selected").click()
816        self.assertMatches('...No ticket selected...', self.browser.contents)
817        ctrl = self.browser.getControl(name='val_id')
818        ctrl.getControl(value='COURSE1').selected = True
819        self.browser.getControl("Remove selected", index=0).click()
820        self.assertTrue('Successfully removed' in self.browser.contents)
821        # Removing course tickets is properly logged
822        logfile = os.path.join(
823            self.app['datacenter'].storage, 'logs', 'students.log')
824        logcontent = open(logfile).read()
825        self.assertTrue(
826            'zope.mgr - students.browser.StudyLevelManageFormPage '
827            '- K1000000 - level 100 - removed: COURSE1' in logcontent)
828        self.browser.getLink("here").click()
829        self.browser.getControl(name="form.course").value = ['COURSE1']
830        self.course.credits = 100
831        self.browser.getControl("Add course ticket").click()
832        self.assertMatches(
833            '...Maximum credits exceeded...', self.browser.contents)
834        self.course.credits = 10
835        self.browser.getControl("Add course ticket").click()
836        self.assertTrue('Successfully added' in self.browser.contents)
837        # We can do the same by adding the course on the manage page directly
838        del self.student['studycourse']['100']['COURSE1']
839        self.browser.getControl(name="course").value = 'COURSE1'
840        self.browser.getControl("Add course ticket").click()
841        self.assertTrue('Successfully added' in self.browser.contents)
842        self.browser.getLink("here").click()
843        self.browser.getControl(name="form.course").value = ['COURSE1']
844        self.browser.getControl("Add course ticket").click()
845        self.assertTrue('The ticket exists' in self.browser.contents)
846        self.browser.getControl("Cancel").click()
847        self.browser.getLink("COURSE1").click()
848        self.browser.getLink("Manage").click()
849        self.browser.getControl("Save").click()
850        self.assertTrue('Form has been saved' in self.browser.contents)
851        # Grade and weight have been determined
852        self.browser.open(self.studycourse_path + '/100/COURSE1')
853        self.assertFalse('Grade' in self.browser.contents)
854        self.assertFalse('Weight' in self.browser.contents)
855        self.student['studycourse']['100']['COURSE1'].score = 55
856        self.browser.open(self.studycourse_path + '/100/COURSE1')
857        self.assertTrue('Grade' in self.browser.contents)
858        self.assertTrue('Weight' in self.browser.contents)
859        self.assertEqual(
860            self.student['studycourse']['100']['COURSE1'].grade, 'C')
861        self.assertEqual(
862            self.student['studycourse']['100']['COURSE1'].weight, 3)
863        # We add another ticket to check if GPA will be correctly
864        # calculated (and rounded)
865        courseticket = createObject('waeup.CourseTicket')
866        courseticket.code = 'ANYCODE'
867        courseticket.title = u'Any TITLE'
868        courseticket.credits = 13
869        courseticket.score = 66
870        courseticket.semester = 1
871        courseticket.dcode = u'ANYDCODE'
872        courseticket.fcode = u'ANYFCODE'
873        self.student['studycourse']['100']['COURSE2'] = courseticket
874        self.browser.open(self.student_path + '/studycourse/100')
875        # total credits
876        self.assertEqual(
877            self.student['studycourse']['100'].gpa_params_rectified[1], 23)
878        # weigheted credits = 3 * 10 + 4 * 13
879        self.assertEqual(
880            self.student['studycourse']['100'].gpa_params_rectified[2], 82.0)
881        # sgpa = 82 / 23
882        self.assertEqual(
883            self.student['studycourse']['100'].gpa_params_rectified[0],
884            3.5652173913043477)
885        # Carry-over courses will be collected when next level is created
886        self.browser.open(self.student_path + '/studycourse/manage')
887        # Add next level
888        self.student['studycourse']['100']['COURSE1'].score = 10
889        self.browser.getControl(name="addlevel").value = ['200']
890        self.browser.getControl(name="level_session").value = ['2005']
891        self.browser.getControl("Add study level").click()
892        self.browser.getLink("200").click()
893        self.assertMatches(
894            '...: 200 (Year 2)...', self.browser.contents)
895        # Since COURSE1 has score 10 it becomes a carry-over course
896        # in level 200
897        self.assertEqual(
898            sorted(self.student['studycourse']['200'].keys()), [u'COURSE1'])
899        self.assertTrue(
900            self.student['studycourse']['200']['COURSE1'].carry_over)
901        # Passed and failed courses have been counted
902        self.assertEqual(
903            self.student['studycourse']['100'].passed_params,
904            (1, 1, 13, 10, 'COURSE1 ', ''))
905        self.assertEqual(
906            self.student['studycourse']['200'].passed_params,
907            (0, 0, 0, 0, '', 'COURSE1 '))
908        # And also cumulative params can be calculated. Meanwhile we have the
909        # following courses: COURSE1 and COURSE2 in level 100 and
910        # COURSE1 as carry-over course in level 200.
911        self.assertEqual(
912            self.student['studycourse']['100'].cumulative_params,
913            (2.260869565217391, 23, 52.0, 23, 13))
914        # COURSE1 in level 200 is not taken into consideration
915        # when calculating the gpa.
916        self.assertEqual(
917            self.student['studycourse']['200'].cumulative_params,
918            (2.260869565217391, 23, 52.0, 33, 13))
919        return
920
921    def test_gpa_calculation_with_carryover(self):
922        studylevel = createObject(u'waeup.StudentStudyLevel')
923        studylevel.level = 100
924        studylevel.level_session = 2005
925        self.student['studycourse'].entry_mode = 'ug_ft'
926        self.student['studycourse'].addStudentStudyLevel(
927            self.certificate, studylevel)
928        # First course has been added automatically.
929        # Set score above passmark.
930        studylevel['COURSE1'].score = studylevel['COURSE1'].passmark + 1
931        # GPA is 1.
932        self.assertEqual(
933            self.student['studycourse']['100'].gpa_params_rectified[0], 1.0)
934        # Set score below passmark.
935        studylevel['COURSE1'].score = studylevel['COURSE1'].passmark - 1
936        # GPA is still 0.
937        self.assertEqual(
938            self.student['studycourse']['100'].gpa_params_rectified[0], 0.0)
939        studylevel2 = createObject(u'waeup.StudentStudyLevel')
940        studylevel2.level = 200
941        studylevel2.level_session = 2006
942        self.student['studycourse'].addStudentStudyLevel(
943            self.certificate, studylevel2)
944        # Carry-over course has been autonatically added.
945        studylevel2['COURSE1'].score = 66
946        # The score of the carry-over course is now used for calculation of the
947        # GPA at level 100 ...
948        self.assertEqual(
949            self.student['studycourse']['100'].gpa_params_rectified[0], 4.0)
950        # ... but not at level 200
951        self.assertEqual(
952            self.student['studycourse']['200'].gpa_params_rectified[0], 0.0)
953        return
954
955    def test_manage_payments(self):
956        # Managers can add online school fee payment tickets
957        # if certain requirements are met
958        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
959        self.browser.open(self.payments_path)
960        IWorkflowState(self.student).setState('cleared')
961        self.browser.getLink("Add current session payment ticket").click()
962        self.browser.getControl(name="form.p_category").value = ['schoolfee']
963        self.browser.getControl("Create ticket").click()
964        self.assertMatches('...ticket created...',
965                           self.browser.contents)
966        ctrl = self.browser.getControl(name='val_id')
967        value = ctrl.options[0]
968        self.browser.getLink(value).click()
969        self.assertMatches('...Amount Authorized...',
970                           self.browser.contents)
971        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
972        payment_url = self.browser.url
973        logfile = os.path.join(
974            self.app['datacenter'].storage, 'logs', 'students.log')
975        logcontent = open(logfile).read()
976        self.assertTrue(
977            ' zope.mgr - students.browser.OnlinePaymentAddFormPage - '
978            'K1000000 - added: %s' % value
979            in logcontent)
980        # The pdf payment slip can't yet be opened
981        #self.browser.open(payment_url + '/payment_slip.pdf')
982        #self.assertMatches('...Ticket not yet paid...',
983        #                   self.browser.contents)
984
985        # The same payment (with same p_item, p_session and
986        # p_category) can be initialized a second time if the former
987        # ticket is not yet paid.
988        self.browser.open(self.payments_path)
989        self.browser.getLink("Add current session payment ticket").click()
990        self.browser.getControl(name="form.p_category").value = ['schoolfee']
991        self.browser.getControl("Create ticket").click()
992        self.assertMatches('...Payment ticket created...',
993                           self.browser.contents)
994
995        # The ticket can be found in the payments_catalog
996        cat = queryUtility(ICatalog, name='payments_catalog')
997        results = list(cat.searchResults(p_state=('unpaid', 'unpaid')))
998        self.assertEqual(len(results), 2)
999        self.assertTrue(results[0] is self.student['payments'][value])
1000        # Managers can approve the payment
1001        # If, by some reason, the state has already changed,
1002        # an access code is created after approval.
1003        IWorkflowState(self.student).setState('school fee paid')
1004        self.assertEqual(len(self.app['accesscodes']['SFE-0']), 0)
1005        self.browser.open(payment_url)
1006        self.browser.getLink("Approve payment").click()
1007        self.assertMatches(
1008            '...Payment approved...', self.browser.contents)
1009        # Approval is logged in students.log ...
1010        logcontent = open(logfile).read()
1011        self.assertTrue(
1012            'zope.mgr - students.browser.OnlinePaymentApproveView '
1013            '- K1000000 - schoolfee payment approved'
1014            in logcontent)
1015        # ... and in payments.log
1016        logfile = os.path.join(
1017            self.app['datacenter'].storage, 'logs', 'payments.log')
1018        logcontent = open(logfile).read()
1019        self.assertTrue(
1020            '"zope.mgr",K1000000,%s,schoolfee,40000.0,AP,,,,,,\n' % value
1021            in logcontent)
1022        # The authorized amount has been stored in the new access code
1023        self.assertEqual(
1024            self.app['accesscodes']['SFE-0'].values()[0].cost, 40000.0)
1025
1026        # The catalog has been updated
1027        results = list(cat.searchResults(p_state=('unpaid', 'unpaid')))
1028        self.assertTrue(len(results), 0)
1029        results = list(cat.searchResults(p_state=('paid', 'paid')))
1030        self.assertTrue(len(results), 1)
1031        self.assertTrue(results[0] is self.student['payments'][value])
1032
1033        # Payments can't be approved twice
1034        self.browser.open(payment_url + '/approve')
1035        self.assertMatches('...This ticket has already been paid...',
1036                          self.browser.contents)
1037
1038        # Now the first ticket is paid and no more ticket of same type
1039        # (with same p_item, p_session and p_category) can be added.
1040        # First we have to reset the workflow state.
1041        IWorkflowState(self.student).setState('cleared')
1042        self.browser.open(self.payments_path)
1043        self.browser.getLink("Add current session payment ticket").click()
1044        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1045        self.browser.getControl("Create ticket").click()
1046        self.assertMatches(
1047            '...This type of payment has already been made...',
1048            self.browser.contents)
1049
1050        # Managers can open the pdf payment slip
1051        self.browser.open(payment_url)
1052        self.browser.getLink("Download payment slip").click()
1053        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1054        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1055
1056        # Managers can remove online school fee payment tickets
1057        self.browser.open(self.payments_path)
1058        self.browser.getControl("Remove selected").click()
1059        self.assertMatches('...No payment selected...', self.browser.contents)
1060        ctrl = self.browser.getControl(name='val_id')
1061        value = ctrl.options[0]
1062        ctrl.getControl(value=value).selected = True
1063        self.browser.getControl("Remove selected", index=0).click()
1064        self.assertTrue('Successfully removed' in self.browser.contents)
1065
1066        # Managers can add online clearance payment tickets
1067        self.browser.open(self.payments_path + '/addop')
1068        self.browser.getControl(name="form.p_category").value = ['clearance']
1069        self.browser.getControl("Create ticket").click()
1070        self.assertMatches('...ticket created...',
1071                           self.browser.contents)
1072
1073        # Managers can approve the payment
1074        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
1075        ctrl = self.browser.getControl(name='val_id')
1076        value = ctrl.options[1] # The clearance payment is the second in the table
1077        self.browser.getLink(value).click()
1078        self.browser.open(self.browser.url + '/approve')
1079        self.assertMatches('...Payment approved...',
1080                          self.browser.contents)
1081        expected = '''...
1082        <td>
1083          <span>Paid</span>
1084        </td>...'''
1085        self.assertMatches(expected,self.browser.contents)
1086        # The new CLR-0 pin has been created
1087        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
1088        pin = self.app['accesscodes']['CLR-0'].keys()[0]
1089        ac = self.app['accesscodes']['CLR-0'][pin]
1090        self.assertEqual(ac.owner, self.student_id)
1091        self.assertEqual(ac.cost, 3456.0)
1092
1093        # Managers can add online transcript payment tickets
1094        self.browser.open(self.payments_path + '/addop')
1095        self.browser.getControl(name="form.p_category").value = ['transcript']
1096        self.browser.getControl("Create ticket").click()
1097        self.assertMatches('...ticket created...',
1098                           self.browser.contents)
1099
1100        # Managers can approve the payment
1101        self.assertEqual(len(self.app['accesscodes']['TSC-0']),0)
1102        ctrl = self.browser.getControl(name='val_id')
1103        value = ctrl.options[2] # The clearance payment is the third in the table
1104        self.browser.getLink(value).click()
1105        self.browser.open(self.browser.url + '/approve')
1106        self.assertMatches('...Payment approved...',
1107                          self.browser.contents)
1108        expected = '''...
1109        <td>
1110          <span>Paid</span>
1111        </td>...'''
1112        self.assertMatches(expected,self.browser.contents)
1113        # The new CLR-0 pin has been created
1114        self.assertEqual(len(self.app['accesscodes']['TSC-0']),1)
1115        pin = self.app['accesscodes']['TSC-0'].keys()[0]
1116        ac = self.app['accesscodes']['TSC-0'][pin]
1117        self.assertEqual(ac.owner, self.student_id)
1118        self.assertEqual(ac.cost, 4567.0)
1119        return
1120
1121    def test_add_transfer_payment(self):
1122        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1123        self.browser.open(self.payments_path)
1124        self.browser.getLink("Add current session payment ticket").click()
1125        self.browser.getControl(name="form.p_category").value = ['transfer']
1126        self.browser.getControl(name="new_programme").value = 'my new study course'
1127        self.browser.getControl("Create ticket").click()
1128        self.assertMatches('...ticket created...',
1129                           self.browser.contents)
1130        ctrl = self.browser.getControl(name='val_id')
1131        value = ctrl.options[0]
1132        self.browser.getLink(value).click()
1133        self.assertMatches('...my new study course...',
1134                           self.browser.contents)
1135        self.assertEqual(self.student['payments'][value].p_item, u'my new study course')
1136
1137    def test_manage_payments_bypass_ac_creation(self):
1138        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1139        self.browser.open(self.payments_path)
1140        IWorkflowState(self.student).setState('cleared')
1141        self.browser.getLink("Add current session payment ticket").click()
1142        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1143        self.browser.getControl("Create ticket").click()
1144        ctrl = self.browser.getControl(name='val_id')
1145        value = ctrl.options[0]
1146        self.browser.getLink(value).click()
1147        payment_url = self.browser.url
1148        logfile = os.path.join(
1149            self.app['datacenter'].storage, 'logs', 'students.log')
1150        # The ticket can be found in the payments_catalog
1151        cat = queryUtility(ICatalog, name='payments_catalog')
1152        results = list(cat.searchResults(p_state=('unpaid', 'unpaid')))
1153        self.assertTrue(len(results), 1)
1154        self.assertTrue(results[0] is self.student['payments'][value])
1155        # Managers can approve the payment
1156        self.browser.open(payment_url)
1157        self.browser.getLink("Approve payment").click()
1158        self.assertMatches('...Payment approved...',
1159                          self.browser.contents)
1160        # Approval is logged in students.log ...
1161        logcontent = open(logfile).read()
1162        self.assertTrue(
1163            'zope.mgr - students.browser.OnlinePaymentApproveView '
1164            '- K1000000 - schoolfee payment approved'
1165            in logcontent)
1166        # ... and in payments.log
1167        logfile = os.path.join(
1168            self.app['datacenter'].storage, 'logs', 'payments.log')
1169        logcontent = open(logfile).read()
1170        self.assertTrue(
1171            '"zope.mgr",K1000000,%s,schoolfee,40000.0,AP,,,,,,\n' % value
1172            in logcontent)
1173        # Student is in state school fee paid, no activation
1174        # code was necessary.
1175        self.assertEqual(self.student.state, 'school fee paid')
1176        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1177        return
1178
1179    def test_payment_disabled(self):
1180        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1181        self.browser.open(self.payments_path)
1182        IWorkflowState(self.student).setState('cleared')
1183        self.browser.getLink("Add current session payment ticket").click()
1184        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1185        self.browser.getControl("Create ticket").click()
1186        self.assertMatches('...ticket created...',
1187                           self.browser.contents)
1188        self.app['configuration']['2004'].payment_disabled = ['sf_all']
1189        self.browser.getLink("Add current session payment ticket").click()
1190        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1191        self.browser.getControl("Create ticket").click()
1192        self.assertMatches('...This category of payments has been disabled...',
1193                           self.browser.contents)
1194        return
1195
1196    def test_manage_balance_payments(self):
1197
1198        # Login
1199        #self.browser.open(self.login_path)
1200        #self.browser.getControl(name="form.login").value = self.student_id
1201        #self.browser.getControl(name="form.password").value = 'spwd'
1202        #self.browser.getControl("Login").click()
1203
1204        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1205        self.browser.open(self.payments_path)
1206
1207        # Managers can add balance school fee payment tickets in any state.
1208        IWorkflowState(self.student).setState('courses registered')
1209        self.browser.open(self.payments_path)
1210        self.browser.getLink("Add balance payment ticket").click()
1211
1212        # Balance payment form is provided
1213        self.assertEqual(self.student.current_session, 2004)
1214        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1215        self.browser.getControl(name="form.balance_session").value = ['2004']
1216        self.browser.getControl(name="form.balance_level").value = ['300']
1217        self.browser.getControl(name="form.balance_amount").value = '-567.8'
1218        self.browser.getControl("Create ticket").click()
1219        self.assertMatches('...Amount must be greater than 0...',
1220                           self.browser.contents)
1221        self.browser.getControl(name="form.balance_amount").value = '0'
1222        self.browser.getControl("Create ticket").click()
1223        self.assertMatches('...Amount must be greater than 0...',
1224                           self.browser.contents)
1225        self.browser.getControl(name="form.balance_amount").value = '567.8'
1226        self.browser.getControl("Create ticket").click()
1227        self.assertMatches('...ticket created...',
1228                           self.browser.contents)
1229        ctrl = self.browser.getControl(name='val_id')
1230        value = ctrl.options[0]
1231        self.browser.getLink(value).click()
1232        self.assertMatches('...Amount Authorized...',
1233                           self.browser.contents)
1234        self.assertEqual(self.student['payments'][value].amount_auth, 567.8)
1235        # Payment attributes are properly set
1236        self.assertEqual(self.student['payments'][value].p_session, 2004)
1237        self.assertEqual(self.student['payments'][value].p_level, 300)
1238        self.assertEqual(self.student['payments'][value].p_item, u'Balance')
1239        self.assertEqual(self.student['payments'][value].p_category, 'schoolfee')
1240        # Adding payment tickets is logged.
1241        logfile = os.path.join(
1242            self.app['datacenter'].storage, 'logs', 'students.log')
1243        logcontent = open(logfile).read()
1244        self.assertTrue('zope.mgr - students.browser.BalancePaymentAddFormPage '
1245                        '- K1000000 - added: %s' % value in logcontent)
1246
1247    def test_manage_accommodation(self):
1248        logfile = os.path.join(
1249            self.app['datacenter'].storage, 'logs', 'students.log')
1250        # Managers can add online booking fee payment tickets and open the
1251        # callback view (see test_manage_payments)
1252        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1253        self.browser.open(self.payments_path)
1254        self.browser.getLink("Add current session payment ticket").click()
1255        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1256        # If student is not in accommodation session, payment cannot be processed
1257        self.app['hostels'].accommodation_session = 2011
1258        self.browser.getControl("Create ticket").click()
1259        self.assertMatches('...Your current session does not match...',
1260                           self.browser.contents)
1261        self.app['hostels'].accommodation_session = 2004
1262        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1263        self.browser.getControl("Create ticket").click()
1264        ctrl = self.browser.getControl(name='val_id')
1265        value = ctrl.options[0]
1266        self.browser.getLink(value).click()
1267        self.browser.open(self.browser.url + '/approve')
1268        # The new HOS-0 pin has been created
1269        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
1270        pin = self.app['accesscodes']['HOS-0'].keys()[0]
1271        ac = self.app['accesscodes']['HOS-0'][pin]
1272        self.assertEqual(ac.owner, self.student_id)
1273        parts = pin.split('-')[1:]
1274        sfeseries, sfenumber = parts
1275        # Managers can use HOS code and book a bed space with it
1276        self.browser.open(self.acco_path)
1277        self.browser.getControl("Book accommodation").click()
1278        self.assertMatches('...You are in the wrong...',
1279                           self.browser.contents)
1280        IWorkflowInfo(self.student).fireTransition('admit')
1281        # An existing HOS code can only be used if students
1282        # are in accommodation session
1283        self.student['studycourse'].current_session = 2003
1284        self.browser.getControl("Book accommodation").click()
1285        self.assertMatches('...Your current session does not match...',
1286                           self.browser.contents)
1287        self.student['studycourse'].current_session = 2004
1288        # All requirements are met and ticket can be created
1289        self.browser.getControl("Book accommodation").click()
1290        self.assertMatches('...Activation Code:...',
1291                           self.browser.contents)
1292        self.browser.getControl(name="ac_series").value = sfeseries
1293        self.browser.getControl(name="ac_number").value = sfenumber
1294        self.browser.getControl("Create bed ticket").click()
1295        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1296                           self.browser.contents)
1297        # Bed has been allocated
1298        bed1 = self.app['hostels']['hall-1']['hall-1_A_101_A']
1299        self.assertTrue(bed1.owner == self.student_id)
1300        # BedTicketAddPage is now blocked
1301        self.browser.getControl("Book accommodation").click()
1302        self.assertMatches('...You already booked a bed space...',
1303            self.browser.contents)
1304        # The bed ticket displays the data correctly
1305        self.browser.open(self.acco_path + '/2004')
1306        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1307                           self.browser.contents)
1308        self.assertMatches('...2004/2005...', self.browser.contents)
1309        self.assertMatches('...regular_male_fr...', self.browser.contents)
1310        self.assertMatches('...%s...' % pin, self.browser.contents)
1311        # Booking is properly logged
1312        logcontent = open(logfile).read()
1313        self.assertTrue('zope.mgr - students.browser.BedTicketAddPage '
1314            '- K1000000 - booked: hall-1_A_101_A' in logcontent)
1315        # Managers can relocate students if the student's bed_type has changed
1316        self.browser.getLink("Relocate student").click()
1317        self.assertMatches(
1318            "...Student can't be relocated...", self.browser.contents)
1319        self.student.sex = u'f'
1320        self.browser.getLink("Relocate student").click()
1321        self.assertMatches(
1322            "...Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1323        self.assertTrue(bed1.owner == NOT_OCCUPIED)
1324        bed2 = self.app['hostels']['hall-1']['hall-1_A_101_B']
1325        self.assertTrue(bed2.owner == self.student_id)
1326        self.assertTrue(self.student['accommodation'][
1327            '2004'].bed_type == u'regular_female_fr')
1328        # Relocation is properly logged
1329        logcontent = open(logfile).read()
1330        self.assertTrue('zope.mgr - students.accommodation.BedTicket '
1331            '- K1000000 - relocated: hall-1_A_101_B' in logcontent)
1332        # The payment object still shows the original payment item
1333        payment_id = self.student['payments'].keys()[0]
1334        payment = self.student['payments'][payment_id]
1335        self.assertTrue(payment.p_item == u'regular_male_fr')
1336        # Managers can relocate students if the bed's bed_type has changed
1337        bed1.bed_type = u'regular_female_fr'
1338        bed2.bed_type = u'regular_male_fr'
1339        notify(grok.ObjectModifiedEvent(bed1))
1340        notify(grok.ObjectModifiedEvent(bed2))
1341        self.browser.getLink("Relocate student").click()
1342        self.assertMatches(
1343            "...Student relocated...", self.browser.contents)
1344        self.assertMatches(
1345            "... Hall 1, Block A, Room 101, Bed A...", self.browser.contents)
1346        self.assertMatches(bed1.owner, self.student_id)
1347        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1348        # Managers can't relocate students if bed is reserved
1349        self.student.sex = u'm'
1350        bed1.bed_type = u'regular_female_reserved'
1351        notify(grok.ObjectModifiedEvent(bed1))
1352        self.browser.getLink("Relocate student").click()
1353        self.assertMatches(
1354            "...Students in reserved beds can't be relocated...",
1355            self.browser.contents)
1356        # Managers can relocate students if booking has been cancelled but
1357        # other bed space has been manually allocated after cancellation
1358        old_owner = bed1.releaseBed()
1359        self.assertMatches(old_owner, self.student_id)
1360        bed2.owner = self.student_id
1361        self.browser.open(self.acco_path + '/2004')
1362        self.assertMatches(
1363            "...booking cancelled...", self.browser.contents)
1364        self.browser.getLink("Relocate student").click()
1365        # We didn't informed the catalog therefore the new owner is not found
1366        self.assertMatches(
1367            "...There is no free bed in your category regular_male_fr...",
1368            self.browser.contents)
1369        # Now we fire the event properly
1370        notify(grok.ObjectModifiedEvent(bed2))
1371        self.browser.getLink("Relocate student").click()
1372        self.assertMatches(
1373            "...Student relocated...", self.browser.contents)
1374        self.assertMatches(
1375            "... Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1376          # Managers can delete bed tickets
1377        self.browser.open(self.acco_path)
1378        ctrl = self.browser.getControl(name='val_id')
1379        value = ctrl.options[0]
1380        ctrl.getControl(value=value).selected = True
1381        self.browser.getControl("Remove selected", index=0).click()
1382        self.assertMatches('...Successfully removed...', self.browser.contents)
1383        # The bed has been properly released by the event handler
1384        self.assertMatches(bed1.owner, NOT_OCCUPIED)
1385        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1386        return
1387
1388    def test_manage_workflow(self):
1389        # Managers can pass through the whole workflow
1390        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1391        student = self.app['students'][self.student_id]
1392        self.browser.open(self.trigtrans_path)
1393        self.assertTrue(student.clearance_locked)
1394        self.browser.getControl(name="transition").value = ['admit']
1395        self.browser.getControl("Save").click()
1396        self.assertTrue(student.clearance_locked)
1397        self.browser.getControl(name="transition").value = ['start_clearance']
1398        self.browser.getControl("Save").click()
1399        self.assertFalse(student.clearance_locked)
1400        self.browser.getControl(name="transition").value = ['request_clearance']
1401        self.browser.getControl("Save").click()
1402        self.assertTrue(student.clearance_locked)
1403        self.browser.getControl(name="transition").value = ['clear']
1404        self.browser.getControl("Save").click()
1405        # Managers approve payment, they do not pay
1406        self.assertFalse('pay_first_school_fee' in self.browser.contents)
1407        self.browser.getControl(
1408            name="transition").value = ['approve_first_school_fee']
1409        self.browser.getControl("Save").click()
1410        self.browser.getControl(name="transition").value = ['reset6']
1411        self.browser.getControl("Save").click()
1412        # In state returning the pay_school_fee transition triggers some
1413        # changes of attributes
1414        self.browser.getControl(name="transition").value = ['approve_school_fee']
1415        self.browser.getControl("Save").click()
1416        self.assertEqual(student['studycourse'].current_session, 2005) # +1
1417        self.assertEqual(student['studycourse'].current_level, 200) # +100
1418        self.assertEqual(student['studycourse'].current_verdict, '0') # 0 = Zero = not set
1419        self.assertEqual(student['studycourse'].previous_verdict, 'A')
1420        self.browser.getControl(name="transition").value = ['register_courses']
1421        self.browser.getControl("Save").click()
1422        self.browser.getControl(name="transition").value = ['validate_courses']
1423        self.browser.getControl("Save").click()
1424        self.browser.getControl(name="transition").value = ['return']
1425        self.browser.getControl("Save").click()
1426        return
1427
1428    def test_manage_pg_workflow(self):
1429        # Managers can pass through the whole workflow
1430        IWorkflowState(self.student).setState('school fee paid')
1431        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1432        self.browser.open(self.trigtrans_path)
1433        self.assertTrue('<option value="reset6">' in self.browser.contents)
1434        self.assertTrue('<option value="register_courses">' in self.browser.contents)
1435        self.assertTrue('<option value="reset5">' in self.browser.contents)
1436        self.certificate.study_mode = 'pg_ft'
1437        self.browser.open(self.trigtrans_path)
1438        self.assertFalse('<option value="reset6">' in self.browser.contents)
1439        self.assertFalse('<option value="register_courses">' in self.browser.contents)
1440        self.assertTrue('<option value="reset5">' in self.browser.contents)
1441        return
1442
1443    def test_manage_import(self):
1444        # Managers can import student data files
1445        datacenter_path = 'http://localhost/app/datacenter'
1446        # Prepare a csv file for students
1447        open('students.csv', 'wb').write(
1448"""firstname,lastname,reg_number,date_of_birth,matric_number,email,phone,sex,password
1449Aaren,Pieri,1,1990-01-02,100000,aa@aa.ng,1234,m,mypwd1
1450Claus,Finau,2,1990-01-03,100001,aa@aa.ng,1234,m,mypwd1
1451Brit,Berson,3,1990-01-04,100001,aa@aa.ng,1234,m,mypwd1
1452""")
1453        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1454        self.browser.open(datacenter_path)
1455        self.browser.getLink('Upload data').click()
1456        filecontents = StringIO(open('students.csv', 'rb').read())
1457        filewidget = self.browser.getControl(name='uploadfile:file')
1458        filewidget.add_file(filecontents, 'text/plain', 'students.csv')
1459        self.browser.getControl(name='SUBMIT').click()
1460        self.browser.getLink('Process data').click()
1461        self.browser.getLink("Switch maintenance mode").click()
1462        button = lookup_submit_value(
1463            'select', 'students_zope.mgr.csv', self.browser)
1464        button.click()
1465        importerselect = self.browser.getControl(name='importer')
1466        modeselect = self.browser.getControl(name='mode')
1467        importerselect.getControl('Student Processor').selected = True
1468        modeselect.getControl(value='create').selected = True
1469        self.browser.getControl('Proceed to step 3').click()
1470        self.assertTrue('Header fields OK' in self.browser.contents)
1471        self.browser.getControl('Perform import').click()
1472        self.assertTrue('Processing of 1 rows failed' in self.browser.contents)
1473        self.assertTrue('Successfully processed 2 rows' in self.browser.contents)
1474        self.assertTrue('Batch processing finished' in self.browser.contents)
1475        open('studycourses.csv', 'wb').write(
1476"""reg_number,matric_number,certificate,current_session,current_level
14771,,CERT1,2008,100
1478,100001,CERT1,2008,100
1479,100002,CERT1,2008,100
1480""")
1481        self.browser.open(datacenter_path)
1482        self.browser.getLink('Upload data').click()
1483        filecontents = StringIO(open('studycourses.csv', 'rb').read())
1484        filewidget = self.browser.getControl(name='uploadfile:file')
1485        filewidget.add_file(filecontents, 'text/plain', 'studycourses.csv')
1486        self.browser.getControl(name='SUBMIT').click()
1487        self.browser.getLink('Process data').click()
1488        # Meanwhile maintenance mode is disabled again.
1489        self.browser.getLink("Switch maintenance mode").click()
1490        button = lookup_submit_value(
1491            'select', 'studycourses_zope.mgr.csv', self.browser)
1492        button.click()
1493        importerselect = self.browser.getControl(name='importer')
1494        modeselect = self.browser.getControl(name='mode')
1495        importerselect.getControl(
1496            'StudentStudyCourse Processor (update only)').selected = True
1497        modeselect.getControl(value='create').selected = True
1498        self.browser.getControl('Proceed to step 3').click()
1499        self.assertTrue('Update mode only' in self.browser.contents)
1500        self.browser.getControl('Proceed to step 3').click()
1501        self.assertTrue('Header fields OK' in self.browser.contents)
1502        self.browser.getControl('Perform import').click()
1503        self.assertTrue('Processing of 1 rows failed' in self.browser.contents)
1504        self.assertTrue('Successfully processed 2 rows'
1505                        in self.browser.contents)
1506        # The students are properly indexed and we can
1507        # thus find a student in  the department
1508        self.browser.open(self.manage_container_path)
1509        self.browser.getControl(name="searchtype").value = ['depcode']
1510        self.browser.getControl(name="searchterm").value = 'dep1'
1511        self.browser.getControl("Find student(s)").click()
1512        self.assertTrue('Aaren Pieri' in self.browser.contents)
1513        # We can search for a new student by name ...
1514        self.browser.getControl(name="searchtype").value = ['fullname']
1515        self.browser.getControl(name="searchterm").value = 'Claus'
1516        self.browser.getControl("Find student(s)").click()
1517        self.assertTrue('Claus Finau' in self.browser.contents)
1518        # ... and check if the imported password has been properly set
1519        ctrl = self.browser.getControl(name='entries')
1520        value = ctrl.options[0]
1521        claus = self.app['students'][value]
1522        self.assertTrue(IUserAccount(claus).checkPassword('mypwd1'))
1523        return
1524
1525    def init_clearance_officer(self):
1526        # Create clearance officer
1527        self.app['users'].addUser('mrclear', SECRET)
1528        self.app['users']['mrclear'].email = 'mrclear@foo.ng'
1529        self.app['users']['mrclear'].title = 'Carlo Pitter'
1530        # Clearance officers need not necessarily to get
1531        # the StudentsOfficer site role
1532        #prmglobal = IPrincipalRoleManager(self.app)
1533        #prmglobal.assignRoleToPrincipal('waeup.StudentsOfficer', 'mrclear')
1534        # Assign local ClearanceOfficer role
1535        self.department = self.app['faculties']['fac1']['dep1']
1536        prmlocal = IPrincipalRoleManager(self.department)
1537        prmlocal.assignRoleToPrincipal('waeup.local.ClearanceOfficer', 'mrclear')
1538        IWorkflowState(self.student).setState('clearance started')
1539        # Add another student for testing
1540        other_student = Student()
1541        other_student.firstname = u'Dep2'
1542        other_student.lastname = u'Student'
1543        self.app['students'].addStudent(other_student)
1544        self.other_student_path = (
1545            'http://localhost/app/students/%s' % other_student.student_id)
1546        # Login as clearance officer
1547        self.browser.open(self.login_path)
1548        self.browser.getControl(name="form.login").value = 'mrclear'
1549        self.browser.getControl(name="form.password").value = SECRET
1550        self.browser.getControl("Login").click()
1551
1552    def test_handle_clearance_by_co(self):
1553        self.init_clearance_officer()
1554        self.assertMatches('...You logged in...', self.browser.contents)
1555        # CO is landing on index page.
1556        self.assertEqual(self.browser.url, 'http://localhost/app/index')
1557        # CO can see his roles
1558        self.browser.getLink("My Roles").click()
1559        self.assertMatches(
1560            '...<div>Academics Officer (view only)</div>...',
1561            self.browser.contents)
1562        # But not his local role ...
1563        self.assertFalse('Clearance Officer' in self.browser.contents)
1564        # ... because we forgot to notify the department that the local role
1565        # has changed.
1566        notify(LocalRoleSetEvent(
1567            self.department, 'waeup.local.ClearanceOfficer', 'mrclear',
1568            granted=True))
1569        self.browser.open('http://localhost/app/users/mrclear/my_roles')
1570        self.assertTrue('Clearance Officer' in self.browser.contents)
1571        self.assertMatches(
1572            '...<a href="http://localhost/app/faculties/fac1/dep1">...',
1573            self.browser.contents)
1574        # CO can view the student ...
1575        self.browser.open(self.clearance_path)
1576        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1577        self.assertEqual(self.browser.url, self.clearance_path)
1578        # ... but not other students.
1579        self.assertRaises(
1580            Unauthorized, self.browser.open, self.other_student_path)
1581        # Clearance is disabled for this session.
1582        self.browser.open(self.clearance_path)
1583        self.assertFalse('Clear student' in self.browser.contents)
1584        self.browser.open(self.student_path + '/clear')
1585        self.assertTrue('Clearance is disabled for this session'
1586            in self.browser.contents)
1587        self.app['configuration']['2004'].clearance_enabled = True
1588        # Only in state clearance requested the CO does see the 'Clear' button.
1589        self.browser.open(self.clearance_path)
1590        self.assertFalse('Clear student' in self.browser.contents)
1591        self.browser.open(self.student_path + '/clear')
1592        self.assertTrue('Student is in wrong state.'
1593            in self.browser.contents)
1594        IWorkflowInfo(self.student).fireTransition('request_clearance')
1595        self.browser.open(self.clearance_path)
1596        self.assertTrue('Clear student' in self.browser.contents)
1597        self.browser.getLink("Clear student").click()
1598        self.assertTrue('Student has been cleared' in self.browser.contents)
1599        self.assertTrue('cleared' in self.browser.contents)
1600        self.browser.open(self.history_path)
1601        self.assertTrue('Cleared by Carlo Pitter' in self.browser.contents)
1602        # Hide real name.
1603        self.app['users']['mrclear'].public_name = 'My Public Name'
1604        self.browser.open(self.clearance_path)
1605        self.browser.getLink("Reject clearance").click()
1606        self.assertEqual(
1607            self.browser.url, self.student_path + '/reject_clearance')
1608        # Type comment why.
1609        self.browser.getControl(name="form.officer_comment").value = (
1610            'Dear Student,\n'
1611            'You did not fill properly.')
1612        self.browser.getControl("Save comment").click()
1613        self.assertTrue('Clearance has been annulled' in self.browser.contents)
1614        url = ('http://localhost/app/students/K1000000/'
1615              'contactstudent?body=Dear+Student%2C%0AYou+did+not+fill+properly.'
1616              '&subject=Clearance+has+been+annulled.')
1617        # CO does now see the prefilled contact form and can send a message.
1618        self.assertEqual(self.browser.url, url)
1619        self.assertTrue('clearance started' in self.browser.contents)
1620        self.assertTrue('name="form.subject" size="20" type="text" '
1621            'value="Clearance has been annulled."'
1622            in self.browser.contents)
1623        self.assertTrue('name="form.body" rows="10" >Dear Student,'
1624            in self.browser.contents)
1625        self.browser.getControl("Send message now").click()
1626        self.assertTrue('Your message has been sent' in self.browser.contents)
1627        # The comment has been stored ...
1628        self.assertEqual(self.student.officer_comment,
1629            u'Dear Student,\nYou did not fill properly.')
1630        # ... and logged.
1631        logfile = os.path.join(
1632            self.app['datacenter'].storage, 'logs', 'students.log')
1633        logcontent = open(logfile).read()
1634        self.assertTrue(
1635            'INFO - mrclear - students.browser.StudentRejectClearancePage - '
1636            'K1000000 - comment: Dear Student,<br>You did not fill '
1637            'properly.\n' in logcontent)
1638        self.browser.open(self.history_path)
1639        self.assertTrue("Reset to 'clearance started' by My Public Name" in
1640            self.browser.contents)
1641        IWorkflowInfo(self.student).fireTransition('request_clearance')
1642        self.browser.open(self.clearance_path)
1643        self.browser.getLink("Reject clearance").click()
1644        self.browser.getControl("Save comment").click()
1645        self.assertTrue('Clearance request has been rejected'
1646            in self.browser.contents)
1647        self.assertTrue('clearance started' in self.browser.contents)
1648        # The CO can't clear students if not in state
1649        # clearance requested.
1650        self.browser.open(self.student_path + '/clear')
1651        self.assertTrue('Student is in wrong state'
1652            in self.browser.contents)
1653        # The CO can go to his department throug the my_roles page ...
1654        self.browser.open('http://localhost/app/users/mrclear/my_roles')
1655        self.browser.getLink("http://localhost/app/faculties/fac1/dep1").click()
1656        # ... and view the list of students.
1657        self.browser.getLink("Show students").click()
1658        self.browser.getControl(name="session").value = ['2004']
1659        self.browser.getControl(name="level").value = ['200']
1660        self.browser.getControl("Show").click()
1661        self.assertFalse(self.student_id in self.browser.contents)
1662        self.browser.getControl(name="session").value = ['2004']
1663        self.browser.getControl(name="level").value = ['100']
1664        self.browser.getControl("Show").click()
1665        self.assertTrue(self.student_id in self.browser.contents)
1666        # The comment is indicated by 'yes'.
1667        self.assertTrue('<td><span>yes</span></td>' in self.browser.contents)
1668        # Check if the enquiries form is not pre-filled with officer_comment
1669        # (regression test).
1670        self.browser.getLink("Logout").click()
1671        self.browser.open('http://localhost/app/enquiries')
1672        self.assertFalse(
1673            'You did not fill properly'
1674            in self.browser.contents)
1675        # When a student is cleared the comment is automatically deleted
1676        IWorkflowInfo(self.student).fireTransition('request_clearance')
1677        IWorkflowInfo(self.student).fireTransition('clear')
1678        self.assertEqual(self.student.officer_comment, None)
1679        return
1680
1681    def test_handle_mass_clearance_by_co(self):
1682        self.init_clearance_officer()
1683        # Additional setups according to test above
1684        notify(LocalRoleSetEvent(
1685            self.department, 'waeup.local.ClearanceOfficer', 'mrclear',
1686            granted=True))
1687        self.app['configuration']['2004'].clearance_enabled = True
1688        IWorkflowState(self.student).setState('clearance requested')
1689        # Update the catalog
1690        notify(grok.ObjectModifiedEvent(self.student))
1691        # The CO can go to the department and clear all students in department
1692        self.browser.open('http://localhost/app/faculties/fac1/dep1')
1693        self.browser.getLink("Clear all students").click()
1694        self.assertTrue('1 students have been cleared' in self.browser.contents)
1695        self.browser.open(self.history_path)
1696        self.assertTrue('Cleared by Carlo Pitter' in self.browser.contents)
1697        logfile = os.path.join(
1698            self.app['datacenter'].storage, 'logs', 'students.log')
1699        logcontent = open(logfile).read()
1700        self.assertTrue(
1701            'INFO - mrclear - K1000000 - Cleared' in logcontent)
1702        self.browser.open('http://localhost/app/faculties/fac1/dep1')
1703        self.browser.getLink("Clear all students").click()
1704        self.assertTrue('0 students have been cleared' in self.browser.contents)
1705        return
1706
1707    def test_handle_courses_by_ca(self):
1708        self.app['users'].addUser('mrsadvise', SECRET)
1709        self.app['users']['mrsadvise'].email = 'mradvise@foo.ng'
1710        self.app['users']['mrsadvise'].title = u'Helen Procter'
1711        # Assign local CourseAdviser100 role for a certificate
1712        cert = self.app['faculties']['fac1']['dep1'].certificates['CERT1']
1713        prmlocal = IPrincipalRoleManager(cert)
1714        prmlocal.assignRoleToPrincipal('waeup.local.CourseAdviser100', 'mrsadvise')
1715        IWorkflowState(self.student).setState('school fee paid')
1716        # Login as course adviser.
1717        self.browser.open(self.login_path)
1718        self.browser.getControl(name="form.login").value = 'mrsadvise'
1719        self.browser.getControl(name="form.password").value = SECRET
1720        self.browser.getControl("Login").click()
1721        self.assertMatches('...You logged in...', self.browser.contents)
1722        # CO can see his roles.
1723        self.browser.getLink("My Roles").click()
1724        self.assertMatches(
1725            '...<div>Academics Officer (view only)</div>...',
1726            self.browser.contents)
1727        # But not his local role ...
1728        self.assertFalse('Course Adviser' in self.browser.contents)
1729        # ... because we forgot to notify the certificate that the local role
1730        # has changed.
1731        notify(LocalRoleSetEvent(
1732            cert, 'waeup.local.CourseAdviser100', 'mrsadvise', granted=True))
1733        self.browser.open('http://localhost/app/users/mrsadvise/my_roles')
1734        self.assertTrue('Course Adviser 100L' in self.browser.contents)
1735        self.assertMatches(
1736            '...<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">...',
1737            self.browser.contents)
1738        # CA can view the student ...
1739        self.browser.open(self.student_path)
1740        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1741        self.assertEqual(self.browser.url, self.student_path)
1742        # ... but not other students.
1743        other_student = Student()
1744        other_student.firstname = u'Dep2'
1745        other_student.lastname = u'Student'
1746        self.app['students'].addStudent(other_student)
1747        other_student_path = (
1748            'http://localhost/app/students/%s' % other_student.student_id)
1749        self.assertRaises(
1750            Unauthorized, self.browser.open, other_student_path)
1751        # We add study level 110 to the student's studycourse.
1752        studylevel = StudentStudyLevel()
1753        studylevel.level = 110
1754        self.student['studycourse'].addStudentStudyLevel(
1755            cert,studylevel)
1756        L110_student_path = self.studycourse_path + '/110'
1757        # The CA can neither see the Validate nor the Edit button.
1758        self.browser.open(L110_student_path)
1759        self.assertFalse('Validate courses' in self.browser.contents)
1760        self.assertFalse('Edit' in self.browser.contents)
1761        IWorkflowInfo(self.student).fireTransition('register_courses')
1762        self.browser.open(L110_student_path)
1763        self.assertFalse('Validate courses' in self.browser.contents)
1764        self.assertFalse('Edit' in self.browser.contents)
1765        # Only in state courses registered and only if the current level
1766        # corresponds with the name of the study level object
1767        # the 100L CA does see the 'Validate' button but not
1768        # the edit button.
1769        self.student['studycourse'].current_level = 110
1770        self.browser.open(L110_student_path)
1771        self.assertFalse('Edit' in self.browser.contents)
1772        self.assertTrue('Validate courses' in self.browser.contents)
1773        # But a 100L CA does not see the button at other levels.
1774        studylevel2 = StudentStudyLevel()
1775        studylevel2.level = 200
1776        self.student['studycourse'].addStudentStudyLevel(
1777            cert,studylevel2)
1778        L200_student_path = self.studycourse_path + '/200'
1779        self.browser.open(L200_student_path)
1780        self.assertFalse('Edit' in self.browser.contents)
1781        self.assertFalse('Validate courses' in self.browser.contents)
1782        self.browser.open(L110_student_path)
1783        self.browser.getLink("Validate courses").click()
1784        self.assertTrue('Course list has been validated' in self.browser.contents)
1785        self.assertTrue('courses validated' in self.browser.contents)
1786        self.assertEqual(self.student['studycourse']['110'].validated_by,
1787            'Helen Procter')
1788        self.assertMatches(
1789            '<YYYY-MM-DD hh:mm:ss>',
1790            self.student['studycourse']['110'].validation_date.strftime(
1791                "%Y-%m-%d %H:%M:%S"))
1792        self.browser.getLink("Reject courses").click()
1793        self.assertTrue('Course list request has been annulled.'
1794            in self.browser.contents)
1795        urlmessage = 'Course+list+request+has+been+annulled.'
1796        self.assertEqual(self.browser.url, self.student_path +
1797            '/contactstudent?subject=%s' % urlmessage)
1798        self.assertTrue('school fee paid' in self.browser.contents)
1799        self.assertTrue(self.student['studycourse']['110'].validated_by is None)
1800        self.assertTrue(self.student['studycourse']['110'].validation_date is None)
1801        IWorkflowInfo(self.student).fireTransition('register_courses')
1802        self.browser.open(L110_student_path)
1803        self.browser.getLink("Reject courses").click()
1804        self.assertTrue('Course list has been unregistered'
1805            in self.browser.contents)
1806        self.assertTrue('school fee paid' in self.browser.contents)
1807        # CA does now see the contact form and can send a message.
1808        self.browser.getControl(name="form.subject").value = 'Important subject'
1809        self.browser.getControl(name="form.body").value = 'Course list rejected'
1810        self.browser.getControl("Send message now").click()
1811        self.assertTrue('Your message has been sent' in self.browser.contents)
1812        # The CA does now see the Edit button and can edit
1813        # current study level.
1814        self.browser.open(L110_student_path)
1815        self.browser.getLink("Edit").click()
1816        self.assertTrue('Edit course list of 100 (Year 1) on 1st probation'
1817            in self.browser.contents)
1818        # The CA can't validate courses if not in state
1819        # courses registered.
1820        self.browser.open(L110_student_path + '/validate_courses')
1821        self.assertTrue('Student is in the wrong state'
1822            in self.browser.contents)
1823        # The CA can go to his certificate through the my_roles page ...
1824        self.browser.open('http://localhost/app/users/mrsadvise/my_roles')
1825        self.browser.getLink(
1826            "http://localhost/app/faculties/fac1/dep1/certificates/CERT1").click()
1827        # ... and view the list of students.
1828        self.browser.getLink("Show students").click()
1829        self.browser.getControl(name="session").value = ['2004']
1830        self.browser.getControl(name="level").value = ['100']
1831        self.browser.getControl("Show").click()
1832        self.assertTrue(self.student_id in self.browser.contents)
1833
1834    def test_change_current_mode(self):
1835        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1836        self.browser.open(self.clearance_path)
1837        self.assertFalse('Employer' in self.browser.contents)
1838        self.browser.open(self.manage_clearance_path)
1839        self.assertFalse('Employer' in self.browser.contents)
1840        self.browser.open(self.edit_clearance_path)
1841        self.assertFalse('Employer' in self.browser.contents)
1842        # Now we change the study mode of the certificate and a different
1843        # interface is used by clearance views.
1844        self.certificate.study_mode = 'pg_ft'
1845        # Invariants are not being checked here?!
1846        self.certificate.end_level = 100
1847        self.browser.open(self.clearance_path)
1848        self.assertTrue('Employer' in self.browser.contents)
1849        self.browser.open(self.manage_clearance_path)
1850        self.assertTrue('Employer' in self.browser.contents)
1851        IWorkflowState(self.student).setState('clearance started')
1852        self.browser.open(self.edit_clearance_path)
1853        self.assertTrue('Employer' in self.browser.contents)
1854
1855    def test_find_students_in_faculties(self):
1856        # Create local students manager in faculty
1857        self.app['users'].addUser('mrmanager', SECRET)
1858        self.app['users']['mrmanager'].email = 'mrmanager@foo.ng'
1859        self.app['users']['mrmanager'].title = u'Volk Wagen'
1860        # Assign LocalStudentsManager role for faculty
1861        fac = self.app['faculties']['fac1']
1862        prmlocal = IPrincipalRoleManager(fac)
1863        prmlocal.assignRoleToPrincipal(
1864            'waeup.local.LocalStudentsManager', 'mrmanager')
1865        notify(LocalRoleSetEvent(
1866            fac, 'waeup.local.LocalStudentsManager', 'mrmanager',
1867            granted=True))
1868        # Login as manager
1869        self.browser.open(self.login_path)
1870        self.browser.getControl(name="form.login").value = 'mrmanager'
1871        self.browser.getControl(name="form.password").value = SECRET
1872        self.browser.getControl("Login").click()
1873        self.assertMatches('...You logged in...', self.browser.contents)
1874        # Manager can see his roles
1875        self.browser.getLink("My Roles").click()
1876        self.assertMatches(
1877            '...<span>Students Manager</span>...',
1878            self.browser.contents)
1879        # The manager can go to his faculty
1880        self.browser.getLink(
1881            "http://localhost/app/faculties/fac1").click()
1882        # and find students
1883        self.browser.getLink("Find students").click()
1884        self.browser.getControl("Find student").click()
1885        self.assertTrue('Empty search string' in self.browser.contents)
1886        self.browser.getControl(name="searchtype").value = ['student_id']
1887        self.browser.getControl(name="searchterm").value = self.student_id
1888        self.browser.getControl("Find student").click()
1889        self.assertTrue('Anna Tester' in self.browser.contents)
1890
1891    def test_activate_deactivate_buttons(self):
1892        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1893        self.browser.open(self.student_path)
1894        self.browser.getLink("Deactivate").click()
1895        self.assertTrue(
1896            'Student account has been deactivated.' in self.browser.contents)
1897        self.assertTrue(
1898            'Base Data (account deactivated)' in self.browser.contents)
1899        self.assertTrue(self.student.suspended)
1900        self.browser.getLink("Activate").click()
1901        self.assertTrue(
1902            'Student account has been activated.' in self.browser.contents)
1903        self.assertFalse(
1904            'Base Data (account deactivated)' in self.browser.contents)
1905        self.assertFalse(self.student.suspended)
1906        # History messages have been added ...
1907        self.browser.getLink("History").click()
1908        self.assertTrue(
1909            'Student account deactivated by Manager<br />' in self.browser.contents)
1910        self.assertTrue(
1911            'Student account activated by Manager<br />' in self.browser.contents)
1912        # ... and actions have been logged.
1913        logfile = os.path.join(
1914            self.app['datacenter'].storage, 'logs', 'students.log')
1915        logcontent = open(logfile).read()
1916        self.assertTrue('zope.mgr - students.browser.StudentDeactivateView - '
1917                        'K1000000 - account deactivated' in logcontent)
1918        self.assertTrue('zope.mgr - students.browser.StudentActivateView - '
1919                        'K1000000 - account activated' in logcontent)
1920
1921    def test_manage_student_transfer(self):
1922        # Add second certificate
1923        self.certificate2 = createObject('waeup.Certificate')
1924        self.certificate2.code = u'CERT2'
1925        self.certificate2.study_mode = 'ug_ft'
1926        self.certificate2.start_level = 999
1927        self.certificate2.end_level = 999
1928        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
1929            self.certificate2)
1930
1931        # Add study level to old study course
1932        studylevel = createObject(u'waeup.StudentStudyLevel')
1933        studylevel.level = 200
1934        self.student['studycourse'].addStudentStudyLevel(
1935            self.certificate, studylevel)
1936        studylevel = createObject(u'waeup.StudentStudyLevel')
1937        studylevel.level = 999
1938        self.student['studycourse'].addStudentStudyLevel(
1939            self.certificate, studylevel)
1940
1941        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1942        self.browser.open(self.student_path)
1943        self.browser.getLink("Transfer").click()
1944        self.browser.getControl(name="form.certificate").value = ['CERT2']
1945        self.browser.getControl(name="form.current_session").value = ['2011']
1946        self.browser.getControl(name="form.current_level").value = ['200']
1947        self.browser.getControl("Transfer").click()
1948        self.assertTrue(
1949            'Current level does not match certificate levels'
1950            in self.browser.contents)
1951        self.browser.getControl(name="form.current_level").value = ['999']
1952        self.browser.getControl("Transfer").click()
1953        self.assertTrue('Successfully transferred' in self.browser.contents)
1954        # The catalog has been updated
1955        cat = queryUtility(ICatalog, name='students_catalog')
1956        results = list(
1957            cat.searchResults(
1958            certcode=('CERT2', 'CERT2')))
1959        self.assertTrue(results[0] is self.student)
1960        results = list(
1961            cat.searchResults(
1962            current_session=(2011, 2011)))
1963        self.assertTrue(results[0] is self.student)
1964        # Add study level to new study course
1965        studylevel = createObject(u'waeup.StudentStudyLevel')
1966        studylevel.level = 999
1967        self.student['studycourse'].addStudentStudyLevel(
1968            self.certificate, studylevel)
1969
1970        # Edit and add pages are locked for old study courses
1971        self.browser.open(self.student_path + '/studycourse/manage')
1972        self.assertFalse('The requested form is locked' in self.browser.contents)
1973        self.browser.open(self.student_path + '/studycourse_1/manage')
1974        self.assertTrue('The requested form is locked' in self.browser.contents)
1975
1976        self.browser.open(self.student_path + '/studycourse/start_session')
1977        self.assertFalse('The requested form is locked' in self.browser.contents)
1978        self.browser.open(self.student_path + '/studycourse_1/start_session')
1979        self.assertTrue('The requested form is locked' in self.browser.contents)
1980
1981        IWorkflowState(self.student).setState('school fee paid')
1982        self.browser.open(self.student_path + '/studycourse/add')
1983        self.assertFalse('The requested form is locked' in self.browser.contents)
1984        self.browser.open(self.student_path + '/studycourse_1/add')
1985        self.assertTrue('The requested form is locked' in self.browser.contents)
1986
1987        self.browser.open(self.student_path + '/studycourse/999/manage')
1988        self.assertFalse('The requested form is locked' in self.browser.contents)
1989        self.browser.open(self.student_path + '/studycourse_1/999/manage')
1990        self.assertTrue('The requested form is locked' in self.browser.contents)
1991
1992        self.browser.open(self.student_path + '/studycourse/999/validate_courses')
1993        self.assertFalse('The requested form is locked' in self.browser.contents)
1994        self.browser.open(self.student_path + '/studycourse_1/999/validate_courses')
1995        self.assertTrue('The requested form is locked' in self.browser.contents)
1996
1997        self.browser.open(self.student_path + '/studycourse/999/reject_courses')
1998        self.assertFalse('The requested form is locked' in self.browser.contents)
1999        self.browser.open(self.student_path + '/studycourse_1/999/reject_courses')
2000        self.assertTrue('The requested form is locked' in self.browser.contents)
2001
2002        self.browser.open(self.student_path + '/studycourse/999/add')
2003        self.assertFalse('The requested form is locked' in self.browser.contents)
2004        self.browser.open(self.student_path + '/studycourse_1/999/add')
2005        self.assertTrue('The requested form is locked' in self.browser.contents)
2006
2007        self.browser.open(self.student_path + '/studycourse/999/edit')
2008        self.assertFalse('The requested form is locked' in self.browser.contents)
2009        self.browser.open(self.student_path + '/studycourse_1/999/edit')
2010        self.assertTrue('The requested form is locked' in self.browser.contents)
2011
2012        # Revert transfer
2013        self.browser.open(self.student_path + '/studycourse_1')
2014        self.browser.getLink("Reactivate").click()
2015        self.browser.getControl("Revert now").click()
2016        self.assertTrue('Previous transfer reverted' in self.browser.contents)
2017        results = list(
2018            cat.searchResults(
2019            certcode=('CERT1', 'CERT1')))
2020        self.assertTrue(results[0] is self.student)
2021        self.assertEqual([i for i in self.student.keys()],
2022            [u'accommodation', u'payments', u'studycourse'])
2023
2024    def test_login_as_student(self):
2025        # StudentImpersonators can login as student
2026        # Create clearance officer
2027        self.app['users'].addUser('mrofficer', SECRET)
2028        self.app['users']['mrofficer'].email = 'mrofficer@foo.ng'
2029        self.app['users']['mrofficer'].title = 'Harry Actor'
2030        prmglobal = IPrincipalRoleManager(self.app)
2031        prmglobal.assignRoleToPrincipal('waeup.StudentImpersonator', 'mrofficer')
2032        prmglobal.assignRoleToPrincipal('waeup.StudentsManager', 'mrofficer')
2033        # Login as student impersonator
2034        self.browser.open(self.login_path)
2035        self.browser.getControl(name="form.login").value = 'mrofficer'
2036        self.browser.getControl(name="form.password").value = SECRET
2037        self.browser.getControl("Login").click()
2038        self.assertMatches('...You logged in...', self.browser.contents)
2039        self.browser.open(self.student_path)
2040        self.browser.getLink("Login as").click()
2041        self.browser.getControl("Set password now").click()
2042        temp_password = self.browser.getControl(name='form.password').value
2043        self.browser.getControl("Login now").click()
2044        self.assertMatches(
2045            '...You successfully logged in as...', self.browser.contents)
2046        # We are logged in as student and can see the 'My Data' tab
2047        self.assertMatches(
2048            '...<a href="#" class="dropdown-toggle" data-toggle="dropdown">...',
2049            self.browser.contents)
2050        self.assertMatches(
2051            '...My Data...',
2052            self.browser.contents)
2053        self.browser.getLink("Logout").click()
2054        # The student can't login with the original password ...
2055        self.browser.open(self.login_path)
2056        self.browser.getControl(name="form.login").value = self.student_id
2057        self.browser.getControl(name="form.password").value = 'spwd'
2058        self.browser.getControl("Login").click()
2059        self.assertMatches(
2060            '...Your account has been temporarily deactivated...',
2061            self.browser.contents)
2062        # ... but with the temporary password
2063        self.browser.open(self.login_path)
2064        self.browser.getControl(name="form.login").value = self.student_id
2065        self.browser.getControl(name="form.password").value = temp_password
2066        self.browser.getControl("Login").click()
2067        self.assertMatches('...You logged in...', self.browser.contents)
2068        # Creation of temp_password is properly logged
2069        logfile = os.path.join(
2070            self.app['datacenter'].storage, 'logs', 'students.log')
2071        logcontent = open(logfile).read()
2072        self.assertTrue(
2073            'mrofficer - students.browser.LoginAsStudentStep1 - K1000000 - '
2074            'temp_password generated: %s' % temp_password in logcontent)
2075
2076    def test_transcripts(self):
2077        studylevel = createObject(u'waeup.StudentStudyLevel')
2078        studylevel.level = 100
2079        studylevel.level_session = 2005
2080        self.student['studycourse'].entry_mode = 'ug_ft'
2081        self.student['studycourse'].addStudentStudyLevel(
2082            self.certificate, studylevel)
2083        studylevel2 = createObject(u'waeup.StudentStudyLevel')
2084        studylevel2.level = 110
2085        studylevel2.level_session = 2006
2086        self.student['studycourse'].addStudentStudyLevel(
2087            self.certificate, studylevel2)
2088        # Add second course (COURSE has been added automatically)
2089        courseticket = createObject('waeup.CourseTicket')
2090        courseticket.code = 'ANYCODE'
2091        courseticket.title = u'Any TITLE'
2092        courseticket.credits = 13
2093        courseticket.score = 66
2094        courseticket.semester = 1
2095        courseticket.dcode = u'ANYDCODE'
2096        courseticket.fcode = u'ANYFCODE'
2097        self.student['studycourse']['110']['COURSE2'] = courseticket
2098        self.student['studycourse']['100']['COURSE1'].score = 55
2099        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[0], 3.0)
2100        self.assertEqual(self.student['studycourse']['110'].gpa_params_rectified[0], 4.0)
2101        # Get transcript data
2102        td = self.student['studycourse'].getTranscriptData()
2103        self.assertEqual(td[0][0]['level_key'], '100')
2104        self.assertEqual(td[0][0]['sgpa'], 3.0)
2105        self.assertEqual(td[0][0]['level'].level, 100)
2106        self.assertEqual(td[0][0]['level'].level_session, 2005)
2107        self.assertEqual(td[0][0]['tickets_1'][0].code, 'COURSE1')
2108        self.assertEqual(td[0][1]['level_key'], '110')
2109        self.assertEqual(td[0][1]['sgpa'], 4.0)
2110        self.assertEqual(td[0][1]['level'].level, 110)
2111        self.assertEqual(td[0][1]['level'].level_session, 2006)
2112        self.assertEqual(td[0][1]['tickets_1'][0].code, 'ANYCODE')
2113        self.assertEqual(td[1], 3.5652173913043477)
2114        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2115        self.browser.open(self.student_path + '/studycourse/transcript')
2116        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2117        self.assertTrue('Transcript' in self.browser.contents)
2118        # Officers can open the pdf transcript
2119        self.browser.open(self.student_path + '/studycourse/transcript.pdf')
2120        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2121        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2122        path = os.path.join(samples_dir(), 'transcript.pdf')
2123        open(path, 'wb').write(self.browser.contents)
2124        print "Sample PDF transcript.pdf written to %s" % path
2125
2126    def test_process_transcript(self):
2127        IWorkflowState(self.student).setState('transcript requested')
2128        notify(grok.ObjectModifiedEvent(self.student))
2129        self.student['studycourse'].transcript_comment = (
2130            u'On 07/08/2013 08:59:54 UTC K1000000 wrote:\n\nComment line 1 \n'
2131            'Comment line2\n\nDispatch Address:\nAddress line 1 \n'
2132            'Address line2\n\n')
2133        # Create officer with both roles
2134        self.app['users'].addUser('mrtranscript', SECRET)
2135        self.app['users']['mrtranscript'].email = 'mrtranscript@foo.ng'
2136        self.app['users']['mrtranscript'].title = 'Ruth Gordon'
2137        prmglobal = IPrincipalRoleManager(self.app)
2138        prmglobal.assignRoleToPrincipal('waeup.TranscriptOfficer', 'mrtranscript')
2139        prmglobal.assignRoleToPrincipal('waeup.StudentsManager', 'mrtranscript')
2140        prmglobal.assignRoleToPrincipal('waeup.TranscriptSignee', 'mrtranscript')
2141        # Login
2142        self.browser.open(self.login_path)
2143        self.browser.getControl(name="form.login").value = 'mrtranscript'
2144        self.browser.getControl(name="form.password").value = SECRET
2145        self.browser.getControl("Login").click()
2146        self.assertMatches('...You logged in...', self.browser.contents)
2147        # Officer can see his roles
2148        self.browser.getLink("My Roles").click()
2149        self.assertMatches(
2150            '...<div>Transcript Officer</div>...',
2151            self.browser.contents)
2152        # Officer can search for students in state 'transcript requested'
2153        self.browser.open(self.container_path)
2154        self.browser.getControl(name="searchtype").value = [
2155            'transcript requested']
2156        self.browser.getControl("Find student(s)").click()
2157        self.assertTrue('Anna Tester' in self.browser.contents)
2158        self.browser.getLink("K1000000").click()
2159        self.assertFalse('Release transcript request' in self.browser.contents)
2160        # Officers can still edit studycourse, studylevel and course tickets.
2161        self.browser.open(self.studycourse_path + '/manage')
2162        self.assertTrue('Undergraduate Full-Time</option>'
2163            in self.browser.contents)
2164        self.browser.getControl(name="form.certificate").value = ['CERT1']
2165        self.browser.getControl(name="form.current_session").value = ['2004']
2166        self.browser.getControl(name="form.current_verdict").value = ['A']
2167        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
2168        self.browser.getControl("Save").click()
2169        self.browser.getControl(name="form.current_level").value = ['100']
2170        self.browser.getControl("Save").click()
2171        self.browser.getControl(name="addlevel").value = ['100']
2172        self.browser.getControl(name="level_session").value = ['2004']
2173        self.browser.getControl("Add study level").click()
2174        self.browser.getLink("100").click()
2175        self.browser.getLink("Manage").click()
2176        self.browser.getControl(name="form.level_session").value = ['2002']
2177        self.browser.getControl("Save").click()
2178        self.browser.getLink("COURSE1").click()
2179        self.browser.getLink("Manage").click()
2180        self.browser.getControl("Save").click()
2181        self.assertTrue('Form has been saved' in self.browser.contents)
2182        # Officer can edit transcript remarks and validate the transcript
2183        self.browser.open(self.studycourse_path + '/transcript')
2184        self.browser.getLink("Validate transcript").click()
2185        self.browser.getLink("Edit").click()
2186        self.assertEqual(
2187            self.browser.url, self.studycourse_path + '/100/remark')
2188        self.browser.getControl(
2189            name="form.transcript_remark").value = 'Oh, the student failed'
2190        self.browser.getControl(
2191            "Save remark and go and back to transcript validation page").click()
2192        self.assertEqual(
2193            self.browser.url,
2194            self.studycourse_path + '/validate_transcript#tab4')
2195        self.assertEqual(self.student['studycourse']['100'].transcript_remark,
2196            'Oh, the student failed')
2197        self.browser.getControl("Save comment and validate transcript").click()
2198        # After validation all manage forms are locked.
2199        self.browser.open(self.studycourse_path + '/manage')
2200        self.assertTrue('The requested form is locked' in self.browser.contents)
2201        self.assertFalse('Undergraduate Full-Time</option>'
2202            in self.browser.contents)
2203        self.browser.open(self.studycourse_path + '/100/manage')
2204        self.assertTrue('The requested form is locked' in self.browser.contents)
2205        self.browser.open(self.studycourse_path + '/100/COURSE1/manage')
2206        self.assertTrue('The requested form is locked' in self.browser.contents)
2207        self.browser.open(self.studycourse_path + '/100/remark')
2208        self.assertTrue('The requested form is locked' in self.browser.contents)
2209
2210        # Transcript can be signed if officer has the permission to sign
2211        #self.browser.open(self.studycourse_path + '/transcript')
2212        #self.assertFalse('Sign transcript' in self.browser.contents)
2213        #prmglobal = IPrincipalRoleManager(self.app)
2214        #prmglobal.assignRoleToPrincipal('waeup.TranscriptSignee', 'mrtranscript')
2215
2216        self.browser.open(self.studycourse_path + '/transcript')
2217        self.browser.getLink("Sign transcript electronically").click()
2218        # Transcript signing has been logged ...
2219        logfile = os.path.join(
2220            self.app['datacenter'].storage, 'logs', 'students.log')
2221        logcontent = open(logfile).read()
2222        self.assertTrue(
2223            'mrtranscript - students.browser.StudentTranscriptSignView - '
2224            'K1000000 - Transcript signed' in logcontent)
2225        # ... appears in the student's history ...
2226        self.browser.open(self.history_path)
2227        self.assertTrue('Transcript signed by Ruth Gordon'
2228            in self.browser.contents)
2229        # ... and is also stored in the transcript_signee attribute.
2230        self.assertTrue(
2231            u'Electronically signed by Ruth Gordon (mrtranscript) on '
2232            in self.student['studycourse'].transcript_signees)
2233        # Officer can release the transcript
2234        self.browser.open(self.studycourse_path + '/transcript')
2235        self.browser.getLink("Release transcript").click()
2236        self.assertTrue(' UTC K1000000 wrote:<br><br>Comment line 1 <br>'
2237        'Comment line2<br><br>Dispatch Address:<br>Address line 1 <br>'
2238        'Address line2<br><br></p>' in self.browser.contents)
2239        self.browser.getControl(name="comment").value = (
2240            'Hello,\nYour transcript has been sent to the address provided.')
2241        self.browser.getControl("Save comment and release transcript").click()
2242        self.assertTrue(
2243            'UTC mrtranscript wrote:\n\nHello,\nYour transcript has '
2244            'been sent to the address provided.\n\n'
2245            in self.student['studycourse'].transcript_comment)
2246        # The comment has been logged
2247        logfile = os.path.join(
2248            self.app['datacenter'].storage, 'logs', 'students.log')
2249        logcontent = open(logfile).read()
2250        self.assertTrue(
2251            'mrtranscript - students.browser.StudentTranscriptReleaseFormPage - '
2252            'K1000000 - comment: Hello,<br>'
2253            'Your transcript has been sent to the address provided'
2254            in logcontent)
2255        # File has been stored in the file system
2256        # Check if transcript exists in the file system and is a PDF file
2257        storage = getUtility(IExtFileStore)
2258        file_id = IFileStoreNameChooser(
2259            self.student).chooseName(attr='final_transcript.pdf')
2260        pdf = storage.getFile(file_id).read()
2261        self.assertTrue(len(pdf) > 0)
2262        self.assertEqual(pdf[:8], '%PDF-1.4')
2263        # Copy the file to samples_dir
2264        path = os.path.join(samples_dir(), 'final_transcript.pdf')
2265        open(path, 'wb').write(pdf)
2266        print "Sample PDF final_transcript.pdf written to %s" % path
2267        # Check if there is an transcript pdf link in UI
2268        self.browser.open(self.student_path)
2269        self.assertTrue('Final Transcript' in self.browser.contents)
2270        self.browser.getLink("Final Transcript").click()
2271        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2272        self.assertEqual(self.browser.headers['Content-Type'],
2273                         'application/pdf')
2274        # Transcript views are no longer accessible
2275        self.browser.open(self.studycourse_path)
2276        self.assertFalse('studycourse/transcript' in self.browser.contents)
2277        self.browser.open(self.studycourse_path + '/transcript')
2278        self.assertTrue('Forbidden!' in self.browser.contents)
2279        self.browser.open(self.studycourse_path + '/transcript.pdf')
2280        self.assertTrue('Forbidden!' in self.browser.contents)
2281        # If we reset the transcript process
2282        # (can't be done by transcript officer), the file will be deleted
2283        IWorkflowInfo(self.student).fireTransition('reset11')
2284        self.browser.open(self.student_path)
2285        self.assertFalse('Final Transcript' in self.browser.contents)
2286        # ... and transcript process information has been removed
2287        self.assertEqual(self.student['studycourse'].transcript_comment, None)
2288        self.assertEqual(self.student['studycourse'].transcript_signees, None)
2289
2290    def test_landingpage_transcript_officer(self):
2291        IWorkflowState(self.student).setState('transcript requested')
2292        notify(grok.ObjectModifiedEvent(self.student))
2293        # Create transcript officer
2294        self.app['users'].addUser('mrtranscript', SECRET)
2295        self.app['users']['mrtranscript'].email = 'mrtranscript@foo.ng'
2296        self.app['users']['mrtranscript'].title = 'Ruth Gordon'
2297        # We assign transcript officer role at faculty level
2298        fac = self.app['faculties']['fac1']
2299        prmlocal = IPrincipalRoleManager(fac)
2300        prmlocal.assignRoleToPrincipal(
2301            'waeup.local.TranscriptOfficer', 'mrtranscript')
2302        notify(LocalRoleSetEvent(
2303            fac, 'waeup.local.TranscriptOfficer', 'mrtranscript', granted=True))
2304        # Login as transcript officer
2305        self.browser.open(self.login_path)
2306        self.browser.getControl(name="form.login").value = 'mrtranscript'
2307        self.browser.getControl(name="form.password").value = SECRET
2308        self.browser.getControl("Login").click()
2309        self.assertMatches('...You logged in...', self.browser.contents)
2310        # Officer is on landing page and does see the transcript link
2311        self.assertTrue(
2312            'http://localhost/app/students/K1000000/studycourse/transcript'
2313            in self.browser.contents)
2314        self.browser.getLink("K1000000").click()
2315        self.assertTrue(
2316            'Anna Tester: Transcript Data' in self.browser.contents)
2317        # Officer is on transcript page and can validate the transcript
2318        self.browser.getLink("Validate transcript").click()
2319        self.browser.getControl("Save comment and validate transcript").click()
2320        self.assertTrue(
2321            '<div class="alert alert-success">Transcript validated.</div>'
2322            in self.browser.contents)
2323        # Officer is still on transcript page and can release the transcript
2324        self.browser.getLink("Release transcript").click()
2325        self.browser.getControl("Save comment and release transcript").click()
2326        self.assertTrue(
2327            '<div class="alert alert-success">'
2328            'Transcript released and final transcript file saved.</div>'
2329            in self.browser.contents)
2330
2331    def test_landingpage_transcript_signee(self):
2332        IWorkflowState(self.student).setState('transcript validated')
2333        notify(grok.ObjectModifiedEvent(self.student))
2334        # Create transcript signee
2335        self.app['users'].addUser('mrtranscript', SECRET)
2336        self.app['users']['mrtranscript'].email = 'mrtranscript@foo.ng'
2337        self.app['users']['mrtranscript'].title = 'Ruth Gordon'
2338        # We assign transcript officer role at faculty level
2339        fac = self.app['faculties']['fac1']
2340        prmlocal = IPrincipalRoleManager(fac)
2341        prmlocal.assignRoleToPrincipal(
2342            'waeup.local.TranscriptSignee', 'mrtranscript')
2343        notify(LocalRoleSetEvent(
2344            fac, 'waeup.local.TranscriptSignee', 'mrtranscript', granted=True))
2345        # Login as transcript officer
2346        self.browser.open(self.login_path)
2347        self.browser.getControl(name="form.login").value = 'mrtranscript'
2348        self.browser.getControl(name="form.password").value = SECRET
2349        self.browser.getControl("Login").click()
2350        self.assertMatches('...You logged in...', self.browser.contents)
2351        # Officer is on landing page and does see the transcript link
2352        self.assertTrue(
2353            'http://localhost/app/students/K1000000/studycourse/transcript'
2354            in self.browser.contents)
2355        self.browser.getLink("K1000000").click()
2356        self.assertTrue(
2357            'Anna Tester: Transcript Data' in self.browser.contents)
2358        # Officer is on transcript page and can sign the transcript
2359        self.browser.getLink("Sign transcript").click()
2360        self.assertTrue(
2361            '<div class="alert alert-success">Transcript signed.</div>'
2362            in self.browser.contents)
2363        # Officer is still on transcript page
2364        self.assertTrue(
2365            'Anna Tester: Transcript Data' in self.browser.contents)
2366        # Officer can sign the transcript only once
2367        self.browser.getLink("Sign transcript").click()
2368        self.assertTrue(
2369            '<div class="alert alert-warning">'
2370            'You have already signed this transcript.</div>'
2371            in self.browser.contents)
2372        # Signature can be seen on transcript page
2373        self.assertTrue(
2374            'Electronically signed by Ruth Gordon (mrtranscript) on'
2375            in self.browser.contents)
2376
2377    def test_update_coursetickets(self):
2378        IWorkflowState(self.student).setState('school fee paid')
2379        studylevel = createObject(u'waeup.StudentStudyLevel')
2380        studylevel.level = 100
2381        studylevel.level_session = 2015
2382        self.student['studycourse'].entry_mode = 'ug_ft'
2383        self.student['studycourse'].addStudentStudyLevel(
2384            self.certificate, studylevel)
2385        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2386        self.browser.open(
2387            'http://localhost/app/faculties/fac1/dep1/courses/COURSE1/')
2388        self.assertFalse(
2389            'Update session 2015/2016 credits' in self.browser.contents)
2390        self.app['configuration'].current_academic_session = 2015
2391        self.browser.open(
2392            'http://localhost/app/faculties/fac1/dep1/courses/COURSE1/')
2393        self.browser.getLink("Update session 2015/2016 credits").click()
2394        self.assertTrue(
2395            'No course ticket found.' in self.browser.contents)
2396        logfile = os.path.join(
2397            self.app['datacenter'].storage, 'logs', 'main.log')
2398        logcontent = open(logfile).read()
2399        self.assertTrue(
2400            'zope.mgr - browser.pages.UpdateCourseTicketsView - '
2401            'course tickets updated: COURSE1' in logcontent)
2402        studylevel['COURSE1'].credits = 12
2403        self.browser.getLink("Update session 2015/2016 credits").click()
2404        self.assertTrue(
2405            'No course ticket found.' in self.browser.contents)
2406        studylevel.level_session = 2015
2407        self.student['studycourse'].current_session = 2015
2408        self.browser.getLink("Update session 2015/2016 credits").click()
2409        self.assertTrue(
2410            '1 course ticket(s) updated.' in self.browser.contents)
2411        logfile = os.path.join(
2412            self.app['datacenter'].storage, 'logs', 'students.log')
2413        logcontent = open(logfile).read()
2414        self.assertTrue(
2415            'zope.mgr - students.utils.StudentsUtils - '
2416            'K1000000 100/COURSE1 credits updated (10->12)' in logcontent)
2417        return
2418
2419
2420class StudentUITests(StudentsFullSetup):
2421    # Tests for Student class views and pages
2422
2423    def test_student_change_password(self):
2424        # Students can change the password
2425        self.student.personal_updated = datetime.utcnow()
2426        self.browser.open(self.login_path)
2427        self.browser.getControl(name="form.login").value = self.student_id
2428        self.browser.getControl(name="form.password").value = 'spwd'
2429        self.browser.getControl("Login").click()
2430        self.assertEqual(self.browser.url, self.student_path)
2431        self.assertTrue('You logged in' in self.browser.contents)
2432        # Change password
2433        self.browser.getLink("Change password").click()
2434        self.browser.getControl(name="change_password").value = 'pw'
2435        self.browser.getControl(
2436            name="change_password_repeat").value = 'pw'
2437        self.browser.getControl("Save").click()
2438        self.assertTrue('Password must have at least' in self.browser.contents)
2439        self.browser.getControl(name="change_password").value = 'new_password'
2440        self.browser.getControl(
2441            name="change_password_repeat").value = 'new_passssword'
2442        self.browser.getControl("Save").click()
2443        self.assertTrue('Passwords do not match' in self.browser.contents)
2444        self.browser.getControl(name="change_password").value = 'new_password'
2445        self.browser.getControl(
2446            name="change_password_repeat").value = 'new_password'
2447        self.browser.getControl("Save").click()
2448        self.assertTrue('Password changed' in self.browser.contents)
2449        # We are still logged in. Changing the password hasn't thrown us out.
2450        self.browser.getLink("Base Data").click()
2451        self.assertEqual(self.browser.url, self.student_path)
2452        # We can logout
2453        self.browser.getLink("Logout").click()
2454        self.assertTrue('You have been logged out' in self.browser.contents)
2455        self.assertEqual(self.browser.url, 'http://localhost/app/index')
2456        # We can login again with the new password
2457        self.browser.getLink("Login").click()
2458        self.browser.open(self.login_path)
2459        self.browser.getControl(name="form.login").value = self.student_id
2460        self.browser.getControl(name="form.password").value = 'new_password'
2461        self.browser.getControl("Login").click()
2462        self.assertEqual(self.browser.url, self.student_path)
2463        self.assertTrue('You logged in' in self.browser.contents)
2464        return
2465
2466    def test_forbidden_name(self):
2467        self.student.lastname = u'<TAG>Tester</TAG>'
2468        self.browser.open(self.login_path)
2469        self.browser.getControl(name="form.login").value = self.student_id
2470        self.browser.getControl(name="form.password").value = 'spwd'
2471        self.browser.getControl("Login").click()
2472        self.assertTrue('XXX: Base Data' in self.browser.contents)
2473        self.assertTrue('&lt;TAG&gt;Tester&lt;/TAG&gt;' in self.browser.contents)
2474        self.assertFalse('<TAG>Tester</TAG>' in self.browser.contents)
2475        return
2476
2477    def test_setpassword(self):
2478        # Set password for first-time access
2479        student = Student()
2480        student.reg_number = u'123456'
2481        student.firstname = u'Klaus'
2482        student.lastname = u'Tester'
2483        self.app['students'].addStudent(student)
2484        setpassword_path = 'http://localhost/app/setpassword'
2485        student_path = 'http://localhost/app/students/%s' % student.student_id
2486        self.browser.open(setpassword_path)
2487        self.browser.getControl(name="ac_series").value = self.existing_pwdseries
2488        self.browser.getControl(name="ac_number").value = self.existing_pwdnumber
2489        self.browser.getControl(name="reg_number").value = '223456'
2490        self.browser.getControl("Set").click()
2491        self.assertMatches('...No student found...',
2492                           self.browser.contents)
2493        self.browser.getControl(name="reg_number").value = '123456'
2494        self.browser.getControl(name="ac_number").value = '999999'
2495        self.browser.getControl("Set").click()
2496        self.assertMatches('...Access code is invalid...',
2497                           self.browser.contents)
2498        self.browser.getControl(name="ac_number").value = self.existing_pwdnumber
2499        self.browser.getControl("Set").click()
2500        self.assertMatches('...Password has been set. Your Student Id is...',
2501                           self.browser.contents)
2502        self.browser.getControl("Set").click()
2503        self.assertMatches(
2504            '...Password has already been set. Your Student Id is...',
2505            self.browser.contents)
2506        existing_pwdpin = self.pwdpins[1]
2507        parts = existing_pwdpin.split('-')[1:]
2508        existing_pwdseries, existing_pwdnumber = parts
2509        self.browser.getControl(name="ac_series").value = existing_pwdseries
2510        self.browser.getControl(name="ac_number").value = existing_pwdnumber
2511        self.browser.getControl(name="reg_number").value = '123456'
2512        self.browser.getControl("Set").click()
2513        self.assertMatches(
2514            '...You are using the wrong Access Code...',
2515            self.browser.contents)
2516        # The student can login with the new credentials
2517        self.browser.open(self.login_path)
2518        self.browser.getControl(name="form.login").value = student.student_id
2519        self.browser.getControl(
2520            name="form.password").value = self.existing_pwdnumber
2521        self.browser.getControl("Login").click()
2522        self.assertEqual(self.browser.url, student_path)
2523        self.assertTrue('You logged in' in self.browser.contents)
2524        return
2525
2526    def test_student_login(self):
2527        # Student cant login if their password is not set
2528        self.student.password = None
2529        self.browser.open(self.login_path)
2530        self.browser.getControl(name="form.login").value = self.student_id
2531        self.browser.getControl(name="form.password").value = 'spwd'
2532        self.browser.getControl("Login").click()
2533        self.assertTrue(
2534            'You entered invalid credentials.' in self.browser.contents)
2535        # We set the password again
2536        IUserAccount(
2537            self.app['students'][self.student_id]).setPassword('spwd')
2538        # Students can't login if their account is suspended/deactivated
2539        self.student.suspended = True
2540        self.browser.open(self.login_path)
2541        self.browser.getControl(name="form.login").value = self.student_id
2542        self.browser.getControl(name="form.password").value = 'spwd'
2543        self.browser.getControl("Login").click()
2544        self.assertMatches(
2545            '...<div class="alert alert-warning">'
2546            'Your account has been deactivated.</div>...', self.browser.contents)
2547        # If suspended_comment is set this message will be flashed instead
2548        self.student.suspended_comment = u'Aetsch baetsch!'
2549        self.browser.getControl(name="form.login").value = self.student_id
2550        self.browser.getControl(name="form.password").value = 'spwd'
2551        self.browser.getControl("Login").click()
2552        self.assertMatches(
2553            '...<div class="alert alert-warning">Aetsch baetsch!</div>...',
2554            self.browser.contents)
2555        self.student.suspended = False
2556        # Students can't login if a temporary password has been set and
2557        # is not expired
2558        self.app['students'][self.student_id].setTempPassword(
2559            'anybody', 'temp_spwd')
2560        self.browser.open(self.login_path)
2561        self.browser.getControl(name="form.login").value = self.student_id
2562        self.browser.getControl(name="form.password").value = 'spwd'
2563        self.browser.getControl("Login").click()
2564        self.assertMatches(
2565            '...Your account has been temporarily deactivated...',
2566            self.browser.contents)
2567        # The student can login with the temporary password
2568        self.browser.open(self.login_path)
2569        self.browser.getControl(name="form.login").value = self.student_id
2570        self.browser.getControl(name="form.password").value = 'temp_spwd'
2571        self.browser.getControl("Login").click()
2572        self.assertMatches(
2573            '...You logged in...', self.browser.contents)
2574        # Student can view the base data
2575        self.browser.open(self.student_path)
2576        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2577        self.assertEqual(self.browser.url, self.student_path)
2578        # When the password expires ...
2579        delta = timedelta(minutes=11)
2580        self.app['students'][self.student_id].temp_password[
2581            'timestamp'] = datetime.utcnow() - delta
2582        self.app['students'][self.student_id]._p_changed = True
2583        # ... the student will be automatically logged out
2584        self.assertRaises(
2585            Unauthorized, self.browser.open, self.student_path)
2586        # Then the student can login with the original password
2587        self.browser.open(self.login_path)
2588        self.browser.getControl(name="form.login").value = self.student_id
2589        self.browser.getControl(name="form.password").value = 'spwd'
2590        self.browser.getControl("Login").click()
2591        self.assertMatches(
2592            '...You logged in...', self.browser.contents)
2593
2594    def test_maintenance_mode(self):
2595        config = grok.getSite()['configuration']
2596        self.browser.open(self.login_path)
2597        self.browser.getControl(name="form.login").value = self.student_id
2598        self.browser.getControl(name="form.password").value = 'spwd'
2599        self.browser.getControl("Login").click()
2600        # Student logged in.
2601        self.assertTrue('You logged in' in self.browser.contents)
2602        self.assertTrue("Anna Tester" in self.browser.contents)
2603        # If maintenance mode is enabled, student is immediately logged out.
2604        config.maintmode_enabled_by = u'any_user'
2605        self.assertRaises(
2606            Unauthorized, self.browser.open, 'http://localhost/app/faculties')
2607        self.browser.open('http://localhost/app/login')
2608        self.assertTrue('The portal is in maintenance mode' in self.browser.contents)
2609        # Student really can't login if maintenance mode is enabled.
2610        self.browser.open(self.login_path)
2611        self.browser.getControl(name="form.login").value = self.student_id
2612        self.browser.getControl(name="form.password").value = 'spwd'
2613        self.browser.getControl("Login").click()
2614        # A second warning is raised.
2615        self.assertTrue(
2616            'The portal is in maintenance mode. You can\'t login!'
2617            in self.browser.contents)
2618        return
2619
2620    def test_student_basedata_slip(self):
2621        IWorkflowState(self.student).setState('school fee paid')
2622        self.browser.open(self.login_path)
2623        self.browser.getControl(name="form.login").value = self.student_id
2624        self.browser.getControl(name="form.password").value = 'spwd'
2625        self.browser.getControl("Login").click()
2626        # Students can open base data slip
2627        # (no button available in base package)
2628        pdf_url = '%s/basedata_slip.pdf' % self.studycourse_path
2629        self.browser.open(pdf_url)
2630        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2631        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2632        path = os.path.join(samples_dir(), 'basedata_slip.pdf')
2633        open(path, 'wb').write(self.browser.contents)
2634        print "Sample PDF basedata_slip.pdf written to %s" % path
2635
2636    def test_student_clearance(self):
2637        # Student cant login if their password is not set
2638        IWorkflowInfo(self.student).fireTransition('admit')
2639        self.browser.open(self.login_path)
2640        self.browser.getControl(name="form.login").value = self.student_id
2641        self.browser.getControl(name="form.password").value = 'spwd'
2642        self.browser.getControl("Login").click()
2643        self.assertMatches(
2644            '...You logged in...', self.browser.contents)
2645        # Admitted student can upload a passport picture
2646        self.browser.open(self.student_path + '/change_portrait')
2647        ctrl = self.browser.getControl(name='passportuploadedit')
2648        file_obj = open(SAMPLE_IMAGE, 'rb')
2649        file_ctrl = ctrl.mech_control
2650        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
2651        self.browser.getControl(
2652            name='upload_passportuploadedit').click()
2653        self.assertTrue(
2654            'src="http://localhost/app/students/K1000000/passport.jpg"'
2655            in self.browser.contents)
2656        # Students can open admission letter
2657        self.browser.getLink("Base Data").click()
2658        self.browser.getLink("Download admission letter").click()
2659        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2660        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2661        path = os.path.join(samples_dir(), 'admission_slip.pdf')
2662        open(path, 'wb').write(self.browser.contents)
2663        print "Sample PDF admission_slip.pdf written to %s" % path
2664        # Student can view the clearance data
2665        self.browser.open(self.student_path)
2666        self.browser.getLink("Clearance Data").click()
2667        # Student can't open clearance edit form before starting clearance
2668        self.browser.open(self.student_path + '/cedit')
2669        self.assertMatches('...The requested form is locked...',
2670                           self.browser.contents)
2671        self.browser.getLink("Clearance Data").click()
2672        self.browser.getLink("Start clearance").click()
2673        self.student.phone = None
2674        # Uups, we forgot to fill the phone fields
2675        self.browser.getControl("Start clearance").click()
2676        self.assertMatches('...Phone number is missing...',
2677                           self.browser.contents)
2678        self.browser.open(self.student_path + '/edit_base')
2679        self.browser.getControl(name="form.phone.ext").value = '12345'
2680        self.browser.getControl("Save").click()
2681        self.browser.open(self.student_path + '/start_clearance')
2682        self.browser.getControl(name="ac_series").value = '3'
2683        self.browser.getControl(name="ac_number").value = '4444444'
2684        self.browser.getControl("Start clearance now").click()
2685        self.assertMatches('...Activation code is invalid...',
2686                           self.browser.contents)
2687        self.browser.getControl(name="ac_series").value = self.existing_clrseries
2688        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
2689        # Owner is Hans Wurst, AC can't be invalidated
2690        self.browser.getControl("Start clearance now").click()
2691        self.assertMatches('...You are not the owner of this access code...',
2692                           self.browser.contents)
2693        # Set the correct owner
2694        self.existing_clrac.owner = self.student_id
2695        # clr_code might be set (and thus returns None) due importing
2696        # an empty clr_code column.
2697        self.student.clr_code = None
2698        self.browser.getControl("Start clearance now").click()
2699        self.assertMatches('...Clearance process has been started...',
2700                           self.browser.contents)
2701        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
2702        self.browser.getControl("Save", index=0).click()
2703        # Student can view the clearance data
2704        self.browser.getLink("Clearance Data").click()
2705        # and go back to the edit form
2706        self.browser.getLink("Edit").click()
2707        # Students can upload documents
2708        ctrl = self.browser.getControl(name='birthcertificateupload')
2709        file_obj = open(SAMPLE_IMAGE, 'rb')
2710        file_ctrl = ctrl.mech_control
2711        file_ctrl.add_file(file_obj, filename='my_birth_certificate.jpg')
2712        self.browser.getControl(
2713            name='upload_birthcertificateupload').click()
2714        self.assertTrue(
2715            'href="http://localhost/app/students/K1000000/birth_certificate"'
2716            in self.browser.contents)
2717        # Students can open clearance slip
2718        self.browser.getLink("View").click()
2719        self.browser.getLink("Download clearance slip").click()
2720        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2721        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2722        # Students can request clearance
2723        self.browser.open(self.edit_clearance_path)
2724        self.browser.getControl("Save and request clearance").click()
2725        self.browser.getControl(name="ac_series").value = self.existing_clrseries
2726        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
2727        self.browser.getControl("Request clearance now").click()
2728        self.assertMatches('...Clearance has been requested...',
2729                           self.browser.contents)
2730        # Student can't reopen clearance form after requesting clearance
2731        self.browser.open(self.student_path + '/cedit')
2732        self.assertMatches('...The requested form is locked...',
2733                           self.browser.contents)
2734
2735    def test_student_course_registration(self):
2736        # Student cant login if their password is not set
2737        IWorkflowInfo(self.student).fireTransition('admit')
2738        self.browser.open(self.login_path)
2739        self.browser.getControl(name="form.login").value = self.student_id
2740        self.browser.getControl(name="form.password").value = 'spwd'
2741        self.browser.getControl("Login").click()
2742        # Student can't add study level if not in state 'school fee paid'
2743        self.browser.open(self.student_path + '/studycourse/add')
2744        self.assertMatches('...The requested form is locked...',
2745                           self.browser.contents)
2746        # ... and must be transferred first
2747        IWorkflowState(self.student).setState('school fee paid')
2748        # Now students can add the current study level
2749        self.browser.getLink("Study Course").click()
2750        self.student['studycourse'].current_level = None
2751        self.browser.getLink("Add course list").click()
2752        self.assertMatches('...Your data are incomplete...',
2753                           self.browser.contents)
2754        self.student['studycourse'].current_level = 100
2755        self.browser.getLink("Add course list").click()
2756        self.assertMatches('...Add current level 100 (Year 1)...',
2757                           self.browser.contents)
2758        self.browser.getControl("Create course list now").click()
2759        # A level with one course ticket was created
2760        self.assertEqual(self.student['studycourse']['100'].number_of_tickets, 1)
2761        self.browser.getLink("100").click()
2762        self.browser.getLink("Edit course list").click()
2763        self.browser.getLink("here").click()
2764        self.browser.getControl(name="form.course").value = ['COURSE1']
2765        self.browser.getControl("Add course ticket").click()
2766        self.assertMatches('...The ticket exists...',
2767                           self.browser.contents)
2768        self.student['studycourse'].current_level = 200
2769        self.browser.getLink("Study Course").click()
2770        self.browser.getLink("Add course list").click()
2771        self.assertMatches('...Add current level 200 (Year 2)...',
2772                           self.browser.contents)
2773        self.browser.getControl("Create course list now").click()
2774        self.browser.getLink("200").click()
2775        self.browser.getLink("Edit course list").click()
2776        self.browser.getLink("here").click()
2777        self.browser.getControl(name="form.course").value = ['COURSE1']
2778        self.course.credits = 100
2779        self.browser.getControl("Add course ticket").click()
2780        self.assertMatches(
2781            '...Maximum credits exceeded...', self.browser.contents)
2782        self.course.credits = 10
2783        self.browser.getControl("Add course ticket").click()
2784        self.assertMatches('...The ticket exists...',
2785                           self.browser.contents)
2786        # Indeed the ticket exists as carry-over course from level 100
2787        # since its score was 0
2788        self.assertTrue(
2789            self.student['studycourse']['200']['COURSE1'].carry_over is True)
2790        self.assertTrue(
2791            self.student['studycourse']['200']['COURSE1'].course_category is None)
2792        # Students can open the pdf course registration slip
2793        self.browser.open(self.student_path + '/studycourse/200')
2794        self.browser.getLink("Download course registration slip").click()
2795        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2796        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2797        path = os.path.join(samples_dir(), 'course_registration_slip.pdf')
2798        open(path, 'wb').write(self.browser.contents)
2799        print "Sample PDF course_registration_slip.pdf written to %s" % path
2800        # Students can remove course tickets
2801        self.browser.open(self.student_path + '/studycourse/200/edit')
2802        self.browser.getControl("Remove selected", index=0).click()
2803        self.assertTrue('No ticket selected' in self.browser.contents)
2804        # No ticket can be selected since the carry-over course is a core course
2805        self.assertRaises(
2806            LookupError, self.browser.getControl, name='val_id')
2807        self.student['studycourse']['200']['COURSE1'].mandatory = False
2808        self.browser.open(self.student_path + '/studycourse/200/edit')
2809        # Course list can't be registered if total_credits exceeds max_credits
2810        self.student['studycourse']['200']['COURSE1'].credits = 60
2811        self.browser.getControl("Register course list").click()
2812        self.assertTrue('Maximum credits exceeded' in self.browser.contents)
2813        # Student can now remove the ticket
2814        ctrl = self.browser.getControl(name='val_id')
2815        ctrl.getControl(value='COURSE1').selected = True
2816        self.browser.getControl("Remove selected", index=0).click()
2817        self.assertTrue('Successfully removed' in self.browser.contents)
2818        # Removing course tickets is properly logged
2819        logfile = os.path.join(
2820            self.app['datacenter'].storage, 'logs', 'students.log')
2821        logcontent = open(logfile).read()
2822        self.assertTrue('K1000000 - students.browser.StudyLevelEditFormPage '
2823        '- K1000000 - level 200 - removed: COURSE1' in logcontent)
2824        # They can add the same ticket using the edit page directly.
2825        # We can do the same by adding the course on the manage page directly
2826        self.browser.getControl(name="course").value = 'COURSE1'
2827        self.browser.getControl("Add course ticket").click()
2828        # Adding course tickets is logged
2829        logfile = os.path.join(
2830            self.app['datacenter'].storage, 'logs', 'students.log')
2831        logcontent = open(logfile).read()
2832        self.assertTrue('K1000000 - students.browser.StudyLevelEditFormPage - '
2833            'K1000000 - level 200 - added: COURSE1|200|2004' in logcontent)
2834        # Course list can be registered
2835        self.browser.getControl("Register course list").click()
2836        self.assertTrue('Course list has been registered' in self.browser.contents)
2837        self.assertEqual(self.student.state, 'courses registered')
2838        # Course list can be unregistered
2839        self.browser.getLink("Unregister courses").click()
2840        self.assertEqual(self.student.state, 'school fee paid')
2841        self.assertTrue('Course list has been unregistered' in self.browser.contents)
2842        self.browser.open(self.student_path + '/studycourse/200/unregister_courses')
2843        self.assertTrue('You are in the wrong state' in self.browser.contents)
2844        # Students can view the transcript
2845        #self.browser.open(self.studycourse_path)
2846        #self.browser.getLink("Transcript").click()
2847        #self.browser.getLink("Academic Transcript").click()
2848        #self.assertEqual(self.browser.headers['Status'], '200 Ok')
2849        #self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2850        return
2851
2852    def test_student_ticket_update(self):
2853        IWorkflowState(self.student).setState('school fee paid')
2854        self.student['studycourse'].current_level = 100
2855        self.browser.open(self.login_path)
2856        self.browser.getControl(name="form.login").value = self.student_id
2857        self.browser.getControl(name="form.password").value = 'spwd'
2858        self.browser.getControl("Login").click()
2859        # Now students can add the current study level
2860        self.browser.getLink("Study Course").click()
2861        self.browser.getLink("Add course list").click()
2862        self.assertMatches('...Add current level 100 (Year 1)...',
2863                           self.browser.contents)
2864        self.browser.getControl("Create course list now").click()
2865        # A level with one course ticket was created
2866        self.assertEqual(
2867            self.student['studycourse']['100'].number_of_tickets, 1)
2868        self.browser.getLink("100").click()
2869        self.assertTrue('<td>Unnamed Course</td>' in self.browser.contents)
2870        self.browser.getLink("Edit course list").click()
2871        self.browser.getControl("Update all tickets").click()
2872        self.assertTrue('All course tickets updated.' in self.browser.contents)
2873        # ... nothing has changed
2874        self.assertTrue('<td>Unnamed Course</td>' in self.browser.contents)
2875        # We change the title of the course
2876        self.course.title = u'New Title'
2877        self.browser.getControl("Update all tickets").click()
2878        self.assertTrue('<td>New Title</td>' in self.browser.contents)
2879        # We remove the course
2880        del self.app['faculties']['fac1']['dep1'].courses['COURSE1']
2881        self.browser.getControl("Update all tickets").click()
2882        self.assertTrue(' <td>New Title (course cancelled)</td>'
2883            in self.browser.contents)
2884        # Course ticket invalidation has been logged
2885        logfile = os.path.join(
2886            self.app['datacenter'].storage, 'logs', 'students.log')
2887        logcontent = open(logfile).read()
2888        self.assertTrue(
2889            'K1000000 - students.browser.StudyLevelEditFormPage - '
2890            'K1000000 - level 100 - course tickets invalidated: COURSE1'
2891            in logcontent)
2892        return
2893
2894    def test_student_course_already_passed(self):
2895        IWorkflowState(self.student).setState('school fee paid')
2896        self.student['studycourse'].current_level = 100
2897        self.browser.open(self.login_path)
2898        self.browser.getControl(name="form.login").value = self.student_id
2899        self.browser.getControl(name="form.password").value = 'spwd'
2900        self.browser.getControl("Login").click()
2901        # Now students can add the current study level
2902        self.browser.getLink("Study Course").click()
2903        self.browser.getLink("Add course list").click()
2904        self.assertMatches('...Add current level 100 (Year 1)...',
2905                           self.browser.contents)
2906        self.browser.getControl("Create course list now").click()
2907        # A level with one course ticket was created
2908        self.assertEqual(self.student['studycourse']['100'].number_of_tickets, 1)
2909        # We set the score above the passmark
2910        self.student['studycourse']['100'][
2911            'COURSE1'].score = self.student['studycourse']['100'][
2912                'COURSE1'].passmark + 1
2913        # We add a second level
2914        self.student['studycourse'].current_level = 200
2915        self.browser.getLink("Study Course").click()
2916        self.browser.getLink("Add course list").click()
2917        self.assertMatches('...Add current level 200 (Year 2)...',
2918                           self.browser.contents)
2919        self.browser.getControl("Create course list now").click()
2920        self.browser.getLink("200").click()
2921        self.browser.getLink("Edit course list").click()
2922        self.browser.getLink("here").click()
2923        self.browser.getControl(name="form.course").value = ['COURSE1']
2924        self.browser.getControl("Add course ticket").click()
2925        self.assertTrue(
2926            'Course has already been passed at previous level'
2927            in self.browser.contents)
2928        self.assertEqual(self.student['studycourse']['200'].number_of_tickets, 0)
2929        # We set the score below the passmark
2930        self.student['studycourse']['100'][
2931            'COURSE1'].score = self.student['studycourse']['100'][
2932                'COURSE1'].passmark - 1
2933        self.browser.getControl("Add course ticket").click()
2934        self.assertTrue(
2935            'Successfully added COURSE1' in self.browser.contents)
2936        self.assertEqual(self.student['studycourse']['200'].number_of_tickets, 1)
2937        return
2938
2939    def test_student_course_registration_outstanding(self):
2940        self.course = createObject('waeup.Course')
2941        self.course.code = 'COURSE2'
2942        self.course.semester = 1
2943        self.course.credits = 45
2944        self.course.passmark = 40
2945        self.app['faculties']['fac1']['dep1'].courses.addCourse(
2946            self.course)
2947        IWorkflowState(self.student).setState('school fee paid')
2948        self.browser.open(self.login_path)
2949        self.browser.getControl(name="form.login").value = self.student_id
2950        self.browser.getControl(name="form.password").value = 'spwd'
2951        self.browser.getControl("Login").click()
2952        self.browser.open(self.student_path + '/studycourse/add')
2953        self.browser.getControl("Create course list now").click()
2954        self.assertEqual(self.student['studycourse']['100'].number_of_tickets, 1)
2955        self.student['studycourse'].current_level = 200
2956        self.browser.getLink("Study Course").click()
2957        self.browser.getLink("Add course list").click()
2958        self.assertMatches('...Add current level 200 (Year 2)...',
2959                           self.browser.contents)
2960        self.browser.getControl("Create course list now").click()
2961        self.browser.getLink("200").click()
2962        self.browser.getLink("Edit course list").click()
2963        self.browser.getLink("here").click()
2964        self.browser.getControl(name="form.course").value = ['COURSE2']
2965        self.browser.getControl("Add course ticket").click()
2966        # Carryover COURSE1 in level 200 already has 10 credits
2967        self.assertMatches(
2968            '...Maximum credits exceeded...', self.browser.contents)
2969        # If COURSE1 is outstanding, its credits won't be considered
2970        self.student['studycourse']['200']['COURSE1'].outstanding = True
2971        self.browser.getControl("Add course ticket").click()
2972        self.assertMatches(
2973            '...Successfully added COURSE2...', self.browser.contents)
2974        return
2975
2976    def test_postgraduate_student_access(self):
2977        self.certificate.study_mode = 'pg_ft'
2978        self.certificate.start_level = 999
2979        self.certificate.end_level = 999
2980        self.student['studycourse'].current_level = 999
2981        IWorkflowState(self.student).setState('school fee paid')
2982        self.browser.open(self.login_path)
2983        self.browser.getControl(name="form.login").value = self.student_id
2984        self.browser.getControl(name="form.password").value = 'spwd'
2985        self.browser.getControl("Login").click()
2986        self.assertTrue(
2987            'You logged in.' in self.browser.contents)
2988        # Now students can add the current study level
2989        self.browser.getLink("Study Course").click()
2990        self.browser.getLink("Add course list").click()
2991        self.assertMatches('...Add current level Postgraduate Level...',
2992                           self.browser.contents)
2993        self.browser.getControl("Create course list now").click()
2994        self.assertTrue("You successfully created a new course list"
2995            in self.browser.contents)
2996        # A level with one course ticket was created
2997        self.assertEqual(self.student['studycourse']['999'].number_of_tickets, 0)
2998        self.browser.getLink("Edit course list").click()
2999        self.browser.getLink("here").click()
3000        self.browser.getControl(name="form.course").value = ['COURSE1']
3001        self.browser.getControl("Add course ticket").click()
3002        self.assertMatches('...Successfully added COURSE1...',
3003                           self.browser.contents)
3004        # Postgraduate students can't register course lists
3005        self.browser.getControl("Register course list").click()
3006        self.assertTrue("your course list can't bee registered"
3007            in self.browser.contents)
3008        self.assertEqual(self.student.state, 'school fee paid')
3009        return
3010
3011    def test_student_clearance_wo_clrcode(self):
3012        IWorkflowState(self.student).setState('clearance started')
3013        self.browser.open(self.login_path)
3014        self.browser.getControl(name="form.login").value = self.student_id
3015        self.browser.getControl(name="form.password").value = 'spwd'
3016        self.browser.getControl("Login").click()
3017        self.browser.open(self.edit_clearance_path)
3018        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
3019        self.browser.getControl("Save and request clearance").click()
3020        self.assertMatches('...Clearance has been requested...',
3021                           self.browser.contents)
3022
3023    def test_student_clearance_payment(self):
3024        # Login
3025        self.browser.open(self.login_path)
3026        self.browser.getControl(name="form.login").value = self.student_id
3027        self.browser.getControl(name="form.password").value = 'spwd'
3028        self.browser.getControl("Login").click()
3029
3030        # Students can add online clearance payment tickets
3031        self.browser.open(self.payments_path + '/addop')
3032        self.browser.getControl(name="form.p_category").value = ['clearance']
3033        self.browser.getControl("Create ticket").click()
3034        self.assertMatches('...ticket created...',
3035                           self.browser.contents)
3036
3037        # Students can't approve the payment
3038        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
3039        ctrl = self.browser.getControl(name='val_id')
3040        value = ctrl.options[0]
3041        self.browser.getLink(value).click()
3042        payment_url = self.browser.url
3043        self.assertRaises(
3044            Unauthorized, self.browser.open, payment_url + '/approve')
3045        # In the base package they can 'use' a fake approval view.
3046        # XXX: I tried to use
3047        # self.student['payments'][value].approveStudentPayment() instead.
3048        # But this function fails in
3049        # w.k.accesscodes.accesscode.create_accesscode.
3050        # grok.getSite returns None in tests.
3051        self.browser.open(payment_url + '/fake_approve')
3052        self.assertMatches('...Payment approved...',
3053                          self.browser.contents)
3054        expected = '''...
3055        <td>
3056          <span>Paid</span>
3057        </td>...'''
3058        expected = '''...
3059        <td>
3060          <span>Paid</span>
3061        </td>...'''
3062        self.assertMatches(expected,self.browser.contents)
3063        payment_id = self.student['payments'].keys()[0]
3064        payment = self.student['payments'][payment_id]
3065        self.assertEqual(payment.p_state, 'paid')
3066        self.assertEqual(payment.r_amount_approved, 3456.0)
3067        self.assertEqual(payment.r_code, 'AP')
3068        self.assertEqual(payment.r_desc, u'Payment approved by Anna Tester')
3069        # The new CLR-0 pin has been created
3070        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
3071        pin = self.app['accesscodes']['CLR-0'].keys()[0]
3072        ac = self.app['accesscodes']['CLR-0'][pin]
3073        self.assertEqual(ac.owner, self.student_id)
3074        self.assertEqual(ac.cost, 3456.0)
3075
3076        # Students can open the pdf payment slip
3077        self.browser.open(payment_url + '/payment_slip.pdf')
3078        self.assertEqual(self.browser.headers['Status'], '200 Ok')
3079        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
3080
3081        # The new CLR-0 pin can be used for starting clearance
3082        # but they have to upload a passport picture first
3083        # which is only possible in state admitted
3084        self.browser.open(self.student_path + '/change_portrait')
3085        self.assertMatches('...form is locked...',
3086                          self.browser.contents)
3087        IWorkflowInfo(self.student).fireTransition('admit')
3088        self.browser.open(self.student_path + '/change_portrait')
3089        image = open(SAMPLE_IMAGE, 'rb')
3090        ctrl = self.browser.getControl(name='passportuploadedit')
3091        file_ctrl = ctrl.mech_control
3092        file_ctrl.add_file(image, filename='my_photo.jpg')
3093        self.browser.getControl(
3094            name='upload_passportuploadedit').click()
3095        self.browser.open(self.student_path + '/start_clearance')
3096        parts = pin.split('-')[1:]
3097        clrseries, clrnumber = parts
3098        self.browser.getControl(name="ac_series").value = clrseries
3099        self.browser.getControl(name="ac_number").value = clrnumber
3100        self.browser.getControl("Start clearance now").click()
3101        self.assertMatches('...Clearance process has been started...',
3102                           self.browser.contents)
3103
3104    def test_student_schoolfee_payment(self):
3105        configuration = createObject('waeup.SessionConfiguration')
3106        configuration.academic_session = 2005
3107        self.app['configuration'].addSessionConfiguration(configuration)
3108        # Login
3109        self.browser.open(self.login_path)
3110        self.browser.getControl(name="form.login").value = self.student_id
3111        self.browser.getControl(name="form.password").value = 'spwd'
3112        self.browser.getControl("Login").click()
3113
3114        # Students can add online school fee payment tickets.
3115        IWorkflowState(self.student).setState('returning')
3116        self.browser.open(self.payments_path)
3117        self.assertRaises(
3118            LookupError, self.browser.getControl, name='val_id')
3119        self.browser.getLink("Add current session payment ticket").click()
3120        self.browser.getControl(name="form.p_category").value = ['schoolfee']
3121        self.browser.getControl("Create ticket").click()
3122        self.assertMatches('...ticket created...',
3123                           self.browser.contents)
3124        ctrl = self.browser.getControl(name='val_id')
3125        value = ctrl.options[0]
3126        self.browser.getLink(value).click()
3127        self.assertMatches('...Amount Authorized...',
3128                           self.browser.contents)
3129        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
3130        # Payment session and will be calculated as defined
3131        # in w.k.students.utils because we set changed the state
3132        # to returning
3133        self.assertEqual(self.student['payments'][value].p_session, 2005)
3134        self.assertEqual(self.student['payments'][value].p_level, 200)
3135
3136        # Student is the payer of the payment ticket.
3137        payer = IPayer(self.student['payments'][value])
3138        self.assertEqual(payer.display_fullname, 'Anna Tester')
3139        self.assertEqual(payer.id, self.student_id)
3140        self.assertEqual(payer.faculty, 'fac1')
3141        self.assertEqual(payer.department, 'dep1')
3142
3143        # We simulate the approval
3144        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
3145        self.browser.open(self.browser.url + '/fake_approve')
3146        self.assertMatches('...Payment approved...',
3147                          self.browser.contents)
3148
3149        ## The new SFE-0 pin can be used for starting new session
3150        #self.browser.open(self.studycourse_path)
3151        #self.browser.getLink('Start new session').click()
3152        #pin = self.app['accesscodes']['SFE-0'].keys()[0]
3153        #parts = pin.split('-')[1:]
3154        #sfeseries, sfenumber = parts
3155        #self.browser.getControl(name="ac_series").value = sfeseries
3156        #self.browser.getControl(name="ac_number").value = sfenumber
3157        #self.browser.getControl("Start now").click()
3158        #self.assertMatches('...Session started...',
3159        #                   self.browser.contents)
3160
3161        self.assertTrue(self.student.state == 'school fee paid')
3162        return
3163
3164    def test_student_bedallocation_payment(self):
3165        # Login
3166        self.browser.open(self.login_path)
3167        self.browser.getControl(name="form.login").value = self.student_id
3168        self.browser.getControl(name="form.password").value = 'spwd'
3169        self.browser.getControl("Login").click()
3170        self.browser.open(self.payments_path)
3171        self.browser.open(self.payments_path + '/addop')
3172        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
3173        self.browser.getControl("Create ticket").click()
3174        self.assertMatches('...ticket created...',
3175                           self.browser.contents)
3176        # Students can remove only online payment tickets which have
3177        # not received a valid callback
3178        self.browser.open(self.payments_path)
3179        ctrl = self.browser.getControl(name='val_id')
3180        value = ctrl.options[0]
3181        ctrl.getControl(value=value).selected = True
3182        self.browser.getControl("Remove selected", index=0).click()
3183        self.assertTrue('Successfully removed' in self.browser.contents)
3184
3185    def test_student_maintenance_payment(self):
3186        # Login
3187        self.browser.open(self.login_path)
3188        self.browser.getControl(name="form.login").value = self.student_id
3189        self.browser.getControl(name="form.password").value = 'spwd'
3190        self.browser.getControl("Login").click()
3191        self.browser.open(self.payments_path)
3192        self.browser.open(self.payments_path + '/addop')
3193        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
3194        self.browser.getControl("Create ticket").click()
3195        self.assertMatches('...You have not yet booked accommodation...',
3196                           self.browser.contents)
3197        # We continue this test in test_student_accommodation
3198
3199    def test_student_previous_payments(self):
3200        configuration = createObject('waeup.SessionConfiguration')
3201        configuration.academic_session = 2000
3202        configuration.clearance_fee = 3456.0
3203        configuration.booking_fee = 123.4
3204        self.app['configuration'].addSessionConfiguration(configuration)
3205        configuration2 = createObject('waeup.SessionConfiguration')
3206        configuration2.academic_session = 2003
3207        configuration2.clearance_fee = 3456.0
3208        configuration2.booking_fee = 123.4
3209        self.app['configuration'].addSessionConfiguration(configuration2)
3210        configuration3 = createObject('waeup.SessionConfiguration')
3211        configuration3.academic_session = 2005
3212        configuration3.clearance_fee = 3456.0
3213        configuration3.booking_fee = 123.4
3214        self.app['configuration'].addSessionConfiguration(configuration3)
3215        self.student['studycourse'].entry_session = 2002
3216
3217        # Login
3218        self.browser.open(self.login_path)
3219        self.browser.getControl(name="form.login").value = self.student_id
3220        self.browser.getControl(name="form.password").value = 'spwd'
3221        self.browser.getControl("Login").click()
3222
3223        # Students can add previous school fee payment tickets in any state.
3224        IWorkflowState(self.student).setState('courses registered')
3225        self.browser.open(self.payments_path)
3226        self.browser.getLink("Add previous session payment ticket").click()
3227
3228        # Previous session payment form is provided
3229        self.assertEqual(self.student.current_session, 2004)
3230        self.browser.getControl(name="form.p_category").value = ['schoolfee']
3231        self.browser.getControl(name="form.p_session").value = ['2000']
3232        self.browser.getControl(name="form.p_level").value = ['300']
3233        self.browser.getControl("Create ticket").click()
3234        self.assertMatches('...The previous session must not fall below...',
3235                           self.browser.contents)
3236        self.browser.getControl(name="form.p_category").value = ['schoolfee']
3237        self.browser.getControl(name="form.p_session").value = ['2005']
3238        self.browser.getControl(name="form.p_level").value = ['300']
3239        self.browser.getControl("Create ticket").click()
3240        self.assertMatches('...This is not a previous session...',
3241                           self.browser.contents)
3242
3243        # Students can pay current session school fee.
3244        self.browser.getControl(name="form.p_category").value = ['schoolfee']
3245        self.browser.getControl(name="form.p_session").value = ['2004']
3246        self.browser.getControl(name="form.p_level").value = ['300']
3247        self.browser.getControl("Create ticket").click()
3248        self.assertMatches('...ticket created...',
3249                           self.browser.contents)
3250        ctrl = self.browser.getControl(name='val_id')
3251        value = ctrl.options[0]
3252        self.browser.getLink(value).click()
3253        self.assertMatches('...Amount Authorized...',
3254                           self.browser.contents)
3255        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
3256
3257        # Ticket creation is logged.
3258        logfile = os.path.join(
3259            self.app['datacenter'].storage, 'logs', 'students.log')
3260        logcontent = open(logfile).read()
3261        self.assertTrue(
3262            'K1000000 - students.browser.PreviousPaymentAddFormPage - '
3263            'K1000000 - added: %s' % value
3264            in logcontent)
3265
3266        # Payment session is properly set
3267        self.assertEqual(self.student['payments'][value].p_session, 2004)
3268        self.assertEqual(self.student['payments'][value].p_level, 300)
3269
3270        # We simulate the approval
3271        self.browser.open(self.browser.url + '/fake_approve')
3272        self.assertMatches('...Payment approved...',
3273                          self.browser.contents)
3274
3275        # No AC has been created
3276        self.assertEqual(len(self.app['accesscodes']['SFE-0'].keys()), 0)
3277        self.assertTrue(self.student['payments'][value].ac is None)
3278
3279        # Current payment flag is set False
3280        self.assertFalse(self.student['payments'][value].p_current)
3281
3282        # Button and form are not available for students who are in
3283        # states up to cleared
3284        self.student['studycourse'].entry_session = 2004
3285        IWorkflowState(self.student).setState('cleared')
3286        self.browser.open(self.payments_path)
3287        self.assertFalse(
3288            "Add previous session payment ticket" in self.browser.contents)
3289        self.browser.open(self.payments_path + '/addpp')
3290        self.assertTrue(
3291            "No previous payment to be made" in self.browser.contents)
3292        return
3293
3294    def test_postgraduate_student_payments(self):
3295        configuration = createObject('waeup.SessionConfiguration')
3296        configuration.academic_session = 2005
3297        self.app['configuration'].addSessionConfiguration(configuration)
3298        self.certificate.study_mode = 'pg_ft'
3299        self.certificate.start_level = 999
3300        self.certificate.end_level = 999
3301        self.student['studycourse'].current_level = 999
3302        # Login
3303        self.browser.open(self.login_path)
3304        self.browser.getControl(name="form.login").value = self.student_id
3305        self.browser.getControl(name="form.password").value = 'spwd'
3306        self.browser.getControl("Login").click()
3307        # Students can add online school fee payment tickets.
3308        IWorkflowState(self.student).setState('cleared')
3309        self.browser.open(self.payments_path)
3310        self.browser.getLink("Add current session payment ticket").click()
3311        self.browser.getControl(name="form.p_category").value = ['schoolfee']
3312        self.browser.getControl("Create ticket").click()
3313        self.assertMatches('...ticket created...',
3314                           self.browser.contents)
3315        ctrl = self.browser.getControl(name='val_id')
3316        value = ctrl.options[0]
3317        self.browser.getLink(value).click()
3318        self.assertMatches('...Amount Authorized...',
3319                           self.browser.contents)
3320        # Payment session and level are current ones.
3321        # Postgrads have to pay school_fee_1.
3322        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
3323        self.assertEqual(self.student['payments'][value].p_session, 2004)
3324        self.assertEqual(self.student['payments'][value].p_level, 999)
3325
3326        # We simulate the approval
3327        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
3328        self.browser.open(self.browser.url + '/fake_approve')
3329        self.assertMatches('...Payment approved...',
3330                          self.browser.contents)
3331
3332        ## The new SFE-0 pin can be used for starting session
3333        #self.browser.open(self.studycourse_path)
3334        #self.browser.getLink('Start new session').click()
3335        #pin = self.app['accesscodes']['SFE-0'].keys()[0]
3336        #parts = pin.split('-')[1:]
3337        #sfeseries, sfenumber = parts
3338        #self.browser.getControl(name="ac_series").value = sfeseries
3339        #self.browser.getControl(name="ac_number").value = sfenumber
3340        #self.browser.getControl("Start now").click()
3341        #self.assertMatches('...Session started...',
3342        #                   self.browser.contents)
3343
3344        self.assertTrue(self.student.state == 'school fee paid')
3345
3346        # Postgrad students do not need to register courses the
3347        # can just pay for the next session.
3348        self.browser.open(self.payments_path)
3349        # Remove first payment to be sure that we access the right ticket
3350        del self.student['payments'][value]
3351        self.browser.getLink("Add current session payment ticket").click()
3352        self.browser.getControl(name="form.p_category").value = ['schoolfee']
3353        self.browser.getControl("Create ticket").click()
3354        ctrl = self.browser.getControl(name='val_id')
3355        value = ctrl.options[0]
3356        self.browser.getLink(value).click()
3357        # Payment session has increased by one, payment level remains the same.
3358        # Returning Postgraduates have to pay school_fee_2.
3359        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
3360        self.assertEqual(self.student['payments'][value].p_session, 2005)
3361        self.assertEqual(self.student['payments'][value].p_level, 999)
3362
3363        # Student is still in old session
3364        self.assertEqual(self.student.current_session, 2004)
3365
3366        # We do not need to pay the ticket if any other
3367        # SFE pin is provided
3368        pin_container = self.app['accesscodes']
3369        pin_container.createBatch(
3370            datetime.utcnow(), 'some_userid', 'SFE', 9.99, 1)
3371        pin = pin_container['SFE-1'].values()[0].representation
3372        sfeseries, sfenumber = pin.split('-')[1:]
3373        # The new SFE-1 pin can be used for starting new session
3374        self.browser.open(self.studycourse_path)
3375        self.browser.getLink('Start new session').click()
3376        self.browser.getControl(name="ac_series").value = sfeseries
3377        self.browser.getControl(name="ac_number").value = sfenumber
3378        self.browser.getControl("Start now").click()
3379        self.assertMatches('...Session started...',
3380                           self.browser.contents)
3381        self.assertTrue(self.student.state == 'school fee paid')
3382        # Student is in new session
3383        self.assertEqual(self.student.current_session, 2005)
3384        self.assertEqual(self.student['studycourse'].current_level, 999)
3385        return
3386
3387    def test_student_accommodation(self):
3388        # Create a second hostel with one bed
3389        hostel = Hostel()
3390        hostel.hostel_id = u'hall-2'
3391        hostel.hostel_name = u'Hall 2'
3392        self.app['hostels'].addHostel(hostel)
3393        bed = Bed()
3394        bed.bed_id = u'hall-2_A_101_A'
3395        bed.bed_number = 1
3396        bed.owner = NOT_OCCUPIED
3397        bed.bed_type = u'regular_female_fr'
3398        self.app['hostels'][hostel.hostel_id].addBed(bed)
3399        self.app['hostels'].allocation_expiration = 7
3400
3401        self.browser.open(self.login_path)
3402        self.browser.getControl(name="form.login").value = self.student_id
3403        self.browser.getControl(name="form.password").value = 'spwd'
3404        self.browser.getControl("Login").click()
3405        # Students can add online booking fee payment tickets and open the
3406        # callback view (see test_manage_payments).
3407        self.browser.getLink("Payments").click()
3408        self.browser.getLink("Add current session payment ticket").click()
3409        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
3410        self.browser.getControl("Create ticket").click()
3411        ctrl = self.browser.getControl(name='val_id')
3412        value = ctrl.options[0]
3413        self.browser.getLink(value).click()
3414        self.browser.open(self.browser.url + '/fake_approve')
3415        # The new HOS-0 pin has been created.
3416        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
3417        pin = self.app['accesscodes']['HOS-0'].keys()[0]
3418        ac = self.app['accesscodes']['HOS-0'][pin]
3419        parts = pin.split('-')[1:]
3420        sfeseries, sfenumber = parts
3421        # Students can use HOS code and book a bed space with it ...
3422        self.browser.open(self.acco_path)
3423        # ... but not if booking period has expired ...
3424        self.app['hostels'].enddate = datetime.now(pytz.utc)
3425        self.browser.getControl("Book accommodation").click()
3426        self.assertMatches('...Outside booking period: ...',
3427                           self.browser.contents)
3428        self.app['hostels'].enddate = datetime.now(pytz.utc) + timedelta(days=10)
3429        # ... or student data are incomplete ...
3430        self.student['studycourse'].current_level = None
3431        self.browser.getControl("Book accommodation").click()
3432        self.assertMatches('...Your data are incomplete...',
3433            self.browser.contents)
3434        self.student['studycourse'].current_level = 100
3435        # ... or student is not the an allowed state ...
3436        self.browser.getControl("Book accommodation").click()
3437        self.assertMatches('...You are in the wrong...',
3438                           self.browser.contents)
3439        # Students can still not see the disired hostel selector.
3440        self.assertFalse('desired hostel' in self.browser.contents)
3441        IWorkflowInfo(self.student).fireTransition('admit')
3442        # Students can now see the disired hostel selector.
3443        self.browser.reload()
3444        self.browser.open(self.acco_path)
3445        self.assertTrue('desired hostel' in self.browser.contents)
3446        self.browser.getControl(name="hostel").value = ['hall-2']
3447        self.browser.getControl("Save").click()
3448        self.assertTrue('selection has been saved' in self.browser.contents)
3449        self.assertTrue('<option selected="selected" value="hall-2">'
3450            in self.browser.contents)
3451        self.browser.getControl("Book accommodation").click()
3452        self.assertMatches('...Activation Code:...',
3453                           self.browser.contents)
3454        # Student can't use faked ACs ...
3455        self.browser.getControl(name="ac_series").value = u'nonsense'
3456        self.browser.getControl(name="ac_number").value = sfenumber
3457        self.browser.getControl("Create bed ticket").click()
3458        self.assertMatches('...Activation code is invalid...',
3459                           self.browser.contents)
3460        # ... or ACs owned by somebody else.
3461        ac.owner = u'Anybody'
3462        self.browser.getControl(name="ac_series").value = sfeseries
3463        self.browser.getControl(name="ac_number").value = sfenumber
3464        # There is no free bed space and the bed selector does not appear
3465        self.assertFalse('<option value="hall-1_A_101_A">'
3466            in self.browser.contents)
3467        self.browser.getControl("Create bed ticket").click()
3468        # Hostel 2 has only a bed for women.
3469        self.assertTrue('There is no free bed in your category regular_male_fr.'
3470            in self.browser.contents)
3471        self.browser.getControl(name="hostel").value = ['hall-1']
3472        self.browser.getControl("Save").click()
3473        self.browser.getControl("Book accommodation").click()
3474        # Student can't use faked ACs ...
3475        self.browser.getControl(name="ac_series").value = sfeseries
3476        self.browser.getControl(name="ac_number").value = sfenumber
3477        self.browser.getControl("Create bed ticket").click()
3478        self.assertMatches('...You are not the owner of this access code...',
3479                           self.browser.contents)
3480        # The bed remains empty.
3481        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
3482        self.assertTrue(bed.owner == NOT_OCCUPIED)
3483        ac.owner = self.student_id
3484        self.browser.open(self.acco_path + '/add')
3485        self.browser.getControl(name="ac_series").value = sfeseries
3486        self.browser.getControl(name="ac_number").value = sfenumber
3487        # Bed can be selected
3488        self.browser.getControl(name="bed").value = ['hall-1_A_101_A']
3489        self.browser.getControl("Create bed ticket").click()
3490        self.assertTrue('Bed ticket created and bed booked'
3491            in self.browser.contents)
3492        # Bed has been allocated.
3493        self.assertTrue(bed.owner == self.student_id)
3494        # BedTicketAddPage is now blocked.
3495        self.browser.getControl("Book accommodation").click()
3496        self.assertMatches('...You already booked a bed space...',
3497            self.browser.contents)
3498        # The bed ticket displays the data correctly.
3499        self.browser.open(self.acco_path + '/2004')
3500        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
3501                           self.browser.contents)
3502        self.assertMatches('...2004/2005...', self.browser.contents)
3503        self.assertMatches('...regular_male_fr...', self.browser.contents)
3504        self.assertMatches('...%s...' % pin, self.browser.contents)
3505        # Students can open the pdf slip.
3506        self.browser.open(self.browser.url + '/bed_allocation_slip.pdf')
3507        self.assertEqual(self.browser.headers['Status'], '200 Ok')
3508        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
3509        path = os.path.join(samples_dir(), 'bed_allocation_slip.pdf')
3510        open(path, 'wb').write(self.browser.contents)
3511        print "Sample PDF bed_allocation_slip.pdf written to %s" % path
3512        # Students can't relocate themselves.
3513        self.assertFalse('Relocate' in self.browser.contents)
3514        relocate_path = self.acco_path + '/2004/relocate'
3515        self.assertRaises(
3516            Unauthorized, self.browser.open, relocate_path)
3517        # Students can't see the Remove button and check boxes.
3518        self.browser.open(self.acco_path)
3519        self.assertFalse('Remove' in self.browser.contents)
3520        self.assertFalse('val_id' in self.browser.contents)
3521        # Students can pay maintenance fee now.
3522        self.browser.open(self.payments_path)
3523        self.browser.open(self.payments_path + '/addop')
3524        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
3525        self.browser.getControl("Create ticket").click()
3526        self.assertMatches('...Payment ticket created...',
3527                           self.browser.contents)
3528        ctrl = self.browser.getControl(name='val_id')
3529        value = ctrl.options[0]
3530        # Maintennace fee is taken from the hostel object.
3531        self.assertEqual(self.student['payments'][value].amount_auth, 876.0)
3532        # If the hostel's maintenance fee isn't set, the fee is
3533        # taken from the session configuration object.
3534        self.app['hostels']['hall-1'].maint_fee = 0.0
3535        self.browser.open(self.payments_path + '/addop')
3536        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
3537        self.browser.getControl("Create ticket").click()
3538        ctrl = self.browser.getControl(name='val_id')
3539        value = ctrl.options[1]
3540        self.assertEqual(self.student['payments'][value].amount_auth, 987.0)
3541        # The bedticket is aware of successfull maintenance fee payment
3542        bedticket = self.student['accommodation']['2004']
3543        self.assertFalse(bedticket.maint_payment_made)
3544        self.student['payments'][value].approve()
3545        self.assertTrue(bedticket.maint_payment_made)
3546        return
3547
3548    def test_change_password_request(self):
3549        self.browser.open('http://localhost/app/changepw')
3550        self.browser.getControl(name="form.identifier").value = '123'
3551        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
3552        self.browser.getControl("Send login credentials").click()
3553        self.assertTrue('An email with' in self.browser.contents)
3554
3555    def test_student_expired_personal_data(self):
3556        # Login
3557        IWorkflowState(self.student).setState('school fee paid')
3558        delta = timedelta(days=180)
3559        self.student.personal_updated = datetime.utcnow() - delta
3560        self.browser.open(self.login_path)
3561        self.browser.getControl(name="form.login").value = self.student_id
3562        self.browser.getControl(name="form.password").value = 'spwd'
3563        self.browser.getControl("Login").click()
3564        self.assertEqual(self.browser.url, self.student_path)
3565        self.assertTrue(
3566            'You logged in' in self.browser.contents)
3567        # Students don't see personal_updated field in edit form
3568        self.browser.open(self.edit_personal_path)
3569        self.assertFalse('Updated' in self.browser.contents)
3570        self.browser.open(self.personal_path)
3571        self.assertTrue('Updated' in self.browser.contents)
3572        self.browser.getLink("Logout").click()
3573        delta = timedelta(days=181)
3574        self.student.personal_updated = datetime.utcnow() - delta
3575        self.browser.open(self.login_path)
3576        self.browser.getControl(name="form.login").value = self.student_id
3577        self.browser.getControl(name="form.password").value = 'spwd'
3578        self.browser.getControl("Login").click()
3579        self.assertEqual(self.browser.url, self.edit_personal_path)
3580        self.assertTrue(
3581            'Your personal data record is outdated.' in self.browser.contents)
3582
3583    def test_request_transcript(self):
3584        IWorkflowState(self.student).setState('graduated')
3585        self.browser.open(self.login_path)
3586        self.browser.getControl(name="form.login").value = self.student_id
3587        self.browser.getControl(name="form.password").value = 'spwd'
3588        self.browser.getControl("Login").click()
3589        self.assertMatches(
3590            '...You logged in...', self.browser.contents)
3591        # Create payment ticket
3592        self.browser.open(self.payments_path)
3593        self.browser.open(self.payments_path + '/addop')
3594        self.browser.getControl(name="form.p_category").value = ['transcript']
3595        self.browser.getControl("Create ticket").click()
3596        ctrl = self.browser.getControl(name='val_id')
3597        value = ctrl.options[0]
3598        self.browser.getLink(value).click()
3599        self.assertMatches('...Amount Authorized...',
3600                           self.browser.contents)
3601        self.assertEqual(self.student['payments'][value].amount_auth, 4567.0)
3602        # Student is the payer of the payment ticket.
3603        payer = IPayer(self.student['payments'][value])
3604        self.assertEqual(payer.display_fullname, 'Anna Tester')
3605        self.assertEqual(payer.id, self.student_id)
3606        self.assertEqual(payer.faculty, 'fac1')
3607        self.assertEqual(payer.department, 'dep1')
3608        # We simulate the approval and fetch the pin
3609        self.assertEqual(len(self.app['accesscodes']['TSC-0']),0)
3610        self.browser.open(self.browser.url + '/fake_approve')
3611        self.assertMatches('...Payment approved...',
3612                          self.browser.contents)
3613        pin = self.app['accesscodes']['TSC-0'].keys()[0]
3614        parts = pin.split('-')[1:]
3615        tscseries, tscnumber = parts
3616        # Student can use the pin to send the transcript request
3617        self.browser.open(self.student_path)
3618        self.browser.getLink("Request transcript").click()
3619        self.browser.getControl(name="ac_series").value = tscseries
3620        self.browser.getControl(name="ac_number").value = tscnumber
3621        self.browser.getControl(name="comment").value = 'Comment line 1 \nComment line2'
3622        self.browser.getControl(name="address").value = 'Address line 1 \nAddress line2'
3623        self.browser.getControl("Submit").click()
3624        self.assertMatches('...Transcript processing has been started...',
3625                          self.browser.contents)
3626        self.assertEqual(self.student.state, 'transcript requested')
3627        self.assertMatches(
3628            '... UTC K1000000 wrote:\n\nComment line 1 \n'
3629            'Comment line2\n\nDispatch Address:\nAddress line 1 \n'
3630            'Address line2\n\n', self.student['studycourse'].transcript_comment)
3631        # The comment has been logged
3632        logfile = os.path.join(
3633            self.app['datacenter'].storage, 'logs', 'students.log')
3634        logcontent = open(logfile).read()
3635        self.assertTrue(
3636            'K1000000 - students.browser.StudentTranscriptRequestPage - '
3637            'K1000000 - comment: Comment line 1 <br>Comment line2\n'
3638            in logcontent)
3639
3640    def test_late_registration(self):
3641        # Login
3642        delta = timedelta(days=10)
3643        self.app['configuration'][
3644            '2004'].coursereg_deadline = datetime.now(pytz.utc) - delta
3645        IWorkflowState(self.student).setState('school fee paid')
3646        self.browser.open(self.login_path)
3647        self.browser.getControl(name="form.login").value = self.student_id
3648        self.browser.getControl(name="form.password").value = 'spwd'
3649        self.browser.getControl("Login").click()
3650        self.browser.open(self.payments_path)
3651        self.browser.open(self.payments_path + '/addop')
3652        self.browser.getControl(name="form.p_category").value = ['late_registration']
3653        self.browser.getControl("Create ticket").click()
3654        self.assertMatches('...ticket created...',
3655                           self.browser.contents)
3656        self.browser.open(self.payments_path)
3657        ctrl = self.browser.getControl(name='val_id')
3658        value = ctrl.options[0]
3659        self.browser.getLink("Study Course").click()
3660        self.browser.getLink("Add course list").click()
3661        self.assertMatches('...Add current level 100 (Year 1)...',
3662                           self.browser.contents)
3663        self.browser.getControl("Create course list now").click()
3664        self.browser.getLink("100").click()
3665        self.browser.getLink("Edit course list").click()
3666        self.browser.getControl("Register course list").click()
3667        self.assertTrue('Course registration has ended. Please pay' in self.browser.contents)
3668        self.student['payments'][value].approve()
3669        self.browser.getControl("Register course list").click()
3670        self.assertTrue('Course list has been registered' in self.browser.contents)
3671        self.assertEqual(self.student.state, 'courses registered')
3672
3673    def test_former_course(self):
3674        IWorkflowState(self.student).setState('school fee paid')
3675        self.student['studycourse'].current_level = 100
3676        self.browser.open(self.login_path)
3677        self.browser.getControl(name="form.login").value = self.student_id
3678        self.browser.getControl(name="form.password").value = 'spwd'
3679        self.browser.getControl("Login").click()
3680        # Now students can add the current study level
3681        self.browser.getLink("Study Course").click()
3682        self.browser.getLink("Add course list").click()
3683        self.assertMatches('...Add current level 100 (Year 1)...',
3684                           self.browser.contents)
3685        self.browser.getControl("Create course list now").click()
3686        # A level with one course ticket was created
3687        self.assertEqual(
3688            self.student['studycourse']['100'].number_of_tickets, 1)
3689        self.browser.getLink("100").click()
3690        self.assertTrue('<td>Unnamed Course</td>' in self.browser.contents)
3691        self.browser.getLink("Edit course list").click()
3692        del self.student['studycourse']['100']['COURSE1']
3693        # Course can be added again via CourseTicketAddFormPage2
3694        self.browser.getLink("here").click()
3695        self.browser.getControl(name="form.course").value = ['COURSE1']
3696        self.browser.getControl("Add course ticket").click()
3697        self.assertTrue('Successfully added COURSE1' in self.browser.contents)
3698        self.assertEqual(len(self.student['studycourse']['100'].keys()),1)
3699        del self.student['studycourse']['100']['COURSE1']
3700        self.course.former_course = True
3701        self.browser.getLink("here").click()
3702        self.browser.getControl(name="form.course").value = ['COURSE1']
3703        self.browser.getControl("Add course ticket").click()
3704        self.assertTrue('Former courses can\'t be added.' in self.browser.contents)
3705        self.assertEqual(len(self.student['studycourse']['100'].keys()),0)
3706        # Course can be added again via StudyLevelEditFormPage
3707        self.browser.getLink("100").click()
3708        self.browser.getLink("Edit course list").click()
3709        self.browser.getControl(name="course").value = 'COURSE1'
3710        self.browser.getControl("Add course ticket").click()
3711        self.assertTrue('Former courses can\'t be added.' in
3712            self.browser.contents)
3713        self.assertEqual(len(self.student['studycourse']['100'].keys()),0)
3714        # but added if current course
3715        self.course.former_course = False
3716        self.browser.getControl(name="course").value = 'COURSE1'
3717        self.browser.getControl("Add course ticket").click()
3718        self.assertTrue('Successfully added COURSE1' in self.browser.contents)
3719        self.assertEqual(len(self.student['studycourse']['100'].keys()),1)
3720        return
3721
3722class StudentRequestPWTests(StudentsFullSetup):
3723    # Tests for student registration
3724
3725    layer = FunctionalLayer
3726
3727    def test_request_pw(self):
3728        # Student with wrong number can't be found.
3729        self.browser.open('http://localhost/app/requestpw')
3730        self.browser.getControl(name="form.lastname").value = 'Tester'
3731        self.browser.getControl(name="form.number").value = 'anynumber'
3732        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
3733        self.browser.getControl("Send login credentials").click()
3734        self.assertTrue('No student record found.'
3735            in self.browser.contents)
3736        # Anonymous is not informed that lastname verification failed.
3737        # It seems that the record doesn't exist.
3738        self.browser.open('http://localhost/app/requestpw')
3739        self.browser.getControl(name="form.lastname").value = 'Johnny'
3740        self.browser.getControl(name="form.number").value = '123'
3741        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
3742        self.browser.getControl("Send login credentials").click()
3743        self.assertTrue('No student record found.'
3744            in self.browser.contents)
3745        # Even with the correct lastname we can't register if a
3746        # password has been set and used.
3747        self.browser.getControl(name="form.lastname").value = 'Tester'
3748        self.browser.getControl(name="form.number").value = '123'
3749        self.browser.getControl("Send login credentials").click()
3750        self.assertTrue('Your password has already been set and used.'
3751            in self.browser.contents)
3752        self.browser.open('http://localhost/app/requestpw')
3753        self.app['students'][self.student_id].password = None
3754        # The lastname field, used for verification, is not case-sensitive.
3755        self.browser.getControl(name="form.lastname").value = 'tESTer'
3756        self.browser.getControl(name="form.number").value = '123'
3757        self.browser.getControl(name="form.email").value = 'new@yy.zz'
3758        self.browser.getControl("Send login credentials").click()
3759        # Yeah, we succeded ...
3760        self.assertTrue('Your password request was successful.'
3761            in self.browser.contents)
3762        # We can also use the matric_number instead.
3763        self.browser.open('http://localhost/app/requestpw')
3764        self.browser.getControl(name="form.lastname").value = 'tESTer'
3765        self.browser.getControl(name="form.number").value = '234'
3766        self.browser.getControl(name="form.email").value = 'new@yy.zz'
3767        self.browser.getControl("Send login credentials").click()
3768        self.assertTrue('Your password request was successful.'
3769            in self.browser.contents)
3770        # ... and  student can be found in the catalog via the email address
3771        cat = queryUtility(ICatalog, name='students_catalog')
3772        results = list(
3773            cat.searchResults(
3774            email=('new@yy.zz', 'new@yy.zz')))
3775        self.assertEqual(self.student,results[0])
3776        logfile = os.path.join(
3777            self.app['datacenter'].storage, 'logs', 'main.log')
3778        logcontent = open(logfile).read()
3779        self.assertTrue('zope.anybody - students.browser.StudentRequestPasswordPage - '
3780                        '234 (K1000000) - new@yy.zz' in logcontent)
3781        return
3782
3783    def test_student_locked_level_forms(self):
3784
3785        # Add two study levels, one current and one previous
3786        studylevel = createObject(u'waeup.StudentStudyLevel')
3787        studylevel.level = 100
3788        self.student['studycourse'].addStudentStudyLevel(
3789            self.certificate, studylevel)
3790        studylevel = createObject(u'waeup.StudentStudyLevel')
3791        studylevel.level = 200
3792        self.student['studycourse'].addStudentStudyLevel(
3793            self.certificate, studylevel)
3794        IWorkflowState(self.student).setState('school fee paid')
3795        self.student['studycourse'].current_level = 200
3796
3797        self.browser.open(self.login_path)
3798        self.browser.getControl(name="form.login").value = self.student_id
3799        self.browser.getControl(name="form.password").value = 'spwd'
3800        self.browser.getControl("Login").click()
3801
3802        self.browser.open(self.student_path + '/studycourse/200/edit')
3803        self.assertFalse('The requested form is locked' in self.browser.contents)
3804        self.browser.open(self.student_path + '/studycourse/100/edit')
3805        self.assertTrue('The requested form is locked' in self.browser.contents)
3806
3807        self.browser.open(self.student_path + '/studycourse/200/ctadd')
3808        self.assertFalse('The requested form is locked' in self.browser.contents)
3809        self.browser.open(self.student_path + '/studycourse/100/ctadd')
3810        self.assertTrue('The requested form is locked' in self.browser.contents)
3811
3812        IWorkflowState(self.student).setState('courses registered')
3813        self.browser.open(self.student_path + '/studycourse/200/edit')
3814        self.assertTrue('The requested form is locked' in self.browser.contents)
3815        self.browser.open(self.student_path + '/studycourse/200/ctadd')
3816        self.assertTrue('The requested form is locked' in self.browser.contents)
3817
3818
3819class PublicPagesTests(StudentsFullSetup):
3820    # Tests for simple webservices
3821
3822    layer = FunctionalLayer
3823
3824    def test_paymentrequest(self):
3825        payment = createObject('waeup.StudentOnlinePayment')
3826        payment.p_category = u'schoolfee'
3827        payment.p_session = self.student.current_session
3828        payment.p_item = u'My Certificate'
3829        payment.p_id = u'anyid'
3830        self.student['payments']['anykey'] = payment
3831        # Request information about unpaid payment ticket
3832        self.browser.open('http://localhost/app/paymentrequest?P_ID=anyid')
3833        self.assertEqual(self.browser.contents, '-1')
3834        # Request information about paid payment ticket
3835        payment.p_state = u'paid'
3836        notify(grok.ObjectModifiedEvent(payment))
3837        self.browser.open('http://localhost/app/paymentrequest?P_ID=anyid')
3838        self.assertEqual(self.browser.contents,
3839            'FULL_NAME=Anna Tester&FACULTY=fac1&DEPARTMENT=dep1'
3840            '&PAYMENT_ITEM=My Certificate&PAYMENT_CATEGORY=School Fee'
3841            '&ACADEMIC_SESSION=2004/2005&MATRIC_NUMBER=234&REG_NUMBER=123'
3842            '&FEE_AMOUNT=0.0')
3843        self.browser.open('http://localhost/app/paymentrequest?NONSENSE=nonsense')
3844        self.assertEqual(self.browser.contents, '-1')
3845        self.browser.open('http://localhost/app/paymentrequest?P_ID=nonsense')
3846        self.assertEqual(self.browser.contents, '-1')
3847
3848class StudentDataExportTests(StudentsFullSetup, FunctionalAsyncTestCase):
3849    # Tests for StudentsContainer class views and pages
3850
3851    layer = FunctionalLayer
3852
3853    def wait_for_export_job_completed(self):
3854        # helper function waiting until the current export job is completed
3855        manager = getUtility(IJobManager)
3856        job_id = self.app['datacenter'].running_exports[0][0]
3857        job = manager.get(job_id)
3858        wait_for_result(job)
3859        return job_id
3860
3861    def add_payment(self, student):
3862        # get a payment with all fields set
3863        payment = StudentOnlinePayment()
3864        payment.creation_date = datetime(2012, 12, 13)
3865        payment.p_id = 'my-id'
3866        payment.p_category = u'schoolfee'
3867        payment.p_state = 'paid'
3868        payment.ac = u'666'
3869        payment.p_item = u'p-item'
3870        payment.p_level = 100
3871        payment.p_session = curr_year - 6
3872        payment.payment_date = datetime(2012, 12, 13)
3873        payment.amount_auth = 12.12
3874        payment.r_amount_approved = 12.12
3875        payment.r_code = u'r-code'
3876        # XXX: there is no addPayment method to give predictable names
3877        self.payment = student['payments']['my-payment'] = payment
3878        return payment
3879
3880    def test_datacenter_export(self):
3881        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3882        self.browser.open('http://localhost/app/datacenter/@@exportconfig')
3883        self.browser.getControl(name="exporter").value = ['bursary']
3884        self.browser.getControl(name="session").value = ['2004']
3885        self.browser.getControl(name="level").value = ['100']
3886        self.browser.getControl(name="mode").value = ['ug_ft']
3887        self.browser.getControl(name="payments_start").value = '13/12/2012'
3888        self.browser.getControl(name="payments_end").value = '14/12/2012'
3889        self.browser.getControl("Create CSV file").click()
3890
3891        # When the job is finished and we reload the page...
3892        job_id = self.wait_for_export_job_completed()
3893        # ... the csv file can be downloaded ...
3894        self.browser.open('http://localhost/app/datacenter/@@export')
3895        self.browser.getLink("Download").click()
3896        self.assertEqual(self.browser.headers['content-type'],
3897            'text/csv; charset=UTF-8')
3898        self.assertTrue(
3899            'filename="WAeUP.Kofa_bursary_%s.csv' % job_id in
3900            self.browser.headers['content-disposition'])
3901        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3902        job_id = self.app['datacenter'].running_exports[0][0]
3903        # ... and discarded
3904        self.browser.open('http://localhost/app/datacenter/@@export')
3905        self.browser.getControl("Discard").click()
3906        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3907        # Creation, downloading and discarding is logged
3908        logfile = os.path.join(
3909            self.app['datacenter'].storage, 'logs', 'datacenter.log')
3910        logcontent = open(logfile).read()
3911        self.assertTrue(
3912            'zope.mgr - students.browser.DatacenterExportJobContainerJobConfig '
3913            '- exported: bursary (2004, 100, ug_ft, None, None, None, '
3914            '13/12/2012, 14/12/2012, all, all, all, all, all), job_id=%s'
3915            % job_id in logcontent
3916            )
3917        self.assertTrue(
3918            'zope.mgr - browser.pages.ExportCSVView '
3919            '- downloaded: WAeUP.Kofa_bursary_%s.csv, job_id=%s'
3920            % (job_id, job_id) in logcontent
3921            )
3922        self.assertTrue(
3923            'zope.mgr - browser.pages.ExportCSVPage '
3924            '- discarded: job_id=%s' % job_id in logcontent
3925            )
3926
3927    def test_datacenter_export_selected(self):
3928        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3929        self.browser.open('http://localhost/app/datacenter/@@exportselected')
3930        self.browser.getControl(name="exporter").value = ['students']
3931        self.browser.getControl(name="students").value = 'K1000000'
3932        self.browser.getControl("Create CSV file").click()
3933        # When the job is finished and we reload the page...
3934        job_id = self.wait_for_export_job_completed()
3935        # ... the csv file can be downloaded ...
3936        self.browser.open('http://localhost/app/datacenter/@@export')
3937        self.browser.getLink("Download").click()
3938        self.assertEqual(self.browser.headers['content-type'],
3939            'text/csv; charset=UTF-8')
3940        self.assertTrue(
3941            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
3942            self.browser.headers['content-disposition'])
3943        self.assertTrue(
3944            'adm_code,clr_code,date_of_birth,email,employer,'
3945            'firstname,flash_notice,lastname,matric_number,middlename,nationality,'
3946            'officer_comment,parents_email,perm_address,'
3947            'personal_updated,phone,reg_number,'
3948            'sex,student_id,suspended,suspended_comment,'
3949            'password,state,history,certcode,is_postgrad,'
3950            'current_level,current_session\r\n'
3951            ',,1981-02-04#,aa@aa.ng,,Anna,,Tester,234,,,,,,,'
3952            '1234#,123,m,K1000000,0,,{SSHA}' in self.browser.contents)
3953        self.browser.open('http://localhost/app/datacenter/@@export')
3954        self.browser.getControl("Discard").click()
3955
3956    def test_payment_dates(self):
3957        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3958        self.browser.open('http://localhost/app/datacenter/@@exportconfig')
3959        self.browser.getControl(name="exporter").value = ['bursary']
3960        self.browser.getControl(name="session").value = ['2004']
3961        self.browser.getControl(name="level").value = ['100']
3962        self.browser.getControl(name="mode").value = ['ug_ft']
3963        self.browser.getControl(name="payments_start").value = '13/12/2012'
3964        # If one payment date is missing, an error message appears
3965        self.browser.getControl(name="payments_end").value = ''
3966        self.browser.getControl("Create CSV file").click()
3967        self.assertTrue('Payment dates do not match format d/m/Y'
3968            in self.browser.contents)
3969
3970    def test_faculties_export(self):
3971        self.add_payment(self.student)
3972        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3973        facs_path = 'http://localhost/app/faculties'
3974        self.browser.open(facs_path)
3975        self.browser.getLink("Export student data").click()
3976        self.browser.getControl("Set export parameters").click()
3977        self.browser.getControl(name="exporter").value = ['bursary']
3978        self.browser.getControl(name="session").value = ['2004']
3979        self.browser.getControl(name="level").value = ['100']
3980        self.browser.getControl(name="mode").value = ['ug_ft']
3981        self.browser.getControl(name="payments_start").value = '13/12/2012'
3982        self.browser.getControl(name="payments_end").value = '14/12/2012'
3983        self.browser.getControl(name="paycat").value = ['schoolfee']
3984        self.browser.getControl("Create CSV file").click()
3985
3986        # When the job is finished and we reload the page...
3987        job_id = self.wait_for_export_job_completed()
3988        self.browser.open(facs_path + '/exports')
3989        # ... the csv file can be downloaded ...
3990        self.browser.getLink("Download").click()
3991        self.assertEqual(self.browser.headers['content-type'],
3992            'text/csv; charset=UTF-8')
3993        self.assertTrue(
3994            'filename="WAeUP.Kofa_bursary_%s.csv' % job_id in
3995            self.browser.headers['content-disposition'])
3996        self.assertTrue(
3997            '666,12.12,2012-12-13 00:00:00#,schoolfee,[],1,my-id,p-item,100,%s,'
3998            'paid,2012-12-13 00:00:00#,12.12,r-code,,K1000000,234,123,Anna,,'
3999            'Tester,created,2004,2004,,fac1,dep1,CERT1'
4000            %(curr_year-6) in self.browser.contents)
4001        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4002        job_id = self.app['datacenter'].running_exports[0][0]
4003        # ... and discarded
4004        self.browser.open(facs_path + '/exports')
4005        self.browser.getControl("Discard").click()
4006        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4007        # Creation, downloading and discarding is logged
4008        logfile = os.path.join(
4009            self.app['datacenter'].storage, 'logs', 'datacenter.log')
4010        logcontent = open(logfile).read()
4011        self.assertTrue(
4012            'zope.mgr - students.browser.FacultiesExportJobContainerJobConfig '
4013            '- exported: bursary (2004, 100, ug_ft, None, None, None, '
4014            '13/12/2012, 14/12/2012, all, all, schoolfee, all, all), job_id=%s'
4015            % job_id in logcontent
4016            )
4017        self.assertTrue(
4018            'zope.mgr - students.browser.ExportJobContainerDownload '
4019            '- downloaded: WAeUP.Kofa_bursary_%s.csv, job_id=%s'
4020            % (job_id, job_id) in logcontent
4021            )
4022        self.assertTrue(
4023            'zope.mgr - students.browser.ExportJobContainerOverview '
4024            '- discarded: job_id=%s' % job_id in logcontent
4025            )
4026        # Officer can also enter student id and gets the same export file
4027        self.browser.open(facs_path)
4028        self.browser.getLink("Export student data").click()
4029        self.browser.getControl("Enter student ids or matric numbers").click()
4030        self.browser.getControl(name="exporter").value = ['bursary']
4031        self.browser.getControl(name="students").value = 'K1000000'
4032        self.browser.getControl("Create CSV file").click()
4033        # When the job is finished and we reload the page...
4034        job_id = self.wait_for_export_job_completed()
4035        # ... the csv file can be downloaded ...
4036        self.browser.open('http://localhost/app/faculties/exports')
4037        self.browser.getLink("Download").click()
4038        self.assertEqual(self.browser.headers['content-type'],
4039            'text/csv; charset=UTF-8')
4040        self.assertTrue(
4041            'filename="WAeUP.Kofa_bursary_%s.csv' % job_id in
4042            self.browser.headers['content-disposition'])
4043        self.assertTrue(
4044            '666,12.12,2012-12-13 00:00:00#,schoolfee,[],1,my-id,p-item,100,%s,'
4045            'paid,2012-12-13 00:00:00#,12.12,r-code,,K1000000,234,123,Anna,,'
4046            'Tester,created,2004,2004,,fac1,dep1,CERT1'
4047            %(curr_year-6) in self.browser.contents)
4048
4049    def test_faculty_export(self):
4050        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
4051        fac1_path = 'http://localhost/app/faculties/fac1'
4052        self.browser.open(fac1_path)
4053        self.browser.getLink("Export student data").click()
4054        self.browser.getControl("Set export parameters").click()
4055        self.browser.getControl(name="exporter").value = ['students']
4056        self.browser.getControl(name="session").value = ['2004']
4057        self.browser.getControl(name="level").value = ['100']
4058        self.browser.getControl(name="mode").value = ['ug_ft']
4059        # The testbrowser does not hide the payment period fields, but
4060        # values are ignored when using the students exporter.
4061        self.browser.getControl(name="payments_start").value = '13/12/2012'
4062        self.browser.getControl(name="payments_end").value = '14/12/2012'
4063        self.browser.getControl("Create CSV file").click()
4064        # When the job is finished and we reload the page...
4065        job_id = self.wait_for_export_job_completed()
4066        self.browser.open(fac1_path + '/exports')
4067        # ... the csv file can be downloaded ...
4068        self.browser.getLink("Download").click()
4069        self.assertEqual(self.browser.headers['content-type'],
4070            'text/csv; charset=UTF-8')
4071        self.assertTrue(
4072            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
4073            self.browser.headers['content-disposition'])
4074        self.assertTrue(
4075            'adm_code,clr_code,date_of_birth,email,employer,'
4076            'firstname,flash_notice,lastname,matric_number,middlename,nationality,'
4077            'officer_comment,parents_email,perm_address,'
4078            'personal_updated,phone,reg_number,'
4079            'sex,student_id,suspended,suspended_comment,'
4080            'password,state,history,certcode,is_postgrad,'
4081            'current_level,current_session\r\n'
4082            ',,1981-02-04#,aa@aa.ng,,Anna,,Tester,234,,,,,,,'
4083            '1234#,123,m,K1000000,0,,{SSHA}' in self.browser.contents)
4084        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4085        job_id = self.app['datacenter'].running_exports[0][0]
4086        # ... and discarded
4087        self.browser.open(fac1_path + '/exports')
4088        self.browser.getControl("Discard").click()
4089        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4090        # Creation, downloading and discarding is logged
4091        logfile = os.path.join(
4092            self.app['datacenter'].storage, 'logs', 'datacenter.log')
4093        logcontent = open(logfile).read()
4094        self.assertTrue(
4095            'zope.mgr - students.browser.FacultyExportJobContainerJobConfig '
4096            '- exported: students (2004, 100, ug_ft, fac1, None, None, '
4097            '13/12/2012, 14/12/2012, all, all, all, all, all), job_id=%s'
4098            % job_id in logcontent
4099            )
4100        self.assertTrue(
4101            'zope.mgr - students.browser.ExportJobContainerDownload '
4102            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
4103            % (job_id, job_id) in logcontent
4104            )
4105        self.assertTrue(
4106            'zope.mgr - students.browser.ExportJobContainerOverview '
4107            '- discarded: job_id=%s' % job_id in logcontent
4108            )
4109        # Officer can set export parameters but cannot enter student id
4110        # at faculty level
4111        self.browser.open(fac1_path + '/exports')
4112        self.assertTrue("Set export parameters"
4113                         in self.browser.contents)
4114        self.assertFalse("Enter student ids or matric numbers"
4115                         in self.browser.contents)
4116
4117    def test_department_export(self):
4118        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
4119        dep1_path = 'http://localhost/app/faculties/fac1/dep1'
4120        self.browser.open(dep1_path)
4121        self.browser.getLink("Export student data").click()
4122        self.browser.getControl("Set export parameters").click()
4123        self.browser.getControl(name="exporter").value = ['students']
4124        self.browser.getControl(name="session").value = ['2004']
4125        self.browser.getControl(name="level").value = ['100']
4126        self.browser.getControl(name="mode").value = ['ug_ft']
4127        # The testbrowser does not hide the payment period fields, but
4128        # values are ignored when using the students exporter.
4129        self.browser.getControl(name="payments_start").value = '13/12/2012'
4130        self.browser.getControl(name="payments_end").value = '14/12/2012'
4131        self.browser.getControl("Create CSV file").click()
4132
4133        # When the job is finished and we reload the page...
4134        job_id = self.wait_for_export_job_completed()
4135        self.browser.open(dep1_path + '/exports')
4136        # ... the csv file can be downloaded ...
4137        self.browser.getLink("Download").click()
4138        self.assertEqual(self.browser.headers['content-type'],
4139            'text/csv; charset=UTF-8')
4140        self.assertTrue(
4141            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
4142            self.browser.headers['content-disposition'])
4143        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4144        job_id = self.app['datacenter'].running_exports[0][0]
4145        # ... and discarded
4146        self.browser.open(dep1_path + '/exports')
4147        self.browser.getControl("Discard").click()
4148        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4149        # Creation, downloading and discarding is logged
4150        logfile = os.path.join(
4151            self.app['datacenter'].storage, 'logs', 'datacenter.log')
4152        logcontent = open(logfile).read()
4153        self.assertTrue(
4154            'zope.mgr - students.browser.DepartmentExportJobContainerJobConfig '
4155            '- exported: students (2004, 100, ug_ft, None, dep1, None, '
4156            '13/12/2012, 14/12/2012, all, all, all, all, all), job_id=%s'
4157            % job_id in logcontent
4158            )
4159        self.assertTrue(
4160            'zope.mgr - students.browser.ExportJobContainerDownload '
4161            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
4162            % (job_id, job_id) in logcontent
4163            )
4164        self.assertTrue(
4165            'zope.mgr - students.browser.ExportJobContainerOverview '
4166            '- discarded: job_id=%s' % job_id in logcontent
4167            )
4168
4169    def test_certificate_export(self):
4170        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
4171        cert1_path = 'http://localhost/app/faculties/fac1/dep1/certificates/CERT1'
4172        self.browser.open(cert1_path)
4173        self.browser.getLink("Export student data").click()
4174        self.browser.getControl("Set export parameters").click()
4175        self.browser.getControl(name="exporter").value = ['students']
4176        self.browser.getControl(name="session").value = ['2004']
4177        self.browser.getControl(name="level").value = ['100']
4178        self.browser.getControl("Create CSV file").click()
4179
4180        # When the job is finished and we reload the page...
4181        job_id = self.wait_for_export_job_completed()
4182        self.browser.open(cert1_path + '/exports')
4183        # ... the csv file can be downloaded ...
4184        self.browser.getLink("Download").click()
4185        self.assertEqual(self.browser.headers['content-type'],
4186            'text/csv; charset=UTF-8')
4187        self.assertTrue(
4188            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
4189            self.browser.headers['content-disposition'])
4190        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4191        job_id = self.app['datacenter'].running_exports[0][0]
4192        # ... and discarded
4193        self.browser.open(cert1_path + '/exports')
4194        self.browser.getControl("Discard").click()
4195        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4196        # Creation, downloading and discarding is logged
4197        logfile = os.path.join(
4198            self.app['datacenter'].storage, 'logs', 'datacenter.log')
4199        logcontent = open(logfile).read()
4200        self.assertTrue(
4201            'zope.mgr - students.browser.CertificateExportJobContainerJobConfig '
4202            '- exported: students '
4203            '(2004, 100, None, None, None, CERT1, , , None, None, '
4204            'None, None, None), '
4205            'job_id=%s'
4206            % job_id in logcontent
4207            )
4208        self.assertTrue(
4209            'zope.mgr - students.browser.ExportJobContainerDownload '
4210            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
4211            % (job_id, job_id) in logcontent
4212            )
4213        self.assertTrue(
4214            'zope.mgr - students.browser.ExportJobContainerOverview '
4215            '- discarded: job_id=%s' % job_id in logcontent
4216            )
4217
4218    def deprecated_test_course_export_students(self):
4219        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
4220        course1_path = 'http://localhost/app/faculties/fac1/dep1/courses/COURSE1'
4221        self.browser.open(course1_path)
4222        self.browser.getLink("Export student data").click()
4223        self.browser.getControl("Set export parameters").click()
4224        self.browser.getControl(name="exporter").value = ['students']
4225        self.browser.getControl(name="session").value = ['2004']
4226        self.browser.getControl(name="level").value = ['100']
4227        self.browser.getControl("Create CSV file").click()
4228
4229        # When the job is finished and we reload the page...
4230        job_id = self.wait_for_export_job_completed()
4231        self.browser.open(course1_path + '/exports')
4232        # ... the csv file can be downloaded ...
4233        self.browser.getLink("Download").click()
4234        self.assertEqual(self.browser.headers['content-type'],
4235            'text/csv; charset=UTF-8')
4236        self.assertTrue(
4237            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
4238            self.browser.headers['content-disposition'])
4239        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4240        job_id = self.app['datacenter'].running_exports[0][0]
4241        # ... and discarded
4242        self.browser.open(course1_path + '/exports')
4243        self.browser.getControl("Discard").click()
4244        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4245        # Creation, downloading and discarding is logged
4246        logfile = os.path.join(
4247            self.app['datacenter'].storage, 'logs', 'datacenter.log')
4248        logcontent = open(logfile).read()
4249        self.assertTrue(
4250            'zope.mgr - students.browser.CourseExportJobContainerJobConfig '
4251            '- exported: students (2004, 100, COURSE1), job_id=%s'
4252            % job_id in logcontent
4253            )
4254        self.assertTrue(
4255            'zope.mgr - students.browser.ExportJobContainerDownload '
4256            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
4257            % (job_id, job_id) in logcontent
4258            )
4259        self.assertTrue(
4260            'zope.mgr - students.browser.ExportJobContainerOverview '
4261            '- discarded: job_id=%s' % job_id in logcontent
4262            )
4263
4264    def test_course_export_lecturer(self):
4265        # We add study level 100 to the student's studycourse
4266        studylevel = StudentStudyLevel()
4267        studylevel.level = 100
4268        studylevel.level_session = 2004
4269        IWorkflowState(self.student).setState('courses validated')
4270        self.student['studycourse'].addStudentStudyLevel(
4271            self.certificate,studylevel)
4272        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
4273        course1_path = 'http://localhost/app/faculties/fac1/dep1/courses/COURSE1'
4274        self.browser.open(course1_path)
4275        self.browser.getLink("Export student data").click()
4276        self.browser.getControl("Set export parameters").click()
4277        self.assertTrue(
4278            'Academic session not set. Please contact the administrator.'
4279            in self.browser.contents)
4280        self.app['configuration'].current_academic_session = 2004
4281        self.browser.getControl("Set export parameters").click()
4282        self.browser.getControl(name="exporter").value = ['lecturer']
4283        self.browser.getControl(name="session").value = ['2004']
4284        self.browser.getControl(name="level").value = ['100']
4285        self.browser.getControl("Create CSV file").click()
4286        # When the job is finished and we reload the page...
4287        job_id = self.wait_for_export_job_completed()
4288        self.browser.open(course1_path + '/exports')
4289        # ... the csv file can be downloaded ...
4290        self.browser.getLink("Download").click()
4291        self.assertEqual(self.browser.headers['content-type'],
4292            'text/csv; charset=UTF-8')
4293        self.assertTrue(
4294            'filename="WAeUP.Kofa_lecturer_%s.csv' % job_id in
4295            self.browser.headers['content-disposition'])
4296        # ... and contains the course ticket COURSE1
4297        self.assertEqual(self.browser.contents,
4298            'matric_number,student_id,display_fullname,level,code,'
4299            'level_session,score\r\n234,K1000000,Anna Tester,'
4300            '100,COURSE1,2004,\r\n')
4301        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4302        job_id = self.app['datacenter'].running_exports[0][0]
4303        # Thew job can be discarded
4304        self.browser.open(course1_path + '/exports')
4305        self.browser.getControl("Discard").click()
4306        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4307        # Creation, downloading and discarding is logged
4308        logfile = os.path.join(
4309            self.app['datacenter'].storage, 'logs', 'datacenter.log')
4310        logcontent = open(logfile).read()
4311        self.assertTrue(
4312            'zope.mgr - students.browser.CourseExportJobContainerJobConfig '
4313            '- exported: lecturer (2004, 100, COURSE1), job_id=%s'
4314            % job_id in logcontent
4315            )
4316        self.assertTrue(
4317            'zope.mgr - students.browser.ExportJobContainerDownload '
4318            '- downloaded: WAeUP.Kofa_lecturer_%s.csv, job_id=%s'
4319            % (job_id, job_id) in logcontent
4320            )
4321        self.assertTrue(
4322            'zope.mgr - students.browser.ExportJobContainerOverview '
4323            '- discarded: job_id=%s' % job_id in logcontent
4324            )
4325
4326    def test_export_departmet_officers(self):
4327        # Create department officer
4328        self.app['users'].addUser('mrdepartment', SECRET)
4329        self.app['users']['mrdepartment'].email = 'mrdepartment@foo.ng'
4330        self.app['users']['mrdepartment'].title = 'Carlo Pitter'
4331        # Assign local role
4332        department = self.app['faculties']['fac1']['dep1']
4333        prmlocal = IPrincipalRoleManager(department)
4334        prmlocal.assignRoleToPrincipal('waeup.local.DepartmentOfficer', 'mrdepartment')
4335        # Login as department officer
4336        self.browser.open(self.login_path)
4337        self.browser.getControl(name="form.login").value = 'mrdepartment'
4338        self.browser.getControl(name="form.password").value = SECRET
4339        self.browser.getControl("Login").click()
4340        self.assertMatches('...You logged in...', self.browser.contents)
4341        self.browser.open("http://localhost/app/faculties/fac1/dep1")
4342        self.browser.getLink("Export student data").click()
4343        self.browser.getControl("Set export parameters").click()
4344        # Only the sfpaymentsoverview exporter is available for department officers
4345        self.assertFalse('<option value="students">' in self.browser.contents)
4346        self.assertTrue(
4347            '<option value="sfpaymentsoverview">' in self.browser.contents)
4348        self.browser.getControl(name="exporter").value = ['sfpaymentsoverview']
4349        self.browser.getControl(name="session").value = ['2004']
4350        self.browser.getControl(name="level").value = ['100']
4351        self.browser.getControl("Create CSV file").click()
4352        self.assertTrue('Export started' in self.browser.contents)
4353        # Thew job can be discarded
4354        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4355        self.wait_for_export_job_completed()
4356        self.browser.open("http://localhost/app/faculties/fac1/dep1/exports")
4357        self.browser.getControl("Discard").click()
4358        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4359
4360    def test_export_bursary_officers(self):
4361        self.add_payment(self.student)
4362        # Create bursary officer
4363        self.app['users'].addUser('mrbursary', SECRET)
4364        self.app['users']['mrbursary'].email = 'mrbursary@foo.ng'
4365        self.app['users']['mrbursary'].title = 'Carlo Pitter'
4366        prmglobal = IPrincipalRoleManager(self.app)
4367        prmglobal.assignRoleToPrincipal('waeup.BursaryOfficer', 'mrbursary')
4368        # Login as bursary officer
4369        self.browser.open(self.login_path)
4370        self.browser.getControl(name="form.login").value = 'mrbursary'
4371        self.browser.getControl(name="form.password").value = SECRET
4372        self.browser.getControl("Login").click()
4373        self.assertMatches('...You logged in...', self.browser.contents)
4374        self.browser.getLink("Academics").click()
4375        self.browser.getLink("Export student data").click()
4376        self.browser.getControl("Set export parameters").click()
4377        # Only the bursary exporter is available for bursary officers
4378        # not only at facultiescontainer level ...
4379        self.assertFalse('<option value="students">' in self.browser.contents)
4380        self.assertTrue('<option value="bursary">' in self.browser.contents)
4381        self.browser.getControl(name="exporter").value = ['bursary']
4382        self.browser.getControl(name="session").value = ['2004']
4383        self.browser.getControl(name="level").value = ['100']
4384        self.browser.getControl("Create CSV file").click()
4385        self.assertTrue('Export started' in self.browser.contents)
4386        # ... but also at other levels
4387        self.browser.open('http://localhost/app/faculties/fac1/dep1')
4388        self.browser.getLink("Export student data").click()
4389        self.browser.getControl("Set export parameters").click()
4390        self.assertFalse('<option value="students">' in self.browser.contents)
4391        self.assertTrue('<option value="bursary">' in self.browser.contents)
4392        # Thew job can be downloaded
4393        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4394        job_id = self.wait_for_export_job_completed()
4395        self.browser.open('http://localhost/app/faculties/exports')
4396        self.browser.getLink("Download").click()
4397        self.assertEqual(self.browser.headers['content-type'],
4398            'text/csv; charset=UTF-8')
4399        self.assertTrue(
4400            'filename="WAeUP.Kofa_bursary_%s.csv' % job_id in
4401            self.browser.headers['content-disposition'])
4402        self.assertTrue(
4403            '666,12.12,2012-12-13 00:00:00#,schoolfee,[],1,my-id,p-item,100,%s,'
4404            'paid,2012-12-13 00:00:00#,12.12,r-code,,K1000000,234,123,Anna,,'
4405            'Tester,created,2004,2004,,fac1,dep1,CERT1'
4406            %(curr_year-6) in self.browser.contents)
4407        # ... and discarded
4408        self.browser.open('http://localhost/app/faculties/exports')
4409        self.browser.getControl("Discard").click()
4410        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4411        # At Academics level bursary officers can also enter student ids
4412        self.browser.getLink("Academics").click()
4413        self.browser.getLink("Export student data").click()
4414        self.browser.getControl("Enter student ids or matric numbers").click()
4415        self.assertFalse('<option value="students">' in self.browser.contents)
4416        self.browser.getControl(name="exporter").value = ['bursary']
4417        self.browser.getControl(name="students").value = 'K1000000'
4418        self.browser.getControl("Create CSV file").click()
4419        # When the job is finished and we reload the page...
4420        job_id = self.wait_for_export_job_completed()
4421        # ... the csv file can be downloaded ...
4422        self.browser.open('http://localhost/app/faculties/exports')
4423        self.browser.getLink("Download").click()
4424        self.assertEqual(self.browser.headers['content-type'],
4425            'text/csv; charset=UTF-8')
4426        self.assertTrue(
4427            'filename="WAeUP.Kofa_bursary_%s.csv' % job_id in
4428            self.browser.headers['content-disposition'])
4429        self.assertTrue(
4430            '666,12.12,2012-12-13 00:00:00#,schoolfee,[],1,my-id,p-item,100,%s,'
4431            'paid,2012-12-13 00:00:00#,12.12,r-code,,K1000000,234,123,Anna,,'
4432            'Tester,created,2004,2004,,fac1,dep1,CERT1'
4433            %(curr_year-6) in self.browser.contents)
4434
4435    def test_export_accommodation_officers(self):
4436        # Create bursary officer
4437        self.app['users'].addUser('mracco', SECRET)
4438        self.app['users']['mracco'].email = 'mracco@foo.ng'
4439        self.app['users']['mracco'].title = 'Carlo Pitter'
4440        prmglobal = IPrincipalRoleManager(self.app)
4441        prmglobal.assignRoleToPrincipal('waeup.AccommodationOfficer', 'mracco')
4442        # Login as bursary officer
4443        self.browser.open(self.login_path)
4444        self.browser.getControl(name="form.login").value = 'mracco'
4445        self.browser.getControl(name="form.password").value = SECRET
4446        self.browser.getControl("Login").click()
4447        self.assertMatches('...You logged in...', self.browser.contents)
4448        self.browser.getLink("Academics").click()
4449        self.browser.getLink("Export student data").click()
4450        self.browser.getControl("Set export parameters").click()
4451        # accommodationpayments and beds exporters are available
4452        # not only at facultiescontainer level ...
4453        self.assertFalse('<option value="students">' in self.browser.contents)
4454        self.assertTrue('<option value="accommodationpayments">'
4455            in self.browser.contents)
4456        self.assertTrue('<option value="bedtickets">' in self.browser.contents)
4457        self.browser.getControl(
4458            name="exporter").value = ['accommodationpayments']
4459        self.browser.getControl(name="session").value = ['2004']
4460        self.browser.getControl(name="level").value = ['100']
4461        self.browser.getControl("Create CSV file").click()
4462        self.assertTrue('Export started' in self.browser.contents)
4463        # ... but also at other levels
4464        self.browser.open('http://localhost/app/faculties/fac1/dep1')
4465        self.browser.getLink("Export student data").click()
4466        self.browser.getControl("Set export parameters").click()
4467        self.assertFalse('<option value="students">' in self.browser.contents)
4468        self.assertTrue('<option value="accommodationpayments">'
4469            in self.browser.contents)
4470        self.assertTrue('<option value="bedtickets">' in self.browser.contents)
4471        # Thew job can be discarded
4472        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
4473        self.wait_for_export_job_completed()
4474        self.browser.open('http://localhost/app/faculties/exports')
4475        self.browser.getControl("Discard").click()
4476        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
4477
4478
4479UPLOAD_CSV_TEMPLATE = (
4480    'matric_number,student_id,display_fullname,level,code,level_session,'
4481    'score\r\n'
4482    '234,K1000000,Anna Tester,100,COURSE1,2004,%s\r\n')
4483
4484class LecturerUITests(StudentsFullSetup):
4485    # Tests for UI actions when acting as lecturer.
4486
4487    def login_as_lecturer(self):
4488        self.app['users'].addUser('mrslecturer', SECRET)
4489        self.app['users']['mrslecturer'].email = 'mrslecturer@foo.ng'
4490        self.app['users']['mrslecturer'].title = u'Mercedes Benz'
4491        # Add course ticket
4492        self.studylevel = createObject(u'waeup.StudentStudyLevel')
4493        self.studylevel.level = 100
4494        self.studylevel.level_session = 2004
4495        self.student['studycourse'].addStudentStudyLevel(
4496            self.certificate, self.studylevel)
4497        # Assign local Lecturer role for a course.
4498        course = self.app['faculties']['fac1']['dep1'].courses['COURSE1']
4499        prmlocal = IPrincipalRoleManager(course)
4500        prmlocal.assignRoleToPrincipal('waeup.local.Lecturer', 'mrslecturer')
4501        notify(LocalRoleSetEvent(
4502            course, 'waeup.local.Lecturer', 'mrslecturer', granted=True))
4503        # Login as lecturer.
4504        self.browser.open(self.login_path)
4505        self.browser.getControl(name="form.login").value = 'mrslecturer'
4506        self.browser.getControl(
4507            name="form.password").value = SECRET
4508        self.browser.getControl("Login").click()
4509        # Store reused urls/paths
4510        self.course_url = (
4511            'http://localhost/app/faculties/fac1/dep1/courses/COURSE1')
4512        self.edit_scores_url = '%s/edit_scores' % self.course_url
4513        # Set standard parameters
4514        self.app['configuration'].current_academic_session = 2004
4515        self.app['faculties']['fac1']['dep1'].score_editing_disabled = False
4516        IWorkflowState(self.student).setState(VALIDATED)
4517
4518    @property
4519    def stud_log_path(self):
4520        return os.path.join(
4521            self.app['datacenter'].storage, 'logs', 'students.log')
4522
4523    def test_lecturer_lands_on_landing_page(self):
4524        # lecturers can login and will be led to landing page.
4525        self.login_as_lecturer()
4526        self.assertMatches('...You logged in...', self.browser.contents)
4527        self.assertEqual(self.browser.url, URL_LECTURER_LANDING)
4528        self.assertTrue(
4529            "<span>Unnamed Course (COURSE1)</span>"
4530            in self.browser.contents)
4531
4532    def test_lecturer_department_role(self):
4533        # lecturers can login and will be led to landing page also if
4534        # role is assigned at department level.
4535        self.login_as_lecturer()
4536        # we remove the role granted above
4537        course = self.app['faculties']['fac1']['dep1'].courses['COURSE1']
4538        prmlocal = IPrincipalRoleManager(course)
4539        prmlocal.removeRoleFromPrincipal('waeup.local.Lecturer', 'mrslecturer')
4540        notify(LocalRoleSetEvent(
4541            course, 'waeup.local.Lecturer', 'mrslecturer', granted=False))
4542        self.browser.open(URL_LECTURER_LANDING)
4543        # no course appears
4544        self.assertFalse(
4545            "<span>Unnamed Course (COURSE1)</span>"
4546            in self.browser.contents)
4547        # we assign lecturer at department level
4548        dep = self.app['faculties']['fac1']['dep1']
4549        prmlocal = IPrincipalRoleManager(dep)
4550        prmlocal.assignRoleToPrincipal('waeup.local.Lecturer', 'mrslecturer')
4551        notify(LocalRoleSetEvent(
4552            dep, 'waeup.local.Lecturer', 'mrslecturer', granted=True))
4553        self.browser.open(URL_LECTURER_LANDING)
4554        # course appears again
4555        self.assertTrue(
4556            "<span>Unnamed Course (COURSE1)</span>"
4557            in self.browser.contents)
4558
4559    def test_my_roles_link_works(self):
4560        # lecturers can see their roles
4561        self.login_as_lecturer()
4562        self.browser.getLink("My Roles").click()
4563        self.assertTrue(
4564            "<div>Academics Officer (view only)</div>"
4565            in self.browser.contents)
4566        self.assertTrue(
4567            '<a href="%s">' % self.course_url in self.browser.contents)
4568
4569    def test_my_roles_page_contains_backlink(self):
4570        # we can get back from 'My Roles' view to landing page
4571        self.login_as_lecturer()
4572        self.browser.getLink("My Roles").click()
4573        self.browser.getLink("My Courses").click()
4574        self.assertEqual(self.browser.url, URL_LECTURER_LANDING)
4575
4576    def test_lecturers_can_reach_their_courses(self):
4577        # lecturers get links to their courses on the landing page
4578        self.login_as_lecturer()
4579        self.browser.getLink("COURSE1").click()
4580        self.assertEqual(self.browser.url, self.course_url)
4581
4582    def test_lecturers_student_access_is_restricted(self):
4583        # lecturers are not able to change other student data
4584        self.login_as_lecturer()
4585        # Lecturers can neither filter students ...
4586        self.assertRaises(
4587            Unauthorized, self.browser.open, '%s/students' % self.course_url)
4588        # ... nor access the student ...
4589        self.assertRaises(
4590            Unauthorized, self.browser.open, self.student_path)
4591        # ... nor the respective course ticket since editing course
4592        # tickets by lecturers is not feasible.
4593        self.assertTrue('COURSE1' in self.student['studycourse']['100'].keys())
4594        course_ticket_path = self.student_path + '/studycourse/100/COURSE1'
4595        self.assertRaises(
4596            Unauthorized, self.browser.open, course_ticket_path)
4597
4598    def test_score_editing_requires_department_permit(self):
4599        # we get a warning if we try to update score while we are not allowed
4600        self.login_as_lecturer()
4601        self.app['faculties']['fac1']['dep1'].score_editing_disabled = True
4602        self.browser.open(self.course_url)
4603        self.browser.getLink("Update session 2004/2005 scores").click()
4604        self.assertTrue('Score editing disabled' in self.browser.contents)
4605        self.app['faculties']['fac1']['dep1'].score_editing_disabled = False
4606        self.browser.open(self.course_url)
4607        self.browser.getLink("Update session 2004/2005 scores").click()
4608        self.assertFalse('Score editing disabled' in self.browser.contents)
4609
4610    def test_score_editing_requires_validated_students(self):
4611        # we can edit only scores of students whose courses have been
4612        # validated.
4613        self.login_as_lecturer()
4614        # set invalid student state
4615        IWorkflowState(self.student).setState(CREATED)
4616        self.browser.open(self.edit_scores_url)
4617        self.assertRaises(
4618            LookupError, self.browser.getControl, name="scores")
4619        # set valid student state
4620        IWorkflowState(self.student).setState(VALIDATED)
4621        self.browser.open(self.edit_scores_url)
4622        self.assertTrue(
4623            self.browser.getControl(name="scores:list") is not None)
4624
4625    def test_score_editing_offers_only_current_scores(self):
4626        # only scores from current academic session can be edited
4627        self.login_as_lecturer()
4628        IWorkflowState(self.student).setState('courses validated')
4629        # with no academic session set
4630        self.app['configuration'].current_academic_session = None
4631        self.browser.open(self.edit_scores_url)
4632        self.assertRaises(
4633            LookupError, self.browser.getControl, name="scores")
4634        # with wrong academic session set
4635        self.app['configuration'].current_academic_session = 1999
4636        self.browser.open(self.edit_scores_url)
4637        self.assertRaises(
4638            LookupError, self.browser.getControl, name="scores")
4639        # with right academic session set
4640        self.app['configuration'].current_academic_session = 2004
4641        self.browser.reload()
4642        self.assertTrue(
4643            self.browser.getControl(name="scores:list") is not None)
4644        # if level_session of studycourse changes, catalog has been updated
4645        # and student disappears
4646        self.studylevel.level_session = 2005
4647        self.browser.reload()
4648        self.assertRaises(
4649            LookupError, self.browser.getControl, name="scores")
4650
4651    def test_score_editing_can_change_scores(self):
4652        # we can really change scores via edit_scores view
4653        self.login_as_lecturer()
4654        self.assertEqual(
4655            self.student['studycourse']['100']['COURSE1'].score, None)
4656        self.browser.open(self.edit_scores_url)
4657        self.browser.getControl(name="scores:list", index=0).value = '55'
4658        self.browser.getControl("Update scores").click()
4659        # the new value is stored in data
4660        self.assertEqual(
4661            self.student['studycourse']['100']['COURSE1'].score, 55)
4662        # the new value is displayed on page/prefilled in form
4663        self.assertEqual(
4664            self.browser.getControl(name="scores:list", index=0).value, '55')
4665        # The change has been logged
4666        with open(self.stud_log_path, 'r') as fd:
4667            self.assertTrue(
4668                'mrslecturer - students.browser.EditScoresPage - '
4669                'K1000000 100/COURSE1 score updated (55)' in fd.read())
4670
4671    def test_scores_editing_scores_must_be_integers(self):
4672        # Non-integer scores won't be accepted.
4673        self.login_as_lecturer()
4674        self.browser.open(self.edit_scores_url)
4675        self.browser.getControl(name="scores:list", index=0).value = 'abc'
4676        self.browser.getControl("Update scores").click()
4677        self.assertTrue(
4678            'Error: Score(s) of following students have not been updated '
4679            '(only integers are allowed): Anna Tester.'
4680            in self.browser.contents)
4681
4682    def test_scores_editing_allows_score_removal(self):
4683        # we can remove scores, once they were set
4684        self.login_as_lecturer()
4685        # without a prior value, we cannot remove
4686        self.student['studycourse']['100']['COURSE1'].score = None
4687        self.browser.open(self.edit_scores_url)
4688        self.browser.getControl(name="scores:list", index=0).value = ''
4689        self.browser.getControl("Update scores").click()
4690        logcontent = open(self.stud_log_path, 'r').read()
4691        self.assertFalse('COURSE1 score updated (None)' in logcontent)
4692        # now retry with some value set
4693        self.student['studycourse']['100']['COURSE1'].score = 55
4694        self.browser.getControl(name="scores:list", index=0).value = ''
4695        self.browser.getControl("Update scores").click()
4696        logcontent = open(self.stud_log_path, 'r').read()
4697        self.assertTrue('COURSE1 score updated (None)' in logcontent)
4698
4699    def test_lecturer_can_validate_courses(self):
4700        # the form is locked after validation
4701        self.login_as_lecturer()
4702        self.student['studycourse']['100']['COURSE1'].score = None
4703        self.browser.open(self.edit_scores_url)
4704        self.browser.getControl(name="scores:list", index=0).value = ''
4705        self.browser.getControl("Update scores").click()
4706        self.browser.getControl("Validate").click()
4707        self.assertTrue(
4708            'No score has been entered.' in self.browser.contents)
4709        self.browser.open(self.edit_scores_url)
4710        self.browser.getControl(name="scores:list", index=0).value = '66'
4711        self.browser.getControl("Update scores").click()
4712        self.browser.getControl("Validate").click()
4713        self.assertTrue(
4714            'You successfully validated the course results'
4715            in self.browser.contents)
4716        self.assertEqual(self.course.results_validation_session, 2004)
4717        self.assertEqual(self.course.results_validated_by, 'Mercedes Benz')
4718        self.assertEqual(self.browser.url, self.course_url)
4719        # Lecturer can't open edit_scores again
4720        self.browser.getLink("Update session 2004/2005 scores").click()
4721        self.assertEqual(self.browser.url, self.course_url)
4722        self.assertTrue(
4723            'Course results have already been validated'
4724            ' and can no longer be changed.'
4725            in self.browser.contents)
4726        # Also DownloadScoresView is blocked
4727        self.browser.open(self.browser.url + '/download_scores')
4728        self.assertEqual(self.browser.url, self.course_url)
4729        self.assertTrue(
4730            'Course results have already been validated'
4731            ' and can no longer be changed.'
4732            in self.browser.contents)
4733        # Students Manager can open page ...
4734        prmlocal = IPrincipalRoleManager(self.course)
4735        prmlocal.assignRoleToPrincipal(
4736            'waeup.local.LocalStudentsManager', 'mrslecturer')
4737        self.browser.getLink("Update session 2004/2005 scores").click()
4738        self.assertEqual(self.browser.url, self.edit_scores_url)
4739        self.browser.getLink("Download csv file").click()
4740        self.assertEqual(self.browser.headers['Status'], '200 Ok')
4741        self.assertEqual(self.browser.headers['Content-Type'],
4742                         'text/csv; charset=UTF-8')
4743        # ... but can't validate courses a second time
4744        self.browser.open(self.edit_scores_url)
4745        self.browser.getControl("Validate").click()
4746        self.assertTrue(
4747            'Course results have already been validated.'
4748            in self.browser.contents)
4749
4750    def test_lecturers_can_download_course_tickets(self):
4751        # A course ticket slip can be downloaded
4752        self.course.title = (u'Lorem ipsum     dolor sit amet, consectetur     adipisici, '
4753                             u'sed         eiusmod tempor    incidunt ut  labore et dolore')
4754        self.login_as_lecturer()
4755        pdf_url = '%s/coursetickets.pdf' % self.course_url
4756        self.browser.open(pdf_url)
4757        self.assertEqual(self.browser.headers['Status'], '200 Ok')
4758        self.assertEqual(
4759            self.browser.headers['Content-Type'], 'application/pdf')
4760        path = os.path.join(samples_dir(), 'coursetickets.pdf')
4761        open(path, 'wb').write(self.browser.contents)
4762        print "Sample PDF coursetickets.pdf written to %s" % path
4763
4764    def test_lecturers_can_download_attendance_sheet(self):
4765        # A course ticket slip can be downloaded
4766        self.course.title = (u'Lorem ipsum     dolor sit amet, consectetur     adipisici, '
4767                             u'sed         eiusmod tempor    incidunt ut  labore et dolore')
4768        self.student.firstname = u'Emmanuella Woyengifigha Mercy Onosemudiana'
4769        self.student.lastname = u'OYAKEMIEGBEGHA'
4770        self.student.matric_number = u'hdk7gd62i872z27zt27ge'
4771        self.login_as_lecturer()
4772        pdf_url = '%s/attendance.pdf' % self.course_url
4773        self.browser.open(pdf_url)
4774        self.assertEqual(self.browser.headers['Status'], '200 Ok')
4775        self.assertEqual(
4776            self.browser.headers['Content-Type'], 'application/pdf')
4777        path = os.path.join(samples_dir(), 'attendance.pdf')
4778        open(path, 'wb').write(self.browser.contents)
4779        print "Sample PDF attendance.pdf written to %s" % path
4780
4781
4782    def test_lecturers_can_download_scores_as_csv(self):
4783        # Lecturers can download course scores as CSV.
4784        self.login_as_lecturer()
4785        self.browser.open(self.edit_scores_url)
4786        self.browser.getLink("Download csv file").click()
4787        self.assertEqual(self.browser.headers['Status'], '200 Ok')
4788        self.assertEqual(self.browser.headers['Content-Type'],
4789                         'text/csv; charset=UTF-8')
4790        self.assertEqual(self.browser.contents, 'matric_number,student_id,'
4791            'display_fullname,level,code,level_session,score\r\n234,'
4792            'K1000000,Anna Tester,100,COURSE1,2004,\r\n')
4793
4794    def test_scores_csv_upload_available(self):
4795        # lecturers can upload a CSV file to set values.
4796        self.login_as_lecturer()
4797        # set value to change from
4798        self.student['studycourse']['100']['COURSE1'].score = 55
4799        self.browser.open(self.edit_scores_url)
4800        upload_ctrl = self.browser.getControl(name='uploadfile:file')
4801        upload_file = StringIO(UPLOAD_CSV_TEMPLATE % '65')
4802        upload_ctrl.add_file(upload_file, 'text/csv', 'myscores.csv')
4803        self.browser.getControl("Update editable scores from").click()
4804        # value changed
4805        self.assertEqual(
4806            self.student['studycourse']['100']['COURSE1'].score, 65)
4807
4808    def test_scores_csv_upload_ignored(self):
4809        # for many type of file contents we simply ignore uploaded data
4810        self.login_as_lecturer()
4811        self.student['studycourse']['100']['COURSE1'].score = 55
4812        self.browser.open(self.edit_scores_url)
4813        for content, mimetype, name in (
4814                # empty file
4815                ('', 'text/foo', 'my.foo'),
4816                # plain ASCII text, w/o comma
4817                ('abcdef' * 200, 'text/plain', 'my.txt'),
4818                # plain UTF-8 text, with umlauts
4819                ('umlauts: äöü', 'text/plain', 'my.txt'),
4820                # csv file with only a header row
4821                ('student_id,score', 'text/csv', 'my.csv'),
4822                # csv with student_id column missing
4823                ('foo,score\r\nbar,66\r\n', 'text/csv', 'my.csv'),
4824                # csv with score column missing
4825                ('student_id,foo\r\nK1000000,bar\r\n', 'text/csv', 'my.csv'),
4826                # csv with non number as score value
4827                (UPLOAD_CSV_TEMPLATE % 'not-a-number', 'text/csv', 'my.csv'),
4828                ):
4829            upload_ctrl = self.browser.getControl(name='uploadfile:file')
4830            upload_ctrl.add_file(StringIO(content), mimetype, name)
4831            self.browser.getControl("Update scores").click()
4832            self.assertEqual(
4833                self.student['studycourse']['100']['COURSE1'].score, 55)
4834            self.assertFalse(
4835                'Uploaded file contains illegal data' in self.browser.contents)
4836
4837    def test_scores_csv_upload_warn_illegal_chars(self):
4838        # for some types of files we issue a warning if upload data
4839        # contains illegal chars (and ignore the data)
4840        self.login_as_lecturer()
4841        self.student['studycourse']['100']['COURSE1'].score = 55
4842        self.browser.open(self.edit_scores_url)
4843        for content, mimetype, name in (
4844                # plain ASCII text, commas, control chars
4845                ('abv,qwe\n\r\r\t\b\n' * 20, 'text/plain', 'my.txt'),
4846                # image data (like a JPEG image)
4847                (open(SAMPLE_IMAGE, 'rb').read(), 'image/jpg', 'my.jpg'),
4848                ):
4849            upload_ctrl = self.browser.getControl(name='uploadfile:file')
4850            upload_ctrl.add_file(StringIO(content), mimetype, name)
4851            self.browser.getControl("Update editable scores").click()
4852            self.assertEqual(
4853                self.student['studycourse']['100']['COURSE1'].score, 55)
4854            self.assertTrue(
4855                'Uploaded file contains illegal data' in self.browser.contents)
4856
4857class ParentsUITests(StudentsFullSetup):
4858    # Tests for UI actions when acting as parents.
4859
4860    def test_request_ppw(self):
4861        self.app['students'][self.student_id].parents_email = 'par@yy.zz'
4862        self.browser.open('http://localhost/app/requestppw')
4863        self.browser.getControl(name="form.lastname").value = 'tESTer'
4864        self.browser.getControl(name="form.number").value = '123'
4865        self.browser.getControl(name="form.email").value = 'par@yy.zz'
4866        self.browser.getControl("Send temporary login credentials").click()
4867        self.assertTrue('Your password request was successful.'
4868            in self.browser.contents)
4869        logfile = os.path.join(
4870            self.app['datacenter'].storage, 'logs', 'main.log')
4871        logcontent = open(logfile).read()
4872        self.assertTrue('zope.anybody - students.browser.RequestParentsPasswordPage - '
4873                        '123 (K1000000) - par@yy.zz' in logcontent)
4874        return
4875
4876    def test_login_as_parents(self):
4877        # Student login still works after all the changes made
4878        self.browser.open(self.login_path)
4879        self.browser.getControl(name="form.login").value = self.student_id
4880        self.browser.getControl(name="form.password").value = 'spwd'
4881        self.browser.getControl("Login").click()
4882        self.assertTrue('You logged in' in self.browser.contents)
4883        self.browser.open(self.edit_personal_path)
4884        self.browser.getLink("Logout").click()
4885        self.assertTrue('You have been logged out' in self.browser.contents)
4886        # We set parents password
4887        self.app['students'][self.student_id].setParentsPassword('ppwd')
4888        self.browser.open(self.login_path)
4889        # Student can't login with original password
4890        self.browser.getControl(name="form.login").value = self.student_id
4891        self.browser.getControl(name="form.password").value = 'spwd'
4892        self.browser.getControl("Login").click()
4893        self.assertEqual(self.browser.url, self.login_path)
4894        self.assertTrue('Your account has been temporarily deactivated '
4895            'because your parents have logged in.' in self.browser.contents)
4896        # Parents can login with their password
4897        self.browser.open(self.login_path)
4898        self.browser.getControl(name="form.login").value = self.student_id
4899        self.browser.getControl(name="form.password").value = 'ppwd'
4900        self.browser.getControl("Login").click()
4901        self.assertTrue(
4902            'You logged in.' in self.browser.contents)
4903        self.assertTrue(
4904            '<a href="http://localhost/app/students/K1000000">Base Data</a>'
4905            in self.browser.contents)
4906        # They do not see all links ...
4907        self.assertFalse(
4908            '<a href="http://localhost/app/students/K1000000/history">History</a>'
4909            in self.browser.contents)
4910        # ... and can't change anything
4911        self.assertRaises(
4912            Unauthorized, self.browser.open, self.edit_personal_path)
4913        # If the password has expired, parents are logged out and the
4914        # student can login again with the original password
4915        delta = timedelta(minutes=11)
4916        self.app['students'][self.student_id].parents_password[
4917            'timestamp'] = datetime.utcnow() - delta
4918        self.app['students'][self.student_id]._p_changed = True
4919        self.assertRaises(
4920            Unauthorized, self.browser.open, self.student_path)
4921        # Parents login is written to log file
4922        logfile = os.path.join(
4923            self.app['datacenter'].storage, 'logs', 'students.log')
4924        logcontent = open(logfile).read()
4925        self.assertTrue(
4926            'K1000000 - browser.pages.LoginPage - K1000000 - Parents logged in'
4927            in logcontent)
Note: See TracBrowser for help on using the repository browser.