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

Last change on this file since 10613 was 10598, checked in by Henrik Bettermann, 11 years ago

Determine cumulative sessional gpa and other cumulative parameters needed for level reports.

  • Property svn:keywords set to Id
File size: 173.9 KB
Line 
1## $Id: test_browser.py 10598 2013-09-10 11:39:24Z henrik $
2##
3## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
4## This program is free software; you can redistribute it and/or modify
5## it under the terms of the GNU General Public License as published by
6## the Free Software Foundation; either version 2 of the License, or
7## (at your option) any later version.
8##
9## This program is distributed in the hope that it will be useful,
10## but WITHOUT ANY WARRANTY; without even the implied warranty of
11## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12## GNU General Public License for more details.
13##
14## You should have received a copy of the GNU General Public License
15## along with this program; if not, write to the Free Software
16## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17##
18"""
19Test the student-related UI components.
20"""
21import shutil
22import tempfile
23import pytz
24import base64
25from datetime import datetime, timedelta, date
26from StringIO import StringIO
27import os
28import grok
29from zc.async.testing import wait_for_result
30from zope.event import notify
31from zope.component import createObject, queryUtility, getUtility
32from zope.component.hooks import setSite, clearSite
33from zope.catalog.interfaces import ICatalog
34from zope.security.interfaces import Unauthorized
35from zope.securitypolicy.interfaces import IPrincipalRoleManager
36from zope.testbrowser.testing import Browser
37from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
38from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
39from waeup.kofa.app import University
40from waeup.kofa.payments.interfaces import IPayer
41from waeup.kofa.students.interfaces import IStudentsUtils
42from waeup.kofa.students.student import Student
43from waeup.kofa.students.studylevel import StudentStudyLevel
44from waeup.kofa.university.faculty import Faculty
45from waeup.kofa.university.department import Department
46from waeup.kofa.interfaces import IUserAccount, IJobManager
47from waeup.kofa.authentication import LocalRoleSetEvent
48from waeup.kofa.hostels.hostel import Hostel, Bed, NOT_OCCUPIED
49from waeup.kofa.tests.test_async import FunctionalAsyncTestCase
50from waeup.kofa.browser.tests.test_pdf import samples_dir
51
52PH_LEN = 15911  # Length of placeholder file
53
54SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
55SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp')
56
57def lookup_submit_value(name, value, browser):
58    """Find a button with a certain value."""
59    for num in range(0, 100):
60        try:
61            button = browser.getControl(name=name, index=num)
62            if button.value.endswith(value):
63                return button
64        except IndexError:
65            break
66    return None
67
68class StudentsFullSetup(FunctionalTestCase):
69    # A test case that only contains a setup and teardown
70    #
71    # Complete setup for students handlings is rather complex and
72    # requires lots of things created before we can start. This is a
73    # setup that does all this, creates a university, creates PINs,
74    # etc.  so that we do not have to bother with that in different
75    # test cases.
76
77    layer = FunctionalLayer
78
79    def setUp(self):
80        super(StudentsFullSetup, self).setUp()
81
82        # Setup a sample site for each test
83        app = University()
84        self.dc_root = tempfile.mkdtemp()
85        app['datacenter'].setStoragePath(self.dc_root)
86
87        # Prepopulate the ZODB...
88        self.getRootFolder()['app'] = app
89        # we add the site immediately after creation to the
90        # ZODB. Catalogs and other local utilities are not setup
91        # before that step.
92        self.app = self.getRootFolder()['app']
93        # Set site here. Some of the following setup code might need
94        # to access grok.getSite() and should get our new app then
95        setSite(app)
96
97        # Add student with subobjects
98        student = createObject('waeup.Student')
99        student.firstname = u'Anna'
100        student.lastname = u'Tester'
101        student.reg_number = u'123'
102        student.matric_number = u'234'
103        student.sex = u'm'
104        student.email = 'aa@aa.ng'
105        student.phone = u'1234'
106        self.app['students'].addStudent(student)
107        self.student_id = student.student_id
108        self.student = self.app['students'][self.student_id]
109
110        # Set password
111        IUserAccount(
112            self.app['students'][self.student_id]).setPassword('spwd')
113
114        self.login_path = 'http://localhost/app/login'
115        self.container_path = 'http://localhost/app/students'
116        self.manage_container_path = self.container_path + '/@@manage'
117        self.add_student_path = self.container_path + '/addstudent'
118        self.student_path = self.container_path + '/' + self.student_id
119        self.manage_student_path = self.student_path + '/manage_base'
120        self.trigtrans_path = self.student_path + '/trigtrans'
121        self.clearance_path = self.student_path + '/view_clearance'
122        self.personal_path = self.student_path + '/view_personal'
123        self.edit_clearance_path = self.student_path + '/cedit'
124        self.manage_clearance_path = self.student_path + '/manage_clearance'
125        self.edit_personal_path = self.student_path + '/edit_personal'
126        self.manage_personal_path = self.student_path + '/manage_personal'
127        self.studycourse_path = self.student_path + '/studycourse'
128        self.payments_path = self.student_path + '/payments'
129        self.acco_path = self.student_path + '/accommodation'
130        self.history_path = self.student_path + '/history'
131
132        # Create 5 access codes with prefix'PWD'
133        pin_container = self.app['accesscodes']
134        pin_container.createBatch(
135            datetime.utcnow(), 'some_userid', 'PWD', 9.99, 5)
136        pins = pin_container['PWD-1'].values()
137        self.pwdpins = [x.representation for x in pins]
138        self.existing_pwdpin = self.pwdpins[0]
139        parts = self.existing_pwdpin.split('-')[1:]
140        self.existing_pwdseries, self.existing_pwdnumber = parts
141        # Create 5 access codes with prefix 'CLR'
142        pin_container.createBatch(
143            datetime.now(), 'some_userid', 'CLR', 9.99, 5)
144        pins = pin_container['CLR-1'].values()
145        pins[0].owner = u'Hans Wurst'
146        self.existing_clrac = pins[0]
147        self.existing_clrpin = pins[0].representation
148        parts = self.existing_clrpin.split('-')[1:]
149        self.existing_clrseries, self.existing_clrnumber = parts
150        # Create 2 access codes with prefix 'HOS'
151        pin_container.createBatch(
152            datetime.now(), 'some_userid', 'HOS', 9.99, 2)
153        pins = pin_container['HOS-1'].values()
154        self.existing_hosac = pins[0]
155        self.existing_hospin = pins[0].representation
156        parts = self.existing_hospin.split('-')[1:]
157        self.existing_hosseries, self.existing_hosnumber = parts
158
159        # Populate university
160        self.certificate = createObject('waeup.Certificate')
161        self.certificate.code = u'CERT1'
162        self.certificate.application_category = 'basic'
163        self.certificate.study_mode = 'ug_ft'
164        self.certificate.start_level = 100
165        self.certificate.end_level = 500
166        self.certificate.school_fee_1 = 40000.0
167        self.certificate.school_fee_2 = 20000.0
168        self.app['faculties']['fac1'] = Faculty(code=u'fac1')
169        self.app['faculties']['fac1']['dep1'] = Department(code=u'dep1')
170        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
171            self.certificate)
172        self.course = createObject('waeup.Course')
173        self.course.code = 'COURSE1'
174        self.course.semester = 1
175        self.course.credits = 10
176        self.course.passmark = 40
177        self.app['faculties']['fac1']['dep1'].courses.addCourse(
178            self.course)
179        self.app['faculties']['fac1']['dep1'].certificates['CERT1'].addCertCourse(
180            self.course, level=100)
181
182        # Configure university and hostels
183        self.app['hostels'].accommodation_states = ['admitted']
184        self.app['hostels'].accommodation_session = 2004
185        delta = timedelta(days=10)
186        self.app['hostels'].startdate = datetime.now(pytz.utc) - delta
187        self.app['hostels'].enddate = datetime.now(pytz.utc) + delta
188        self.app['configuration'].carry_over = True
189        configuration = createObject('waeup.SessionConfiguration')
190        configuration.academic_session = 2004
191        configuration.clearance_fee = 3456.0
192        configuration.transcript_fee = 4567.0
193        configuration.booking_fee = 123.4
194        configuration.maint_fee = 987.0
195        self.app['configuration'].addSessionConfiguration(configuration)
196
197        # Create a hostel with two beds
198        hostel = Hostel()
199        hostel.hostel_id = u'hall-1'
200        hostel.hostel_name = u'Hall 1'
201        self.app['hostels'].addHostel(hostel)
202        bed = Bed()
203        bed.bed_id = u'hall-1_A_101_A'
204        bed.bed_number = 1
205        bed.owner = NOT_OCCUPIED
206        bed.bed_type = u'regular_male_fr'
207        self.app['hostels'][hostel.hostel_id].addBed(bed)
208        bed = Bed()
209        bed.bed_id = u'hall-1_A_101_B'
210        bed.bed_number = 2
211        bed.owner = NOT_OCCUPIED
212        bed.bed_type = u'regular_female_fr'
213        self.app['hostels'][hostel.hostel_id].addBed(bed)
214
215        # Set study course attributes of test student
216        self.student['studycourse'].certificate = self.certificate
217        self.student['studycourse'].current_session = 2004
218        self.student['studycourse'].entry_session = 2004
219        self.student['studycourse'].current_verdict = 'A'
220        self.student['studycourse'].current_level = 100
221        # Update the catalog
222        notify(grok.ObjectModifiedEvent(self.student))
223
224        # Put the prepopulated site into test ZODB and prepare test
225        # browser
226        self.browser = Browser()
227        self.browser.handleErrors = False
228
229    def tearDown(self):
230        super(StudentsFullSetup, self).tearDown()
231        clearSite()
232        shutil.rmtree(self.dc_root)
233
234
235
236class StudentsContainerUITests(StudentsFullSetup):
237    # Tests for StudentsContainer class views and pages
238
239    layer = FunctionalLayer
240
241    def test_anonymous_access(self):
242        # Anonymous users can't access students containers
243        self.assertRaises(
244            Unauthorized, self.browser.open, self.container_path)
245        self.assertRaises(
246            Unauthorized, self.browser.open, self.manage_container_path)
247        return
248
249    def test_manage_access(self):
250        # Managers can access the view page of students
251        # containers and can perform actions
252        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
253        self.browser.open(self.container_path)
254        self.assertEqual(self.browser.headers['Status'], '200 Ok')
255        self.assertEqual(self.browser.url, self.container_path)
256        self.browser.getLink("Manage student section").click()
257        self.assertEqual(self.browser.headers['Status'], '200 Ok')
258        self.assertEqual(self.browser.url, self.manage_container_path)
259        return
260
261    def test_add_search_delete_students(self):
262        # Managers can add search and remove students
263        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
264        self.browser.open(self.manage_container_path)
265        self.browser.getLink("Add student").click()
266        self.assertEqual(self.browser.headers['Status'], '200 Ok')
267        self.assertEqual(self.browser.url, self.add_student_path)
268        self.browser.getControl(name="form.firstname").value = 'Bob'
269        self.browser.getControl(name="form.lastname").value = 'Tester'
270        self.browser.getControl(name="form.reg_number").value = '123'
271        self.browser.getControl("Create student record").click()
272        self.assertTrue('Registration number exists already'
273            in self.browser.contents)
274        self.browser.getControl(name="form.reg_number").value = '1234'
275        self.browser.getControl("Create student record").click()
276        self.assertTrue('Student record created' in self.browser.contents)
277
278        # Registration and matric numbers must be unique
279        self.browser.getLink("Manage").click()
280        self.browser.getControl(name="form.reg_number").value = '123'
281        self.browser.getControl("Save").click()
282        self.assertMatches('...Registration number exists...',
283                           self.browser.contents)
284        self.browser.getControl(name="form.reg_number").value = '789'
285        self.browser.getControl(name="form.matric_number").value = '234'
286        self.browser.getControl("Save").click()
287        self.assertMatches('...Matriculation number exists...',
288                           self.browser.contents)
289
290        # We can find a student with a certain student_id
291        self.browser.open(self.container_path)
292        self.browser.getControl("Search").click()
293        self.assertTrue('Empty search string' in self.browser.contents)
294        self.browser.getControl(name="searchtype").value = ['student_id']
295        self.browser.getControl(name="searchterm").value = self.student_id
296        self.browser.getControl("Search").click()
297        self.assertTrue('Anna Tester' in self.browser.contents)
298
299        # We can find a student in a certain session
300        self.browser.open(self.container_path)
301        self.browser.getControl(name="searchtype").value = ['current_session']
302        self.browser.getControl(name="searchterm").value = '2004'
303        self.browser.getControl("Search").click()
304        self.assertTrue('Anna Tester' in self.browser.contents)
305        # Session fileds require integer values
306        self.browser.open(self.container_path)
307        self.browser.getControl(name="searchtype").value = ['current_session']
308        self.browser.getControl(name="searchterm").value = '2004/2005'
309        self.browser.getControl("Search").click()
310        self.assertTrue('Only year dates allowed' in self.browser.contents)
311        self.browser.open(self.manage_container_path)
312        self.browser.getControl(name="searchtype").value = ['current_session']
313        self.browser.getControl(name="searchterm").value = '2004/2005'
314        self.browser.getControl("Search").click()
315        self.assertTrue('Only year dates allowed' in self.browser.contents)
316
317        # We can find a student in a certain study_mode
318        self.browser.open(self.container_path)
319        self.browser.getControl(name="searchtype").value = ['current_mode']
320        self.browser.getControl(name="searchterm").value = 'ug_ft'
321        self.browser.getControl("Search").click()
322        self.assertTrue('Anna Tester' in self.browser.contents)
323
324        # We can find a student in a certain department
325        self.browser.open(self.container_path)
326        self.browser.getControl(name="searchtype").value = ['depcode']
327        self.browser.getControl(name="searchterm").value = 'dep1'
328        self.browser.getControl("Search").click()
329        self.assertTrue('Anna Tester' in self.browser.contents)
330
331        # We can find a student by searching for all kind of name parts
332        self.browser.open(self.manage_container_path)
333        self.browser.getControl("Search").click()
334        self.assertTrue('Empty search string' in self.browser.contents)
335        self.browser.getControl(name="searchtype").value = ['fullname']
336        self.browser.getControl(name="searchterm").value = 'Anna Tester'
337        self.browser.getControl("Search").click()
338        self.assertTrue('Anna Tester' in self.browser.contents)
339        self.browser.open(self.manage_container_path)
340        self.browser.getControl(name="searchtype").value = ['fullname']
341        self.browser.getControl(name="searchterm").value = 'Anna'
342        self.browser.getControl("Search").click()
343        self.assertTrue('Anna Tester' in self.browser.contents)
344        self.browser.open(self.manage_container_path)
345        self.browser.getControl(name="searchtype").value = ['fullname']
346        self.browser.getControl(name="searchterm").value = 'Tester'
347        self.browser.getControl("Search").click()
348        self.assertTrue('Anna Tester' in self.browser.contents)
349        self.browser.open(self.manage_container_path)
350        self.browser.getControl(name="searchtype").value = ['fullname']
351        self.browser.getControl(name="searchterm").value = 'An'
352        self.browser.getControl("Search").click()
353        self.assertFalse('Anna Tester' in self.browser.contents)
354        self.browser.open(self.manage_container_path)
355        self.browser.getControl(name="searchtype").value = ['fullname']
356        self.browser.getControl(name="searchterm").value = 'An*'
357        self.browser.getControl("Search").click()
358        self.assertTrue('Anna Tester' in self.browser.contents)
359        self.browser.open(self.manage_container_path)
360        self.browser.getControl(name="searchtype").value = ['fullname']
361        self.browser.getControl(name="searchterm").value = 'tester'
362        self.browser.getControl("Search").click()
363        self.assertTrue('Anna Tester' in self.browser.contents)
364        self.browser.open(self.manage_container_path)
365        self.browser.getControl(name="searchtype").value = ['fullname']
366        self.browser.getControl(name="searchterm").value = 'Tester Ana'
367        self.browser.getControl("Search").click()
368        self.assertFalse('Anna Tester' in self.browser.contents)
369        self.browser.open(self.manage_container_path)
370        self.browser.getControl(name="searchtype").value = ['fullname']
371        self.browser.getControl(name="searchterm").value = 'Tester Anna'
372        self.browser.getControl("Search").click()
373        self.assertTrue('Anna Tester' in self.browser.contents)
374        # The old searchterm will be used again
375        self.browser.getControl("Search").click()
376        self.assertTrue('Anna Tester' in self.browser.contents)
377
378        # We can find suspended students
379        self.student.suspended = True
380        notify(grok.ObjectModifiedEvent(self.student))
381        self.browser.open(self.manage_container_path)
382        self.browser.getControl(name="searchtype").value = ['suspended']
383        self.browser.getControl("Search").click()
384        self.assertTrue('Anna Tester' in self.browser.contents)
385        self.browser.open(self.container_path)
386        self.browser.getControl(name="searchtype").value = ['suspended']
387        self.browser.getControl("Search").click()
388        self.assertTrue('Anna Tester' in self.browser.contents)
389
390        # The catalog is informed when studycourse objects have been
391        # edited
392        self.browser.open(self.studycourse_path + '/manage')
393        self.browser.getControl(name="form.current_session").value = ['2010']
394        self.browser.getControl(name="form.entry_session").value = ['2010']
395        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
396        self.browser.getControl("Save").click()
397
398        # We can find the student in the new session
399        self.browser.open(self.manage_container_path)
400        self.browser.getControl(name="searchtype").value = ['current_session']
401        self.browser.getControl(name="searchterm").value = '2010'
402        self.browser.getControl("Search").click()
403        self.assertTrue('Anna Tester' in self.browser.contents)
404
405        ctrl = self.browser.getControl(name='entries')
406        ctrl.getControl(value=self.student_id).selected = True
407        self.browser.getControl("Remove selected", index=0).click()
408        self.assertTrue('Successfully removed' in self.browser.contents)
409        self.browser.getControl(name="searchtype").value = ['student_id']
410        self.browser.getControl(name="searchterm").value = self.student_id
411        self.browser.getControl("Search").click()
412        self.assertTrue('No student found' in self.browser.contents)
413
414        self.browser.open(self.container_path)
415        self.browser.getControl(name="searchtype").value = ['student_id']
416        self.browser.getControl(name="searchterm").value = self.student_id
417        self.browser.getControl("Search").click()
418        self.assertTrue('No student found' in self.browser.contents)
419        return
420
421class OfficerUITests(StudentsFullSetup):
422    # Tests for Student class views and pages
423
424    def test_student_properties(self):
425        self.student['studycourse'].current_level = 100
426        self.assertEqual(self.student.current_level, 100)
427        self.student['studycourse'].current_session = 2011
428        self.assertEqual(self.student.current_session, 2011)
429        self.student['studycourse'].current_verdict = 'A'
430        self.assertEqual(self.student.current_verdict, 'A')
431        return
432
433    def test_studylevelmanagepage(self):
434        studylevel = StudentStudyLevel()
435        studylevel.level = 100
436        cert = self.app['faculties']['fac1']['dep1'].certificates['CERT1']
437        self.student['studycourse'].addStudentStudyLevel(
438            cert,studylevel)
439        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
440        self.browser.open(self.studycourse_path + '/100/manage')
441        self.assertEqual(self.browser.url, self.studycourse_path + '/100/manage')
442        self.assertEqual(self.browser.headers['Status'], '200 Ok')
443
444    def test_basic_auth(self):
445        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
446        self.browser.open('http://localhost/app')
447        self.browser.getLink("Logout").click()
448        self.assertTrue('You have been logged out' in self.browser.contents)
449        # But we are still logged in since we've used basic authentication here.
450        # Wikipedia says: Existing browsers retain authentication information
451        # until the tab or browser is closed or the user clears the history.
452        # HTTP does not provide a method for a server to direct clients to
453        # discard these cached credentials. This means that there is no
454        # effective way for a server to "log out" the user without closing
455        # the browser. This is a significant defect that requires browser
456        # manufacturers to support a "logout" user interface element ...
457        self.assertTrue('Manager' in self.browser.contents)
458
459    def test_basic_auth_base64(self):
460        auth_token = base64.b64encode('mgr:mgrpw')
461        self.browser.addHeader('Authorization', 'Basic %s' % auth_token)
462        self.browser.open(self.manage_container_path)
463        self.assertEqual(self.browser.headers['Status'], '200 Ok')
464
465    def test_manage_access(self):
466        # Managers can access the pages of students
467        # and can perform actions
468        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
469        self.browser.open(self.student_path)
470        self.assertEqual(self.browser.headers['Status'], '200 Ok')
471        self.assertEqual(self.browser.url, self.student_path)
472        self.browser.getLink("Trigger").click()
473        self.assertEqual(self.browser.headers['Status'], '200 Ok')
474        # Managers can trigger transitions
475        self.browser.getControl(name="transition").value = ['admit']
476        self.browser.getControl("Save").click()
477        # Managers can edit base
478        self.browser.open(self.student_path)
479        self.browser.getLink("Manage").click()
480        self.assertEqual(self.browser.url, self.manage_student_path)
481        self.assertEqual(self.browser.headers['Status'], '200 Ok')
482        self.browser.getControl(name="form.firstname").value = 'John'
483        self.browser.getControl(name="form.lastname").value = 'Tester'
484        self.browser.getControl(name="form.reg_number").value = '345'
485        self.browser.getControl(name="password").value = 'secret'
486        self.browser.getControl(name="control_password").value = 'secret'
487        self.browser.getControl("Save").click()
488        self.assertMatches('...Form has been saved...',
489                           self.browser.contents)
490        self.browser.open(self.student_path)
491        self.browser.getLink("Clearance Data").click()
492        self.assertEqual(self.browser.headers['Status'], '200 Ok')
493        self.assertEqual(self.browser.url, self.clearance_path)
494        self.browser.getLink("Manage").click()
495        self.assertEqual(self.browser.headers['Status'], '200 Ok')
496        self.assertEqual(self.browser.url, self.manage_clearance_path)
497        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
498        self.browser.getControl("Save").click()
499        self.assertMatches('...Form has been saved...',
500                           self.browser.contents)
501
502        self.browser.open(self.student_path)
503        self.browser.getLink("Personal Data").click()
504        self.assertEqual(self.browser.headers['Status'], '200 Ok')
505        self.assertEqual(self.browser.url, self.personal_path)
506        self.browser.getLink("Manage").click()
507        self.assertEqual(self.browser.headers['Status'], '200 Ok')
508        self.assertEqual(self.browser.url, self.manage_personal_path)
509        self.browser.open(self.personal_path)
510        self.assertTrue('Updated' in self.browser.contents)
511        self.browser.getLink("Edit").click()
512        self.assertEqual(self.browser.headers['Status'], '200 Ok')
513        self.assertEqual(self.browser.url, self.edit_personal_path)
514        self.browser.getControl("Save").click()
515        # perm_address is required in IStudentPersonalEdit
516        self.assertMatches('...Required input is missing...',
517                           self.browser.contents)
518        self.browser.getControl(name="form.perm_address").value = 'My address!'
519        self.browser.getControl("Save").click()
520        self.assertMatches('...Form has been saved...',
521                           self.browser.contents)
522
523        # Managers can browse all subobjects
524        self.browser.open(self.student_path)
525        self.browser.getLink("Payments").click()
526        self.assertEqual(self.browser.headers['Status'], '200 Ok')
527        self.assertEqual(self.browser.url, self.payments_path)
528        self.browser.open(self.student_path)
529        self.browser.getLink("Accommodation").click()
530        self.assertEqual(self.browser.headers['Status'], '200 Ok')
531        self.assertEqual(self.browser.url, self.acco_path)
532        self.browser.open(self.student_path)
533        self.browser.getLink("History").click()
534        self.assertEqual(self.browser.headers['Status'], '200 Ok')
535        self.assertEqual(self.browser.url, self.history_path)
536        self.assertMatches('...Admitted by Manager...',
537                           self.browser.contents)
538        # Only the Application Slip does not exist
539        self.assertFalse('Application Slip' in self.browser.contents)
540        return
541
542    def test_manage_contact_student(self):
543        # Managers can contact student
544        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
545        self.student.email = None
546        self.browser.open(self.student_path)
547        self.browser.getLink("Send email").click()
548        self.browser.getControl(name="form.subject").value = 'Important subject'
549        self.browser.getControl(name="form.body").value = 'Hello!'
550        self.browser.getControl("Send message now").click()
551        self.assertTrue('An smtp server error occurred' in self.browser.contents)
552        self.student.email = 'xx@yy.zz'
553        self.browser.getControl("Send message now").click()
554        self.assertTrue('Your message has been sent' in self.browser.contents)
555        return
556
557    def test_manage_remove_department(self):
558        # Lazy student is studying CERT1
559        lazystudent = Student()
560        lazystudent.firstname = u'Lazy'
561        lazystudent.lastname = u'Student'
562        self.app['students'].addStudent(lazystudent)
563        student_id = lazystudent.student_id
564        student_path = self.container_path + '/' + student_id
565        lazystudent['studycourse'].certificate = self.certificate
566        notify(grok.ObjectModifiedEvent(lazystudent))
567        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
568        self.browser.open(student_path + '/studycourse')
569        self.assertTrue('CERT1' in self.browser.contents)
570        # After some years the department is removed
571        del self.app['faculties']['fac1']['dep1']
572        # So CERT1 does no longer exist and lazy student's
573        # certificate reference is removed too
574        self.browser.open(student_path + '/studycourse')
575        self.assertEqual(self.browser.headers['Status'], '200 Ok')
576        self.assertEqual(self.browser.url, student_path + '/studycourse')
577        self.assertFalse('CERT1' in self.browser.contents)
578        self.assertMatches('...<div>--</div>...',
579                           self.browser.contents)
580
581    def test_manage_upload_file(self):
582        # Managers can upload a file via the StudentClearanceManageFormPage
583        # The image is stored even if form has errors
584        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
585        self.browser.open(self.manage_clearance_path)
586        # No birth certificate has been uploaded yet
587        # Browsing the link shows a placerholder image
588        self.browser.open('birth_certificate')
589        self.assertEqual(
590            self.browser.headers['content-type'], 'image/jpeg')
591        self.assertEqual(len(self.browser.contents), PH_LEN)
592        # Create a pseudo image file and select it to be uploaded in form
593        # as birth certificate
594        self.browser.open(self.manage_clearance_path)
595        image = open(SAMPLE_IMAGE, 'rb')
596        ctrl = self.browser.getControl(name='birthcertificateupload')
597        file_ctrl = ctrl.mech_control
598        file_ctrl.add_file(image, filename='my_birth_certificate.jpg')
599        # The Save action does not upload files
600        self.browser.getControl("Save").click() # submit form
601        self.assertFalse(
602            '<a target="image" href="birth_certificate">'
603            in self.browser.contents)
604        # ... but the correct upload submit button does
605        image = open(SAMPLE_IMAGE)
606        ctrl = self.browser.getControl(name='birthcertificateupload')
607        file_ctrl = ctrl.mech_control
608        file_ctrl.add_file(image, filename='my_birth_certificate.jpg')
609        self.browser.getControl(
610            name='upload_birthcertificateupload').click()
611        # There is a correct <img> link included
612        self.assertTrue(
613            '<a target="image" href="birth_certificate">'
614            in self.browser.contents)
615        # Browsing the link shows a real image
616        self.browser.open('birth_certificate')
617        self.assertEqual(
618            self.browser.headers['content-type'], 'image/jpeg')
619        self.assertEqual(len(self.browser.contents), 2787)
620        # Reuploading a file which is bigger than 150k will raise an error
621        self.browser.open(self.manage_clearance_path)
622        # An image > 150K
623        big_image = StringIO(open(SAMPLE_IMAGE, 'rb').read() * 75)
624        ctrl = self.browser.getControl(name='birthcertificateupload')
625        file_ctrl = ctrl.mech_control
626        file_ctrl.add_file(big_image, filename='my_birth_certificate.jpg')
627        self.browser.getControl(
628            name='upload_birthcertificateupload').click()
629        self.assertTrue(
630            'Uploaded file is too big' in self.browser.contents)
631        # we do not rely on filename extensions given by uploaders
632        image = open(SAMPLE_IMAGE, 'rb') # a jpg-file
633        ctrl = self.browser.getControl(name='birthcertificateupload')
634        file_ctrl = ctrl.mech_control
635        # tell uploaded file is bmp
636        file_ctrl.add_file(image, filename='my_birth_certificate.bmp')
637        self.browser.getControl(
638            name='upload_birthcertificateupload').click()
639        self.assertTrue(
640            # jpg file was recognized
641            'File birth_certificate.jpg uploaded.' in self.browser.contents)
642        # File names must meet several conditions
643        bmp_image = open(SAMPLE_IMAGE_BMP, 'rb')
644        ctrl = self.browser.getControl(name='birthcertificateupload')
645        file_ctrl = ctrl.mech_control
646        file_ctrl.add_file(bmp_image, filename='my_birth_certificate.bmp')
647        self.browser.getControl(
648            name='upload_birthcertificateupload').click()
649        self.assertTrue('Only the following extensions are allowed'
650            in self.browser.contents)
651        # Managers can delete files
652        self.browser.getControl(name='delete_birthcertificateupload').click()
653        self.assertTrue(
654            'birth_certificate deleted' in self.browser.contents)
655
656        # Managers can upload a file via the StudentBaseManageFormPage
657        self.browser.open(self.manage_student_path)
658        image = open(SAMPLE_IMAGE_BMP, 'rb')
659        ctrl = self.browser.getControl(name='passportuploadmanage')
660        file_ctrl = ctrl.mech_control
661        file_ctrl.add_file(image, filename='my_photo.bmp')
662        self.browser.getControl(
663            name='upload_passportuploadmanage').click()
664        self.assertTrue('jpg file extension expected'
665            in self.browser.contents)
666        ctrl = self.browser.getControl(name='passportuploadmanage')
667        file_ctrl = ctrl.mech_control
668        image = open(SAMPLE_IMAGE, 'rb')
669        file_ctrl.add_file(image, filename='my_photo.jpg')
670        self.browser.getControl(
671            name='upload_passportuploadmanage').click()
672        self.assertTrue(
673            '<img align="middle" height="125px" src="passport.jpg" />'
674            in self.browser.contents)
675        # We remove the passport file again
676        self.browser.open(self.manage_student_path)
677        self.browser.getControl('Delete').click()
678        self.browser.open(self.student_path + '/clearance_slip.pdf')
679        self.assertEqual(self.browser.headers['Status'], '200 Ok')
680        self.assertEqual(self.browser.headers['Content-Type'],
681                         'application/pdf')
682
683    def test_manage_course_lists(self):
684        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
685        self.browser.open(self.student_path)
686        self.browser.getLink("Study Course").click()
687        self.assertEqual(self.browser.headers['Status'], '200 Ok')
688        self.assertEqual(self.browser.url, self.studycourse_path)
689        self.assertTrue('Undergraduate Full-Time' in self.browser.contents)
690        self.browser.getLink("Manage").click()
691        self.assertTrue('Manage study course' in self.browser.contents)
692        # Before we can select a level, the certificate must
693        # be selected and saved
694        self.browser.getControl(name="form.certificate").value = ['CERT1']
695        self.browser.getControl(name="form.current_session").value = ['2004']
696        self.browser.getControl(name="form.current_verdict").value = ['A']
697        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
698        self.browser.getControl("Save").click()
699        # Now we can save also the current level which depends on start and end
700        # level of the certificate
701        self.browser.getControl(name="form.current_level").value = ['100']
702        self.browser.getControl("Save").click()
703        # Managers can add and remove any study level (course list)
704        self.browser.getControl(name="addlevel").value = ['100']
705        self.browser.getControl("Add study level").click()
706        self.assertMatches(
707            '...You must select a session...', self.browser.contents)
708        self.browser.getControl(name="addlevel").value = ['100']
709        self.browser.getControl(name="level_session").value = ['2004']
710        self.browser.getControl("Add study level").click()
711        self.assertMatches('...<span>100</span>...', self.browser.contents)
712        self.assertEqual(self.student['studycourse']['100'].level, 100)
713        self.assertEqual(self.student['studycourse']['100'].level_session, 2004)
714        self.browser.getControl(name="addlevel").value = ['100']
715        self.browser.getControl(name="level_session").value = ['2004']
716        self.browser.getControl("Add study level").click()
717        self.assertMatches('...This level exists...', self.browser.contents)
718        self.browser.getControl("Remove selected").click()
719        self.assertMatches(
720            '...No study level selected...', self.browser.contents)
721        self.browser.getControl(name="val_id").value = ['100']
722        self.browser.getControl(name="level_session").value = ['2004']
723        self.browser.getControl("Remove selected").click()
724        self.assertMatches('...Successfully removed...', self.browser.contents)
725        # Removing levels is properly logged
726        logfile = os.path.join(
727            self.app['datacenter'].storage, 'logs', 'students.log')
728        logcontent = open(logfile).read()
729        self.assertTrue('zope.mgr - students.browser.StudyCourseManageFormPage '
730                        '- K1000000 - removed: 100' in logcontent)
731        # Add level again
732        self.browser.getControl(name="addlevel").value = ['100']
733        self.browser.getControl(name="level_session").value = ['2004']
734        self.browser.getControl("Add study level").click()
735
736        # Managers can view and manage course lists
737        self.browser.getLink("100").click()
738        self.assertMatches(
739            '...: Study Level 100 (Year 1)...', self.browser.contents)
740        self.browser.getLink("Manage").click()
741        self.browser.getControl(name="form.level_session").value = ['2002']
742        self.browser.getControl("Save").click()
743        self.browser.getControl("Remove selected").click()
744        self.assertMatches('...No ticket selected...', self.browser.contents)
745        ctrl = self.browser.getControl(name='val_id')
746        ctrl.getControl(value='COURSE1').selected = True
747        self.browser.getControl("Remove selected", index=0).click()
748        self.assertTrue('Successfully removed' in self.browser.contents)
749        # Removing course tickets is properly logged
750        logfile = os.path.join(
751            self.app['datacenter'].storage, 'logs', 'students.log')
752        logcontent = open(logfile).read()
753        self.assertTrue('zope.mgr - students.browser.StudyLevelManageFormPage '
754        '- K1000000 - removed: COURSE1 at 100' in logcontent)
755        self.browser.getLink("here").click()
756        self.browser.getControl(name="form.course").value = ['COURSE1']
757        self.course.credits = 100
758        self.browser.getControl("Add course ticket").click()
759        self.assertMatches(
760            '...Total credits exceed 50...', self.browser.contents)
761        self.course.credits = 10
762        self.browser.getControl("Add course ticket").click()
763        self.assertTrue('Successfully added' in self.browser.contents)
764        # We can do the same by adding the course on the manage page directly
765        del self.student['studycourse']['100']['COURSE1']
766        self.browser.getControl(name="course").value = 'COURSE1'
767        self.browser.getControl("Add course ticket").click()
768        self.assertTrue('Successfully added' in self.browser.contents)
769        self.browser.getLink("here").click()
770        self.browser.getControl(name="form.course").value = ['COURSE1']
771        self.browser.getControl("Add course ticket").click()
772        self.assertTrue('The ticket exists' in self.browser.contents)
773        self.browser.getControl("Cancel").click()
774        self.browser.getLink("COURSE1").click()
775        self.browser.getLink("Manage").click()
776        self.browser.getControl("Save").click()
777        self.assertTrue('Form has been saved' in self.browser.contents)
778        # Grade and weight have been determined
779        self.browser.open(self.studycourse_path + '/100/COURSE1')
780        self.assertFalse('Grade' in self.browser.contents)
781        self.assertFalse('Weight' in self.browser.contents)
782        self.student['studycourse']['100']['COURSE1'].score = 55
783        self.browser.open(self.studycourse_path + '/100/COURSE1')
784        self.assertTrue('Grade' in self.browser.contents)
785        self.assertTrue('Weight' in self.browser.contents)
786        self.assertEqual(self.student['studycourse']['100']['COURSE1'].grade, 'C')
787        self.assertEqual(self.student['studycourse']['100']['COURSE1'].weight, 3)
788        # We add another ticket to check if GPA will be correctly calculated
789        # (and rounded)
790        courseticket = createObject('waeup.CourseTicket')
791        courseticket.code = 'ANYCODE'
792        courseticket.title = u'Any TITLE'
793        courseticket.credits = 13
794        courseticket.score = 66
795        courseticket.semester = 1
796        courseticket.dcode = u'ANYDCODE'
797        courseticket.fcode = u'ANYFCODE'
798        self.student['studycourse']['100']['COURSE2'] = courseticket
799        self.browser.open(self.student_path + '/studycourse/100')
800        # total credits
801        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[1], 23)
802        # weigheted credits = 3 * 10 + 4 * 13
803        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[2], 82.0)
804        # sgpa = 82 / 23
805        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[0], 3.565)
806        # Carry-over courses will be collected when next level is created
807        self.browser.open(self.student_path + '/studycourse/manage')
808        # Add next level
809        self.student['studycourse']['100']['COURSE1'].score = 10
810        self.browser.getControl(name="addlevel").value = ['200']
811        self.browser.getControl(name="level_session").value = ['2005']
812        self.browser.getControl("Add study level").click()
813        self.browser.getLink("200").click()
814        self.assertMatches(
815            '...: Study Level 200 (Year 2)...', self.browser.contents)
816        # Since COURSE1 has score 10 it becomes a carry-over course
817        # in level 200
818        self.assertEqual(
819            sorted(self.student['studycourse']['200'].keys()), [u'COURSE1'])
820        self.assertTrue(
821            self.student['studycourse']['200']['COURSE1'].carry_over)
822        # Passed and failed courses have been counted
823        self.assertEqual(
824            self.student['studycourse']['100'].passed_params,
825            (1, 1, 13, 10, ['COURSE1']))
826        self.assertEqual(
827            self.student['studycourse']['200'].passed_params,
828            (0, 0, 0, 0, []))
829        # And also cumulative params can be calculated. Meanwhile we have the
830        # following courses: COURSE1 and COURSE2 in level 100 and
831        # COURSE1 as carry-over course in level 200.
832        self.assertEqual(
833            self.student['studycourse']['100'].cumulative_params,
834            (2.261, 23, 52.0, 23, 13))
835        # COURSE1 in level 200 is not taken into consideration
836        # when calculating the gpa.
837        self.assertEqual(
838            self.student['studycourse']['200'].cumulative_params,
839            (2.261, 23, 52.0, 33, 13))
840        return
841
842    def test_gpa_calculation_with_carryover(self):
843        studylevel = createObject(u'waeup.StudentStudyLevel')
844        studylevel.level = 100
845        studylevel.level_session = 2005
846        self.student['studycourse'].entry_mode = 'ug_ft'
847        self.student['studycourse'].addStudentStudyLevel(
848            self.certificate, studylevel)
849        # First course has been added automatically.
850        # Set score above passmark.
851        studylevel['COURSE1'].score = studylevel['COURSE1'].passmark + 1
852        # GPA is 1.
853        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[0], 1.0)
854        # Set score below passmark.
855        studylevel['COURSE1'].score = studylevel['COURSE1'].passmark - 1
856        # GPA is still 0.
857        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[0], 0.0)
858        studylevel2 = createObject(u'waeup.StudentStudyLevel')
859        studylevel2.level = 200
860        studylevel2.level_session = 2006
861        self.student['studycourse'].addStudentStudyLevel(
862            self.certificate, studylevel2)
863        # Carry-over course has been autonatically added.
864        studylevel2['COURSE1'].score = 66
865        # The score of the carry-over course is now used for calculation of the
866        # GPA at level 100 ...
867        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[0], 4.0)
868        # ... but not at level 200
869        self.assertEqual(self.student['studycourse']['200'].gpa_params_rectified[0], 0.0)
870        return
871
872    def test_manage_payments(self):
873        # Managers can add online school fee payment tickets
874        # if certain requirements are met
875        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
876        self.browser.open(self.payments_path)
877        IWorkflowState(self.student).setState('cleared')
878        self.browser.getLink("Add current session payment ticket").click()
879        self.browser.getControl(name="form.p_category").value = ['schoolfee']
880        self.browser.getControl("Create ticket").click()
881        self.assertMatches('...ticket created...',
882                           self.browser.contents)
883        ctrl = self.browser.getControl(name='val_id')
884        value = ctrl.options[0]
885        self.browser.getLink(value).click()
886        self.assertMatches('...Amount Authorized...',
887                           self.browser.contents)
888        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
889        payment_url = self.browser.url
890
891        # The pdf payment slip can't yet be opened
892        #self.browser.open(payment_url + '/payment_slip.pdf')
893        #self.assertMatches('...Ticket not yet paid...',
894        #                   self.browser.contents)
895
896        # The same payment (with same p_item, p_session and p_category)
897        # can be initialized a second time if the former ticket is not yet paid.
898        self.browser.open(self.payments_path)
899        self.browser.getLink("Add current session payment ticket").click()
900        self.browser.getControl(name="form.p_category").value = ['schoolfee']
901        self.browser.getControl("Create ticket").click()
902        self.assertMatches('...Payment ticket created...',
903                           self.browser.contents)
904
905        # The ticket can be found in the payments_catalog
906        cat = queryUtility(ICatalog, name='payments_catalog')
907        results = list(cat.searchResults(p_state=('unpaid', 'unpaid')))
908        self.assertTrue(len(results), 1)
909        self.assertTrue(results[0] is self.student['payments'][value])
910
911        # Managers can approve the payment
912        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
913        self.browser.open(payment_url)
914        self.browser.getLink("Approve payment").click()
915        self.assertMatches('...Payment approved...',
916                          self.browser.contents)
917        # Approval is logged in students.log ...
918        logfile = os.path.join(
919            self.app['datacenter'].storage, 'logs', 'students.log')
920        logcontent = open(logfile).read()
921        self.assertTrue(
922            'zope.mgr - students.browser.OnlinePaymentApprovePage '
923            '- K1000000 - schoolfee payment approved'
924            in logcontent)
925        # ... and in payments.log
926        logfile = os.path.join(
927            self.app['datacenter'].storage, 'logs', 'payments.log')
928        logcontent = open(logfile).read()
929        self.assertTrue(
930            '"zope.mgr",K1000000,%s,schoolfee,40000.0,AP,,,,,,\n' % value
931            in logcontent)
932
933        # The authorized amount has been stored in the access code
934        self.assertEqual(
935            self.app['accesscodes']['SFE-0'].values()[0].cost,40000.0)
936
937        # The catalog has been updated
938        results = list(cat.searchResults(p_state=('unpaid', 'unpaid')))
939        self.assertTrue(len(results), 0)
940        results = list(cat.searchResults(p_state=('paid', 'paid')))
941        self.assertTrue(len(results), 1)
942        self.assertTrue(results[0] is self.student['payments'][value])
943
944        # Payments can't be approved twice
945        self.browser.open(payment_url + '/approve')
946        self.assertMatches('...This ticket has already been paid...',
947                          self.browser.contents)
948
949        # Now the first ticket is paid and no more ticket of same type
950        # (with same p_item, p_session and p_category) can be added
951        self.browser.open(self.payments_path)
952        self.browser.getLink("Add current session payment ticket").click()
953        self.browser.getControl(name="form.p_category").value = ['schoolfee']
954        self.browser.getControl("Create ticket").click()
955        self.assertMatches(
956            '...This type of payment has already been made...',
957            self.browser.contents)
958
959        # Managers can open the pdf payment slip
960        self.browser.open(payment_url)
961        self.browser.getLink("Download payment slip").click()
962        self.assertEqual(self.browser.headers['Status'], '200 Ok')
963        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
964
965        # Managers can remove online school fee payment tickets
966        self.browser.open(self.payments_path)
967        self.browser.getControl("Remove selected").click()
968        self.assertMatches('...No payment selected...', self.browser.contents)
969        ctrl = self.browser.getControl(name='val_id')
970        value = ctrl.options[0]
971        ctrl.getControl(value=value).selected = True
972        self.browser.getControl("Remove selected", index=0).click()
973        self.assertTrue('Successfully removed' in self.browser.contents)
974
975        # Managers can add online clearance payment tickets
976        self.browser.open(self.payments_path + '/addop')
977        self.browser.getControl(name="form.p_category").value = ['clearance']
978        self.browser.getControl("Create ticket").click()
979        self.assertMatches('...ticket created...',
980                           self.browser.contents)
981
982        # Managers can approve the payment
983        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
984        ctrl = self.browser.getControl(name='val_id')
985        value = ctrl.options[1] # The clearance payment is the second in the table
986        self.browser.getLink(value).click()
987        self.browser.open(self.browser.url + '/approve')
988        self.assertMatches('...Payment approved...',
989                          self.browser.contents)
990        expected = '''...
991        <td>
992          <span>Paid</span>
993        </td>...'''
994        self.assertMatches(expected,self.browser.contents)
995        # The new CLR-0 pin has been created
996        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
997        pin = self.app['accesscodes']['CLR-0'].keys()[0]
998        ac = self.app['accesscodes']['CLR-0'][pin]
999        self.assertEqual(ac.owner, self.student_id)
1000        self.assertEqual(ac.cost, 3456.0)
1001
1002        # Managers can add online transcript payment tickets
1003        self.browser.open(self.payments_path + '/addop')
1004        self.browser.getControl(name="form.p_category").value = ['transcript']
1005        self.browser.getControl("Create ticket").click()
1006        self.assertMatches('...ticket created...',
1007                           self.browser.contents)
1008
1009        # Managers can approve the payment
1010        self.assertEqual(len(self.app['accesscodes']['TSC-0']),0)
1011        ctrl = self.browser.getControl(name='val_id')
1012        value = ctrl.options[2] # The clearance payment is the third in the table
1013        self.browser.getLink(value).click()
1014        self.browser.open(self.browser.url + '/approve')
1015        self.assertMatches('...Payment approved...',
1016                          self.browser.contents)
1017        expected = '''...
1018        <td>
1019          <span>Paid</span>
1020        </td>...'''
1021        self.assertMatches(expected,self.browser.contents)
1022        # The new CLR-0 pin has been created
1023        self.assertEqual(len(self.app['accesscodes']['TSC-0']),1)
1024        pin = self.app['accesscodes']['TSC-0'].keys()[0]
1025        ac = self.app['accesscodes']['TSC-0'][pin]
1026        self.assertEqual(ac.owner, self.student_id)
1027        self.assertEqual(ac.cost, 4567.0)
1028        return
1029
1030    def test_manage_balance_payments(self):
1031
1032        # Login
1033        #self.browser.open(self.login_path)
1034        #self.browser.getControl(name="form.login").value = self.student_id
1035        #self.browser.getControl(name="form.password").value = 'spwd'
1036        #self.browser.getControl("Login").click()
1037
1038        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1039        self.browser.open(self.payments_path)
1040
1041        # Managers can add previous school fee payment tickets in any state.
1042        IWorkflowState(self.student).setState('courses registered')
1043        self.browser.open(self.payments_path)
1044        self.browser.getLink("Add balance payment ticket").click()
1045
1046        # Previous session payment form is provided
1047        self.assertEqual(self.student.current_session, 2004)
1048        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1049        self.browser.getControl(name="form.balance_session").value = ['2004']
1050        self.browser.getControl(name="form.balance_level").value = ['300']
1051        self.browser.getControl(name="form.balance_amount").value = '-567.8'
1052        self.browser.getControl("Create ticket").click()
1053        self.assertMatches('...Amount must be greater than 0...',
1054                           self.browser.contents)
1055        self.browser.getControl(name="form.balance_amount").value = '0'
1056        self.browser.getControl("Create ticket").click()
1057        self.assertMatches('...Amount must be greater than 0...',
1058                           self.browser.contents)
1059        self.browser.getControl(name="form.balance_amount").value = '567.8'
1060        self.browser.getControl("Create ticket").click()
1061        self.assertMatches('...ticket created...',
1062                           self.browser.contents)
1063        ctrl = self.browser.getControl(name='val_id')
1064        value = ctrl.options[0]
1065        self.browser.getLink(value).click()
1066        self.assertMatches('...Amount Authorized...',
1067                           self.browser.contents)
1068        self.assertEqual(self.student['payments'][value].amount_auth, 567.8)
1069        # Payment attributes are properly set
1070        self.assertEqual(self.student['payments'][value].p_session, 2004)
1071        self.assertEqual(self.student['payments'][value].p_level, 300)
1072        self.assertEqual(self.student['payments'][value].p_item, u'Balance')
1073        self.assertEqual(self.student['payments'][value].p_category, 'schoolfee')
1074
1075    def test_manage_accommodation(self):
1076        logfile = os.path.join(
1077            self.app['datacenter'].storage, 'logs', 'students.log')
1078        # Managers can add online booking fee payment tickets and open the
1079        # callback view (see test_manage_payments)
1080        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1081        self.browser.open(self.payments_path)
1082        self.browser.getLink("Add current session payment ticket").click()
1083        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1084        # If student is not in accommodation session, payment cannot be processed
1085        self.app['hostels'].accommodation_session = 2011
1086        self.browser.getControl("Create ticket").click()
1087        self.assertMatches('...Your current session does not match...',
1088                           self.browser.contents)
1089        self.app['hostels'].accommodation_session = 2004
1090        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1091        self.browser.getControl("Create ticket").click()
1092        ctrl = self.browser.getControl(name='val_id')
1093        value = ctrl.options[0]
1094        self.browser.getLink(value).click()
1095        self.browser.open(self.browser.url + '/approve')
1096        # The new HOS-0 pin has been created
1097        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
1098        pin = self.app['accesscodes']['HOS-0'].keys()[0]
1099        ac = self.app['accesscodes']['HOS-0'][pin]
1100        self.assertEqual(ac.owner, self.student_id)
1101        parts = pin.split('-')[1:]
1102        sfeseries, sfenumber = parts
1103        # Managers can use HOS code and book a bed space with it
1104        self.browser.open(self.acco_path)
1105        self.browser.getLink("Book accommodation").click()
1106        self.assertMatches('...You are in the wrong...',
1107                           self.browser.contents)
1108        IWorkflowInfo(self.student).fireTransition('admit')
1109        # An existing HOS code can only be used if students
1110        # are in accommodation session
1111        self.student['studycourse'].current_session = 2003
1112        self.browser.getLink("Book accommodation").click()
1113        self.assertMatches('...Your current session does not match...',
1114                           self.browser.contents)
1115        self.student['studycourse'].current_session = 2004
1116        # All requirements are met and ticket can be created
1117        self.browser.getLink("Book accommodation").click()
1118        self.assertMatches('...Activation Code:...',
1119                           self.browser.contents)
1120        self.browser.getControl(name="ac_series").value = sfeseries
1121        self.browser.getControl(name="ac_number").value = sfenumber
1122        self.browser.getControl("Create bed ticket").click()
1123        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1124                           self.browser.contents)
1125        # Bed has been allocated
1126        bed1 = self.app['hostels']['hall-1']['hall-1_A_101_A']
1127        self.assertTrue(bed1.owner == self.student_id)
1128        # BedTicketAddPage is now blocked
1129        self.browser.getLink("Book accommodation").click()
1130        self.assertMatches('...You already booked a bed space...',
1131            self.browser.contents)
1132        # The bed ticket displays the data correctly
1133        self.browser.open(self.acco_path + '/2004')
1134        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1135                           self.browser.contents)
1136        self.assertMatches('...2004/2005...', self.browser.contents)
1137        self.assertMatches('...regular_male_fr...', self.browser.contents)
1138        self.assertMatches('...%s...' % pin, self.browser.contents)
1139        # Booking is properly logged
1140        logcontent = open(logfile).read()
1141        self.assertTrue('zope.mgr - students.browser.BedTicketAddPage '
1142            '- K1000000 - booked: hall-1_A_101_A' in logcontent)
1143        # Managers can relocate students if the student's bed_type has changed
1144        self.browser.getLink("Relocate student").click()
1145        self.assertMatches(
1146            "...Student can't be relocated...", self.browser.contents)
1147        self.student.sex = u'f'
1148        self.browser.getLink("Relocate student").click()
1149        self.assertMatches(
1150            "...Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1151        self.assertTrue(bed1.owner == NOT_OCCUPIED)
1152        bed2 = self.app['hostels']['hall-1']['hall-1_A_101_B']
1153        self.assertTrue(bed2.owner == self.student_id)
1154        self.assertTrue(self.student['accommodation'][
1155            '2004'].bed_type == u'regular_female_fr')
1156        # Relocation is properly logged
1157        logcontent = open(logfile).read()
1158        self.assertTrue('zope.mgr - students.browser.BedTicketRelocationPage '
1159            '- K1000000 - relocated: hall-1_A_101_B' in logcontent)
1160        # The payment object still shows the original payment item
1161        payment_id = self.student['payments'].keys()[0]
1162        payment = self.student['payments'][payment_id]
1163        self.assertTrue(payment.p_item == u'regular_male_fr')
1164        # Managers can relocate students if the bed's bed_type has changed
1165        bed1.bed_type = u'regular_female_fr'
1166        bed2.bed_type = u'regular_male_fr'
1167        notify(grok.ObjectModifiedEvent(bed1))
1168        notify(grok.ObjectModifiedEvent(bed2))
1169        self.browser.getLink("Relocate student").click()
1170        self.assertMatches(
1171            "...Student relocated...", self.browser.contents)
1172        self.assertMatches(
1173            "... Hall 1, Block A, Room 101, Bed A...", self.browser.contents)
1174        self.assertMatches(bed1.owner, self.student_id)
1175        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1176        # Managers can't relocate students if bed is reserved
1177        self.student.sex = u'm'
1178        bed1.bed_type = u'regular_female_reserved'
1179        notify(grok.ObjectModifiedEvent(bed1))
1180        self.browser.getLink("Relocate student").click()
1181        self.assertMatches(
1182            "...Students in reserved beds can't be relocated...",
1183            self.browser.contents)
1184        # Managers can relocate students if booking has been cancelled but
1185        # other bed space has been manually allocated after cancellation
1186        old_owner = bed1.releaseBed()
1187        self.assertMatches(old_owner, self.student_id)
1188        bed2.owner = self.student_id
1189        self.browser.open(self.acco_path + '/2004')
1190        self.assertMatches(
1191            "...booking cancelled...", self.browser.contents)
1192        self.browser.getLink("Relocate student").click()
1193        # We didn't informed the catalog therefore the new owner is not found
1194        self.assertMatches(
1195            "...There is no free bed in your category regular_male_fr...",
1196            self.browser.contents)
1197        # Now we fire the event properly
1198        notify(grok.ObjectModifiedEvent(bed2))
1199        self.browser.getLink("Relocate student").click()
1200        self.assertMatches(
1201            "...Student relocated...", self.browser.contents)
1202        self.assertMatches(
1203            "... Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1204          # Managers can delete bed tickets
1205        self.browser.open(self.acco_path)
1206        ctrl = self.browser.getControl(name='val_id')
1207        value = ctrl.options[0]
1208        ctrl.getControl(value=value).selected = True
1209        self.browser.getControl("Remove selected", index=0).click()
1210        self.assertMatches('...Successfully removed...', self.browser.contents)
1211        # The bed has been properly released by the event handler
1212        self.assertMatches(bed1.owner, NOT_OCCUPIED)
1213        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1214        return
1215
1216    def test_manage_workflow(self):
1217        # Managers can pass through the whole workflow
1218        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1219        student = self.app['students'][self.student_id]
1220        self.browser.open(self.trigtrans_path)
1221        self.assertTrue(student.clearance_locked)
1222        self.browser.getControl(name="transition").value = ['admit']
1223        self.browser.getControl("Save").click()
1224        self.assertTrue(student.clearance_locked)
1225        self.browser.getControl(name="transition").value = ['start_clearance']
1226        self.browser.getControl("Save").click()
1227        self.assertFalse(student.clearance_locked)
1228        self.browser.getControl(name="transition").value = ['request_clearance']
1229        self.browser.getControl("Save").click()
1230        self.assertTrue(student.clearance_locked)
1231        self.browser.getControl(name="transition").value = ['clear']
1232        self.browser.getControl("Save").click()
1233        # Managers approve payment, they do not pay
1234        self.assertFalse('pay_first_school_fee' in self.browser.contents)
1235        self.browser.getControl(
1236            name="transition").value = ['approve_first_school_fee']
1237        self.browser.getControl("Save").click()
1238        self.browser.getControl(name="transition").value = ['reset6']
1239        self.browser.getControl("Save").click()
1240        # In state returning the pay_school_fee transition triggers some
1241        # changes of attributes
1242        self.browser.getControl(name="transition").value = ['approve_school_fee']
1243        self.browser.getControl("Save").click()
1244        self.assertEqual(student['studycourse'].current_session, 2005) # +1
1245        self.assertEqual(student['studycourse'].current_level, 200) # +100
1246        self.assertEqual(student['studycourse'].current_verdict, '0') # 0 = Zero = not set
1247        self.assertEqual(student['studycourse'].previous_verdict, 'A')
1248        self.browser.getControl(name="transition").value = ['register_courses']
1249        self.browser.getControl("Save").click()
1250        self.browser.getControl(name="transition").value = ['validate_courses']
1251        self.browser.getControl("Save").click()
1252        self.browser.getControl(name="transition").value = ['return']
1253        self.browser.getControl("Save").click()
1254        return
1255
1256    def test_manage_pg_workflow(self):
1257        # Managers can pass through the whole workflow
1258        IWorkflowState(self.student).setState('school fee paid')
1259        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1260        student = self.app['students'][self.student_id]
1261        self.browser.open(self.trigtrans_path)
1262        self.assertTrue('<option value="reset6">' in self.browser.contents)
1263        self.assertTrue('<option value="register_courses">' in self.browser.contents)
1264        self.assertTrue('<option value="reset5">' in self.browser.contents)
1265        self.certificate.study_mode = 'pg_ft'
1266        self.browser.open(self.trigtrans_path)
1267        self.assertFalse('<option value="reset6">' in self.browser.contents)
1268        self.assertFalse('<option value="register_courses">' in self.browser.contents)
1269        self.assertTrue('<option value="reset5">' in self.browser.contents)
1270        return
1271
1272    def test_manage_import(self):
1273        # Managers can import student data files
1274        datacenter_path = 'http://localhost/app/datacenter'
1275        # Prepare a csv file for students
1276        open('students.csv', 'wb').write(
1277"""firstname,lastname,reg_number,date_of_birth,matric_number,email,phone,sex,password
1278Aaren,Pieri,1,1990-01-02,100000,aa@aa.ng,1234,m,mypwd1
1279Claus,Finau,2,1990-01-03,100001,aa@aa.ng,1234,m,mypwd1
1280Brit,Berson,3,1990-01-04,100001,aa@aa.ng,1234,m,mypwd1
1281""")
1282        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1283        self.browser.open(datacenter_path)
1284        self.browser.getLink('Upload data').click()
1285        filecontents = StringIO(open('students.csv', 'rb').read())
1286        filewidget = self.browser.getControl(name='uploadfile:file')
1287        filewidget.add_file(filecontents, 'text/plain', 'students.csv')
1288        self.browser.getControl(name='SUBMIT').click()
1289        self.browser.getLink('Process data').click()
1290        button = lookup_submit_value(
1291            'select', 'students_zope.mgr.csv', self.browser)
1292        button.click()
1293        importerselect = self.browser.getControl(name='importer')
1294        modeselect = self.browser.getControl(name='mode')
1295        importerselect.getControl('Student Processor').selected = True
1296        modeselect.getControl(value='create').selected = True
1297        self.browser.getControl('Proceed to step 3').click()
1298        self.assertTrue('Header fields OK' in self.browser.contents)
1299        self.browser.getControl('Perform import').click()
1300        self.assertTrue('Processing of 1 rows failed' in self.browser.contents)
1301        self.assertTrue('Successfully processed 2 rows' in self.browser.contents)
1302        self.assertTrue('Batch processing finished' in self.browser.contents)
1303        open('studycourses.csv', 'wb').write(
1304"""reg_number,matric_number,certificate,current_session,current_level
13051,,CERT1,2008,100
1306,100001,CERT1,2008,100
1307,100002,CERT1,2008,100
1308""")
1309        self.browser.open(datacenter_path)
1310        self.browser.getLink('Upload data').click()
1311        filecontents = StringIO(open('studycourses.csv', 'rb').read())
1312        filewidget = self.browser.getControl(name='uploadfile:file')
1313        filewidget.add_file(filecontents, 'text/plain', 'studycourses.csv')
1314        self.browser.getControl(name='SUBMIT').click()
1315        self.browser.getLink('Process data').click()
1316        button = lookup_submit_value(
1317            'select', 'studycourses_zope.mgr.csv', self.browser)
1318        button.click()
1319        importerselect = self.browser.getControl(name='importer')
1320        modeselect = self.browser.getControl(name='mode')
1321        importerselect.getControl(
1322            'StudentStudyCourse Processor (update only)').selected = True
1323        modeselect.getControl(value='create').selected = True
1324        self.browser.getControl('Proceed to step 3').click()
1325        self.assertTrue('Update mode only' in self.browser.contents)
1326        self.browser.getControl('Proceed to step 3').click()
1327        self.assertTrue('Header fields OK' in self.browser.contents)
1328        self.browser.getControl('Perform import').click()
1329        self.assertTrue('Processing of 1 rows failed' in self.browser.contents)
1330        self.assertTrue('Successfully processed 2 rows'
1331                        in self.browser.contents)
1332        # The students are properly indexed and we can
1333        # thus find a student in  the department
1334        self.browser.open(self.manage_container_path)
1335        self.browser.getControl(name="searchtype").value = ['depcode']
1336        self.browser.getControl(name="searchterm").value = 'dep1'
1337        self.browser.getControl("Search").click()
1338        self.assertTrue('Aaren Pieri' in self.browser.contents)
1339        # We can search for a new student by name ...
1340        self.browser.getControl(name="searchtype").value = ['fullname']
1341        self.browser.getControl(name="searchterm").value = 'Claus'
1342        self.browser.getControl("Search").click()
1343        self.assertTrue('Claus Finau' in self.browser.contents)
1344        # ... and check if the imported password has been properly set
1345        ctrl = self.browser.getControl(name='entries')
1346        value = ctrl.options[0]
1347        claus = self.app['students'][value]
1348        self.assertTrue(IUserAccount(claus).checkPassword('mypwd1'))
1349        return
1350
1351    def test_handle_clearance_by_co(self):
1352        # Create clearance officer
1353        self.app['users'].addUser('mrclear', 'mrclearsecret')
1354        self.app['users']['mrclear'].email = 'mrclear@foo.ng'
1355        self.app['users']['mrclear'].title = 'Carlo Pitter'
1356        # Clearance officers need not necessarily to get
1357        # the StudentsOfficer site role
1358        #prmglobal = IPrincipalRoleManager(self.app)
1359        #prmglobal.assignRoleToPrincipal('waeup.StudentsOfficer', 'mrclear')
1360        # Assign local ClearanceOfficer role
1361        department = self.app['faculties']['fac1']['dep1']
1362        prmlocal = IPrincipalRoleManager(department)
1363        prmlocal.assignRoleToPrincipal('waeup.local.ClearanceOfficer', 'mrclear')
1364        IWorkflowState(self.student).setState('clearance started')
1365        # Login as clearance officer
1366        self.browser.open(self.login_path)
1367        self.browser.getControl(name="form.login").value = 'mrclear'
1368        self.browser.getControl(name="form.password").value = 'mrclearsecret'
1369        self.browser.getControl("Login").click()
1370        self.assertMatches('...You logged in...', self.browser.contents)
1371        # CO can see his roles
1372        self.browser.getLink("My Roles").click()
1373        self.assertMatches(
1374            '...<div>Academics Officer (view only)</div>...',
1375            self.browser.contents)
1376        #self.assertMatches(
1377        #    '...<div>Students Officer (view only)</div>...',
1378        #    self.browser.contents)
1379        # But not his local role ...
1380        self.assertFalse('Clearance Officer' in self.browser.contents)
1381        # ... because we forgot to notify the department that the local role
1382        # has changed
1383        notify(LocalRoleSetEvent(
1384            department, 'waeup.local.ClearanceOfficer', 'mrclear', granted=True))
1385        self.browser.open('http://localhost/app/users/mrclear/my_roles')
1386        self.assertTrue('Clearance Officer' in self.browser.contents)
1387        self.assertMatches(
1388            '...<a href="http://localhost/app/faculties/fac1/dep1">...',
1389            self.browser.contents)
1390        # CO can view the student ...
1391        self.browser.open(self.clearance_path)
1392        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1393        self.assertEqual(self.browser.url, self.clearance_path)
1394        # ... but not other students
1395        other_student = Student()
1396        other_student.firstname = u'Dep2'
1397        other_student.lastname = u'Student'
1398        self.app['students'].addStudent(other_student)
1399        other_student_path = (
1400            'http://localhost/app/students/%s' % other_student.student_id)
1401        self.assertRaises(
1402            Unauthorized, self.browser.open, other_student_path)
1403        # Clearance is disabled for this session
1404        self.browser.open(self.clearance_path)
1405        self.assertFalse('Clear student' in self.browser.contents)
1406        self.browser.open(self.student_path + '/clear')
1407        self.assertTrue('Clearance is disabled for this session'
1408            in self.browser.contents)
1409        self.app['configuration']['2004'].clearance_enabled = True
1410        # Only in state clearance requested the CO does see the 'Clear' button
1411        self.browser.open(self.clearance_path)
1412        self.assertFalse('Clear student' in self.browser.contents)
1413        IWorkflowInfo(self.student).fireTransition('request_clearance')
1414        self.browser.open(self.clearance_path)
1415        self.assertTrue('Clear student' in self.browser.contents)
1416        self.browser.getLink("Clear student").click()
1417        self.assertTrue('Student has been cleared' in self.browser.contents)
1418        self.assertTrue('cleared' in self.browser.contents)
1419        self.browser.open(self.history_path)
1420        self.assertTrue('Cleared by Carlo Pitter' in self.browser.contents)
1421        # Hide real name.
1422        self.app['users']['mrclear'].public_name = 'My Public Name'
1423        self.browser.open(self.clearance_path)
1424        self.browser.getLink("Reject clearance").click()
1425        self.assertEqual(
1426            self.browser.url, self.student_path + '/reject_clearance')
1427        # Type comment why
1428        self.browser.getControl(name="form.officer_comment").value = """Dear Student,
1429You did not fill properly.
1430"""
1431        self.browser.getControl("Save comment").click()
1432        self.assertTrue('Clearance has been annulled' in self.browser.contents)
1433        url = ('http://localhost/app/students/K1000000/'
1434              'contactstudent?body=Dear+Student%2C%0AYou+did+not+fill+properly.'
1435              '%0A&subject=Clearance+has+been+annulled.')
1436        # CO does now see the prefilled contact form and can send a message
1437        self.assertEqual(self.browser.url, url)
1438        self.assertTrue('clearance started' in self.browser.contents)
1439        self.assertTrue('name="form.subject" size="20" type="text" '
1440            'value="Clearance has been annulled."'
1441            in self.browser.contents)
1442        self.assertTrue('name="form.body" rows="10" >Dear Student,'
1443            in self.browser.contents)
1444        self.browser.getControl("Send message now").click()
1445        self.assertTrue('Your message has been sent' in self.browser.contents)
1446        # The comment has been stored ...
1447        self.assertEqual(self.student.officer_comment,
1448            u'Dear Student,\nYou did not fill properly.\n')
1449        # ... and logged
1450        logfile = os.path.join(
1451            self.app['datacenter'].storage, 'logs', 'students.log')
1452        logcontent = open(logfile).read()
1453        self.assertTrue(
1454            'INFO - mrclear - students.browser.StudentRejectClearancePage - '
1455            'K1000000 - comment: Dear Student,<br>You did not fill '
1456            'properly.<br>\n' in logcontent)
1457        self.browser.open(self.history_path)
1458        self.assertTrue("Reset to 'clearance started' by My Public Name" in
1459            self.browser.contents)
1460        IWorkflowInfo(self.student).fireTransition('request_clearance')
1461        self.browser.open(self.clearance_path)
1462        self.browser.getLink("Reject clearance").click()
1463        self.browser.getControl("Save comment").click()
1464        self.assertTrue('Clearance request has been rejected'
1465            in self.browser.contents)
1466        self.assertTrue('clearance started' in self.browser.contents)
1467        # The CO can't clear students if not in state
1468        # clearance requested
1469        self.browser.open(self.student_path + '/clear')
1470        self.assertTrue('Student is in wrong state'
1471            in self.browser.contents)
1472        # The CO can go to his department throug the my_roles page ...
1473        self.browser.open('http://localhost/app/users/mrclear/my_roles')
1474        self.browser.getLink("http://localhost/app/faculties/fac1/dep1").click()
1475        # ... and view the list of students
1476        self.browser.getLink("Show students").click()
1477        self.browser.getControl(name="session").value = ['2004']
1478        self.browser.getControl(name="level").value = ['200']
1479        self.browser.getControl("Show").click()
1480        self.assertFalse(self.student_id in self.browser.contents)
1481        self.browser.getControl(name="session").value = ['2004']
1482        self.browser.getControl(name="level").value = ['100']
1483        self.browser.getControl("Show").click()
1484        self.assertTrue(self.student_id in self.browser.contents)
1485        # The comment is indicated by 'yes'
1486        self.assertTrue('<td><span>yes</span></td>' in self.browser.contents)
1487        # Check if the enquiries form is not pre-filled with officer_comment
1488        # (regression test)
1489        self.browser.getLink("Logout").click()
1490        self.browser.open('http://localhost/app/enquiries')
1491        self.assertFalse(
1492            'You did not fill properly'
1493            in self.browser.contents)
1494        # When a student is cleared the comment is automatically deleted
1495        IWorkflowInfo(self.student).fireTransition('request_clearance')
1496        IWorkflowInfo(self.student).fireTransition('clear')
1497        self.assertEqual(self.student.officer_comment, None)
1498
1499    def test_handle_courses_by_ca(self):
1500        # Create course adviser
1501        self.app['users'].addUser('mrsadvise', 'mrsadvisesecret')
1502        self.app['users']['mrsadvise'].email = 'mradvise@foo.ng'
1503        self.app['users']['mrsadvise'].title = u'Helen Procter'
1504        # Assign local CourseAdviser100 role for a certificate
1505        cert = self.app['faculties']['fac1']['dep1'].certificates['CERT1']
1506        prmlocal = IPrincipalRoleManager(cert)
1507        prmlocal.assignRoleToPrincipal('waeup.local.CourseAdviser100', 'mrsadvise')
1508        IWorkflowState(self.student).setState('school fee paid')
1509        # Login as course adviser
1510        self.browser.open(self.login_path)
1511        self.browser.getControl(name="form.login").value = 'mrsadvise'
1512        self.browser.getControl(name="form.password").value = 'mrsadvisesecret'
1513        self.browser.getControl("Login").click()
1514        self.assertMatches('...You logged in...', self.browser.contents)
1515        # CO can see his roles
1516        self.browser.getLink("My Roles").click()
1517        self.assertMatches(
1518            '...<div>Academics Officer (view only)</div>...',
1519            self.browser.contents)
1520        # But not his local role ...
1521        self.assertFalse('Course Adviser' in self.browser.contents)
1522        # ... because we forgot to notify the certificate that the local role
1523        # has changed
1524        notify(LocalRoleSetEvent(
1525            cert, 'waeup.local.CourseAdviser100', 'mrsadvise', granted=True))
1526        self.browser.open('http://localhost/app/users/mrsadvise/my_roles')
1527        self.assertTrue('Course Adviser 100L' in self.browser.contents)
1528        self.assertMatches(
1529            '...<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">...',
1530            self.browser.contents)
1531        # CA can view the student ...
1532        self.browser.open(self.student_path)
1533        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1534        self.assertEqual(self.browser.url, self.student_path)
1535        # ... but not other students
1536        other_student = Student()
1537        other_student.firstname = u'Dep2'
1538        other_student.lastname = u'Student'
1539        self.app['students'].addStudent(other_student)
1540        other_student_path = (
1541            'http://localhost/app/students/%s' % other_student.student_id)
1542        self.assertRaises(
1543            Unauthorized, self.browser.open, other_student_path)
1544        # We add study level 110 to the student's studycourse
1545        studylevel = StudentStudyLevel()
1546        studylevel.level = 110
1547        self.student['studycourse'].addStudentStudyLevel(
1548            cert,studylevel)
1549        L110_student_path = self.studycourse_path + '/110'
1550        # The CA can neither see the Validate nor the Edit button
1551        self.browser.open(L110_student_path)
1552        self.assertFalse('Validate courses' in self.browser.contents)
1553        self.assertFalse('Edit' in self.browser.contents)
1554        IWorkflowInfo(self.student).fireTransition('register_courses')
1555        self.browser.open(L110_student_path)
1556        self.assertFalse('Validate courses' in self.browser.contents)
1557        self.assertFalse('Edit' in self.browser.contents)
1558        # Only in state courses registered and only if the current level
1559        # corresponds with the name of the study level object
1560        # the 100L CA does see the 'Validate' button but not
1561        # the edit button
1562        self.student['studycourse'].current_level = 110
1563        self.browser.open(L110_student_path)
1564        self.assertFalse('Edit' in self.browser.contents)
1565        self.assertTrue('Validate courses' in self.browser.contents)
1566        # But a 100L CA does not see the button at other levels
1567        studylevel2 = StudentStudyLevel()
1568        studylevel2.level = 200
1569        self.student['studycourse'].addStudentStudyLevel(
1570            cert,studylevel2)
1571        L200_student_path = self.studycourse_path + '/200'
1572        self.browser.open(L200_student_path)
1573        self.assertFalse('Edit' in self.browser.contents)
1574        self.assertFalse('Validate courses' in self.browser.contents)
1575        self.browser.open(L110_student_path)
1576        self.browser.getLink("Validate courses").click()
1577        self.assertTrue('Course list has been validated' in self.browser.contents)
1578        self.assertTrue('courses validated' in self.browser.contents)
1579        self.assertEqual(self.student['studycourse']['110'].validated_by,
1580            'Helen Procter')
1581        self.assertMatches(
1582            '<YYYY-MM-DD hh:mm:ss>',
1583            self.student['studycourse']['110'].validation_date.strftime(
1584                "%Y-%m-%d %H:%M:%S"))
1585        self.browser.getLink("Reject courses").click()
1586        self.assertTrue('Course list request has been annulled.'
1587            in self.browser.contents)
1588        urlmessage = 'Course+list+request+has+been+annulled.'
1589        self.assertEqual(self.browser.url, self.student_path +
1590            '/contactstudent?subject=%s' % urlmessage)
1591        self.assertTrue('school fee paid' in self.browser.contents)
1592        self.assertTrue(self.student['studycourse']['110'].validated_by is None)
1593        self.assertTrue(self.student['studycourse']['110'].validation_date is None)
1594        IWorkflowInfo(self.student).fireTransition('register_courses')
1595        self.browser.open(L110_student_path)
1596        self.browser.getLink("Reject courses").click()
1597        self.assertTrue('Course list request has been rejected'
1598            in self.browser.contents)
1599        self.assertTrue('school fee paid' in self.browser.contents)
1600        # CA does now see the contact form and can send a message
1601        self.browser.getControl(name="form.subject").value = 'Important subject'
1602        self.browser.getControl(name="form.body").value = 'Course list rejected'
1603        self.browser.getControl("Send message now").click()
1604        self.assertTrue('Your message has been sent' in self.browser.contents)
1605        # The CA does now see the Edit button and can edit
1606        # current study level
1607        self.browser.open(L110_student_path)
1608        self.browser.getLink("Edit").click()
1609        self.assertTrue('Edit course list of 100 (Year 1) on 1st probation'
1610            in self.browser.contents)
1611        # The CA can't validate courses if not in state
1612        # courses registered
1613        self.browser.open(L110_student_path + '/validate_courses')
1614        self.assertTrue('Student is in the wrong state'
1615            in self.browser.contents)
1616        # The CA can go to his certificate through the my_roles page
1617        self.browser.open('http://localhost/app/users/mrsadvise/my_roles')
1618        self.browser.getLink(
1619            "http://localhost/app/faculties/fac1/dep1/certificates/CERT1").click()
1620        # and view the list of students
1621        self.browser.getLink("Show students").click()
1622        self.browser.getControl(name="session").value = ['2004']
1623        self.browser.getControl(name="level").value = ['100']
1624        self.browser.getControl("Show").click()
1625        self.assertTrue(self.student_id in self.browser.contents)
1626
1627    def test_handle_courses_by_lecturer(self):
1628        # Create course lecturer
1629        self.app['users'].addUser('mrslecturer', 'mrslecturersecret')
1630        self.app['users']['mrslecturer'].email = 'mrslecturer@foo.ng'
1631        self.app['users']['mrslecturer'].title = u'Mercedes Benz'
1632        # Assign local Lecturer role for a certificate
1633        course = self.app['faculties']['fac1']['dep1'].courses['COURSE1']
1634        prmlocal = IPrincipalRoleManager(course)
1635        prmlocal.assignRoleToPrincipal('waeup.local.Lecturer', 'mrslecturer')
1636        # Login as lecturer
1637        self.browser.open(self.login_path)
1638        self.browser.getControl(name="form.login").value = 'mrslecturer'
1639        self.browser.getControl(name="form.password").value = 'mrslecturersecret'
1640        self.browser.getControl("Login").click()
1641        self.assertMatches('...You logged in...', self.browser.contents)
1642        # CO can see her roles
1643        self.browser.getLink("My Roles").click()
1644        self.assertMatches(
1645            '...<div>Academics Officer (view only)</div>...',
1646            self.browser.contents)
1647        # But not her local role ...
1648        self.assertFalse('Lecturer' in self.browser.contents)
1649        # ... because we forgot to notify the course that the local role
1650        # has changed
1651        notify(LocalRoleSetEvent(
1652            course, 'waeup.local.Lecturer', 'mrslecturer', granted=True))
1653        self.browser.open('http://localhost/app/users/mrslecturer/my_roles')
1654        self.assertTrue('Lecturer' in self.browser.contents)
1655        self.assertMatches(
1656            '...<a href="http://localhost/app/faculties/fac1/dep1/courses/COURSE1">...',
1657            self.browser.contents)
1658        # The lecturer can go to her course
1659        self.browser.getLink(
1660            "http://localhost/app/faculties/fac1/dep1/courses/COURSE1").click()
1661        # and view the list of students
1662        self.browser.getLink("Show students").click()
1663        self.browser.getControl(name="session").value = ['2004']
1664        self.browser.getControl(name="level").value = ['100']
1665        self.browser.getControl("Show").click()
1666        self.assertTrue('No student found.' in self.browser.contents)
1667        # No student in course so far
1668        self.assertFalse(self.student_id in self.browser.contents)
1669        studylevel = createObject(u'waeup.StudentStudyLevel')
1670        studylevel.level = 100
1671        studylevel.level_session = 2004
1672        self.student['studycourse'].addStudentStudyLevel(
1673            self.certificate, studylevel)
1674        # Now the student has registered the course and can
1675        # be seen by the lecturer.
1676        self.browser.open("http://localhost/app/faculties/fac1/dep1/courses/COURSE1/students")
1677        self.browser.getControl(name="session").value = ['2004']
1678        self.browser.getControl(name="level").value = ['100']
1679        self.browser.getControl("Show").click()
1680        self.assertTrue(self.student_id in self.browser.contents)
1681        # The course ticket can be linked with the course.
1682        self.assertEqual(
1683            self.student['studycourse']['100']['COURSE1'].course,
1684            self.course)
1685        # Lecturer can neither access ths student ...
1686        self.assertRaises(
1687            Unauthorized, self.browser.open, self.student_path)
1688        # ... nor the respective course ticket since a
1689        # editing course tickets by lecturers is not feasible.
1690        # Course results must be imported.
1691        self.assertTrue('COURSE1' in self.student['studycourse']['100'].keys())
1692        course_ticket_path = self.student_path + '/studycourse/100/COURSE1'
1693        self.assertRaises(
1694            Unauthorized, self.browser.open, course_ticket_path)
1695
1696    def test_change_current_mode(self):
1697        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1698        self.browser.open(self.clearance_path)
1699        self.assertFalse('Employer' in self.browser.contents)
1700        self.browser.open(self.manage_clearance_path)
1701        self.assertFalse('Employer' in self.browser.contents)
1702        self.student.clearance_locked = False
1703        self.browser.open(self.edit_clearance_path)
1704        self.assertFalse('Employer' in self.browser.contents)
1705        # Now we change the study mode of the certificate and a different
1706        # interface is used by clearance views.
1707        self.certificate.study_mode = 'pg_ft'
1708        # Invariants are not being checked here?!
1709        self.certificate.end_level = 100
1710        self.browser.open(self.clearance_path)
1711        self.assertTrue('Employer' in self.browser.contents)
1712        self.browser.open(self.manage_clearance_path)
1713        self.assertTrue('Employer' in self.browser.contents)
1714        self.browser.open(self.edit_clearance_path)
1715        self.assertTrue('Employer' in self.browser.contents)
1716
1717    def test_activate_deactivate_buttons(self):
1718        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1719        self.browser.open(self.student_path)
1720        self.browser.getLink("Deactivate").click()
1721        self.assertTrue(
1722            'Student account has been deactivated.' in self.browser.contents)
1723        self.assertTrue(
1724            'Base Data (account deactivated)' in self.browser.contents)
1725        self.assertTrue(self.student.suspended)
1726        self.browser.getLink("Activate").click()
1727        self.assertTrue(
1728            'Student account has been activated.' in self.browser.contents)
1729        self.assertFalse(
1730            'Base Data (account deactivated)' in self.browser.contents)
1731        self.assertFalse(self.student.suspended)
1732        # History messages have been added ...
1733        self.browser.getLink("History").click()
1734        self.assertTrue(
1735            'Student account deactivated by Manager<br />' in self.browser.contents)
1736        self.assertTrue(
1737            'Student account activated by Manager<br />' in self.browser.contents)
1738        # ... and actions have been logged.
1739        logfile = os.path.join(
1740            self.app['datacenter'].storage, 'logs', 'students.log')
1741        logcontent = open(logfile).read()
1742        self.assertTrue('zope.mgr - students.browser.StudentDeactivatePage - '
1743                        'K1000000 - account deactivated' in logcontent)
1744        self.assertTrue('zope.mgr - students.browser.StudentActivatePage - '
1745                        'K1000000 - account activated' in logcontent)
1746
1747    def test_manage_student_transfer(self):
1748        # Add second certificate
1749        self.certificate2 = createObject('waeup.Certificate')
1750        self.certificate2.code = u'CERT2'
1751        self.certificate2.study_mode = 'ug_ft'
1752        self.certificate2.start_level = 999
1753        self.certificate2.end_level = 999
1754        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
1755            self.certificate2)
1756
1757        # Add study level to old study course
1758        studylevel = createObject(u'waeup.StudentStudyLevel')
1759        studylevel.level = 200
1760        self.student['studycourse'].addStudentStudyLevel(
1761            self.certificate, studylevel)
1762        studylevel = createObject(u'waeup.StudentStudyLevel')
1763        studylevel.level = 999
1764        self.student['studycourse'].addStudentStudyLevel(
1765            self.certificate, studylevel)
1766
1767        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1768        self.browser.open(self.student_path)
1769        self.browser.getLink("Transfer").click()
1770        self.browser.getControl(name="form.certificate").value = ['CERT2']
1771        self.browser.getControl(name="form.current_session").value = ['2011']
1772        self.browser.getControl(name="form.current_level").value = ['200']
1773        self.browser.getControl("Transfer").click()
1774        self.assertTrue(
1775            'Current level does not match certificate levels'
1776            in self.browser.contents)
1777        self.browser.getControl(name="form.current_level").value = ['999']
1778        self.browser.getControl("Transfer").click()
1779        self.assertTrue('Successfully transferred' in self.browser.contents)
1780        # The catalog has been updated
1781        cat = queryUtility(ICatalog, name='students_catalog')
1782        results = list(
1783            cat.searchResults(
1784            certcode=('CERT2', 'CERT2')))
1785        self.assertTrue(results[0] is self.student)
1786        results = list(
1787            cat.searchResults(
1788            current_session=(2011, 2011)))
1789        self.assertTrue(results[0] is self.student)
1790        # Add study level to new study course
1791        studylevel = createObject(u'waeup.StudentStudyLevel')
1792        studylevel.level = 999
1793        self.student['studycourse'].addStudentStudyLevel(
1794            self.certificate, studylevel)
1795
1796        # Edit and add pages are locked for old study courses
1797        self.browser.open(self.student_path + '/studycourse/manage')
1798        self.assertFalse('The requested form is locked' in self.browser.contents)
1799        self.browser.open(self.student_path + '/studycourse_1/manage')
1800        self.assertTrue('The requested form is locked' in self.browser.contents)
1801
1802        self.browser.open(self.student_path + '/studycourse/start_session')
1803        self.assertFalse('The requested form is locked' in self.browser.contents)
1804        self.browser.open(self.student_path + '/studycourse_1/start_session')
1805        self.assertTrue('The requested form is locked' in self.browser.contents)
1806
1807        IWorkflowState(self.student).setState('school fee paid')
1808        self.browser.open(self.student_path + '/studycourse/add')
1809        self.assertFalse('The requested form is locked' in self.browser.contents)
1810        self.browser.open(self.student_path + '/studycourse_1/add')
1811        self.assertTrue('The requested form is locked' in self.browser.contents)
1812
1813        self.browser.open(self.student_path + '/studycourse/999/manage')
1814        self.assertFalse('The requested form is locked' in self.browser.contents)
1815        self.browser.open(self.student_path + '/studycourse_1/999/manage')
1816        self.assertTrue('The requested form is locked' in self.browser.contents)
1817
1818        self.browser.open(self.student_path + '/studycourse/999/validate_courses')
1819        self.assertFalse('The requested form is locked' in self.browser.contents)
1820        self.browser.open(self.student_path + '/studycourse_1/999/validate_courses')
1821        self.assertTrue('The requested form is locked' in self.browser.contents)
1822
1823        self.browser.open(self.student_path + '/studycourse/999/reject_courses')
1824        self.assertFalse('The requested form is locked' in self.browser.contents)
1825        self.browser.open(self.student_path + '/studycourse_1/999/reject_courses')
1826        self.assertTrue('The requested form is locked' in self.browser.contents)
1827
1828        self.browser.open(self.student_path + '/studycourse/999/add')
1829        self.assertFalse('The requested form is locked' in self.browser.contents)
1830        self.browser.open(self.student_path + '/studycourse_1/999/add')
1831        self.assertTrue('The requested form is locked' in self.browser.contents)
1832
1833        self.browser.open(self.student_path + '/studycourse/999/edit')
1834        self.assertFalse('The requested form is locked' in self.browser.contents)
1835        self.browser.open(self.student_path + '/studycourse_1/999/edit')
1836        self.assertTrue('The requested form is locked' in self.browser.contents)
1837
1838        # Revert transfer
1839        self.browser.open(self.student_path + '/studycourse_1')
1840        self.browser.getLink("Reactivate").click()
1841        self.browser.getControl("Revert now").click()
1842        self.assertTrue('Previous transfer reverted' in self.browser.contents)
1843        results = list(
1844            cat.searchResults(
1845            certcode=('CERT1', 'CERT1')))
1846        self.assertTrue(results[0] is self.student)
1847        self.assertEqual([i for i in self.student.keys()],
1848            [u'accommodation', u'payments', u'studycourse'])
1849
1850    def test_login_as_student(self):
1851        # StudentImpersonators can login as student
1852        # Create clearance officer
1853        self.app['users'].addUser('mrofficer', 'mrofficersecret')
1854        self.app['users']['mrofficer'].email = 'mrofficer@foo.ng'
1855        self.app['users']['mrofficer'].title = 'Harry Actor'
1856        prmglobal = IPrincipalRoleManager(self.app)
1857        prmglobal.assignRoleToPrincipal('waeup.StudentImpersonator', 'mrofficer')
1858        prmglobal.assignRoleToPrincipal('waeup.StudentsManager', 'mrofficer')
1859        # Login as student impersonator
1860        self.browser.open(self.login_path)
1861        self.browser.getControl(name="form.login").value = 'mrofficer'
1862        self.browser.getControl(name="form.password").value = 'mrofficersecret'
1863        self.browser.getControl("Login").click()
1864        self.assertMatches('...You logged in...', self.browser.contents)
1865        self.browser.open(self.student_path)
1866        self.browser.getLink("Login as").click()
1867        self.browser.getControl("Set password now").click()
1868        temp_password = self.browser.getControl(name='form.password').value
1869        self.browser.getControl("Login now").click()
1870        self.assertMatches(
1871            '...You successfully logged in as...', self.browser.contents)
1872        # We are logged in as student and can see the 'My Data' tab
1873        self.assertMatches(
1874            '...<a href="#" class="dropdown-toggle">My Data</a>...',
1875            self.browser.contents)
1876        self.browser.getLink("Logout").click()
1877        # The student can't login with the original password ...
1878        self.browser.open(self.login_path)
1879        self.browser.getControl(name="form.login").value = self.student_id
1880        self.browser.getControl(name="form.password").value = 'spwd'
1881        self.browser.getControl("Login").click()
1882        self.assertMatches(
1883            '...Your account has been temporarily deactivated...',
1884            self.browser.contents)
1885        # ... but with the temporary password
1886        self.browser.open(self.login_path)
1887        self.browser.getControl(name="form.login").value = self.student_id
1888        self.browser.getControl(name="form.password").value = temp_password
1889        self.browser.getControl("Login").click()
1890        self.assertMatches('...You logged in...', self.browser.contents)
1891        # Creation of temp_password is properly logged
1892        logfile = os.path.join(
1893            self.app['datacenter'].storage, 'logs', 'students.log')
1894        logcontent = open(logfile).read()
1895        self.assertTrue(
1896            'mrofficer - students.browser.LoginAsStudentStep1 - K1000000 - '
1897            'temp_password generated: %s' % temp_password in logcontent)
1898
1899    def test_transcripts(self):
1900        studylevel = createObject(u'waeup.StudentStudyLevel')
1901        studylevel.level = 100
1902        studylevel.level_session = 2005
1903        self.student['studycourse'].entry_mode = 'ug_ft'
1904        self.student['studycourse'].addStudentStudyLevel(
1905            self.certificate, studylevel)
1906        studylevel2 = createObject(u'waeup.StudentStudyLevel')
1907        studylevel2.level = 110
1908        studylevel2.level_session = 2006
1909        self.student['studycourse'].addStudentStudyLevel(
1910            self.certificate, studylevel2)
1911        # Add second course (COURSE has been added automatically)
1912        courseticket = createObject('waeup.CourseTicket')
1913        courseticket.code = 'ANYCODE'
1914        courseticket.title = u'Any TITLE'
1915        courseticket.credits = 13
1916        courseticket.score = 66
1917        courseticket.semester = 1
1918        courseticket.dcode = u'ANYDCODE'
1919        courseticket.fcode = u'ANYFCODE'
1920        self.student['studycourse']['110']['COURSE2'] = courseticket
1921        self.student['studycourse']['100']['COURSE1'].score = 55
1922        self.assertEqual(self.student['studycourse']['100'].gpa_params_rectified[0], 3.0)
1923        self.assertEqual(self.student['studycourse']['110'].gpa_params_rectified[0], 4.0)
1924        # Get transcript data
1925        td = self.student['studycourse'].getTranscriptData()
1926        self.assertEqual(td[0][0]['level_key'], '100')
1927        self.assertEqual(td[0][0]['sgpa'], 3.0)
1928        self.assertEqual(td[0][0]['level'].level, 100)
1929        self.assertEqual(td[0][0]['level'].level_session, 2005)
1930        self.assertEqual(td[0][0]['tickets_1'][0].code, 'COURSE1')
1931        self.assertEqual(td[0][1]['level_key'], '110')
1932        self.assertEqual(td[0][1]['sgpa'], 4.0)
1933        self.assertEqual(td[0][1]['level'].level, 110)
1934        self.assertEqual(td[0][1]['level'].level_session, 2006)
1935        self.assertEqual(td[0][1]['tickets_1'][0].code, 'ANYCODE')
1936        self.assertEqual(td[1], 3.57)
1937        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1938        self.browser.open(self.student_path + '/studycourse/transcript')
1939        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1940        self.assertTrue('Transcript' in self.browser.contents)
1941        # Officers can open the pdf transcript
1942        self.browser.open(self.student_path + '/studycourse/transcript.pdf')
1943        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1944        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1945        path = os.path.join(samples_dir(), 'transcript.pdf')
1946        open(path, 'wb').write(self.browser.contents)
1947        print "Sample PDF transcript.pdf written to %s" % path
1948
1949    def test_process_transcript_request(self):
1950        IWorkflowState(self.student).setState('transcript requested')
1951        notify(grok.ObjectModifiedEvent(self.student))
1952        self.student.transcript_comment = (
1953            u'On 07/08/2013 08:59:54 UTC K1000000 wrote:\n\nComment line 1 \n'
1954            'Comment line2\n\nDispatch Address:\nAddress line 1 \n'
1955            'Address line2\n\n')
1956        # Create transcript officer
1957        self.app['users'].addUser('mrtranscript', 'mrtranscriptsecret')
1958        self.app['users']['mrtranscript'].email = 'mrtranscript@foo.ng'
1959        self.app['users']['mrtranscript'].title = 'Ruth Gordon'
1960        prmglobal = IPrincipalRoleManager(self.app)
1961        prmglobal.assignRoleToPrincipal('waeup.TranscriptOfficer', 'mrtranscript')
1962        # Login as transcript officer
1963        self.browser.open(self.login_path)
1964        self.browser.getControl(name="form.login").value = 'mrtranscript'
1965        self.browser.getControl(name="form.password").value = 'mrtranscriptsecret'
1966        self.browser.getControl("Login").click()
1967        self.assertMatches('...You logged in...', self.browser.contents)
1968        # Officer can see his roles
1969        self.browser.getLink("My Roles").click()
1970        self.assertMatches(
1971            '...<div>Transcript Officer</div>...',
1972            self.browser.contents)
1973        # Officer can search for students in state 'transcripr requested'
1974        self.browser.open(self.container_path)
1975        self.browser.getControl(name="searchtype").value = ['transcript']
1976        self.browser.getControl("Search").click()
1977        self.assertTrue('Anna Tester' in self.browser.contents)
1978        self.browser.getLink("K1000000").click()
1979        self.browser.getLink("Manage transcript request").click()
1980        self.assertTrue(' UTC K1000000 wrote:<br><br>Comment line 1 <br>'
1981        'Comment line2<br><br>Dispatch Address:<br>Address line 1 <br>'
1982        'Address line2<br><br></p>' in self.browser.contents)
1983        self.browser.getControl(name="comment").value = (
1984            'Hello,\nYour transcript has been sent to the address provided.')
1985        self.browser.getControl("Save comment and mark as processed").click()
1986        self.assertTrue(
1987            'UTC mrtranscript wrote:\n\nHello,\nYour transcript has '
1988            'been sent to the address provided.\n\n'
1989            in self.student.transcript_comment)
1990        # The comment has been logged
1991        logfile = os.path.join(
1992            self.app['datacenter'].storage, 'logs', 'students.log')
1993        logcontent = open(logfile).read()
1994        self.assertTrue(
1995            'mrtranscript - students.browser.StudentTranscriptRequestProcessFormPage - '
1996            'K1000000 - comment: Hello,<br>'
1997            'Your transcript has been sent to the address provided'
1998            in logcontent)
1999
2000class StudentUITests(StudentsFullSetup):
2001    # Tests for Student class views and pages
2002
2003    def test_student_change_password(self):
2004        # Students can change the password
2005        self.student.personal_updated = datetime.utcnow()
2006        self.browser.open(self.login_path)
2007        self.browser.getControl(name="form.login").value = self.student_id
2008        self.browser.getControl(name="form.password").value = 'spwd'
2009        self.browser.getControl("Login").click()
2010        self.assertEqual(self.browser.url, self.student_path)
2011        self.assertTrue('You logged in' in self.browser.contents)
2012        # Change password
2013        self.browser.getLink("Change password").click()
2014        self.browser.getControl(name="change_password").value = 'pw'
2015        self.browser.getControl(
2016            name="change_password_repeat").value = 'pw'
2017        self.browser.getControl("Save").click()
2018        self.assertTrue('Password must have at least' in self.browser.contents)
2019        self.browser.getControl(name="change_password").value = 'new_password'
2020        self.browser.getControl(
2021            name="change_password_repeat").value = 'new_passssword'
2022        self.browser.getControl("Save").click()
2023        self.assertTrue('Passwords do not match' in self.browser.contents)
2024        self.browser.getControl(name="change_password").value = 'new_password'
2025        self.browser.getControl(
2026            name="change_password_repeat").value = 'new_password'
2027        self.browser.getControl("Save").click()
2028        self.assertTrue('Password changed' in self.browser.contents)
2029        # We are still logged in. Changing the password hasn't thrown us out.
2030        self.browser.getLink("Base Data").click()
2031        self.assertEqual(self.browser.url, self.student_path)
2032        # We can logout
2033        self.browser.getLink("Logout").click()
2034        self.assertTrue('You have been logged out' in self.browser.contents)
2035        self.assertEqual(self.browser.url, 'http://localhost/app')
2036        # We can login again with the new password
2037        self.browser.getLink("Login").click()
2038        self.browser.open(self.login_path)
2039        self.browser.getControl(name="form.login").value = self.student_id
2040        self.browser.getControl(name="form.password").value = 'new_password'
2041        self.browser.getControl("Login").click()
2042        self.assertEqual(self.browser.url, self.student_path)
2043        self.assertTrue('You logged in' in self.browser.contents)
2044        return
2045
2046    def test_setpassword(self):
2047        # Set password for first-time access
2048        student = Student()
2049        student.reg_number = u'123456'
2050        student.firstname = u'Klaus'
2051        student.lastname = u'Tester'
2052        self.app['students'].addStudent(student)
2053        setpassword_path = 'http://localhost/app/setpassword'
2054        student_path = 'http://localhost/app/students/%s' % student.student_id
2055        self.browser.open(setpassword_path)
2056        self.browser.getControl(name="ac_series").value = self.existing_pwdseries
2057        self.browser.getControl(name="ac_number").value = self.existing_pwdnumber
2058        self.browser.getControl(name="reg_number").value = '223456'
2059        self.browser.getControl("Set").click()
2060        self.assertMatches('...No student found...',
2061                           self.browser.contents)
2062        self.browser.getControl(name="reg_number").value = '123456'
2063        self.browser.getControl(name="ac_number").value = '999999'
2064        self.browser.getControl("Set").click()
2065        self.assertMatches('...Access code is invalid...',
2066                           self.browser.contents)
2067        self.browser.getControl(name="ac_number").value = self.existing_pwdnumber
2068        self.browser.getControl("Set").click()
2069        self.assertMatches('...Password has been set. Your Student Id is...',
2070                           self.browser.contents)
2071        self.browser.getControl("Set").click()
2072        self.assertMatches(
2073            '...Password has already been set. Your Student Id is...',
2074            self.browser.contents)
2075        existing_pwdpin = self.pwdpins[1]
2076        parts = existing_pwdpin.split('-')[1:]
2077        existing_pwdseries, existing_pwdnumber = parts
2078        self.browser.getControl(name="ac_series").value = existing_pwdseries
2079        self.browser.getControl(name="ac_number").value = existing_pwdnumber
2080        self.browser.getControl(name="reg_number").value = '123456'
2081        self.browser.getControl("Set").click()
2082        self.assertMatches(
2083            '...You are using the wrong Access Code...',
2084            self.browser.contents)
2085        # The student can login with the new credentials
2086        self.browser.open(self.login_path)
2087        self.browser.getControl(name="form.login").value = student.student_id
2088        self.browser.getControl(
2089            name="form.password").value = self.existing_pwdnumber
2090        self.browser.getControl("Login").click()
2091        self.assertEqual(self.browser.url, student_path)
2092        self.assertTrue('You logged in' in self.browser.contents)
2093        return
2094
2095    def test_student_login(self):
2096        # Student cant login if their password is not set
2097        self.student.password = None
2098        self.browser.open(self.login_path)
2099        self.browser.getControl(name="form.login").value = self.student_id
2100        self.browser.getControl(name="form.password").value = 'spwd'
2101        self.browser.getControl("Login").click()
2102        self.assertTrue(
2103            'You entered invalid credentials.' in self.browser.contents)
2104        # We set the password again
2105        IUserAccount(
2106            self.app['students'][self.student_id]).setPassword('spwd')
2107        # Students can't login if their account is suspended/deactivated
2108        self.student.suspended = True
2109        self.browser.open(self.login_path)
2110        self.browser.getControl(name="form.login").value = self.student_id
2111        self.browser.getControl(name="form.password").value = 'spwd'
2112        self.browser.getControl("Login").click()
2113        self.assertMatches(
2114            '...<div class="alert-message warning">'
2115            'Your account has been deactivated.</div>...', self.browser.contents)
2116        # If suspended_comment is set this message will be flashed instead
2117        self.student.suspended_comment = u'Aetsch baetsch!'
2118        self.browser.getControl(name="form.login").value = self.student_id
2119        self.browser.getControl(name="form.password").value = 'spwd'
2120        self.browser.getControl("Login").click()
2121        self.assertMatches(
2122            '...<div class="alert-message warning">Aetsch baetsch!</div>...',
2123            self.browser.contents)
2124        self.student.suspended = False
2125        # Students can't login if a temporary password has been set and
2126        # is not expired
2127        self.app['students'][self.student_id].setTempPassword(
2128            'anybody', 'temp_spwd')
2129        self.browser.open(self.login_path)
2130        self.browser.getControl(name="form.login").value = self.student_id
2131        self.browser.getControl(name="form.password").value = 'spwd'
2132        self.browser.getControl("Login").click()
2133        self.assertMatches(
2134            '...Your account has been temporarily deactivated...',
2135            self.browser.contents)
2136        # The student can login with the temporary password
2137        self.browser.open(self.login_path)
2138        self.browser.getControl(name="form.login").value = self.student_id
2139        self.browser.getControl(name="form.password").value = 'temp_spwd'
2140        self.browser.getControl("Login").click()
2141        self.assertMatches(
2142            '...You logged in...', self.browser.contents)
2143        # Student can view the base data
2144        self.browser.open(self.student_path)
2145        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2146        self.assertEqual(self.browser.url, self.student_path)
2147        # When the password expires ...
2148        delta = timedelta(minutes=11)
2149        self.app['students'][self.student_id].temp_password[
2150            'timestamp'] = datetime.utcnow() - delta
2151        self.app['students'][self.student_id]._p_changed = True
2152        # ... the student will be automatically logged out
2153        self.assertRaises(
2154            Unauthorized, self.browser.open, self.student_path)
2155        # Then the student can login with the original password
2156        self.browser.open(self.login_path)
2157        self.browser.getControl(name="form.login").value = self.student_id
2158        self.browser.getControl(name="form.password").value = 'spwd'
2159        self.browser.getControl("Login").click()
2160        self.assertMatches(
2161            '...You logged in...', self.browser.contents)
2162
2163    def test_student_clearance(self):
2164        # Student cant login if their password is not set
2165        IWorkflowInfo(self.student).fireTransition('admit')
2166        self.browser.open(self.login_path)
2167        self.browser.getControl(name="form.login").value = self.student_id
2168        self.browser.getControl(name="form.password").value = 'spwd'
2169        self.browser.getControl("Login").click()
2170        self.assertMatches(
2171            '...You logged in...', self.browser.contents)
2172        # Admitted student can upload a passport picture
2173        self.browser.open(self.student_path + '/change_portrait')
2174        ctrl = self.browser.getControl(name='passportuploadedit')
2175        file_obj = open(SAMPLE_IMAGE, 'rb')
2176        file_ctrl = ctrl.mech_control
2177        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
2178        self.browser.getControl(
2179            name='upload_passportuploadedit').click()
2180        self.assertTrue(
2181            '<img align="middle" height="125px" src="passport.jpg" />'
2182            in self.browser.contents)
2183        # Students can open admission letter
2184        self.browser.getLink("Base Data").click()
2185        self.browser.getLink("Download admission letter").click()
2186        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2187        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2188        # Student can view the clearance data
2189        self.browser.open(self.student_path)
2190        self.browser.getLink("Clearance Data").click()
2191        # Student can't open clearance edit form before starting clearance
2192        self.browser.open(self.student_path + '/cedit')
2193        self.assertMatches('...The requested form is locked...',
2194                           self.browser.contents)
2195        self.browser.getLink("Clearance Data").click()
2196        self.browser.getLink("Start clearance").click()
2197        self.student.email = None
2198        # Uups, we forgot to fill the email fields
2199        self.browser.getControl("Start clearance").click()
2200        self.assertMatches('...Not all required fields filled...',
2201                           self.browser.contents)
2202        self.browser.open(self.student_path + '/edit_base')
2203        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
2204        self.browser.getControl("Save").click()
2205        self.browser.open(self.student_path + '/start_clearance')
2206        self.browser.getControl(name="ac_series").value = '3'
2207        self.browser.getControl(name="ac_number").value = '4444444'
2208        self.browser.getControl("Start clearance now").click()
2209        self.assertMatches('...Activation code is invalid...',
2210                           self.browser.contents)
2211        self.browser.getControl(name="ac_series").value = self.existing_clrseries
2212        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
2213        # Owner is Hans Wurst, AC can't be invalidated
2214        self.browser.getControl("Start clearance now").click()
2215        self.assertMatches('...You are not the owner of this access code...',
2216                           self.browser.contents)
2217        # Set the correct owner
2218        self.existing_clrac.owner = self.student_id
2219        # clr_code might be set (and thus returns None) due importing
2220        # an empty clr_code column.
2221        self.student.clr_code = None
2222        self.browser.getControl("Start clearance now").click()
2223        self.assertMatches('...Clearance process has been started...',
2224                           self.browser.contents)
2225        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
2226        self.browser.getControl("Save", index=0).click()
2227        # Student can view the clearance data
2228        self.browser.getLink("Clearance Data").click()
2229        # and go back to the edit form
2230        self.browser.getLink("Edit").click()
2231        # Students can upload documents
2232        ctrl = self.browser.getControl(name='birthcertificateupload')
2233        file_obj = open(SAMPLE_IMAGE, 'rb')
2234        file_ctrl = ctrl.mech_control
2235        file_ctrl.add_file(file_obj, filename='my_birth_certificate.jpg')
2236        self.browser.getControl(
2237            name='upload_birthcertificateupload').click()
2238        self.assertTrue(
2239            '<a target="image" href="birth_certificate">Birth Certificate Scan</a>'
2240            in self.browser.contents)
2241        # Students can open clearance slip
2242        self.browser.getLink("View").click()
2243        self.browser.getLink("Download clearance slip").click()
2244        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2245        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2246        # Students can request clearance
2247        self.browser.open(self.edit_clearance_path)
2248        self.browser.getControl("Save and request clearance").click()
2249        self.browser.getControl(name="ac_series").value = self.existing_clrseries
2250        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
2251        self.browser.getControl("Request clearance now").click()
2252        self.assertMatches('...Clearance has been requested...',
2253                           self.browser.contents)
2254        # Student can't reopen clearance form after requesting clearance
2255        self.browser.open(self.student_path + '/cedit')
2256        self.assertMatches('...The requested form is locked...',
2257                           self.browser.contents)
2258
2259    def test_student_course_registration(self):
2260        # Student cant login if their password is not set
2261        IWorkflowInfo(self.student).fireTransition('admit')
2262        self.browser.open(self.login_path)
2263        self.browser.getControl(name="form.login").value = self.student_id
2264        self.browser.getControl(name="form.password").value = 'spwd'
2265        self.browser.getControl("Login").click()
2266        # Student can't add study level if not in state 'school fee paid'
2267        self.browser.open(self.student_path + '/studycourse/add')
2268        self.assertMatches('...The requested form is locked...',
2269                           self.browser.contents)
2270        # ... and must be transferred first
2271        IWorkflowState(self.student).setState('school fee paid')
2272        # Now students can add the current study level
2273        self.browser.getLink("Study Course").click()
2274        self.browser.getLink("Add course list").click()
2275        self.assertMatches('...Add current level 100 (Year 1)...',
2276                           self.browser.contents)
2277        self.browser.getControl("Create course list now").click()
2278        # A level with one course ticket was created
2279        self.assertEqual(self.student['studycourse']['100'].number_of_tickets, 1)
2280        self.browser.getLink("100").click()
2281        self.browser.getLink("Edit course list").click()
2282        self.browser.getLink("here").click()
2283        self.browser.getControl(name="form.course").value = ['COURSE1']
2284        self.browser.getControl("Add course ticket").click()
2285        self.assertMatches('...The ticket exists...',
2286                           self.browser.contents)
2287        self.student['studycourse'].current_level = 200
2288        self.browser.getLink("Study Course").click()
2289        self.browser.getLink("Add course list").click()
2290        self.assertMatches('...Add current level 200 (Year 2)...',
2291                           self.browser.contents)
2292        self.browser.getControl("Create course list now").click()
2293        self.browser.getLink("200").click()
2294        self.browser.getLink("Edit course list").click()
2295        self.browser.getLink("here").click()
2296        self.browser.getControl(name="form.course").value = ['COURSE1']
2297        self.course.credits = 100
2298        self.browser.getControl("Add course ticket").click()
2299        self.assertMatches(
2300            '...Total credits exceed 50...', self.browser.contents)
2301        self.course.credits = 10
2302        self.browser.getControl("Add course ticket").click()
2303        self.assertMatches('...The ticket exists...',
2304                           self.browser.contents)
2305        # Indeed the ticket exists as carry-over course from level 100
2306        # since its score was 0
2307        self.assertTrue(
2308            self.student['studycourse']['200']['COURSE1'].carry_over is True)
2309        # Students can open the pdf course registration slip
2310        self.browser.open(self.student_path + '/studycourse/200')
2311        self.browser.getLink("Download course registration slip").click()
2312        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2313        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2314        # Students can remove course tickets
2315        self.browser.open(self.student_path + '/studycourse/200/edit')
2316        self.browser.getControl("Remove selected", index=0).click()
2317        self.assertTrue('No ticket selected' in self.browser.contents)
2318        # No ticket can be selected since the carry-over course is a core course
2319        self.assertRaises(
2320            LookupError, self.browser.getControl, name='val_id')
2321        self.student['studycourse']['200']['COURSE1'].mandatory = False
2322        self.browser.open(self.student_path + '/studycourse/200/edit')
2323        # Course list can't be registered if total_credits exceeds max_credits
2324        self.student['studycourse']['200']['COURSE1'].credits = 60
2325        self.browser.getControl("Register course list").click()
2326        self.assertTrue('Maximum credits of 50 exceeded' in self.browser.contents)
2327        # Student can now remove the ticket
2328        ctrl = self.browser.getControl(name='val_id')
2329        ctrl.getControl(value='COURSE1').selected = True
2330        self.browser.getControl("Remove selected", index=0).click()
2331        self.assertTrue('Successfully removed' in self.browser.contents)
2332        # Removing course tickets is properly logged
2333        logfile = os.path.join(
2334            self.app['datacenter'].storage, 'logs', 'students.log')
2335        logcontent = open(logfile).read()
2336        self.assertTrue('K1000000 - students.browser.StudyLevelEditFormPage '
2337        '- K1000000 - removed: COURSE1 at 200' in logcontent)
2338        # They can add the same ticket using the edit page directly.
2339        # We can do the same by adding the course on the manage page directly
2340        self.browser.getControl(name="course").value = 'COURSE1'
2341        self.browser.getControl("Add course ticket").click()
2342        # Adding course tickets is logged
2343        logfile = os.path.join(
2344            self.app['datacenter'].storage, 'logs', 'students.log')
2345        logcontent = open(logfile).read()
2346        self.assertTrue('K1000000 - students.browser.StudyLevelEditFormPage - '
2347            'K1000000 - added: COURSE1|200|2004' in logcontent)
2348        # Course list can be registered
2349        self.browser.getControl("Register course list").click()
2350        self.assertTrue('Course list has been registered' in self.browser.contents)
2351        self.assertEqual(self.student.state, 'courses registered')
2352        # Students can view the transcript
2353        #self.browser.open(self.studycourse_path)
2354        #self.browser.getLink("Transcript").click()
2355        #self.browser.getLink("Academic Transcript").click()
2356        #self.assertEqual(self.browser.headers['Status'], '200 Ok')
2357        #self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2358        return
2359
2360    def test_postgraduate_student_access(self):
2361        self.certificate.study_mode = 'pg_ft'
2362        self.certificate.start_level = 999
2363        self.certificate.end_level = 999
2364        self.student['studycourse'].current_level = 999
2365        IWorkflowState(self.student).setState('school fee paid')
2366        self.browser.open(self.login_path)
2367        self.browser.getControl(name="form.login").value = self.student_id
2368        self.browser.getControl(name="form.password").value = 'spwd'
2369        self.browser.getControl("Login").click()
2370        self.assertTrue(
2371            'You logged in.' in self.browser.contents)
2372        # Now students can add the current study level
2373        self.browser.getLink("Study Course").click()
2374        self.browser.getLink("Add course list").click()
2375        self.assertMatches('...Add current level Postgraduate Level...',
2376                           self.browser.contents)
2377        self.browser.getControl("Create course list now").click()
2378        # A level with one course ticket was created
2379        self.assertEqual(self.student['studycourse']['999'].number_of_tickets, 0)
2380        self.browser.getLink("999").click()
2381        self.browser.getLink("Edit course list").click()
2382        self.browser.getLink("here").click()
2383        self.browser.getControl(name="form.course").value = ['COURSE1']
2384        self.browser.getControl("Add course ticket").click()
2385        self.assertMatches('...Successfully added COURSE1...',
2386                           self.browser.contents)
2387        # Postgraduate students can't register course lists
2388        self.browser.getControl("Register course list").click()
2389        self.assertTrue("your course list can't bee registered"
2390            in self.browser.contents)
2391        self.assertEqual(self.student.state, 'school fee paid')
2392        return
2393
2394    def test_student_clearance_wo_clrcode(self):
2395        IWorkflowState(self.student).setState('clearance started')
2396        self.browser.open(self.login_path)
2397        self.browser.getControl(name="form.login").value = self.student_id
2398        self.browser.getControl(name="form.password").value = 'spwd'
2399        self.browser.getControl("Login").click()
2400        self.student.clearance_locked = False
2401        self.browser.open(self.edit_clearance_path)
2402        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
2403        self.browser.getControl("Save and request clearance").click()
2404        self.assertMatches('...Clearance has been requested...',
2405                           self.browser.contents)
2406
2407    def test_student_clearance_payment(self):
2408        # Login
2409        self.browser.open(self.login_path)
2410        self.browser.getControl(name="form.login").value = self.student_id
2411        self.browser.getControl(name="form.password").value = 'spwd'
2412        self.browser.getControl("Login").click()
2413
2414        # Students can add online clearance payment tickets
2415        self.browser.open(self.payments_path + '/addop')
2416        self.browser.getControl(name="form.p_category").value = ['clearance']
2417        self.browser.getControl("Create ticket").click()
2418        self.assertMatches('...ticket created...',
2419                           self.browser.contents)
2420
2421        # Students can't approve the payment
2422        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
2423        ctrl = self.browser.getControl(name='val_id')
2424        value = ctrl.options[0]
2425        self.browser.getLink(value).click()
2426        payment_url = self.browser.url
2427        self.assertRaises(
2428            Unauthorized, self.browser.open, payment_url + '/approve')
2429        # In the base package they can 'use' a fake approval view.
2430        # XXX: I tried to use
2431        # self.student['payments'][value].approveStudentPayment() instead.
2432        # But this function fails in
2433        # w.k.accesscodes.accesscode.create_accesscode.
2434        # grok.getSite returns None in tests.
2435        self.browser.open(payment_url + '/fake_approve')
2436        self.assertMatches('...Payment approved...',
2437                          self.browser.contents)
2438        expected = '''...
2439        <td>
2440          <span>Paid</span>
2441        </td>...'''
2442        expected = '''...
2443        <td>
2444          <span>Paid</span>
2445        </td>...'''
2446        self.assertMatches(expected,self.browser.contents)
2447        payment_id = self.student['payments'].keys()[0]
2448        payment = self.student['payments'][payment_id]
2449        self.assertEqual(payment.p_state, 'paid')
2450        self.assertEqual(payment.r_amount_approved, 3456.0)
2451        self.assertEqual(payment.r_code, 'AP')
2452        self.assertEqual(payment.r_desc, u'Payment approved by Anna Tester')
2453        # The new CLR-0 pin has been created
2454        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
2455        pin = self.app['accesscodes']['CLR-0'].keys()[0]
2456        ac = self.app['accesscodes']['CLR-0'][pin]
2457        self.assertEqual(ac.owner, self.student_id)
2458        self.assertEqual(ac.cost, 3456.0)
2459
2460        # Students can open the pdf payment slip
2461        self.browser.open(payment_url + '/payment_slip.pdf')
2462        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2463        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2464
2465        # The new CLR-0 pin can be used for starting clearance
2466        # but they have to upload a passport picture first
2467        # which is only possible in state admitted
2468        self.browser.open(self.student_path + '/change_portrait')
2469        self.assertMatches('...form is locked...',
2470                          self.browser.contents)
2471        IWorkflowInfo(self.student).fireTransition('admit')
2472        self.browser.open(self.student_path + '/change_portrait')
2473        image = open(SAMPLE_IMAGE, 'rb')
2474        ctrl = self.browser.getControl(name='passportuploadedit')
2475        file_ctrl = ctrl.mech_control
2476        file_ctrl.add_file(image, filename='my_photo.jpg')
2477        self.browser.getControl(
2478            name='upload_passportuploadedit').click()
2479        self.browser.open(self.student_path + '/start_clearance')
2480        parts = pin.split('-')[1:]
2481        clrseries, clrnumber = parts
2482        self.browser.getControl(name="ac_series").value = clrseries
2483        self.browser.getControl(name="ac_number").value = clrnumber
2484        self.browser.getControl("Start clearance now").click()
2485        self.assertMatches('...Clearance process has been started...',
2486                           self.browser.contents)
2487
2488    def test_student_schoolfee_payment(self):
2489        configuration = createObject('waeup.SessionConfiguration')
2490        configuration.academic_session = 2005
2491        self.app['configuration'].addSessionConfiguration(configuration)
2492        # Login
2493        self.browser.open(self.login_path)
2494        self.browser.getControl(name="form.login").value = self.student_id
2495        self.browser.getControl(name="form.password").value = 'spwd'
2496        self.browser.getControl("Login").click()
2497
2498        # Students can add online school fee payment tickets.
2499        IWorkflowState(self.student).setState('returning')
2500        self.browser.open(self.payments_path)
2501        self.assertRaises(
2502            LookupError, self.browser.getControl, name='val_id')
2503        self.browser.getLink("Add current session payment ticket").click()
2504        self.browser.getControl(name="form.p_category").value = ['schoolfee']
2505        self.browser.getControl("Create ticket").click()
2506        self.assertMatches('...ticket created...',
2507                           self.browser.contents)
2508        ctrl = self.browser.getControl(name='val_id')
2509        value = ctrl.options[0]
2510        self.browser.getLink(value).click()
2511        self.assertMatches('...Amount Authorized...',
2512                           self.browser.contents)
2513        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
2514        # Payment session and will be calculated as defined
2515        # in w.k.students.utils because we set changed the state
2516        # to returning
2517        self.assertEqual(self.student['payments'][value].p_session, 2005)
2518        self.assertEqual(self.student['payments'][value].p_level, 200)
2519
2520        # Student is the payer of the payment ticket.
2521        payer = IPayer(self.student['payments'][value])
2522        self.assertEqual(payer.display_fullname, 'Anna Tester')
2523        self.assertEqual(payer.id, self.student_id)
2524        self.assertEqual(payer.faculty, 'fac1')
2525        self.assertEqual(payer.department, 'dep1')
2526
2527        # We simulate the approval
2528        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
2529        self.browser.open(self.browser.url + '/fake_approve')
2530        self.assertMatches('...Payment approved...',
2531                          self.browser.contents)
2532
2533        # The new SFE-0 pin can be used for starting new session
2534        self.browser.open(self.studycourse_path)
2535        self.browser.getLink('Start new session').click()
2536        pin = self.app['accesscodes']['SFE-0'].keys()[0]
2537        parts = pin.split('-')[1:]
2538        sfeseries, sfenumber = parts
2539        self.browser.getControl(name="ac_series").value = sfeseries
2540        self.browser.getControl(name="ac_number").value = sfenumber
2541        self.browser.getControl("Start now").click()
2542        self.assertMatches('...Session started...',
2543                           self.browser.contents)
2544        self.assertTrue(self.student.state == 'school fee paid')
2545        return
2546
2547    def test_student_bedallocation_payment(self):
2548        # Login
2549        self.browser.open(self.login_path)
2550        self.browser.getControl(name="form.login").value = self.student_id
2551        self.browser.getControl(name="form.password").value = 'spwd'
2552        self.browser.getControl("Login").click()
2553        self.browser.open(self.payments_path)
2554        self.browser.open(self.payments_path + '/addop')
2555        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
2556        self.browser.getControl("Create ticket").click()
2557        self.assertMatches('...ticket created...',
2558                           self.browser.contents)
2559        # Students can remove only online payment tickets which have
2560        # not received a valid callback
2561        self.browser.open(self.payments_path)
2562        ctrl = self.browser.getControl(name='val_id')
2563        value = ctrl.options[0]
2564        ctrl.getControl(value=value).selected = True
2565        self.browser.getControl("Remove selected", index=0).click()
2566        self.assertTrue('Successfully removed' in self.browser.contents)
2567
2568    def test_student_maintenance_payment(self):
2569        # Login
2570        self.browser.open(self.login_path)
2571        self.browser.getControl(name="form.login").value = self.student_id
2572        self.browser.getControl(name="form.password").value = 'spwd'
2573        self.browser.getControl("Login").click()
2574        self.browser.open(self.payments_path)
2575        self.browser.open(self.payments_path + '/addop')
2576        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
2577        self.browser.getControl("Create ticket").click()
2578        self.assertMatches('...You have not yet booked accommodation...',
2579                           self.browser.contents)
2580        # We continue this test in test_student_accommodation
2581
2582    def test_student_previous_payments(self):
2583        configuration = createObject('waeup.SessionConfiguration')
2584        configuration.academic_session = 2000
2585        configuration.clearance_fee = 3456.0
2586        configuration.booking_fee = 123.4
2587        self.app['configuration'].addSessionConfiguration(configuration)
2588        configuration2 = createObject('waeup.SessionConfiguration')
2589        configuration2.academic_session = 2003
2590        configuration2.clearance_fee = 3456.0
2591        configuration2.booking_fee = 123.4
2592        self.app['configuration'].addSessionConfiguration(configuration2)
2593        configuration3 = createObject('waeup.SessionConfiguration')
2594        configuration3.academic_session = 2005
2595        configuration3.clearance_fee = 3456.0
2596        configuration3.booking_fee = 123.4
2597        self.app['configuration'].addSessionConfiguration(configuration3)
2598        self.student['studycourse'].entry_session = 2002
2599
2600        # Login
2601        self.browser.open(self.login_path)
2602        self.browser.getControl(name="form.login").value = self.student_id
2603        self.browser.getControl(name="form.password").value = 'spwd'
2604        self.browser.getControl("Login").click()
2605
2606        # Students can add previous school fee payment tickets in any state.
2607        IWorkflowState(self.student).setState('courses registered')
2608        self.browser.open(self.payments_path)
2609        self.browser.getLink("Add previous session payment ticket").click()
2610
2611        # Previous session payment form is provided
2612        self.assertEqual(self.student.current_session, 2004)
2613        self.browser.getControl(name="form.p_category").value = ['schoolfee']
2614        self.browser.getControl(name="form.p_session").value = ['2000']
2615        self.browser.getControl(name="form.p_level").value = ['300']
2616        self.browser.getControl("Create ticket").click()
2617        self.assertMatches('...The previous session must not fall below...',
2618                           self.browser.contents)
2619        self.browser.getControl(name="form.p_category").value = ['schoolfee']
2620        self.browser.getControl(name="form.p_session").value = ['2005']
2621        self.browser.getControl(name="form.p_level").value = ['300']
2622        self.browser.getControl("Create ticket").click()
2623        self.assertMatches('...This is not a previous session...',
2624                           self.browser.contents)
2625        # Students can pay current session school fee.
2626        self.browser.getControl(name="form.p_category").value = ['schoolfee']
2627        self.browser.getControl(name="form.p_session").value = ['2004']
2628        self.browser.getControl(name="form.p_level").value = ['300']
2629        self.browser.getControl("Create ticket").click()
2630        self.assertMatches('...ticket created...',
2631                           self.browser.contents)
2632        ctrl = self.browser.getControl(name='val_id')
2633        value = ctrl.options[0]
2634        self.browser.getLink(value).click()
2635        self.assertMatches('...Amount Authorized...',
2636                           self.browser.contents)
2637        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
2638
2639        # Payment session is properly set
2640        self.assertEqual(self.student['payments'][value].p_session, 2004)
2641        self.assertEqual(self.student['payments'][value].p_level, 300)
2642
2643        # We simulate the approval
2644        self.browser.open(self.browser.url + '/fake_approve')
2645        self.assertMatches('...Payment approved...',
2646                          self.browser.contents)
2647
2648        # No AC has been created
2649        self.assertEqual(len(self.app['accesscodes']['SFE-0'].keys()), 0)
2650        self.assertTrue(self.student['payments'][value].ac is None)
2651
2652        # Current payment flag is set False
2653        self.assertFalse(self.student['payments'][value].p_current)
2654
2655        # Button and form are not available for students who are in
2656        # states up to cleared
2657        self.student['studycourse'].entry_session = 2004
2658        IWorkflowState(self.student).setState('cleared')
2659        self.browser.open(self.payments_path)
2660        self.assertFalse(
2661            "Add previous session payment ticket" in self.browser.contents)
2662        self.browser.open(self.payments_path + '/addpp')
2663        self.assertTrue(
2664            "No previous payment to be made" in self.browser.contents)
2665        return
2666
2667    def test_postgraduate_student_payments(self):
2668        configuration = createObject('waeup.SessionConfiguration')
2669        configuration.academic_session = 2005
2670        self.app['configuration'].addSessionConfiguration(configuration)
2671        self.certificate.study_mode = 'pg_ft'
2672        self.certificate.start_level = 999
2673        self.certificate.end_level = 999
2674        self.student['studycourse'].current_level = 999
2675        # Login
2676        self.browser.open(self.login_path)
2677        self.browser.getControl(name="form.login").value = self.student_id
2678        self.browser.getControl(name="form.password").value = 'spwd'
2679        self.browser.getControl("Login").click()
2680        # Students can add online school fee payment tickets.
2681        IWorkflowState(self.student).setState('cleared')
2682        self.browser.open(self.payments_path)
2683        self.browser.getLink("Add current session payment ticket").click()
2684        self.browser.getControl(name="form.p_category").value = ['schoolfee']
2685        self.browser.getControl("Create ticket").click()
2686        self.assertMatches('...ticket created...',
2687                           self.browser.contents)
2688        ctrl = self.browser.getControl(name='val_id')
2689        value = ctrl.options[0]
2690        self.browser.getLink(value).click()
2691        self.assertMatches('...Amount Authorized...',
2692                           self.browser.contents)
2693        # Payment session and level are current ones.
2694        # Postgrads have to pay school_fee_1.
2695        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
2696        self.assertEqual(self.student['payments'][value].p_session, 2004)
2697        self.assertEqual(self.student['payments'][value].p_level, 999)
2698
2699        # We simulate the approval
2700        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
2701        self.browser.open(self.browser.url + '/fake_approve')
2702        self.assertMatches('...Payment approved...',
2703                          self.browser.contents)
2704
2705        # The new SFE-0 pin can be used for starting session
2706        self.browser.open(self.studycourse_path)
2707        self.browser.getLink('Start new session').click()
2708        pin = self.app['accesscodes']['SFE-0'].keys()[0]
2709        parts = pin.split('-')[1:]
2710        sfeseries, sfenumber = parts
2711        self.browser.getControl(name="ac_series").value = sfeseries
2712        self.browser.getControl(name="ac_number").value = sfenumber
2713        self.browser.getControl("Start now").click()
2714        self.assertMatches('...Session started...',
2715                           self.browser.contents)
2716        self.assertTrue(self.student.state == 'school fee paid')
2717
2718        # Postgrad students do not need to register courses the
2719        # can just pay for the next session.
2720        self.browser.open(self.payments_path)
2721        # Remove first payment to be sure that we access the right ticket
2722        del self.student['payments'][value]
2723        self.browser.getLink("Add current session payment ticket").click()
2724        self.browser.getControl(name="form.p_category").value = ['schoolfee']
2725        self.browser.getControl("Create ticket").click()
2726        ctrl = self.browser.getControl(name='val_id')
2727        value = ctrl.options[0]
2728        self.browser.getLink(value).click()
2729        # Payment session has increased by one, payment level remains the same.
2730        # Returning Postgraduates have to pay school_fee_2.
2731        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
2732        self.assertEqual(self.student['payments'][value].p_session, 2005)
2733        self.assertEqual(self.student['payments'][value].p_level, 999)
2734
2735        # Student is still in old session
2736        self.assertEqual(self.student.current_session, 2004)
2737
2738        # We do not need to pay the ticket if any other
2739        # SFE pin is provided
2740        pin_container = self.app['accesscodes']
2741        pin_container.createBatch(
2742            datetime.utcnow(), 'some_userid', 'SFE', 9.99, 1)
2743        pin = pin_container['SFE-1'].values()[0].representation
2744        sfeseries, sfenumber = pin.split('-')[1:]
2745        # The new SFE-1 pin can be used for starting new session
2746        self.browser.open(self.studycourse_path)
2747        self.browser.getLink('Start new session').click()
2748        self.browser.getControl(name="ac_series").value = sfeseries
2749        self.browser.getControl(name="ac_number").value = sfenumber
2750        self.browser.getControl("Start now").click()
2751        self.assertMatches('...Session started...',
2752                           self.browser.contents)
2753        self.assertTrue(self.student.state == 'school fee paid')
2754        # Student is in new session
2755        self.assertEqual(self.student.current_session, 2005)
2756        self.assertEqual(self.student['studycourse'].current_level, 999)
2757        return
2758
2759    def test_student_accommodation(self):
2760        # Login
2761        self.browser.open(self.login_path)
2762        self.browser.getControl(name="form.login").value = self.student_id
2763        self.browser.getControl(name="form.password").value = 'spwd'
2764        self.browser.getControl("Login").click()
2765
2766        # Students can add online booking fee payment tickets and open the
2767        # callback view (see test_manage_payments)
2768        self.browser.getLink("Payments").click()
2769        self.browser.getLink("Add current session payment ticket").click()
2770        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
2771        self.browser.getControl("Create ticket").click()
2772        ctrl = self.browser.getControl(name='val_id')
2773        value = ctrl.options[0]
2774        self.browser.getLink(value).click()
2775        self.browser.open(self.browser.url + '/fake_approve')
2776        # The new HOS-0 pin has been created
2777        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
2778        pin = self.app['accesscodes']['HOS-0'].keys()[0]
2779        ac = self.app['accesscodes']['HOS-0'][pin]
2780        parts = pin.split('-')[1:]
2781        sfeseries, sfenumber = parts
2782
2783        # Students can use HOS code and book a bed space with it ...
2784        self.browser.open(self.acco_path)
2785        # ... but not if booking period has expired ...
2786        self.app['hostels'].enddate = datetime.now(pytz.utc)
2787        self.browser.getLink("Book accommodation").click()
2788        self.assertMatches('...Outside booking period: ...',
2789                           self.browser.contents)
2790        self.app['hostels'].enddate = datetime.now(pytz.utc) + timedelta(days=10)
2791        # ... or student is not the an allowed state ...
2792        self.browser.getLink("Book accommodation").click()
2793        self.assertMatches('...You are in the wrong...',
2794                           self.browser.contents)
2795        IWorkflowInfo(self.student).fireTransition('admit')
2796        self.browser.getLink("Book accommodation").click()
2797        self.assertMatches('...Activation Code:...',
2798                           self.browser.contents)
2799        # Student can't used faked ACs ...
2800        self.browser.getControl(name="ac_series").value = u'nonsense'
2801        self.browser.getControl(name="ac_number").value = sfenumber
2802        self.browser.getControl("Create bed ticket").click()
2803        self.assertMatches('...Activation code is invalid...',
2804                           self.browser.contents)
2805        # ... or ACs owned by somebody else.
2806        ac.owner = u'Anybody'
2807        self.browser.getControl(name="ac_series").value = sfeseries
2808        self.browser.getControl(name="ac_number").value = sfenumber
2809        self.browser.getControl("Create bed ticket").click()
2810        self.assertMatches('...You are not the owner of this access code...',
2811                           self.browser.contents)
2812        ac.owner = self.student_id
2813        self.browser.getControl(name="ac_series").value = sfeseries
2814        self.browser.getControl(name="ac_number").value = sfenumber
2815        self.browser.getControl("Create bed ticket").click()
2816        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
2817                           self.browser.contents)
2818
2819        # Bed has been allocated
2820        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
2821        self.assertTrue(bed.owner == self.student_id)
2822
2823        # BedTicketAddPage is now blocked
2824        self.browser.getLink("Book accommodation").click()
2825        self.assertMatches('...You already booked a bed space...',
2826            self.browser.contents)
2827
2828        # The bed ticket displays the data correctly
2829        self.browser.open(self.acco_path + '/2004')
2830        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
2831                           self.browser.contents)
2832        self.assertMatches('...2004/2005...', self.browser.contents)
2833        self.assertMatches('...regular_male_fr...', self.browser.contents)
2834        self.assertMatches('...%s...' % pin, self.browser.contents)
2835
2836        # Students can open the pdf slip
2837        self.browser.open(self.browser.url + '/bed_allocation_slip.pdf')
2838        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2839        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2840
2841        # Students can't relocate themselves
2842        self.assertFalse('Relocate' in self.browser.contents)
2843        relocate_path = self.acco_path + '/2004/relocate'
2844        self.assertRaises(
2845            Unauthorized, self.browser.open, relocate_path)
2846
2847        # Students can't the Remove button and check boxes
2848        self.browser.open(self.acco_path)
2849        self.assertFalse('Remove' in self.browser.contents)
2850        self.assertFalse('val_id' in self.browser.contents)
2851
2852        # Students can pay maintenance fee now
2853        self.browser.open(self.payments_path)
2854        self.browser.open(self.payments_path + '/addop')
2855        self.browser.getControl(name="form.p_category").value = ['hostel_maintenance']
2856        self.browser.getControl("Create ticket").click()
2857        self.assertMatches('...Payment ticket created...',
2858                           self.browser.contents)
2859        return
2860
2861    def test_change_password_request(self):
2862        self.browser.open('http://localhost/app/changepw')
2863        self.browser.getControl(name="form.identifier").value = '123'
2864        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
2865        self.browser.getControl("Send login credentials").click()
2866        self.assertTrue('An email with' in self.browser.contents)
2867
2868    def test_student_expired_personal_data(self):
2869        # Login
2870        IWorkflowState(self.student).setState('school fee paid')
2871        delta = timedelta(days=180)
2872        self.student.personal_updated = datetime.utcnow() - delta
2873        self.browser.open(self.login_path)
2874        self.browser.getControl(name="form.login").value = self.student_id
2875        self.browser.getControl(name="form.password").value = 'spwd'
2876        self.browser.getControl("Login").click()
2877        self.assertEqual(self.browser.url, self.student_path)
2878        self.assertTrue(
2879            'You logged in' in self.browser.contents)
2880        # Students don't see personal_updated field in edit form
2881        self.browser.open(self.edit_personal_path)
2882        self.assertFalse('Updated' in self.browser.contents)
2883        self.browser.open(self.personal_path)
2884        self.assertTrue('Updated' in self.browser.contents)
2885        self.browser.getLink("Logout").click()
2886        delta = timedelta(days=181)
2887        self.student.personal_updated = datetime.utcnow() - delta
2888        self.browser.open(self.login_path)
2889        self.browser.getControl(name="form.login").value = self.student_id
2890        self.browser.getControl(name="form.password").value = 'spwd'
2891        self.browser.getControl("Login").click()
2892        self.assertEqual(self.browser.url, self.edit_personal_path)
2893        self.assertTrue(
2894            'Your personal data record is outdated.' in self.browser.contents)
2895
2896    def test_setReturningData(self):
2897        utils = getUtility(IStudentsUtils)
2898        self.student['studycourse'].current_level = 600
2899        utils.setReturningData(self.student)
2900        # The new level exceeds the certificates end_level.
2901        # In this case current_level remains unchanged and no error is raised.
2902        self.assertEqual(self.student['studycourse'].current_level, 600)
2903
2904    def test_request_transcript(self):
2905        IWorkflowState(self.student).setState('graduated')
2906        self.browser.open(self.login_path)
2907        self.browser.getControl(name="form.login").value = self.student_id
2908        self.browser.getControl(name="form.password").value = 'spwd'
2909        self.browser.getControl("Login").click()
2910        self.assertMatches(
2911            '...You logged in...', self.browser.contents)
2912        # Create payment ticket
2913        self.browser.open(self.payments_path)
2914        self.browser.open(self.payments_path + '/addop')
2915        self.browser.getControl(name="form.p_category").value = ['transcript']
2916        self.browser.getControl("Create ticket").click()
2917        ctrl = self.browser.getControl(name='val_id')
2918        value = ctrl.options[0]
2919        self.browser.getLink(value).click()
2920        self.assertMatches('...Amount Authorized...',
2921                           self.browser.contents)
2922        self.assertEqual(self.student['payments'][value].amount_auth, 4567.0)
2923        # Student is the payer of the payment ticket.
2924        payer = IPayer(self.student['payments'][value])
2925        self.assertEqual(payer.display_fullname, 'Anna Tester')
2926        self.assertEqual(payer.id, self.student_id)
2927        self.assertEqual(payer.faculty, 'fac1')
2928        self.assertEqual(payer.department, 'dep1')
2929        # We simulate the approval and fetch the pin
2930        self.assertEqual(len(self.app['accesscodes']['TSC-0']),0)
2931        self.browser.open(self.browser.url + '/fake_approve')
2932        self.assertMatches('...Payment approved...',
2933                          self.browser.contents)
2934        pin = self.app['accesscodes']['TSC-0'].keys()[0]
2935        parts = pin.split('-')[1:]
2936        tscseries, tscnumber = parts
2937        # Student can use the pin to send the transcript request
2938        self.browser.open(self.student_path)
2939        self.browser.getLink("Request transcript").click()
2940        self.browser.getControl(name="ac_series").value = tscseries
2941        self.browser.getControl(name="ac_number").value = tscnumber
2942        self.browser.getControl(name="comment").value = 'Comment line 1 \nComment line2'
2943        self.browser.getControl(name="address").value = 'Address line 1 \nAddress line2'
2944        self.browser.getControl("Submit").click()
2945        self.assertMatches('...Transcript processing has been started...',
2946                          self.browser.contents)
2947        self.assertEqual(self.student.state, 'transcript requested')
2948        self.assertMatches(
2949            '... UTC K1000000 wrote:\n\nComment line 1 \n'
2950            'Comment line2\n\nDispatch Address:\nAddress line 1 \n'
2951            'Address line2\n\n', self.student.transcript_comment)
2952        # The comment has been logged
2953        logfile = os.path.join(
2954            self.app['datacenter'].storage, 'logs', 'students.log')
2955        logcontent = open(logfile).read()
2956        self.assertTrue(
2957            'K1000000 - students.browser.StudentTranscriptRequestPage - '
2958            'K1000000 - comment: Comment line 1 <br>Comment line2\n'
2959            in logcontent)
2960
2961class StudentRequestPWTests(StudentsFullSetup):
2962    # Tests for student registration
2963
2964    layer = FunctionalLayer
2965
2966    def test_request_pw(self):
2967        # Student with wrong number can't be found.
2968        self.browser.open('http://localhost/app/requestpw')
2969        self.browser.getControl(name="form.firstname").value = 'Anna'
2970        self.browser.getControl(name="form.number").value = 'anynumber'
2971        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
2972        self.browser.getControl("Send login credentials").click()
2973        self.assertTrue('No student record found.'
2974            in self.browser.contents)
2975        # Anonymous is not informed that firstname verification failed.
2976        # It seems that the record doesn't exist.
2977        self.browser.open('http://localhost/app/requestpw')
2978        self.browser.getControl(name="form.firstname").value = 'Johnny'
2979        self.browser.getControl(name="form.number").value = '123'
2980        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
2981        self.browser.getControl("Send login credentials").click()
2982        self.assertTrue('No student record found.'
2983            in self.browser.contents)
2984        # Even with the correct firstname we can't register if a
2985        # password has been set and used.
2986        self.browser.getControl(name="form.firstname").value = 'Anna'
2987        self.browser.getControl(name="form.number").value = '123'
2988        self.browser.getControl("Send login credentials").click()
2989        self.assertTrue('Your password has already been set and used.'
2990            in self.browser.contents)
2991        self.browser.open('http://localhost/app/requestpw')
2992        self.app['students'][self.student_id].password = None
2993        # The firstname field, used for verification, is not case-sensitive.
2994        self.browser.getControl(name="form.firstname").value = 'aNNa'
2995        self.browser.getControl(name="form.number").value = '123'
2996        self.browser.getControl(name="form.email").value = 'new@yy.zz'
2997        self.browser.getControl("Send login credentials").click()
2998        # Yeah, we succeded ...
2999        self.assertTrue('Your password request was successful.'
3000            in self.browser.contents)
3001        # We can also use the matric_number instead.
3002        self.browser.open('http://localhost/app/requestpw')
3003        self.browser.getControl(name="form.firstname").value = 'aNNa'
3004        self.browser.getControl(name="form.number").value = '234'
3005        self.browser.getControl(name="form.email").value = 'new@yy.zz'
3006        self.browser.getControl("Send login credentials").click()
3007        self.assertTrue('Your password request was successful.'
3008            in self.browser.contents)
3009        # ... and  student can be found in the catalog via the email address
3010        cat = queryUtility(ICatalog, name='students_catalog')
3011        results = list(
3012            cat.searchResults(
3013            email=('new@yy.zz', 'new@yy.zz')))
3014        self.assertEqual(self.student,results[0])
3015        logfile = os.path.join(
3016            self.app['datacenter'].storage, 'logs', 'main.log')
3017        logcontent = open(logfile).read()
3018        self.assertTrue('zope.anybody - students.browser.StudentRequestPasswordPage - '
3019                        '234 (K1000000) - new@yy.zz' in logcontent)
3020        return
3021
3022    def test_student_locked_level_forms(self):
3023
3024        # Add two study levels, one current and one previous
3025        studylevel = createObject(u'waeup.StudentStudyLevel')
3026        studylevel.level = 100
3027        self.student['studycourse'].addStudentStudyLevel(
3028            self.certificate, studylevel)
3029        studylevel = createObject(u'waeup.StudentStudyLevel')
3030        studylevel.level = 200
3031        self.student['studycourse'].addStudentStudyLevel(
3032            self.certificate, studylevel)
3033        IWorkflowState(self.student).setState('school fee paid')
3034        self.student['studycourse'].current_level = 200
3035
3036        self.browser.open(self.login_path)
3037        self.browser.getControl(name="form.login").value = self.student_id
3038        self.browser.getControl(name="form.password").value = 'spwd'
3039        self.browser.getControl("Login").click()
3040
3041        self.browser.open(self.student_path + '/studycourse/200/edit')
3042        self.assertFalse('The requested form is locked' in self.browser.contents)
3043        self.browser.open(self.student_path + '/studycourse/100/edit')
3044        self.assertTrue('The requested form is locked' in self.browser.contents)
3045
3046        self.browser.open(self.student_path + '/studycourse/200/ctadd')
3047        self.assertFalse('The requested form is locked' in self.browser.contents)
3048        self.browser.open(self.student_path + '/studycourse/100/ctadd')
3049        self.assertTrue('The requested form is locked' in self.browser.contents)
3050
3051        IWorkflowState(self.student).setState('courses registered')
3052        self.browser.open(self.student_path + '/studycourse/200/edit')
3053        self.assertTrue('The requested form is locked' in self.browser.contents)
3054        self.browser.open(self.student_path + '/studycourse/200/ctadd')
3055        self.assertTrue('The requested form is locked' in self.browser.contents)
3056
3057
3058class PublicPagesTests(StudentsFullSetup):
3059    # Tests for simple webservices
3060
3061    layer = FunctionalLayer
3062
3063    def test_paymentrequest(self):
3064        payment = createObject('waeup.StudentOnlinePayment')
3065        payment.p_category = u'schoolfee'
3066        payment.p_session = self.student.current_session
3067        payment.p_item = u'My Certificate'
3068        payment.p_id = u'anyid'
3069        self.student['payments']['anykey'] = payment
3070        # Request information about unpaid payment ticket
3071        self.browser.open('http://localhost/app/paymentrequest?P_ID=anyid')
3072        self.assertEqual(self.browser.contents, '-1')
3073        # Request information about paid payment ticket
3074        payment.p_state = u'paid'
3075        notify(grok.ObjectModifiedEvent(payment))
3076        self.browser.open('http://localhost/app/paymentrequest?P_ID=anyid')
3077        self.assertEqual(self.browser.contents,
3078            'FULL_NAME=Anna Tester&FACULTY=fac1&DEPARTMENT=dep1'
3079            '&PAYMENT_ITEM=My Certificate&PAYMENT_CATEGORY=School Fee'
3080            '&ACADEMIC_SESSION=2004/2005&MATRIC_NUMBER=234&REG_NUMBER=123'
3081            '&FEE_AMOUNT=0.0')
3082        self.browser.open('http://localhost/app/paymentrequest?NONSENSE=nonsense')
3083        self.assertEqual(self.browser.contents, '-1')
3084        self.browser.open('http://localhost/app/paymentrequest?P_ID=nonsense')
3085        self.assertEqual(self.browser.contents, '-1')
3086
3087class StudentDataExportTests(StudentsFullSetup, FunctionalAsyncTestCase):
3088    # Tests for StudentsContainer class views and pages
3089
3090    layer = FunctionalLayer
3091
3092    def wait_for_export_job_completed(self):
3093        # helper function waiting until the current export job is completed
3094        manager = getUtility(IJobManager)
3095        job_id = self.app['datacenter'].running_exports[0][0]
3096        job = manager.get(job_id)
3097        wait_for_result(job)
3098        return job_id
3099
3100    def test_faculties_export(self):
3101        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3102        facs_path = 'http://localhost/app/faculties'
3103        self.browser.open(facs_path)
3104        self.browser.getLink("Export student data").click()
3105        self.browser.getControl("Configure new export").click()
3106        self.browser.getControl(name="exporter").value = ['bursary']
3107        self.browser.getControl(name="session").value = ['2004']
3108        self.browser.getControl(name="level").value = ['100']
3109        self.browser.getControl(name="mode").value = ['ug_ft']
3110        self.browser.getControl("Create CSV file").click()
3111
3112        # When the job is finished and we reload the page...
3113        job_id = self.wait_for_export_job_completed()
3114        self.browser.open(facs_path + '/exports')
3115        # ... the csv file can be downloaded ...
3116        self.browser.getLink("Download").click()
3117        self.assertEqual(self.browser.headers['content-type'],
3118            'text/csv; charset=UTF-8')
3119        self.assertTrue(
3120            'filename="WAeUP.Kofa_bursary_%s.csv' % job_id in
3121            self.browser.headers['content-disposition'])
3122        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3123        job_id = self.app['datacenter'].running_exports[0][0]
3124        # ... and discarded
3125        self.browser.open(facs_path + '/exports')
3126        self.browser.getControl("Discard").click()
3127        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3128        # Creation, downloading and discarding is logged
3129        logfile = os.path.join(
3130            self.app['datacenter'].storage, 'logs', 'datacenter.log')
3131        logcontent = open(logfile).read()
3132        self.assertTrue(
3133            'zope.mgr - students.browser.FacultiesExportJobContainerJobConfig '
3134            '- exported: bursary (2004, 100, ug_ft, None, None), job_id=%s'
3135            % job_id in logcontent
3136            )
3137        self.assertTrue(
3138            'zope.mgr - students.browser.ExportJobContainerDownload '
3139            '- downloaded: WAeUP.Kofa_bursary_%s.csv, job_id=%s'
3140            % (job_id, job_id) in logcontent
3141            )
3142        self.assertTrue(
3143            'zope.mgr - students.browser.ExportJobContainerOverview '
3144            '- discarded: job_id=%s' % job_id in logcontent
3145            )
3146
3147    def test_department_export(self):
3148        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3149        dep1_path = 'http://localhost/app/faculties/fac1/dep1'
3150        self.browser.open(dep1_path)
3151        self.browser.getLink("Export student data").click()
3152        self.browser.getControl("Configure new export").click()
3153        self.browser.getControl(name="exporter").value = ['students']
3154        self.browser.getControl(name="session").value = ['2004']
3155        self.browser.getControl(name="level").value = ['100']
3156        self.browser.getControl(name="mode").value = ['ug_ft']
3157        self.browser.getControl("Create CSV file").click()
3158
3159        # When the job is finished and we reload the page...
3160        job_id = self.wait_for_export_job_completed()
3161        self.browser.open(dep1_path + '/exports')
3162        # ... the csv file can be downloaded ...
3163        self.browser.getLink("Download").click()
3164        self.assertEqual(self.browser.headers['content-type'],
3165            'text/csv; charset=UTF-8')
3166        self.assertTrue(
3167            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
3168            self.browser.headers['content-disposition'])
3169        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3170        job_id = self.app['datacenter'].running_exports[0][0]
3171        # ... and discarded
3172        self.browser.open(dep1_path + '/exports')
3173        self.browser.getControl("Discard").click()
3174        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3175        # Creation, downloading and discarding is logged
3176        logfile = os.path.join(
3177            self.app['datacenter'].storage, 'logs', 'datacenter.log')
3178        logcontent = open(logfile).read()
3179        self.assertTrue(
3180            'zope.mgr - students.browser.DepartmentExportJobContainerJobConfig '
3181            '- exported: students (2004, 100, ug_ft, dep1, None), job_id=%s'
3182            % job_id in logcontent
3183            )
3184        self.assertTrue(
3185            'zope.mgr - students.browser.ExportJobContainerDownload '
3186            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
3187            % (job_id, job_id) in logcontent
3188            )
3189        self.assertTrue(
3190            'zope.mgr - students.browser.ExportJobContainerOverview '
3191            '- discarded: job_id=%s' % job_id in logcontent
3192            )
3193
3194    def test_certificate_export(self):
3195        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3196        cert1_path = 'http://localhost/app/faculties/fac1/dep1/certificates/CERT1'
3197        self.browser.open(cert1_path)
3198        self.browser.getLink("Export student data").click()
3199        self.browser.getControl("Configure new export").click()
3200        self.browser.getControl(name="exporter").value = ['students']
3201        self.browser.getControl(name="session").value = ['2004']
3202        self.browser.getControl(name="level").value = ['100']
3203        self.browser.getControl("Create CSV file").click()
3204
3205        # When the job is finished and we reload the page...
3206        job_id = self.wait_for_export_job_completed()
3207        self.browser.open(cert1_path + '/exports')
3208        # ... the csv file can be downloaded ...
3209        self.browser.getLink("Download").click()
3210        self.assertEqual(self.browser.headers['content-type'],
3211            'text/csv; charset=UTF-8')
3212        self.assertTrue(
3213            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
3214            self.browser.headers['content-disposition'])
3215        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3216        job_id = self.app['datacenter'].running_exports[0][0]
3217        # ... and discarded
3218        self.browser.open(cert1_path + '/exports')
3219        self.browser.getControl("Discard").click()
3220        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3221        # Creation, downloading and discarding is logged
3222        logfile = os.path.join(
3223            self.app['datacenter'].storage, 'logs', 'datacenter.log')
3224        logcontent = open(logfile).read()
3225        self.assertTrue(
3226            'zope.mgr - students.browser.CertificateExportJobContainerJobConfig '
3227            '- exported: students (2004, 100, None, None, CERT1), job_id=%s'
3228            % job_id in logcontent
3229            )
3230        self.assertTrue(
3231            'zope.mgr - students.browser.ExportJobContainerDownload '
3232            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
3233            % (job_id, job_id) in logcontent
3234            )
3235        self.assertTrue(
3236            'zope.mgr - students.browser.ExportJobContainerOverview '
3237            '- discarded: job_id=%s' % job_id in logcontent
3238            )
3239
3240    def test_course_export_students(self):
3241        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3242        course1_path = 'http://localhost/app/faculties/fac1/dep1/courses/COURSE1'
3243        self.browser.open(course1_path)
3244        self.browser.getLink("Export student data").click()
3245        self.browser.getControl("Configure new export").click()
3246        self.browser.getControl(name="exporter").value = ['students']
3247        self.browser.getControl(name="session").value = ['2004']
3248        self.browser.getControl(name="level").value = ['100']
3249        self.browser.getControl("Create CSV file").click()
3250
3251        # When the job is finished and we reload the page...
3252        job_id = self.wait_for_export_job_completed()
3253        self.browser.open(course1_path + '/exports')
3254        # ... the csv file can be downloaded ...
3255        self.browser.getLink("Download").click()
3256        self.assertEqual(self.browser.headers['content-type'],
3257            'text/csv; charset=UTF-8')
3258        self.assertTrue(
3259            'filename="WAeUP.Kofa_students_%s.csv' % job_id in
3260            self.browser.headers['content-disposition'])
3261        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3262        job_id = self.app['datacenter'].running_exports[0][0]
3263        # ... and discarded
3264        self.browser.open(course1_path + '/exports')
3265        self.browser.getControl("Discard").click()
3266        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3267        # Creation, downloading and discarding is logged
3268        logfile = os.path.join(
3269            self.app['datacenter'].storage, 'logs', 'datacenter.log')
3270        logcontent = open(logfile).read()
3271        self.assertTrue(
3272            'zope.mgr - students.browser.CourseExportJobContainerJobConfig '
3273            '- exported: students (2004, 100, COURSE1), job_id=%s'
3274            % job_id in logcontent
3275            )
3276        self.assertTrue(
3277            'zope.mgr - students.browser.ExportJobContainerDownload '
3278            '- downloaded: WAeUP.Kofa_students_%s.csv, job_id=%s'
3279            % (job_id, job_id) in logcontent
3280            )
3281        self.assertTrue(
3282            'zope.mgr - students.browser.ExportJobContainerOverview '
3283            '- discarded: job_id=%s' % job_id in logcontent
3284            )
3285
3286    def test_course_export_coursetickets(self):
3287        # We add study level 100 to the student's studycourse
3288        studylevel = StudentStudyLevel()
3289        studylevel.level = 100
3290        studylevel.level_session = 2004
3291        self.student['studycourse'].addStudentStudyLevel(
3292            self.certificate,studylevel)
3293        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
3294        course1_path = 'http://localhost/app/faculties/fac1/dep1/courses/COURSE1'
3295        self.browser.open(course1_path)
3296        self.browser.getLink("Export student data").click()
3297        self.browser.getControl("Configure new export").click()
3298        self.browser.getControl(name="exporter").value = ['coursetickets']
3299        self.browser.getControl(name="session").value = ['2004']
3300        self.browser.getControl(name="level").value = ['100']
3301        self.browser.getControl("Create CSV file").click()
3302        # When the job is finished and we reload the page...
3303        job_id = self.wait_for_export_job_completed()
3304        self.browser.open(course1_path + '/exports')
3305        # ... the csv file can be downloaded ...
3306        self.browser.getLink("Download").click()
3307        self.assertEqual(self.browser.headers['content-type'],
3308            'text/csv; charset=UTF-8')
3309        self.assertTrue(
3310            'filename="WAeUP.Kofa_coursetickets_%s.csv' % job_id in
3311            self.browser.headers['content-disposition'])
3312        # ... and contains the course ticket COURSE1
3313        self.assertEqual(self.browser.contents,
3314            'automatic,carry_over,code,credits,dcode,fcode,level,'
3315            'level_session,mandatory,passmark,score,semester,title,'
3316            'student_id,certcode\r\n1,0,COURSE1,10,dep1,fac1,100,2004,1,40,,1,'
3317            'Unnamed Course,K1000000,CERT1\r\n')
3318
3319        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3320        job_id = self.app['datacenter'].running_exports[0][0]
3321        # Thew job can be discarded
3322        self.browser.open(course1_path + '/exports')
3323        self.browser.getControl("Discard").click()
3324        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3325        # Creation, downloading and discarding is logged
3326        logfile = os.path.join(
3327            self.app['datacenter'].storage, 'logs', 'datacenter.log')
3328        logcontent = open(logfile).read()
3329        self.assertTrue(
3330            'zope.mgr - students.browser.CourseExportJobContainerJobConfig '
3331            '- exported: coursetickets (2004, 100, COURSE1), job_id=%s'
3332            % job_id in logcontent
3333            )
3334        self.assertTrue(
3335            'zope.mgr - students.browser.ExportJobContainerDownload '
3336            '- downloaded: WAeUP.Kofa_coursetickets_%s.csv, job_id=%s'
3337            % (job_id, job_id) in logcontent
3338            )
3339        self.assertTrue(
3340            'zope.mgr - students.browser.ExportJobContainerOverview '
3341            '- discarded: job_id=%s' % job_id in logcontent
3342            )
3343
3344    def test_export_departmet_officers(self):
3345        # Create department officer
3346        self.app['users'].addUser('mrdepartment', 'mrdepartmentsecret')
3347        self.app['users']['mrdepartment'].email = 'mrdepartment@foo.ng'
3348        self.app['users']['mrdepartment'].title = 'Carlo Pitter'
3349        # Assign local role
3350        department = self.app['faculties']['fac1']['dep1']
3351        prmlocal = IPrincipalRoleManager(department)
3352        prmlocal.assignRoleToPrincipal('waeup.local.DepartmentOfficer', 'mrdepartment')
3353        # Login as department officer
3354        self.browser.open(self.login_path)
3355        self.browser.getControl(name="form.login").value = 'mrdepartment'
3356        self.browser.getControl(name="form.password").value = 'mrdepartmentsecret'
3357        self.browser.getControl("Login").click()
3358        self.assertMatches('...You logged in...', self.browser.contents)
3359        self.browser.open("http://localhost/app/faculties/fac1/dep1")
3360        self.browser.getLink("Export student data").click()
3361        self.browser.getControl("Configure new export").click()
3362        # Only the paymentsoverview exporter is available for department officers
3363        self.assertFalse('<option value="students">' in self.browser.contents)
3364        self.assertTrue(
3365            '<option value="paymentsoverview">' in self.browser.contents)
3366        self.browser.getControl(name="exporter").value = ['paymentsoverview']
3367        self.browser.getControl(name="session").value = ['2004']
3368        self.browser.getControl(name="level").value = ['100']
3369        self.browser.getControl("Create CSV file").click()
3370        self.assertTrue('Export started' in self.browser.contents)
3371        # Thew job can be discarded
3372        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3373        #job_id = self.app['datacenter'].running_exports[0][0]
3374        job_id = self.wait_for_export_job_completed()
3375        self.browser.open("http://localhost/app/faculties/fac1/dep1/exports")
3376        self.browser.getControl("Discard").click()
3377        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
3378
3379    def test_export_bursary_officers(self):
3380        # Create bursary officer
3381        self.app['users'].addUser('mrbursary', 'mrbursarysecret')
3382        self.app['users']['mrbursary'].email = 'mrbursary@foo.ng'
3383        self.app['users']['mrbursary'].title = 'Carlo Pitter'
3384        prmglobal = IPrincipalRoleManager(self.app)
3385        prmglobal.assignRoleToPrincipal('waeup.BursaryOfficer', 'mrbursary')
3386        # Login as bursary officer
3387        self.browser.open(self.login_path)
3388        self.browser.getControl(name="form.login").value = 'mrbursary'
3389        self.browser.getControl(name="form.password").value = 'mrbursarysecret'
3390        self.browser.getControl("Login").click()
3391        self.assertMatches('...You logged in...', self.browser.contents)
3392        self.browser.getLink("Academics").click()
3393        self.browser.getLink("Export student data").click()
3394        self.browser.getControl("Configure new export").click()
3395        # Only the bursary exporter is available for bursary officers
3396        # not only at facultiescontainer level ...
3397        self.assertFalse('<option value="students">' in self.browser.contents)
3398        self.assertTrue('<option value="bursary">' in self.browser.contents)
3399        self.browser.getControl(name="exporter").value = ['bursary']
3400        self.browser.getControl(name="session").value = ['2004']
3401        self.browser.getControl(name="level").value = ['100']
3402        self.browser.getControl("Create CSV file").click()
3403        self.assertTrue('Export started' in self.browser.contents)
3404        # ... but also at other levels
3405        self.browser.open('http://localhost/app/faculties/fac1/dep1')
3406        self.browser.getLink("Export student data").click()
3407        self.browser.getControl("Configure new export").click()
3408        self.assertFalse('<option value="students">' in self.browser.contents)
3409        self.assertTrue('<option value="bursary">' in self.browser.contents)
3410        # Thew job can be discarded
3411        self.assertEqual(len(self.app['datacenter'].running_exports), 1)
3412        #job_id = self.app['datacenter'].running_exports[0][0]
3413        job_id = self.wait_for_export_job_completed()
3414        self.browser.open('http://localhost/app/faculties/exports')
3415        self.browser.getControl("Discard").click()
3416        self.assertEqual(len(self.app['datacenter'].running_exports), 0)
Note: See TracBrowser for help on using the repository browser.