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

Last change on this file since 9273 was 9273, checked in by Henrik Bettermann, 12 years ago

Add special permission and view for triggering transitions

  • Property svn:keywords set to Id
File size: 114.9 KB
Line 
1## $Id: test_browser.py 9273 2012-10-02 16:50:57Z 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
24from datetime import datetime, timedelta
25from StringIO import StringIO
26import os
27import grok
28from zope.event import notify
29from zope.component import createObject, queryUtility
30from zope.component.hooks import setSite, clearSite
31from zope.catalog.interfaces import ICatalog
32from zope.security.interfaces import Unauthorized
33from zope.securitypolicy.interfaces import IPrincipalRoleManager
34from zope.testbrowser.testing import Browser
35from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState
36from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
37from waeup.kofa.app import University
38from waeup.kofa.payments.interfaces import IPaymentWebservice
39from waeup.kofa.students.student import Student
40from waeup.kofa.students.studylevel import StudentStudyLevel
41from waeup.kofa.university.faculty import Faculty
42from waeup.kofa.university.department import Department
43from waeup.kofa.interfaces import IUserAccount
44from waeup.kofa.authentication import LocalRoleSetEvent
45from waeup.kofa.hostels.hostel import Hostel, Bed, NOT_OCCUPIED
46
47PH_LEN = 2059  # Length of placeholder file
48
49SAMPLE_IMAGE = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
50SAMPLE_IMAGE_BMP = os.path.join(os.path.dirname(__file__), 'test_image.bmp')
51
52def lookup_submit_value(name, value, browser):
53    """Find a button with a certain value."""
54    for num in range(0, 100):
55        try:
56            button = browser.getControl(name=name, index=num)
57            if button.value.endswith(value):
58                return button
59        except IndexError:
60            break
61    return None
62
63class StudentsFullSetup(FunctionalTestCase):
64    # A test case that only contains a setup and teardown
65    #
66    # Complete setup for students handlings is rather complex and
67    # requires lots of things created before we can start. This is a
68    # setup that does all this, creates a university, creates PINs,
69    # etc.  so that we do not have to bother with that in different
70    # test cases.
71
72    layer = FunctionalLayer
73
74    def setUp(self):
75        super(StudentsFullSetup, self).setUp()
76
77        # Setup a sample site for each test
78        app = University()
79        self.dc_root = tempfile.mkdtemp()
80        app['datacenter'].setStoragePath(self.dc_root)
81
82        # Prepopulate the ZODB...
83        self.getRootFolder()['app'] = app
84        # we add the site immediately after creation to the
85        # ZODB. Catalogs and other local utilities are not setup
86        # before that step.
87        self.app = self.getRootFolder()['app']
88        # Set site here. Some of the following setup code might need
89        # to access grok.getSite() and should get our new app then
90        setSite(app)
91
92        # Add student with subobjects
93        student = createObject('waeup.Student')
94        student.firstname = u'Anna'
95        student.lastname = u'Tester'
96        student.reg_number = u'123'
97        student.matric_number = u'234'
98        student.sex = u'm'
99        student.email = 'aa@aa.ng'
100        student.phone = u'1234'
101        self.app['students'].addStudent(student)
102        self.student_id = student.student_id
103        self.student = self.app['students'][self.student_id]
104
105        # Set password
106        IUserAccount(
107            self.app['students'][self.student_id]).setPassword('spwd')
108
109        self.login_path = 'http://localhost/app/login'
110        self.container_path = 'http://localhost/app/students'
111        self.manage_container_path = self.container_path + '/@@manage'
112        self.add_student_path = self.container_path + '/addstudent'
113        self.student_path = self.container_path + '/' + self.student_id
114        self.manage_student_path = self.student_path + '/manage_base'
115        self.trigtrans_path = self.student_path + '/trigtrans'
116        self.clearance_path = self.student_path + '/view_clearance'
117        self.personal_path = self.student_path + '/view_personal'
118        self.edit_clearance_path = self.student_path + '/cedit'
119        self.manage_clearance_path = self.student_path + '/manage_clearance'
120        self.edit_personal_path = self.student_path + '/edit_personal'
121        self.manage_personal_path = self.student_path + '/manage_personal'
122        self.studycourse_path = self.student_path + '/studycourse'
123        self.payments_path = self.student_path + '/payments'
124        self.acco_path = self.student_path + '/accommodation'
125        self.history_path = self.student_path + '/history'
126
127        # Create 5 access codes with prefix'PWD'
128        pin_container = self.app['accesscodes']
129        pin_container.createBatch(
130            datetime.utcnow(), 'some_userid', 'PWD', 9.99, 5)
131        pins = pin_container['PWD-1'].values()
132        self.pwdpins = [x.representation for x in pins]
133        self.existing_pwdpin = self.pwdpins[0]
134        parts = self.existing_pwdpin.split('-')[1:]
135        self.existing_pwdseries, self.existing_pwdnumber = parts
136        # Create 5 access codes with prefix 'CLR'
137        pin_container.createBatch(
138            datetime.now(), 'some_userid', 'CLR', 9.99, 5)
139        pins = pin_container['CLR-1'].values()
140        pins[0].owner = u'Hans Wurst'
141        self.existing_clrac = pins[0]
142        self.existing_clrpin = pins[0].representation
143        parts = self.existing_clrpin.split('-')[1:]
144        self.existing_clrseries, self.existing_clrnumber = parts
145        # Create 2 access codes with prefix 'HOS'
146        pin_container.createBatch(
147            datetime.now(), 'some_userid', 'HOS', 9.99, 2)
148        pins = pin_container['HOS-1'].values()
149        self.existing_hosac = pins[0]
150        self.existing_hospin = pins[0].representation
151        parts = self.existing_hospin.split('-')[1:]
152        self.existing_hosseries, self.existing_hosnumber = parts
153
154        # Populate university
155        self.certificate = createObject('waeup.Certificate')
156        self.certificate.code = u'CERT1'
157        self.certificate.application_category = 'basic'
158        self.certificate.study_mode = 'ug_ft'
159        self.certificate.start_level = 100
160        self.certificate.end_level = 500
161        self.certificate.school_fee_1 = 40000.0
162        self.certificate.school_fee_2 = 20000.0
163        self.app['faculties']['fac1'] = Faculty(code='fac1')
164        self.app['faculties']['fac1']['dep1'] = Department(code='dep1')
165        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
166            self.certificate)
167        self.course = createObject('waeup.Course')
168        self.course.code = 'COURSE1'
169        self.course.semester = 1
170        self.course.credits = 10
171        self.course.passmark = 40
172        self.app['faculties']['fac1']['dep1'].courses.addCourse(
173            self.course)
174        self.app['faculties']['fac1']['dep1'].certificates['CERT1'].addCertCourse(
175            self.course, level=100)
176
177        # Configure university and hostels
178        self.app['hostels'].accommodation_states = ['admitted']
179        self.app['hostels'].accommodation_session = 2004
180        delta = timedelta(days=10)
181        self.app['hostels'].startdate = datetime.now(pytz.utc) - delta
182        self.app['hostels'].enddate = datetime.now(pytz.utc) + delta
183        self.app['configuration'].carry_over = True
184        configuration = createObject('waeup.SessionConfiguration')
185        configuration.academic_session = 2004
186        configuration.clearance_fee = 3456.0
187        configuration.booking_fee = 123.4
188        self.app['configuration'].addSessionConfiguration(configuration)
189
190        # Create a hostel with two beds
191        hostel = Hostel()
192        hostel.hostel_id = u'hall-1'
193        hostel.hostel_name = u'Hall 1'
194        self.app['hostels'].addHostel(hostel)
195        bed = Bed()
196        bed.bed_id = u'hall-1_A_101_A'
197        bed.bed_number = 1
198        bed.owner = NOT_OCCUPIED
199        bed.bed_type = u'regular_male_fr'
200        self.app['hostels'][hostel.hostel_id].addBed(bed)
201        bed = Bed()
202        bed.bed_id = u'hall-1_A_101_B'
203        bed.bed_number = 2
204        bed.owner = NOT_OCCUPIED
205        bed.bed_type = u'regular_female_fr'
206        self.app['hostels'][hostel.hostel_id].addBed(bed)
207
208        # Set study course attributes of test student
209        self.student['studycourse'].certificate = self.certificate
210        self.student['studycourse'].current_session = 2004
211        self.student['studycourse'].entry_session = 2004
212        self.student['studycourse'].current_verdict = 'A'
213        self.student['studycourse'].current_level = 100
214        # Update the catalog
215        notify(grok.ObjectModifiedEvent(self.student))
216
217        # Put the prepopulated site into test ZODB and prepare test
218        # browser
219        self.browser = Browser()
220        self.browser.handleErrors = False
221
222    def tearDown(self):
223        super(StudentsFullSetup, self).tearDown()
224        clearSite()
225        shutil.rmtree(self.dc_root)
226
227
228
229class StudentsContainerUITests(StudentsFullSetup):
230    # Tests for StudentsContainer class views and pages
231
232    layer = FunctionalLayer
233
234    def test_anonymous_access(self):
235        # Anonymous users can't access students containers
236        self.assertRaises(
237            Unauthorized, self.browser.open, self.container_path)
238        self.assertRaises(
239            Unauthorized, self.browser.open, self.manage_container_path)
240        return
241
242    def test_manage_access(self):
243        # Managers can access the view page of students
244        # containers and can perform actions
245        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
246        self.browser.open(self.container_path)
247        self.assertEqual(self.browser.headers['Status'], '200 Ok')
248        self.assertEqual(self.browser.url, self.container_path)
249        self.browser.getLink("Manage student section").click()
250        self.assertEqual(self.browser.headers['Status'], '200 Ok')
251        self.assertEqual(self.browser.url, self.manage_container_path)
252        return
253
254    def test_add_search_delete_students(self):
255        # Managers can add search and remove students
256        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
257        self.browser.open(self.manage_container_path)
258        self.browser.getLink("Add student").click()
259        self.assertEqual(self.browser.headers['Status'], '200 Ok')
260        self.assertEqual(self.browser.url, self.add_student_path)
261        self.browser.getControl(name="form.firstname").value = 'Bob'
262        self.browser.getControl(name="form.lastname").value = 'Tester'
263        self.browser.getControl(name="form.reg_number").value = '123'
264        self.browser.getControl("Create student record").click()
265        self.assertTrue('Registration number exists already'
266            in self.browser.contents)
267        self.browser.getControl(name="form.reg_number").value = '1234'
268        self.browser.getControl("Create student record").click()
269        self.assertTrue('Student record created' in self.browser.contents)
270
271        # Registration and matric numbers must be unique
272        self.browser.getLink("Manage").click()
273        self.browser.getControl(name="form.reg_number").value = '123'
274        self.browser.getControl("Save").click()
275        self.assertMatches('...Registration number exists...',
276                           self.browser.contents)
277        self.browser.getControl(name="form.reg_number").value = '789'
278        self.browser.getControl(name="form.matric_number").value = '234'
279        self.browser.getControl("Save").click()
280        self.assertMatches('...Matriculation number exists...',
281                           self.browser.contents)
282
283        # We can find a student with a certain student_id
284        self.browser.open(self.container_path)
285        self.browser.getControl("Search").click()
286        self.assertTrue('Empty search string' in self.browser.contents)
287        self.browser.getControl(name="searchtype").value = ['student_id']
288        self.browser.getControl(name="searchterm").value = self.student_id
289        self.browser.getControl("Search").click()
290        self.assertTrue('Anna Tester' in self.browser.contents)
291
292        # We can find a student in a certain session
293        self.browser.open(self.container_path)
294        self.browser.getControl(name="searchtype").value = ['current_session']
295        self.browser.getControl(name="searchterm").value = '2004'
296        self.browser.getControl("Search").click()
297        self.assertTrue('Anna Tester' in self.browser.contents)
298        # Session fileds require integer values
299        self.browser.open(self.container_path)
300        self.browser.getControl(name="searchtype").value = ['current_session']
301        self.browser.getControl(name="searchterm").value = '2004/2005'
302        self.browser.getControl("Search").click()
303        self.assertTrue('Only year dates allowed' in self.browser.contents)
304        self.browser.open(self.manage_container_path)
305        self.browser.getControl(name="searchtype").value = ['current_session']
306        self.browser.getControl(name="searchterm").value = '2004/2005'
307        self.browser.getControl("Search").click()
308        self.assertTrue('Only year dates allowed' in self.browser.contents)
309
310        # We can find a student in a certain study_mode
311        self.browser.open(self.container_path)
312        self.browser.getControl(name="searchtype").value = ['current_mode']
313        self.browser.getControl(name="searchterm").value = 'ug_ft'
314        self.browser.getControl("Search").click()
315        self.assertTrue('Anna Tester' in self.browser.contents)
316
317        # We can find a student in a certain department
318        self.browser.open(self.container_path)
319        self.browser.getControl(name="searchtype").value = ['depcode']
320        self.browser.getControl(name="searchterm").value = 'dep1'
321        self.browser.getControl("Search").click()
322        self.assertTrue('Anna Tester' in self.browser.contents)
323
324        # We can find a student by searching for all kind of name parts
325        self.browser.open(self.manage_container_path)
326        self.browser.getControl("Search").click()
327        self.assertTrue('Empty search string' in self.browser.contents)
328        self.browser.getControl(name="searchtype").value = ['fullname']
329        self.browser.getControl(name="searchterm").value = 'Anna Tester'
330        self.browser.getControl("Search").click()
331        self.assertTrue('Anna Tester' in self.browser.contents)
332        self.browser.open(self.manage_container_path)
333        self.browser.getControl(name="searchtype").value = ['fullname']
334        self.browser.getControl(name="searchterm").value = 'Anna'
335        self.browser.getControl("Search").click()
336        self.assertTrue('Anna Tester' in self.browser.contents)
337        self.browser.open(self.manage_container_path)
338        self.browser.getControl(name="searchtype").value = ['fullname']
339        self.browser.getControl(name="searchterm").value = 'Tester'
340        self.browser.getControl("Search").click()
341        self.assertTrue('Anna Tester' in self.browser.contents)
342        self.browser.open(self.manage_container_path)
343        self.browser.getControl(name="searchtype").value = ['fullname']
344        self.browser.getControl(name="searchterm").value = 'An'
345        self.browser.getControl("Search").click()
346        self.assertFalse('Anna Tester' in self.browser.contents)
347        self.browser.open(self.manage_container_path)
348        self.browser.getControl(name="searchtype").value = ['fullname']
349        self.browser.getControl(name="searchterm").value = 'An*'
350        self.browser.getControl("Search").click()
351        self.assertTrue('Anna Tester' in self.browser.contents)
352        self.browser.open(self.manage_container_path)
353        self.browser.getControl(name="searchtype").value = ['fullname']
354        self.browser.getControl(name="searchterm").value = 'tester'
355        self.browser.getControl("Search").click()
356        self.assertTrue('Anna Tester' in self.browser.contents)
357        self.browser.open(self.manage_container_path)
358        self.browser.getControl(name="searchtype").value = ['fullname']
359        self.browser.getControl(name="searchterm").value = 'Tester Ana'
360        self.browser.getControl("Search").click()
361        self.assertFalse('Anna Tester' in self.browser.contents)
362        self.browser.open(self.manage_container_path)
363        self.browser.getControl(name="searchtype").value = ['fullname']
364        self.browser.getControl(name="searchterm").value = 'Tester Anna'
365        self.browser.getControl("Search").click()
366        self.assertTrue('Anna Tester' in self.browser.contents)
367        # The old searchterm will be used again
368        self.browser.getControl("Search").click()
369        self.assertTrue('Anna Tester' in self.browser.contents)
370
371        # The catalog is informed when studycourse objects have been
372        # edited
373        self.browser.open(self.studycourse_path + '/manage')
374        self.browser.getControl(name="form.current_session").value = ['2010']
375        self.browser.getControl(name="form.entry_session").value = ['2010']
376        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
377        self.browser.getControl("Save").click()
378
379        # We can find the student in the new session
380        self.browser.open(self.manage_container_path)
381        self.browser.getControl(name="searchtype").value = ['current_session']
382        self.browser.getControl(name="searchterm").value = '2010'
383        self.browser.getControl("Search").click()
384        self.assertTrue('Anna Tester' in self.browser.contents)
385
386        ctrl = self.browser.getControl(name='entries')
387        ctrl.getControl(value=self.student_id).selected = True
388        self.browser.getControl("Remove selected", index=0).click()
389        self.assertTrue('Successfully removed' in self.browser.contents)
390        self.browser.getControl(name="searchtype").value = ['student_id']
391        self.browser.getControl(name="searchterm").value = self.student_id
392        self.browser.getControl("Search").click()
393        self.assertTrue('No student found' in self.browser.contents)
394
395        self.browser.open(self.container_path)
396        self.browser.getControl(name="searchtype").value = ['student_id']
397        self.browser.getControl(name="searchterm").value = self.student_id
398        self.browser.getControl("Search").click()
399        self.assertTrue('No student found' in self.browser.contents)
400        return
401
402class StudentUITests(StudentsFullSetup):
403    # Tests for Student class views and pages
404
405    layer = FunctionalLayer
406
407    def test_student_properties(self):
408        self.student['studycourse'].current_level = 100
409        self.assertEqual(self.student.current_level, 100)
410        self.student['studycourse'].current_session = 2011
411        self.assertEqual(self.student.current_session, 2011)
412        self.student['studycourse'].current_verdict = 'A'
413        self.assertEqual(self.student.current_verdict, 'A')
414        return
415
416    def test_basic_auth(self):
417        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
418        self.browser.open('http://localhost/app')
419        self.browser.getLink("Logout").click()
420        self.assertTrue('You have been logged out' in self.browser.contents)
421        # But we are still logged in since we've used basic authentication here.
422        # Wikipedia says: Existing browsers retain authentication information
423        # until the tab or browser is closed or the user clears the history.
424        # HTTP does not provide a method for a server to direct clients to
425        # discard these cached credentials. This means that there is no
426        # effective way for a server to "log out" the user without closing
427        # the browser. This is a significant defect that requires browser
428        # manufacturers to support a "logout" user interface element ...
429        self.assertTrue('Manager' in self.browser.contents)
430
431    def test_manage_access(self):
432        # Managers can access the pages of students
433        # and can perform actions
434        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
435        self.browser.open(self.student_path)
436        self.assertEqual(self.browser.headers['Status'], '200 Ok')
437        self.assertEqual(self.browser.url, self.student_path)
438        self.browser.getLink("Trigger").click()
439        self.assertEqual(self.browser.headers['Status'], '200 Ok')
440        # Managers can trigger transitions
441        self.browser.getControl(name="transition").value = ['admit']
442        self.browser.getControl("Save").click()
443        # Managers can edit base
444        self.browser.open(self.student_path)
445        self.browser.getLink("Manage").click()
446        self.assertEqual(self.browser.url, self.manage_student_path)
447        self.assertEqual(self.browser.headers['Status'], '200 Ok')
448        self.browser.getControl(name="form.firstname").value = 'John'
449        self.browser.getControl(name="form.lastname").value = 'Tester'
450        self.browser.getControl(name="form.reg_number").value = '345'
451        self.browser.getControl(name="password").value = 'secret'
452        self.browser.getControl(name="control_password").value = 'secret'
453        self.browser.getControl("Save").click()
454        self.assertMatches('...Form has been saved...',
455                           self.browser.contents)
456        self.browser.open(self.student_path)
457        self.browser.getLink("Clearance Data").click()
458        self.assertEqual(self.browser.headers['Status'], '200 Ok')
459        self.assertEqual(self.browser.url, self.clearance_path)
460        self.browser.getLink("Manage").click()
461        self.assertEqual(self.browser.headers['Status'], '200 Ok')
462        self.assertEqual(self.browser.url, self.manage_clearance_path)
463        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
464        self.browser.getControl("Save").click()
465        self.assertMatches('...Form has been saved...',
466                           self.browser.contents)
467
468        self.browser.open(self.student_path)
469        self.browser.getLink("Personal Data").click()
470        self.assertEqual(self.browser.headers['Status'], '200 Ok')
471        self.assertEqual(self.browser.url, self.personal_path)
472        self.browser.getLink("Manage").click()
473        self.assertEqual(self.browser.headers['Status'], '200 Ok')
474        self.assertEqual(self.browser.url, self.manage_personal_path)
475        self.browser.open(self.personal_path)
476        self.browser.getLink("Edit").click()
477        self.assertEqual(self.browser.headers['Status'], '200 Ok')
478        self.assertEqual(self.browser.url, self.edit_personal_path)
479        self.browser.getControl("Save").click()
480        self.assertMatches('...Form has been saved...',
481                           self.browser.contents)
482
483        # Managers can browse all subobjects
484        self.browser.open(self.student_path)
485        self.browser.getLink("Payments").click()
486        self.assertEqual(self.browser.headers['Status'], '200 Ok')
487        self.assertEqual(self.browser.url, self.payments_path)
488        self.browser.open(self.student_path)
489        self.browser.getLink("Accommodation").click()
490        self.assertEqual(self.browser.headers['Status'], '200 Ok')
491        self.assertEqual(self.browser.url, self.acco_path)
492        self.browser.open(self.student_path)
493        self.browser.getLink("History").click()
494        self.assertEqual(self.browser.headers['Status'], '200 Ok')
495        self.assertEqual(self.browser.url, self.history_path)
496        self.assertMatches('...Admitted by Manager...',
497                           self.browser.contents)
498        # Only the Application Slip does not exist
499        self.assertFalse('Application Slip' in self.browser.contents)
500        return
501
502    def test_manage_contact_student(self):
503        # Managers can contact student
504        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
505        self.student.email = None
506        self.browser.open(self.student_path)
507        self.browser.getLink("Send email").click()
508        self.browser.getControl(name="form.subject").value = 'Important subject'
509        self.browser.getControl(name="form.body").value = 'Hello!'
510        self.browser.getControl("Send message now").click()
511        self.assertTrue('An smtp server error occurred' in self.browser.contents)
512        self.student.email = 'xx@yy.zz'
513        self.browser.getControl("Send message now").click()
514        self.assertTrue('Your message has been sent' in self.browser.contents)
515        return
516
517    def test_manage_remove_department(self):
518        # Lazy student is studying CERT1
519        lazystudent = Student()
520        lazystudent.firstname = u'Lazy'
521        lazystudent.lastname = u'Student'
522        self.app['students'].addStudent(lazystudent)
523        student_id = lazystudent.student_id
524        student_path = self.container_path + '/' + student_id
525        lazystudent['studycourse'].certificate = self.certificate
526        notify(grok.ObjectModifiedEvent(lazystudent))
527        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
528        self.browser.open(student_path + '/studycourse')
529        self.assertTrue('CERT1' in self.browser.contents)
530        # After some years the department is removed
531        del self.app['faculties']['fac1']['dep1']
532        # So CERT1 does no longer exist and lazy student's
533        # certificate reference is removed too
534        self.browser.open(student_path + '/studycourse')
535        self.assertEqual(self.browser.headers['Status'], '200 Ok')
536        self.assertEqual(self.browser.url, student_path + '/studycourse')
537        self.assertFalse('CERT1' in self.browser.contents)
538        self.assertMatches('...<div>--</div>...',
539                           self.browser.contents)
540
541    def test_manage_upload_file(self):
542        # Managers can upload a file via the StudentClearanceManageFormPage
543        # The image is stored even if form has errors
544        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
545        self.browser.open(self.manage_clearance_path)
546        # No birth certificate has been uploaded yet
547        # Browsing the link shows a placerholder image
548        self.browser.open('birth_certificate')
549        self.assertEqual(
550            self.browser.headers['content-type'], 'image/jpeg')
551        self.assertEqual(len(self.browser.contents), PH_LEN)
552        # Create a pseudo image file and select it to be uploaded in form
553        # as birth certificate
554        self.browser.open(self.manage_clearance_path)
555        image = open(SAMPLE_IMAGE, 'rb')
556        ctrl = self.browser.getControl(name='birthcertificateupload')
557        file_ctrl = ctrl.mech_control
558        file_ctrl.add_file(image, filename='my_birth_certificate.jpg')
559        # The Save action does not upload files
560        self.browser.getControl("Save").click() # submit form
561        self.assertFalse(
562            '<a target="image" href="birth_certificate">'
563            in self.browser.contents)
564        # ... but the correct upload submit button does
565        image = open(SAMPLE_IMAGE)
566        ctrl = self.browser.getControl(name='birthcertificateupload')
567        file_ctrl = ctrl.mech_control
568        file_ctrl.add_file(image, filename='my_birth_certificate.jpg')
569        self.browser.getControl(
570            name='upload_birthcertificateupload').click()
571        # There is a correct <img> link included
572        self.assertTrue(
573            '<a target="image" href="birth_certificate">'
574            in self.browser.contents)
575        # Browsing the link shows a real image
576        self.browser.open('birth_certificate')
577        self.assertEqual(
578            self.browser.headers['content-type'], 'image/jpeg')
579        self.assertEqual(len(self.browser.contents), 2787)
580        # Reuploading a file which is bigger than 150k will raise an error
581        self.browser.open(self.manage_clearance_path)
582        # An image > 150K
583        big_image = StringIO(open(SAMPLE_IMAGE, 'rb').read() * 75)
584        ctrl = self.browser.getControl(name='birthcertificateupload')
585        file_ctrl = ctrl.mech_control
586        file_ctrl.add_file(big_image, filename='my_birth_certificate.jpg')
587        self.browser.getControl(
588            name='upload_birthcertificateupload').click()
589        self.assertTrue(
590            'Uploaded file is too big' in self.browser.contents)
591        # we do not rely on filename extensions given by uploaders
592        image = open(SAMPLE_IMAGE, 'rb') # a jpg-file
593        ctrl = self.browser.getControl(name='birthcertificateupload')
594        file_ctrl = ctrl.mech_control
595        # tell uploaded file is bmp
596        file_ctrl.add_file(image, filename='my_birth_certificate.bmp')
597        self.browser.getControl(
598            name='upload_birthcertificateupload').click()
599        self.assertTrue(
600            # jpg file was recognized
601            'File birth_certificate.jpg uploaded.' in self.browser.contents)
602        # File names must meet several conditions
603        bmp_image = open(SAMPLE_IMAGE_BMP, 'rb')
604        ctrl = self.browser.getControl(name='birthcertificateupload')
605        file_ctrl = ctrl.mech_control
606        file_ctrl.add_file(bmp_image, filename='my_birth_certificate.bmp')
607        self.browser.getControl(
608            name='upload_birthcertificateupload').click()
609        self.assertTrue('Only the following extensions are allowed'
610            in self.browser.contents)
611        # Managers can delete files
612        self.browser.getControl(name='delete_birthcertificateupload').click()
613        self.assertTrue(
614            'birth_certificate deleted' in self.browser.contents)
615
616        # Managers can upload a file via the StudentBaseManageFormPage
617        self.browser.open(self.manage_student_path)
618        image = open(SAMPLE_IMAGE_BMP, 'rb')
619        ctrl = self.browser.getControl(name='passportuploadmanage')
620        file_ctrl = ctrl.mech_control
621        file_ctrl.add_file(image, filename='my_photo.bmp')
622        self.browser.getControl(
623            name='upload_passportuploadmanage').click()
624        self.assertTrue('jpg file extension expected'
625            in self.browser.contents)
626        ctrl = self.browser.getControl(name='passportuploadmanage')
627        file_ctrl = ctrl.mech_control
628        image = open(SAMPLE_IMAGE, 'rb')
629        file_ctrl.add_file(image, filename='my_photo.jpg')
630        self.browser.getControl(
631            name='upload_passportuploadmanage').click()
632        self.assertTrue(
633            '<img align="middle" height="125px" src="passport.jpg" />'
634            in self.browser.contents)
635        # We remove the passport file again
636        self.browser.open(self.manage_student_path)
637        self.browser.getControl('Delete').click()
638        self.browser.open(self.student_path + '/clearance.pdf')
639        self.assertEqual(self.browser.headers['Status'], '200 Ok')
640        self.assertEqual(self.browser.headers['Content-Type'],
641                         'application/pdf')
642
643    def test_manage_course_lists(self):
644        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
645        self.browser.open(self.student_path)
646        self.browser.getLink("Study Course").click()
647        self.assertEqual(self.browser.headers['Status'], '200 Ok')
648        self.assertEqual(self.browser.url, self.studycourse_path)
649        self.assertTrue('Undergraduate Full-Time' in self.browser.contents)
650        self.browser.getLink("Manage").click()
651        self.assertTrue('Manage study course' in self.browser.contents)
652        # Before we can select a level, the certificate must
653        # be selected and saved
654        self.browser.getControl(name="form.certificate").value = ['CERT1']
655        self.browser.getControl(name="form.current_session").value = ['2004']
656        self.browser.getControl(name="form.current_verdict").value = ['A']
657        self.browser.getControl(name="form.entry_mode").value = ['ug_ft']
658        self.browser.getControl("Save").click()
659        # Now we can save also the current level which depends on start and end
660        # level of the certificate
661        self.browser.getControl(name="form.current_level").value = ['100']
662        self.browser.getControl("Save").click()
663        # Managers can add and remove any study level (course list)
664        self.browser.getControl(name="addlevel").value = ['100']
665        self.browser.getControl("Add study level").click()
666        self.assertMatches('...<span>100</span>...', self.browser.contents)
667        self.assertEqual(self.student['studycourse']['100'].level, 100)
668        self.assertEqual(self.student['studycourse']['100'].level_session, 2004)
669        self.browser.getControl("Add study level").click()
670        self.assertMatches('...This level exists...', self.browser.contents)
671        self.browser.getControl("Remove selected").click()
672        self.assertMatches(
673            '...No study level selected...', self.browser.contents)
674        self.browser.getControl(name="val_id").value = ['100']
675        self.browser.getControl("Remove selected").click()
676        self.assertMatches('...Successfully removed...', self.browser.contents)
677        # Add level again
678        self.browser.getControl(name="addlevel").value = ['100']
679        self.browser.getControl("Add study level").click()
680
681        # Managers can view and manage course lists
682        self.browser.getLink("100").click()
683        self.assertMatches(
684            '...: Study Level 100 (Year 1)...', self.browser.contents)
685        self.browser.getLink("Manage").click()
686        self.browser.getControl(name="form.level_session").value = ['2002']
687        self.browser.getControl("Save").click()
688        self.browser.getControl("Remove selected").click()
689        self.assertMatches('...No ticket selected...', self.browser.contents)
690        ctrl = self.browser.getControl(name='val_id')
691        ctrl.getControl(value='COURSE1').selected = True
692        self.browser.getControl("Remove selected", index=0).click()
693        self.assertTrue('Successfully removed' in self.browser.contents)
694        self.browser.getControl("Add course ticket").click()
695        self.browser.getControl(name="form.course").value = ['COURSE1']
696        self.browser.getControl("Add course ticket").click()
697        self.assertTrue('Successfully added' in self.browser.contents)
698        self.browser.getControl("Add course ticket").click()
699        self.browser.getControl(name="form.course").value = ['COURSE1']
700        self.browser.getControl("Add course ticket").click()
701        self.assertTrue('The ticket exists' in self.browser.contents)
702        self.browser.getControl("Cancel").click()
703        self.browser.getLink("COURSE1").click()
704        self.browser.getLink("Manage").click()
705        self.browser.getControl(name="form.score").value = '10'
706        self.browser.getControl("Save").click()
707        self.assertTrue('Form has been saved' in self.browser.contents)
708        # Carry-over courses will be collected when next level is created
709        self.browser.open(self.student_path + '/studycourse/manage')
710        # Add next level
711        self.browser.getControl(name="addlevel").value = ['200']
712        self.browser.getControl("Add study level").click()
713        self.browser.getLink("200").click()
714        self.assertMatches(
715            '...: Study Level 200 (Year 2)...', self.browser.contents)
716        # COURSE1 has score 0 and thus will become a carry-over course
717        # in level 200
718        self.assertEqual(
719            sorted(self.student['studycourse']['200'].keys()), [u'COURSE1'])
720        self.assertTrue(
721            self.student['studycourse']['200']['COURSE1'].carry_over)
722        return
723
724    def test_manage_workflow(self):
725        # Managers can pass through the whole workflow
726        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
727        student = self.app['students'][self.student_id]
728        self.browser.open(self.trigtrans_path)
729        self.assertTrue(student.clearance_locked)
730        self.browser.getControl(name="transition").value = ['admit']
731        self.browser.getControl("Save").click()
732        self.assertTrue(student.clearance_locked)
733        self.browser.getControl(name="transition").value = ['start_clearance']
734        self.browser.getControl("Save").click()
735        self.assertFalse(student.clearance_locked)
736        self.browser.getControl(name="transition").value = ['request_clearance']
737        self.browser.getControl("Save").click()
738        self.assertTrue(student.clearance_locked)
739        self.browser.getControl(name="transition").value = ['clear']
740        self.browser.getControl("Save").click()
741        # Managers approve payment, they do not pay
742        self.assertFalse('pay_first_school_fee' in self.browser.contents)
743        self.browser.getControl(
744            name="transition").value = ['approve_first_school_fee']
745        self.browser.getControl("Save").click()
746        self.browser.getControl(name="transition").value = ['reset6']
747        self.browser.getControl("Save").click()
748        # In state returning the pay_school_fee transition triggers some
749        # changes of attributes
750        self.browser.getControl(name="transition").value = ['approve_school_fee']
751        self.browser.getControl("Save").click()
752        self.assertEqual(student['studycourse'].current_session, 2005) # +1
753        self.assertEqual(student['studycourse'].current_level, 200) # +100
754        self.assertEqual(student['studycourse'].current_verdict, '0') # 0 = Zero = not set
755        self.assertEqual(student['studycourse'].previous_verdict, 'A')
756        self.browser.getControl(name="transition").value = ['register_courses']
757        self.browser.getControl("Save").click()
758        self.browser.getControl(name="transition").value = ['validate_courses']
759        self.browser.getControl("Save").click()
760        self.browser.getControl(name="transition").value = ['return']
761        self.browser.getControl("Save").click()
762        return
763
764    def test_manage_pg_workflow(self):
765        # Managers can pass through the whole workflow
766        IWorkflowState(self.student).setState('school fee paid')
767        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
768        student = self.app['students'][self.student_id]
769        self.browser.open(self.trigtrans_path)
770        self.assertTrue('<option value="reset6">' in self.browser.contents)
771        self.assertTrue('<option value="register_courses">' in self.browser.contents)
772        self.assertTrue('<option value="reset5">' in self.browser.contents)
773        self.certificate.study_mode = 'pg_ft'
774        self.browser.open(self.trigtrans_path)
775        self.assertFalse('<option value="reset6">' in self.browser.contents)
776        self.assertFalse('<option value="register_courses">' in self.browser.contents)
777        self.assertTrue('<option value="reset5">' in self.browser.contents)
778        return
779
780    def test_manage_import(self):
781        # Managers can import student data files
782        datacenter_path = 'http://localhost/app/datacenter'
783        # Prepare a csv file for students
784        open('students.csv', 'wb').write(
785"""firstname,lastname,reg_number,date_of_birth,matric_number,email,phone,sex,password
786Aaren,Pieri,1,1990-01-02,100000,aa@aa.ng,1234,m,mypwd1
787Claus,Finau,2,1990-01-03,100001,aa@aa.ng,1234,m,mypwd1
788Brit,Berson,3,1990-01-04,100001,aa@aa.ng,1234,m,mypwd1
789""")
790        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
791        self.browser.open(datacenter_path)
792        self.browser.getLink('Upload data').click()
793        filecontents = StringIO(open('students.csv', 'rb').read())
794        filewidget = self.browser.getControl(name='uploadfile:file')
795        filewidget.add_file(filecontents, 'text/plain', 'students.csv')
796        self.browser.getControl(name='SUBMIT').click()
797        self.browser.getLink('Process data').click()
798        button = lookup_submit_value(
799            'select', 'students_zope.mgr.csv', self.browser)
800        button.click()
801        importerselect = self.browser.getControl(name='importer')
802        modeselect = self.browser.getControl(name='mode')
803        importerselect.getControl('Student Processor').selected = True
804        modeselect.getControl(value='create').selected = True
805        self.browser.getControl('Proceed to step 3').click()
806        self.assertTrue('Header fields OK' in self.browser.contents)
807        self.browser.getControl('Perform import').click()
808        self.assertTrue('Processing of 1 rows failed' in self.browser.contents)
809        self.assertTrue('Successfully processed 2 rows' in self.browser.contents)
810        self.assertTrue('Batch processing finished' in self.browser.contents)
811        open('studycourses.csv', 'wb').write(
812"""reg_number,matric_number,certificate,current_session,current_level
8131,,CERT1,2008,100
814,100001,CERT1,2008,100
815,100002,CERT1,2008,100
816""")
817        self.browser.open(datacenter_path)
818        self.browser.getLink('Upload data').click()
819        filecontents = StringIO(open('studycourses.csv', 'rb').read())
820        filewidget = self.browser.getControl(name='uploadfile:file')
821        filewidget.add_file(filecontents, 'text/plain', 'studycourses.csv')
822        self.browser.getControl(name='SUBMIT').click()
823        self.browser.getLink('Process data').click()
824        button = lookup_submit_value(
825            'select', 'studycourses_zope.mgr.csv', self.browser)
826        button.click()
827        importerselect = self.browser.getControl(name='importer')
828        modeselect = self.browser.getControl(name='mode')
829        importerselect.getControl(
830            'StudentStudyCourse Processor (update only)').selected = True
831        modeselect.getControl(value='create').selected = True
832        self.browser.getControl('Proceed to step 3').click()
833        self.assertTrue('Update mode only' in self.browser.contents)
834        self.browser.getControl('Proceed to step 3').click()
835        self.assertTrue('Header fields OK' in self.browser.contents)
836        self.browser.getControl('Perform import').click()
837        self.assertTrue('Processing of 1 rows failed' in self.browser.contents)
838        self.assertTrue('Successfully processed 2 rows'
839                        in self.browser.contents)
840        # The students are properly indexed and we can
841        # thus find a student in  the department
842        self.browser.open(self.manage_container_path)
843        self.browser.getControl(name="searchtype").value = ['depcode']
844        self.browser.getControl(name="searchterm").value = 'dep1'
845        self.browser.getControl("Search").click()
846        self.assertTrue('Aaren Pieri' in self.browser.contents)
847        # We can search for a new student by name ...
848        self.browser.getControl(name="searchtype").value = ['fullname']
849        self.browser.getControl(name="searchterm").value = 'Claus'
850        self.browser.getControl("Search").click()
851        self.assertTrue('Claus Finau' in self.browser.contents)
852        # ... and check if the imported password has been properly set
853        ctrl = self.browser.getControl(name='entries')
854        value = ctrl.options[0]
855        claus = self.app['students'][value]
856        self.assertTrue(IUserAccount(claus).checkPassword('mypwd1'))
857        return
858
859    def test_handle_clearance_by_co(self):
860        # Create clearance officer
861        self.app['users'].addUser('mrclear', 'mrclearsecret')
862        self.app['users']['mrclear'].email = 'mrclear@foo.ng'
863        self.app['users']['mrclear'].title = 'Carlo Pitter'
864        # Clearance officers need not necessarily to get
865        # the StudentsOfficer site role
866        #prmglobal = IPrincipalRoleManager(self.app)
867        #prmglobal.assignRoleToPrincipal('waeup.StudentsOfficer', 'mrclear')
868        # Assign local ClearanceOfficer role
869        department = self.app['faculties']['fac1']['dep1']
870        prmlocal = IPrincipalRoleManager(department)
871        prmlocal.assignRoleToPrincipal('waeup.local.ClearanceOfficer', 'mrclear')
872        IWorkflowState(self.student).setState('clearance started')
873        # Login as clearance officer
874        self.browser.open(self.login_path)
875        self.browser.getControl(name="form.login").value = 'mrclear'
876        self.browser.getControl(name="form.password").value = 'mrclearsecret'
877        self.browser.getControl("Login").click()
878        self.assertMatches('...You logged in...', self.browser.contents)
879        # CO can see his roles
880        self.browser.getLink("My Roles").click()
881        self.assertMatches(
882            '...<div>Academics Officer (view only)</div>...',
883            self.browser.contents)
884        #self.assertMatches(
885        #    '...<div>Students Officer (view only)</div>...',
886        #    self.browser.contents)
887        # But not his local role ...
888        self.assertFalse('Clearance Officer' in self.browser.contents)
889        # ... because we forgot to notify the department that the local role
890        # has changed
891        notify(LocalRoleSetEvent(
892            department, 'waeup.local.ClearanceOfficer', 'mrclear', granted=True))
893        self.browser.open('http://localhost/app/users/mrclear/my_roles')
894        self.assertTrue('Clearance Officer' in self.browser.contents)
895        self.assertMatches(
896            '...<a href="http://localhost/app/faculties/fac1/dep1">...',
897            self.browser.contents)
898        # CO can view the student ...
899        self.browser.open(self.clearance_path)
900        self.assertEqual(self.browser.headers['Status'], '200 Ok')
901        self.assertEqual(self.browser.url, self.clearance_path)
902        # ... but not other students
903        other_student = Student()
904        other_student.firstname = u'Dep2'
905        other_student.lastname = u'Student'
906        self.app['students'].addStudent(other_student)
907        other_student_path = (
908            'http://localhost/app/students/%s' % other_student.student_id)
909        self.assertRaises(
910            Unauthorized, self.browser.open, other_student_path)
911        # Only in state clearance requested the CO does see the 'Clear' button
912        self.browser.open(self.clearance_path)
913        self.assertFalse('Clear student' in self.browser.contents)
914        IWorkflowInfo(self.student).fireTransition('request_clearance')
915        self.browser.open(self.clearance_path)
916        self.assertTrue('Clear student' in self.browser.contents)
917        self.browser.getLink("Clear student").click()
918        self.assertTrue('Student has been cleared' in self.browser.contents)
919        self.assertTrue('cleared' in self.browser.contents)
920        self.browser.open(self.history_path)
921        self.assertTrue('Cleared by Carlo Pitter' in self.browser.contents)
922        # Hide real name.
923        self.app['users']['mrclear'].public_name = 'My Public Name'
924        self.browser.open(self.clearance_path)
925        self.browser.getLink("Reject clearance").click()
926        self.assertTrue('Clearance has been annulled' in self.browser.contents)
927        urlmessage = 'Clearance+has+been+annulled.'
928        # CO does now see the contact form
929        self.assertEqual(self.browser.url, self.student_path +
930            '/contactstudent?subject=%s' % urlmessage)
931        self.assertTrue('clearance started' in self.browser.contents)
932        self.browser.open(self.history_path)
933        self.assertTrue("Reset to 'clearance started' by My Public Name" in
934            self.browser.contents)
935        IWorkflowInfo(self.student).fireTransition('request_clearance')
936        self.browser.open(self.clearance_path)
937        self.browser.getLink("Reject clearance").click()
938        self.assertTrue('Clearance request has been rejected'
939            in self.browser.contents)
940        self.assertTrue('clearance started' in self.browser.contents)
941        # CO does now also see the contact form and can send a message
942        self.browser.getControl(name="form.subject").value = 'Important subject'
943        self.browser.getControl(name="form.body").value = 'Clearance rejected'
944        self.browser.getControl("Send message now").click()
945        self.assertTrue('Your message has been sent' in self.browser.contents)
946        # The CO can't clear students if not in state
947        # clearance requested
948        self.browser.open(self.student_path + '/clear')
949        self.assertTrue('Student is in wrong state'
950            in self.browser.contents)
951        # The CO can go to his department throug the my_roles page
952        self.browser.open('http://localhost/app/users/mrclear/my_roles')
953        self.browser.getLink("http://localhost/app/faculties/fac1/dep1").click()
954        # and view the list of students
955        self.browser.getLink("Show students").click()
956        self.assertTrue(self.student_id in self.browser.contents)
957
958    def test_handle_courses_by_ca(self):
959        # Create course adviser
960        self.app['users'].addUser('mrsadvise', 'mrsadvisesecret')
961        self.app['users']['mrsadvise'].email = 'mradvise@foo.ng'
962        self.app['users']['mrsadvise'].title = u'Helen Procter'
963        # Assign local CourseAdviser100 role for a certificate
964        cert = self.app['faculties']['fac1']['dep1'].certificates['CERT1']
965        prmlocal = IPrincipalRoleManager(cert)
966        prmlocal.assignRoleToPrincipal('waeup.local.CourseAdviser100', 'mrsadvise')
967        IWorkflowState(self.student).setState('school fee paid')
968        # Login as course adviser
969        self.browser.open(self.login_path)
970        self.browser.getControl(name="form.login").value = 'mrsadvise'
971        self.browser.getControl(name="form.password").value = 'mrsadvisesecret'
972        self.browser.getControl("Login").click()
973        self.assertMatches('...You logged in...', self.browser.contents)
974        # CO can see his roles
975        self.browser.getLink("My Roles").click()
976        self.assertMatches(
977            '...<div>Academics Officer (view only)</div>...',
978            self.browser.contents)
979        # But not his local role ...
980        self.assertFalse('Course Adviser' in self.browser.contents)
981        # ... because we forgot to notify the certificate that the local role
982        # has changed
983        notify(LocalRoleSetEvent(
984            cert, 'waeup.local.CourseAdviser100', 'mrsadvise', granted=True))
985        self.browser.open('http://localhost/app/users/mrsadvise/my_roles')
986        self.assertTrue('Course Adviser 100L' in self.browser.contents)
987        self.assertMatches(
988            '...<a href="http://localhost/app/faculties/fac1/dep1/certificates/CERT1">...',
989            self.browser.contents)
990        # CA can view the student ...
991        self.browser.open(self.student_path)
992        self.assertEqual(self.browser.headers['Status'], '200 Ok')
993        self.assertEqual(self.browser.url, self.student_path)
994        # ... but not other students
995        other_student = Student()
996        other_student.firstname = u'Dep2'
997        other_student.lastname = u'Student'
998        self.app['students'].addStudent(other_student)
999        other_student_path = (
1000            'http://localhost/app/students/%s' % other_student.student_id)
1001        self.assertRaises(
1002            Unauthorized, self.browser.open, other_student_path)
1003        # We add study level 110 to the student's studycourse
1004        studylevel = StudentStudyLevel()
1005        studylevel.level = 110
1006        self.student['studycourse'].addStudentStudyLevel(
1007            cert,studylevel)
1008        L110_student_path = self.studycourse_path + '/110'
1009        # Only in state courses registered and only if the current level
1010        # corresponds with the name of the study level object
1011        # the 100L CA does see the 'Validate' button
1012        self.browser.open(L110_student_path)
1013        self.assertFalse('Validate courses' in self.browser.contents)
1014        IWorkflowInfo(self.student).fireTransition('register_courses')
1015        self.browser.open(L110_student_path)
1016        self.assertFalse('Validate courses' in self.browser.contents)
1017        self.student['studycourse'].current_level = 110
1018        self.browser.open(L110_student_path)
1019        self.assertTrue('Validate courses' in self.browser.contents)
1020        # ... but a 100L CA does not see the button on other levels
1021        studylevel2 = StudentStudyLevel()
1022        studylevel2.level = 200
1023        self.student['studycourse'].addStudentStudyLevel(
1024            cert,studylevel2)
1025        L200_student_path = self.studycourse_path + '/200'
1026        self.browser.open(L200_student_path)
1027        self.assertFalse('Validate courses' in self.browser.contents)
1028        self.browser.open(L110_student_path)
1029        self.browser.getLink("Validate courses").click()
1030        self.assertTrue('Course list has been validated' in self.browser.contents)
1031        self.assertTrue('courses validated' in self.browser.contents)
1032        self.assertEqual(self.student['studycourse']['110'].validated_by,
1033            'Helen Procter')
1034        self.assertMatches(
1035            '<YYYY-MM-DD hh:mm:ss>',
1036            self.student['studycourse']['110'].validation_date.strftime(
1037                "%Y-%m-%d %H:%M:%S"))
1038        self.browser.getLink("Reject courses").click()
1039        self.assertTrue('Course list request has been annulled.'
1040            in self.browser.contents)
1041        urlmessage = 'Course+list+request+has+been+annulled.'
1042        self.assertEqual(self.browser.url, self.student_path +
1043            '/contactstudent?subject=%s' % urlmessage)
1044        self.assertTrue('school fee paid' in self.browser.contents)
1045        self.assertTrue(self.student['studycourse']['110'].validated_by is None)
1046        self.assertTrue(self.student['studycourse']['110'].validation_date is None)
1047        IWorkflowInfo(self.student).fireTransition('register_courses')
1048        self.browser.open(L110_student_path)
1049        self.browser.getLink("Reject courses").click()
1050        self.assertTrue('Course list request has been rejected'
1051            in self.browser.contents)
1052        self.assertTrue('school fee paid' in self.browser.contents)
1053        # CA does now see the contact form and can send a message
1054        self.browser.getControl(name="form.subject").value = 'Important subject'
1055        self.browser.getControl(name="form.body").value = 'Course list rejected'
1056        self.browser.getControl("Send message now").click()
1057        self.assertTrue('Your message has been sent' in self.browser.contents)
1058        # The CA can't validate courses if not in state
1059        # courses registered
1060        self.browser.open(L110_student_path + '/validate_courses')
1061        self.assertTrue('Student is in the wrong state'
1062            in self.browser.contents)
1063        # The CA can go to his certificate through the my_roles page
1064        self.browser.open('http://localhost/app/users/mrsadvise/my_roles')
1065        self.browser.getLink(
1066            "http://localhost/app/faculties/fac1/dep1/certificates/CERT1").click()
1067        # and view the list of students
1068        self.browser.getLink("Show students").click()
1069        self.assertTrue(self.student_id in self.browser.contents)
1070
1071    def test_student_change_password(self):
1072        # Students can change the password
1073        self.browser.open(self.login_path)
1074        self.browser.getControl(name="form.login").value = self.student_id
1075        self.browser.getControl(name="form.password").value = 'spwd'
1076        self.browser.getControl("Login").click()
1077        self.assertEqual(self.browser.url, self.student_path)
1078        self.assertTrue('You logged in' in self.browser.contents)
1079        # Change password
1080        self.browser.getLink("Change password").click()
1081        self.browser.getControl(name="change_password").value = 'pw'
1082        self.browser.getControl(
1083            name="change_password_repeat").value = 'pw'
1084        self.browser.getControl("Save").click()
1085        self.assertTrue('Password must have at least' in self.browser.contents)
1086        self.browser.getControl(name="change_password").value = 'new_password'
1087        self.browser.getControl(
1088            name="change_password_repeat").value = 'new_passssword'
1089        self.browser.getControl("Save").click()
1090        self.assertTrue('Passwords do not match' in self.browser.contents)
1091        self.browser.getControl(name="change_password").value = 'new_password'
1092        self.browser.getControl(
1093            name="change_password_repeat").value = 'new_password'
1094        self.browser.getControl("Save").click()
1095        self.assertTrue('Password changed' in self.browser.contents)
1096        # We are still logged in. Changing the password hasn't thrown us out.
1097        self.browser.getLink("Base Data").click()
1098        self.assertEqual(self.browser.url, self.student_path)
1099        # We can logout
1100        self.browser.getLink("Logout").click()
1101        self.assertTrue('You have been logged out' in self.browser.contents)
1102        self.assertEqual(self.browser.url, 'http://localhost/app')
1103        # We can login again with the new password
1104        self.browser.getLink("Login").click()
1105        self.browser.open(self.login_path)
1106        self.browser.getControl(name="form.login").value = self.student_id
1107        self.browser.getControl(name="form.password").value = 'new_password'
1108        self.browser.getControl("Login").click()
1109        self.assertEqual(self.browser.url, self.student_path)
1110        self.assertTrue('You logged in' in self.browser.contents)
1111        return
1112
1113    def test_setpassword(self):
1114        # Set password for first-time access
1115        student = Student()
1116        student.reg_number = u'123456'
1117        student.firstname = u'Klaus'
1118        student.lastname = u'Tester'
1119        self.app['students'].addStudent(student)
1120        setpassword_path = 'http://localhost/app/setpassword'
1121        student_path = 'http://localhost/app/students/%s' % student.student_id
1122        self.browser.open(setpassword_path)
1123        self.browser.getControl(name="ac_series").value = self.existing_pwdseries
1124        self.browser.getControl(name="ac_number").value = self.existing_pwdnumber
1125        self.browser.getControl(name="reg_number").value = '223456'
1126        self.browser.getControl("Set").click()
1127        self.assertMatches('...No student found...',
1128                           self.browser.contents)
1129        self.browser.getControl(name="reg_number").value = '123456'
1130        self.browser.getControl(name="ac_number").value = '999999'
1131        self.browser.getControl("Set").click()
1132        self.assertMatches('...Access code is invalid...',
1133                           self.browser.contents)
1134        self.browser.getControl(name="ac_number").value = self.existing_pwdnumber
1135        self.browser.getControl("Set").click()
1136        self.assertMatches('...Password has been set. Your Student Id is...',
1137                           self.browser.contents)
1138        self.browser.getControl("Set").click()
1139        self.assertMatches(
1140            '...Password has already been set. Your Student Id is...',
1141            self.browser.contents)
1142        existing_pwdpin = self.pwdpins[1]
1143        parts = existing_pwdpin.split('-')[1:]
1144        existing_pwdseries, existing_pwdnumber = parts
1145        self.browser.getControl(name="ac_series").value = existing_pwdseries
1146        self.browser.getControl(name="ac_number").value = existing_pwdnumber
1147        self.browser.getControl(name="reg_number").value = '123456'
1148        self.browser.getControl("Set").click()
1149        self.assertMatches(
1150            '...You are using the wrong Access Code...',
1151            self.browser.contents)
1152        # The student can login with the new credentials
1153        self.browser.open(self.login_path)
1154        self.browser.getControl(name="form.login").value = student.student_id
1155        self.browser.getControl(
1156            name="form.password").value = self.existing_pwdnumber
1157        self.browser.getControl("Login").click()
1158        self.assertEqual(self.browser.url, student_path)
1159        self.assertTrue('You logged in' in self.browser.contents)
1160        return
1161
1162    def test_student_access(self):
1163        # Students can access their own objects
1164        # and can perform actions
1165        IWorkflowInfo(self.student).fireTransition('admit')
1166        # Students can't login if their account is suspended/deactivated
1167        self.student.suspended = True
1168        self.browser.open(self.login_path)
1169        self.browser.getControl(name="form.login").value = self.student_id
1170        self.browser.getControl(name="form.password").value = 'spwd'
1171        self.browser.getControl("Login").click()
1172        self.assertTrue(
1173            'Your account has been deactivated.' in self.browser.contents)
1174        self.student.suspended = False
1175        self.browser.getControl("Login").click()
1176        self.assertTrue(
1177            'You logged in.' in self.browser.contents)
1178        # Admitted student can upload a passport picture
1179        self.browser.open(self.student_path + '/change_portrait')
1180        ctrl = self.browser.getControl(name='passportuploadedit')
1181        file_obj = open(SAMPLE_IMAGE, 'rb')
1182        file_ctrl = ctrl.mech_control
1183        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
1184        self.browser.getControl(
1185            name='upload_passportuploadedit').click()
1186        self.assertTrue(
1187            '<img align="middle" height="125px" src="passport.jpg" />'
1188            in self.browser.contents)
1189        # Students can open admission letter
1190        self.browser.getLink("Base Data").click()
1191        self.browser.getLink("Download admission letter").click()
1192        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1193        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1194        # Student can view the clearance data
1195        self.browser.open(self.student_path)
1196        self.browser.getLink("Clearance Data").click()
1197        # Student can't open clearance edit form before starting clearance
1198        self.browser.open(self.student_path + '/cedit')
1199        self.assertMatches('...The requested form is locked...',
1200                           self.browser.contents)
1201        self.browser.getLink("Clearance Data").click()
1202        self.browser.getLink("Start clearance").click()
1203        self.student.email = None
1204        # Uups, we forgot to fill the email fields
1205        self.browser.getControl("Start clearance").click()
1206        self.assertMatches('...Not all required fields filled...',
1207                           self.browser.contents)
1208        self.browser.open(self.student_path + '/edit_base')
1209        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
1210        self.browser.getControl("Save").click()
1211        self.browser.open(self.student_path + '/start_clearance')
1212        self.browser.getControl(name="ac_series").value = '3'
1213        self.browser.getControl(name="ac_number").value = '4444444'
1214        self.browser.getControl("Start clearance now").click()
1215        self.assertMatches('...Activation code is invalid...',
1216                           self.browser.contents)
1217        self.browser.getControl(name="ac_series").value = self.existing_clrseries
1218        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
1219        # Owner is Hans Wurst, AC can't be invalidated
1220        self.browser.getControl("Start clearance now").click()
1221        self.assertMatches('...You are not the owner of this access code...',
1222                           self.browser.contents)
1223        # Set the correct owner
1224        self.existing_clrac.owner = self.student_id
1225        # clr_code might be set (and thus returns None) due importing
1226        # an empty clr_code column.
1227        self.student.clr_code = None
1228        self.browser.getControl("Start clearance now").click()
1229        self.assertMatches('...Clearance process has been started...',
1230                           self.browser.contents)
1231        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
1232        self.browser.getControl("Save", index=0).click()
1233        # Student can view the clearance data
1234        self.browser.getLink("Clearance Data").click()
1235        # and go back to the edit form
1236        self.browser.getLink("Edit").click()
1237        # Students can upload documents
1238        ctrl = self.browser.getControl(name='birthcertificateupload')
1239        file_obj = open(SAMPLE_IMAGE, 'rb')
1240        file_ctrl = ctrl.mech_control
1241        file_ctrl.add_file(file_obj, filename='my_birth_certificate.jpg')
1242        self.browser.getControl(
1243            name='upload_birthcertificateupload').click()
1244        self.assertTrue(
1245            '<a target="image" href="birth_certificate">Birth Certificate Scan</a>'
1246            in self.browser.contents)
1247        # Students can open clearance slip
1248        self.browser.getLink("View").click()
1249        self.browser.getLink("Download clearance slip").click()
1250        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1251        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1252        # Students can request clearance
1253        self.browser.open(self.edit_clearance_path)
1254        self.browser.getControl("Save and request clearance").click()
1255        self.browser.getControl(name="ac_series").value = self.existing_clrseries
1256        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
1257        self.browser.getControl("Request clearance now").click()
1258        self.assertMatches('...Clearance has been requested...',
1259                           self.browser.contents)
1260        # Student can't reopen clearance form after requesting clearance
1261        self.browser.open(self.student_path + '/cedit')
1262        self.assertMatches('...The requested form is locked...',
1263                           self.browser.contents)
1264        # Student can't add study level if not in state 'school fee paid'
1265        self.browser.open(self.student_path + '/studycourse/add')
1266        self.assertMatches('...The requested form is locked...',
1267                           self.browser.contents)
1268        # ... and must be transferred first
1269        IWorkflowInfo(self.student).fireTransition('clear')
1270        IWorkflowInfo(self.student).fireTransition('pay_first_school_fee')
1271        # Now students can add the current study level
1272        self.browser.getLink("Study Course").click()
1273        self.browser.getLink("Add course list").click()
1274        self.assertMatches('...Add current level 100 (Year 1)...',
1275                           self.browser.contents)
1276        self.browser.getControl("Create course list now").click()
1277        # A level with one course ticket was created
1278        self.assertEqual(self.student['studycourse']['100'].number_of_tickets, 1)
1279        self.browser.getLink("100").click()
1280        self.browser.getLink("Edit course list").click()
1281        self.browser.getControl("Add course ticket").click()
1282        self.browser.getControl(name="form.course").value = ['COURSE1']
1283        self.browser.getControl("Add course ticket").click()
1284        self.assertMatches('...The ticket exists...',
1285                           self.browser.contents)
1286        self.student['studycourse'].current_level = 200
1287        self.browser.getLink("Study Course").click()
1288        self.browser.getLink("Add course list").click()
1289        self.assertMatches('...Add current level 200 (Year 2)...',
1290                           self.browser.contents)
1291        self.browser.getControl("Create course list now").click()
1292        self.browser.getLink("200").click()
1293        self.browser.getLink("Edit course list").click()
1294        self.browser.getControl("Add course ticket").click()
1295        self.browser.getControl(name="form.course").value = ['COURSE1']
1296        self.browser.getControl("Add course ticket").click()
1297        self.assertMatches('...The ticket exists...',
1298                           self.browser.contents)
1299        # Indeed the ticket exists as carry-over course from level 100
1300        # since its score was 0
1301        self.assertTrue(
1302            self.student['studycourse']['200']['COURSE1'].carry_over is True)
1303        # Students can open the pdf course registration slip
1304        self.browser.open(self.student_path + '/studycourse/200')
1305        self.browser.getLink("Download course registration slip").click()
1306        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1307        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1308        # Students can remove course tickets
1309        self.browser.open(self.student_path + '/studycourse/200/edit')
1310        self.browser.getControl("Remove selected", index=0).click()
1311        self.assertTrue('No ticket selected' in self.browser.contents)
1312        # No ticket can be selected since the carry-over course is a core course
1313        self.assertRaises(
1314            LookupError, self.browser.getControl, name='val_id')
1315        self.student['studycourse']['200']['COURSE1'].mandatory = False
1316        self.browser.open(self.student_path + '/studycourse/200/edit')
1317        # Course list can't be registered if total_credits exceeds max_credits
1318        self.student['studycourse']['200']['COURSE1'].credits = 60
1319        self.browser.getControl("Register course list").click()
1320        self.assertTrue('Maximum credits of 50 exceeded' in self.browser.contents)
1321        # Student can now remove the ticket
1322        ctrl = self.browser.getControl(name='val_id')
1323        ctrl.getControl(value='COURSE1').selected = True
1324        self.browser.getControl("Remove selected", index=0).click()
1325        self.assertTrue('Successfully removed' in self.browser.contents)
1326        # Course list can be registered, even if it's empty
1327        self.browser.getControl("Register course list").click()
1328        self.assertTrue('Course list has been registered' in self.browser.contents)
1329        self.assertEqual(self.student.state, 'courses registered')
1330        return
1331
1332    def test_postgraduate_student_access(self):
1333        self.certificate.study_mode = 'pg_ft'
1334        self.certificate.start_level = 999
1335        self.certificate.end_level = 999
1336        self.student['studycourse'].current_level = 999
1337        IWorkflowState(self.student).setState('school fee paid')
1338        self.certificate.study_mode = 'pg_ft'
1339        self.browser.open(self.login_path)
1340        self.browser.getControl(name="form.login").value = self.student_id
1341        self.browser.getControl(name="form.password").value = 'spwd'
1342        self.browser.getControl("Login").click()
1343        self.assertTrue(
1344            'You logged in.' in self.browser.contents)
1345        # Now students can add the current study level
1346        self.browser.getLink("Study Course").click()
1347        self.browser.getLink("Add course list").click()
1348        self.assertMatches('...Add current level Postgraduate Level...',
1349                           self.browser.contents)
1350        self.browser.getControl("Create course list now").click()
1351        # A level with one course ticket was created
1352        self.assertEqual(self.student['studycourse']['999'].number_of_tickets, 0)
1353        self.browser.getLink("999").click()
1354        self.browser.getLink("Edit course list").click()
1355        self.browser.getControl("Add course ticket").click()
1356        self.browser.getControl(name="form.course").value = ['COURSE1']
1357        self.browser.getControl("Add course ticket").click()
1358        self.assertMatches('...Successfully added COURSE1...',
1359                           self.browser.contents)
1360        # Postgraduate students can't register course lists
1361        self.browser.getControl("Register course list").click()
1362        self.assertTrue("your course list can't bee registered"
1363            in self.browser.contents)
1364        self.assertEqual(self.student.state, 'school fee paid')
1365        return
1366
1367    def test_student_clearance_wo_clrcode(self):
1368        IWorkflowState(self.student).setState('clearance started')
1369        self.browser.open(self.login_path)
1370        self.browser.getControl(name="form.login").value = self.student_id
1371        self.browser.getControl(name="form.password").value = 'spwd'
1372        self.browser.getControl("Login").click()
1373        self.student.clearance_locked = False
1374        self.browser.open(self.edit_clearance_path)
1375        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
1376        self.browser.getControl("Save and request clearance").click()
1377        self.assertMatches('...Clearance has been requested...',
1378                           self.browser.contents)
1379
1380    def test_manage_payments(self):
1381        # Managers can add online school fee payment tickets
1382        # if certain requirements are met
1383        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1384        self.browser.open(self.payments_path)
1385        IWorkflowState(self.student).setState('cleared')
1386        self.browser.getControl("Add online payment ticket").click()
1387        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1388        self.browser.getControl("Create ticket").click()
1389        self.assertMatches('...ticket created...',
1390                           self.browser.contents)
1391        ctrl = self.browser.getControl(name='val_id')
1392        value = ctrl.options[0]
1393        self.browser.getLink(value).click()
1394        self.assertMatches('...Amount Authorized...',
1395                           self.browser.contents)
1396        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
1397        payment_url = self.browser.url
1398
1399        # The pdf payment slip can't yet be opened
1400        #self.browser.open(payment_url + '/payment_slip.pdf')
1401        #self.assertMatches('...Ticket not yet paid...',
1402        #                   self.browser.contents)
1403
1404        # The same payment (with same p_item, p_session and p_category)
1405        # can be initialized a second time if the former ticket is not yet paid.
1406        self.browser.open(self.payments_path)
1407        self.browser.getControl("Add online payment ticket").click()
1408        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1409        self.browser.getControl("Create ticket").click()
1410        self.assertMatches('...Payment ticket created...',
1411                           self.browser.contents)
1412
1413        # Managers can approve the payment
1414        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1415        self.browser.open(payment_url)
1416        self.browser.getLink("Approve payment").click()
1417        self.assertMatches('...Payment approved...',
1418                          self.browser.contents)
1419
1420        # The authorized amount has been stored in the access code
1421        self.assertEqual(
1422            self.app['accesscodes']['SFE-0'].values()[0].cost,40000.0)
1423
1424        # Payments can't be approved twice
1425        self.browser.open(payment_url + '/approve')
1426        self.assertMatches('...This ticket has already been paid...',
1427                          self.browser.contents)
1428
1429        # Now the first ticket is paid and no more ticket of same type
1430        # (with same p_item, p_session and p_category) can be added
1431        self.browser.open(self.payments_path)
1432        self.browser.getControl("Add online payment ticket").click()
1433        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1434        self.browser.getControl("Create ticket").click()
1435        self.assertMatches(
1436            '...This type of payment has already been made...',
1437            self.browser.contents)
1438
1439        # Managers can open the pdf payment slip
1440        self.browser.open(payment_url)
1441        self.browser.getLink("Download payment slip").click()
1442        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1443        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1444
1445        # Managers can remove online school fee payment tickets
1446        self.browser.open(self.payments_path)
1447        self.browser.getControl("Remove selected").click()
1448        self.assertMatches('...No payment selected...', self.browser.contents)
1449        ctrl = self.browser.getControl(name='val_id')
1450        value = ctrl.options[0]
1451        ctrl.getControl(value=value).selected = True
1452        self.browser.getControl("Remove selected", index=0).click()
1453        self.assertTrue('Successfully removed' in self.browser.contents)
1454
1455        # Managers can add online clearance payment tickets
1456        self.browser.open(self.payments_path + '/addop')
1457        self.browser.getControl(name="form.p_category").value = ['clearance']
1458        self.browser.getControl("Create ticket").click()
1459        self.assertMatches('...ticket created...',
1460                           self.browser.contents)
1461
1462        # Managers can approve the payment
1463        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
1464        ctrl = self.browser.getControl(name='val_id')
1465        value = ctrl.options[1] # The clearance payment is the second in the table
1466        self.browser.getLink(value).click()
1467        self.browser.open(self.browser.url + '/approve')
1468        self.assertMatches('...Payment approved...',
1469                          self.browser.contents)
1470        expected = '''...
1471        <td>
1472          <span>Paid</span>
1473        </td>...'''
1474        self.assertMatches(expected,self.browser.contents)
1475        # The new CLR-0 pin has been created
1476        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
1477        pin = self.app['accesscodes']['CLR-0'].keys()[0]
1478        ac = self.app['accesscodes']['CLR-0'][pin]
1479        self.assertEqual(ac.owner, self.student_id)
1480        self.assertEqual(ac.cost, 3456.0)
1481        return
1482
1483    def test_student_payments(self):
1484        # Login
1485        self.browser.open(self.login_path)
1486        self.browser.getControl(name="form.login").value = self.student_id
1487        self.browser.getControl(name="form.password").value = 'spwd'
1488        self.browser.getControl("Login").click()
1489
1490        # Students can add online clearance payment tickets
1491        self.browser.open(self.payments_path + '/addop')
1492        self.browser.getControl(name="form.p_category").value = ['clearance']
1493        self.browser.getControl("Create ticket").click()
1494        self.assertMatches('...ticket created...',
1495                           self.browser.contents)
1496
1497        # Students can't approve the payment
1498        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
1499        ctrl = self.browser.getControl(name='val_id')
1500        value = ctrl.options[0]
1501        self.browser.getLink(value).click()
1502        payment_url = self.browser.url
1503        self.assertRaises(
1504            Unauthorized, self.browser.open, payment_url + '/approve')
1505        # In the base package they can 'use' a fake approval view
1506        self.browser.open(payment_url + '/fake_approve')
1507        self.assertMatches('...Payment approved...',
1508                          self.browser.contents)
1509        expected = '''...
1510        <td>
1511          <span>Paid</span>
1512        </td>...'''
1513        expected = '''...
1514        <td>
1515          <span>Paid</span>
1516        </td>...'''
1517        self.assertMatches(expected,self.browser.contents)
1518        payment_id = self.student['payments'].keys()[0]
1519        payment = self.student['payments'][payment_id]
1520        self.assertEqual(payment.p_state, 'paid')
1521        self.assertEqual(payment.r_amount_approved, 3456.0)
1522        self.assertEqual(payment.r_code, 'AP')
1523        self.assertEqual(payment.r_desc, u'Payment approved by Anna Tester')
1524        # The new CLR-0 pin has been created
1525        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
1526        pin = self.app['accesscodes']['CLR-0'].keys()[0]
1527        ac = self.app['accesscodes']['CLR-0'][pin]
1528        self.assertEqual(ac.owner, self.student_id)
1529        self.assertEqual(ac.cost, 3456.0)
1530
1531        # Students can open the pdf payment slip
1532        self.browser.open(payment_url + '/payment_slip.pdf')
1533        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1534        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1535
1536        # The new CLR-0 pin can be used for starting clearance
1537        # but they have to upload a passport picture first
1538        # which is only possible in state admitted
1539        self.browser.open(self.student_path + '/change_portrait')
1540        self.assertMatches('...form is locked...',
1541                          self.browser.contents)
1542        IWorkflowInfo(self.student).fireTransition('admit')
1543        self.browser.open(self.student_path + '/change_portrait')
1544        image = open(SAMPLE_IMAGE, 'rb')
1545        ctrl = self.browser.getControl(name='passportuploadedit')
1546        file_ctrl = ctrl.mech_control
1547        file_ctrl.add_file(image, filename='my_photo.jpg')
1548        self.browser.getControl(
1549            name='upload_passportuploadedit').click()
1550        self.browser.open(self.student_path + '/start_clearance')
1551        parts = pin.split('-')[1:]
1552        clrseries, clrnumber = parts
1553        self.browser.getControl(name="ac_series").value = clrseries
1554        self.browser.getControl(name="ac_number").value = clrnumber
1555        self.browser.getControl("Start clearance now").click()
1556        self.assertMatches('...Clearance process has been started...',
1557                           self.browser.contents)
1558
1559        # Students can add online school fee payment tickets.
1560        IWorkflowState(self.student).setState('returning')
1561        self.browser.open(self.payments_path)
1562        self.browser.getControl("Add online payment ticket").click()
1563        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1564        self.browser.getControl("Create ticket").click()
1565        self.assertMatches('...ticket created...',
1566                           self.browser.contents)
1567        ctrl = self.browser.getControl(name='val_id')
1568        value = ctrl.options[0]
1569        self.browser.getLink(value).click()
1570        self.assertMatches('...Amount Authorized...',
1571                           self.browser.contents)
1572        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
1573        # Payment session and will be calculated as defined
1574        # in w.k.students.utils because we set changed the state
1575        # to returning
1576        self.assertEqual(self.student['payments'][value].p_session, 2005)
1577        self.assertEqual(self.student['payments'][value].p_level, 200)
1578
1579        # Student is the payee of the payment ticket.
1580        webservice = IPaymentWebservice(self.student['payments'][value])
1581        self.assertEqual(webservice.display_fullname, 'Anna Tester')
1582        self.assertEqual(webservice.id, self.student_id)
1583        self.assertEqual(webservice.faculty, 'fac1')
1584        self.assertEqual(webservice.department, 'dep1')
1585
1586        # We simulate the approval
1587        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1588        self.browser.open(self.browser.url + '/fake_approve')
1589        self.assertMatches('...Payment approved...',
1590                          self.browser.contents)
1591
1592        # Students can remove only online payment tickets which have
1593        # not received a valid callback
1594        self.browser.open(self.payments_path)
1595        self.assertRaises(
1596            LookupError, self.browser.getControl, name='val_id')
1597        self.browser.open(self.payments_path + '/addop')
1598        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1599        self.browser.getControl("Create ticket").click()
1600        self.browser.open(self.payments_path)
1601        ctrl = self.browser.getControl(name='val_id')
1602        value = ctrl.options[0]
1603        ctrl.getControl(value=value).selected = True
1604        self.browser.getControl("Remove selected", index=0).click()
1605        self.assertTrue('Successfully removed' in self.browser.contents)
1606
1607        # The new SFE-0 pin can be used for starting new session
1608        self.browser.open(self.studycourse_path)
1609        self.browser.getLink('Start new session').click()
1610        pin = self.app['accesscodes']['SFE-0'].keys()[0]
1611        parts = pin.split('-')[1:]
1612        sfeseries, sfenumber = parts
1613        self.browser.getControl(name="ac_series").value = sfeseries
1614        self.browser.getControl(name="ac_number").value = sfenumber
1615        self.browser.getControl("Start now").click()
1616        self.assertMatches('...Session started...',
1617                           self.browser.contents)
1618        self.assertTrue(self.student.state == 'school fee paid')
1619        return
1620
1621    def test_student_previous_payments(self):
1622        configuration = createObject('waeup.SessionConfiguration')
1623        configuration.academic_session = 2000
1624        configuration.clearance_fee = 3456.0
1625        configuration.booking_fee = 123.4
1626        self.app['configuration'].addSessionConfiguration(configuration)
1627        configuration2 = createObject('waeup.SessionConfiguration')
1628        configuration2.academic_session = 2003
1629        configuration2.clearance_fee = 3456.0
1630        configuration2.booking_fee = 123.4
1631        self.app['configuration'].addSessionConfiguration(configuration2)
1632
1633        self.student['studycourse'].entry_session = 2002
1634
1635        # Login
1636        self.browser.open(self.login_path)
1637        self.browser.getControl(name="form.login").value = self.student_id
1638        self.browser.getControl(name="form.password").value = 'spwd'
1639        self.browser.getControl("Login").click()
1640
1641        # Students can add previous school fee payment tickets in any state.
1642        IWorkflowState(self.student).setState('courses registered')
1643        self.browser.open(self.payments_path)
1644        self.browser.getControl("Add online payment ticket").click()
1645        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1646        self.browser.getControl("Create ticket").click()
1647
1648        # Amount cannot be determined since the state is not
1649        # 'cleared' or 'returning'
1650        self.assertMatches('...Amount could not be determined...',
1651                           self.browser.contents)
1652        self.assertMatches('...Would you like to pay for a previous session?...',
1653                           self.browser.contents)
1654
1655        # Previous session payment form is provided
1656        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1657        self.browser.getControl(name="form.p_session").value = ['2000']
1658        self.browser.getControl(name="form.p_level").value = ['300']
1659        self.browser.getControl("Create ticket").click()
1660        self.assertMatches('...The previous session must not fall below...',
1661                           self.browser.contents)
1662        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1663        self.browser.getControl(name="form.p_session").value = ['2004']
1664        self.browser.getControl(name="form.p_level").value = ['300']
1665        self.browser.getControl("Create ticket").click()
1666        self.assertMatches('...This is not a previous session...',
1667                           self.browser.contents)
1668        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1669        self.browser.getControl(name="form.p_session").value = ['2003']
1670        self.browser.getControl(name="form.p_level").value = ['300']
1671        self.browser.getControl("Create ticket").click()
1672        self.assertMatches('...ticket created...',
1673                           self.browser.contents)
1674        ctrl = self.browser.getControl(name='val_id')
1675        value = ctrl.options[0]
1676        self.browser.getLink(value).click()
1677        self.assertMatches('...Amount Authorized...',
1678                           self.browser.contents)
1679        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
1680
1681        # Payment session is properly set
1682        self.assertEqual(self.student['payments'][value].p_session, 2003)
1683        self.assertEqual(self.student['payments'][value].p_level, 300)
1684
1685        # We simulate the approval
1686        self.browser.open(self.browser.url + '/fake_approve')
1687        self.assertMatches('...Payment approved...',
1688                          self.browser.contents)
1689
1690        # No AC has been created
1691        self.assertEqual(len(self.app['accesscodes']['SFE-0'].keys()), 0)
1692        self.assertTrue(self.student['payments'][value].ac is None)
1693
1694        # Current payment flag is set False
1695        self.assertFalse(self.student['payments'][value].p_current)
1696        return
1697
1698    def test_postgraduate_student_payments(self):
1699        self.certificate.study_mode = 'pg_ft'
1700        self.certificate.start_level = 999
1701        self.certificate.end_level = 999
1702        self.student['studycourse'].current_level = 999
1703        # Login
1704        self.browser.open(self.login_path)
1705        self.browser.getControl(name="form.login").value = self.student_id
1706        self.browser.getControl(name="form.password").value = 'spwd'
1707        self.browser.getControl("Login").click()
1708        # Students can add online school fee payment tickets.
1709        IWorkflowState(self.student).setState('cleared')
1710        self.browser.open(self.payments_path)
1711        self.browser.getControl("Add online payment ticket").click()
1712        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1713        self.browser.getControl("Create ticket").click()
1714        self.assertMatches('...ticket created...',
1715                           self.browser.contents)
1716        ctrl = self.browser.getControl(name='val_id')
1717        value = ctrl.options[0]
1718        self.browser.getLink(value).click()
1719        self.assertMatches('...Amount Authorized...',
1720                           self.browser.contents)
1721        # Payment session and level are current ones.
1722        # Postgrads have to pay school_fee_1.
1723        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
1724        self.assertEqual(self.student['payments'][value].p_session, 2004)
1725        self.assertEqual(self.student['payments'][value].p_level, 999)
1726
1727        # We simulate the approval
1728        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1729        self.browser.open(self.browser.url + '/fake_approve')
1730        self.assertMatches('...Payment approved...',
1731                          self.browser.contents)
1732
1733        # The new SFE-0 pin can be used for starting session
1734        self.browser.open(self.studycourse_path)
1735        self.browser.getLink('Start new session').click()
1736        pin = self.app['accesscodes']['SFE-0'].keys()[0]
1737        parts = pin.split('-')[1:]
1738        sfeseries, sfenumber = parts
1739        self.browser.getControl(name="ac_series").value = sfeseries
1740        self.browser.getControl(name="ac_number").value = sfenumber
1741        self.browser.getControl("Start now").click()
1742        self.assertMatches('...Session started...',
1743                           self.browser.contents)
1744        self.assertTrue(self.student.state == 'school fee paid')
1745
1746        # Postgrad students do not need to register courses the
1747        # can just pay for the next session.
1748        self.browser.open(self.payments_path)
1749        # Remove first payment to be sure that we access the right ticket
1750        del self.student['payments'][value]
1751        self.browser.getControl("Add online payment ticket").click()
1752        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1753        self.browser.getControl("Create ticket").click()
1754        ctrl = self.browser.getControl(name='val_id')
1755        value = ctrl.options[0]
1756        self.browser.getLink(value).click()
1757        # Payment session has increased by one, payment level remains the same.
1758        # Returning Postgraduates have to pay school_fee_2.
1759        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
1760        self.assertEqual(self.student['payments'][value].p_session, 2005)
1761        self.assertEqual(self.student['payments'][value].p_level, 999)
1762
1763        # Student is still in old session
1764        self.assertEqual(self.student.current_session, 2004)
1765
1766        # We do not need to pay the ticket if any other
1767        # SFE pin is provided
1768        pin_container = self.app['accesscodes']
1769        pin_container.createBatch(
1770            datetime.utcnow(), 'some_userid', 'SFE', 9.99, 1)
1771        pin = pin_container['SFE-1'].values()[0].representation
1772        sfeseries, sfenumber = pin.split('-')[1:]
1773        # The new SFE-1 pin can be used for starting new session
1774        self.browser.open(self.studycourse_path)
1775        self.browser.getLink('Start new session').click()
1776        self.browser.getControl(name="ac_series").value = sfeseries
1777        self.browser.getControl(name="ac_number").value = sfenumber
1778        self.browser.getControl("Start now").click()
1779        self.assertMatches('...Session started...',
1780                           self.browser.contents)
1781        self.assertTrue(self.student.state == 'school fee paid')
1782        # Student is in new session
1783        self.assertEqual(self.student.current_session, 2005)
1784        self.assertEqual(self.student['studycourse'].current_level, 999)
1785        return
1786
1787    def test_manage_accommodation(self):
1788        # Managers can add online booking fee payment tickets and open the
1789        # callback view (see test_manage_payments)
1790        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1791        self.browser.open(self.payments_path)
1792        self.browser.getControl("Add online payment ticket").click()
1793        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1794        # If student is not in accommodation session, payment cannot be processed
1795        self.app['hostels'].accommodation_session = 2011
1796        self.browser.getControl("Create ticket").click()
1797        self.assertMatches('...Your current session does not match...',
1798                           self.browser.contents)
1799        self.app['hostels'].accommodation_session = 2004
1800        self.browser.getControl("Add online payment ticket").click()
1801        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1802        self.browser.getControl("Create ticket").click()
1803        ctrl = self.browser.getControl(name='val_id')
1804        value = ctrl.options[0]
1805        self.browser.getLink(value).click()
1806        self.browser.open(self.browser.url + '/approve')
1807        # The new HOS-0 pin has been created
1808        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
1809        pin = self.app['accesscodes']['HOS-0'].keys()[0]
1810        ac = self.app['accesscodes']['HOS-0'][pin]
1811        self.assertEqual(ac.owner, self.student_id)
1812        parts = pin.split('-')[1:]
1813        sfeseries, sfenumber = parts
1814        # Managers can use HOS code and book a bed space with it
1815        self.browser.open(self.acco_path)
1816        self.browser.getLink("Book accommodation").click()
1817        self.assertMatches('...You are in the wrong...',
1818                           self.browser.contents)
1819        IWorkflowInfo(self.student).fireTransition('admit')
1820        # An existing HOS code can only be used if students
1821        # are in accommodation session
1822        self.student['studycourse'].current_session = 2003
1823        self.browser.getLink("Book accommodation").click()
1824        self.assertMatches('...Your current session does not match...',
1825                           self.browser.contents)
1826        self.student['studycourse'].current_session = 2004
1827        # All requirements are met and ticket can be created
1828        self.browser.getLink("Book accommodation").click()
1829        self.assertMatches('...Activation Code:...',
1830                           self.browser.contents)
1831        self.browser.getControl(name="ac_series").value = sfeseries
1832        self.browser.getControl(name="ac_number").value = sfenumber
1833        self.browser.getControl("Create bed ticket").click()
1834        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1835                           self.browser.contents)
1836        # Bed has been allocated
1837        bed1 = self.app['hostels']['hall-1']['hall-1_A_101_A']
1838        self.assertTrue(bed1.owner == self.student_id)
1839        # BedTicketAddPage is now blocked
1840        self.browser.getLink("Book accommodation").click()
1841        self.assertMatches('...You already booked a bed space...',
1842            self.browser.contents)
1843        # The bed ticket displays the data correctly
1844        self.browser.open(self.acco_path + '/2004')
1845        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1846                           self.browser.contents)
1847        self.assertMatches('...2004/2005...', self.browser.contents)
1848        self.assertMatches('...regular_male_fr...', self.browser.contents)
1849        self.assertMatches('...%s...' % pin, self.browser.contents)
1850        # Managers can relocate students if the student's bed_type has changed
1851        self.browser.getLink("Relocate student").click()
1852        self.assertMatches(
1853            "...Student can't be relocated...", self.browser.contents)
1854        self.student.sex = u'f'
1855        self.browser.getLink("Relocate student").click()
1856        self.assertMatches(
1857            "...Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1858        self.assertTrue(bed1.owner == NOT_OCCUPIED)
1859        bed2 = self.app['hostels']['hall-1']['hall-1_A_101_B']
1860        self.assertTrue(bed2.owner == self.student_id)
1861        self.assertTrue(self.student['accommodation'][
1862            '2004'].bed_type == u'regular_female_fr')
1863        # The payment object still shows the original payment item
1864        payment_id = self.student['payments'].keys()[0]
1865        payment = self.student['payments'][payment_id]
1866        self.assertTrue(payment.p_item == u'regular_male_fr')
1867        # Managers can relocate students if the bed's bed_type has changed
1868        bed1.bed_type = u'regular_female_fr'
1869        bed2.bed_type = u'regular_male_fr'
1870        notify(grok.ObjectModifiedEvent(bed1))
1871        notify(grok.ObjectModifiedEvent(bed2))
1872        self.browser.getLink("Relocate student").click()
1873        self.assertMatches(
1874            "...Student relocated...", self.browser.contents)
1875        self.assertMatches(
1876            "... Hall 1, Block A, Room 101, Bed A...", self.browser.contents)
1877        self.assertMatches(bed1.owner, self.student_id)
1878        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1879        # Managers can't relocate students if bed is reserved
1880        self.student.sex = u'm'
1881        bed1.bed_type = u'regular_female_reserved'
1882        notify(grok.ObjectModifiedEvent(bed1))
1883        self.browser.getLink("Relocate student").click()
1884        self.assertMatches(
1885            "...Students in reserved beds can't be relocated...",
1886            self.browser.contents)
1887        # Managers can relocate students if booking has been cancelled but
1888        # other bed space has been manually allocated after cancellation
1889        old_owner = bed1.releaseBed()
1890        self.assertMatches(old_owner, self.student_id)
1891        bed2.owner = self.student_id
1892        self.browser.open(self.acco_path + '/2004')
1893        self.assertMatches(
1894            "...booking cancelled...", self.browser.contents)
1895        self.browser.getLink("Relocate student").click()
1896        # We didn't informed the catalog therefore the new owner is not found
1897        self.assertMatches(
1898            "...There is no free bed in your category regular_male_fr...",
1899            self.browser.contents)
1900        # Now we fire the event properly
1901        notify(grok.ObjectModifiedEvent(bed2))
1902        self.browser.getLink("Relocate student").click()
1903        self.assertMatches(
1904            "...Student relocated...", self.browser.contents)
1905        self.assertMatches(
1906            "... Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1907          # Managers can delete bed tickets
1908        self.browser.open(self.acco_path)
1909        ctrl = self.browser.getControl(name='val_id')
1910        value = ctrl.options[0]
1911        ctrl.getControl(value=value).selected = True
1912        self.browser.getControl("Remove selected", index=0).click()
1913        self.assertMatches('...Successfully removed...', self.browser.contents)
1914        # The bed has been properly released by the event handler
1915        self.assertMatches(bed1.owner, NOT_OCCUPIED)
1916        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1917        return
1918
1919    def test_student_accommodation(self):
1920        # Login
1921        self.browser.open(self.login_path)
1922        self.browser.getControl(name="form.login").value = self.student_id
1923        self.browser.getControl(name="form.password").value = 'spwd'
1924        self.browser.getControl("Login").click()
1925
1926        # Students can add online booking fee payment tickets and open the
1927        # callback view (see test_manage_payments)
1928        self.browser.getLink("Payments").click()
1929        self.browser.getControl("Add online payment ticket").click()
1930        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1931        self.browser.getControl("Create ticket").click()
1932        ctrl = self.browser.getControl(name='val_id')
1933        value = ctrl.options[0]
1934        self.browser.getLink(value).click()
1935        self.browser.open(self.browser.url + '/fake_approve')
1936        # The new HOS-0 pin has been created
1937        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
1938        pin = self.app['accesscodes']['HOS-0'].keys()[0]
1939        ac = self.app['accesscodes']['HOS-0'][pin]
1940        parts = pin.split('-')[1:]
1941        sfeseries, sfenumber = parts
1942
1943        # Students can use HOS code and book a bed space with it ...
1944        self.browser.open(self.acco_path)
1945        # ... but not if booking period has expired ...
1946        self.app['hostels'].enddate = datetime.now(pytz.utc)
1947        self.browser.getLink("Book accommodation").click()
1948        self.assertMatches('...Outside booking period: ...',
1949                           self.browser.contents)
1950        self.app['hostels'].enddate = datetime.now(pytz.utc) + timedelta(days=10)
1951        # ... or student is not the an allowed state ...
1952        self.browser.getLink("Book accommodation").click()
1953        self.assertMatches('...You are in the wrong...',
1954                           self.browser.contents)
1955        IWorkflowInfo(self.student).fireTransition('admit')
1956        self.browser.getLink("Book accommodation").click()
1957        self.assertMatches('...Activation Code:...',
1958                           self.browser.contents)
1959        # Student can't used faked ACs ...
1960        self.browser.getControl(name="ac_series").value = u'nonsense'
1961        self.browser.getControl(name="ac_number").value = sfenumber
1962        self.browser.getControl("Create bed ticket").click()
1963        self.assertMatches('...Activation code is invalid...',
1964                           self.browser.contents)
1965        # ... or ACs owned by somebody else.
1966        ac.owner = u'Anybody'
1967        self.browser.getControl(name="ac_series").value = sfeseries
1968        self.browser.getControl(name="ac_number").value = sfenumber
1969        self.browser.getControl("Create bed ticket").click()
1970        self.assertMatches('...You are not the owner of this access code...',
1971                           self.browser.contents)
1972        ac.owner = self.student_id
1973        self.browser.getControl(name="ac_series").value = sfeseries
1974        self.browser.getControl(name="ac_number").value = sfenumber
1975        self.browser.getControl("Create bed ticket").click()
1976        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1977                           self.browser.contents)
1978
1979        # Bed has been allocated
1980        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
1981        self.assertTrue(bed.owner == self.student_id)
1982
1983        # BedTicketAddPage is now blocked
1984        self.browser.getLink("Book accommodation").click()
1985        self.assertMatches('...You already booked a bed space...',
1986            self.browser.contents)
1987
1988        # The bed ticket displays the data correctly
1989        self.browser.open(self.acco_path + '/2004')
1990        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1991                           self.browser.contents)
1992        self.assertMatches('...2004/2005...', self.browser.contents)
1993        self.assertMatches('...regular_male_fr...', self.browser.contents)
1994        self.assertMatches('...%s...' % pin, self.browser.contents)
1995
1996        # Students can open the pdf slip
1997        self.browser.open(self.browser.url + '/bed_allocation.pdf')
1998        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1999        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2000
2001        # Students can't relocate themselves
2002        self.assertFalse('Relocate' in self.browser.contents)
2003        relocate_path = self.acco_path + '/2004/relocate'
2004        self.assertRaises(
2005            Unauthorized, self.browser.open, relocate_path)
2006
2007        # Students can't the Remove button and check boxes
2008        self.browser.open(self.acco_path)
2009        self.assertFalse('Remove' in self.browser.contents)
2010        self.assertFalse('val_id' in self.browser.contents)
2011        return
2012
2013    def test_change_password_request(self):
2014        self.browser.open('http://localhost/app/changepw')
2015        self.browser.getControl(name="form.identifier").value = '123'
2016        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
2017        self.browser.getControl("Send login credentials").click()
2018        self.assertTrue('An email with' in self.browser.contents)
2019
2020    def test_change_current_mode(self):
2021        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2022        self.browser.open(self.clearance_path)
2023        self.assertFalse('Employer' in self.browser.contents)
2024        self.browser.open(self.manage_clearance_path)
2025        self.assertFalse('Employer' in self.browser.contents)
2026        self.student.clearance_locked = False
2027        self.browser.open(self.edit_clearance_path)
2028        self.assertFalse('Employer' in self.browser.contents)
2029        # Now we change the study mode of the certificate and a different
2030        # interface is used by clearance views.
2031        self.certificate.study_mode = 'pg_ft'
2032        # Invariants are not being checked here?!
2033        self.certificate.end_level = 100
2034        self.browser.open(self.clearance_path)
2035        self.assertTrue('Employer' in self.browser.contents)
2036        self.browser.open(self.manage_clearance_path)
2037        self.assertTrue('Employer' in self.browser.contents)
2038        self.browser.open(self.edit_clearance_path)
2039        self.assertTrue('Employer' in self.browser.contents)
2040
2041    def test_activate_deactivate_buttons(self):
2042        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2043        self.browser.open(self.student_path)
2044        self.browser.getLink("Deactivate").click()
2045        self.assertTrue(
2046            'Student account has been deactivated.' in self.browser.contents)
2047        self.assertTrue(
2048            'Base Data (account deactivated)' in self.browser.contents)
2049        self.assertTrue(self.student.suspended)
2050        self.browser.getLink("Activate").click()
2051        self.assertTrue(
2052            'Student account has been activated.' in self.browser.contents)
2053        self.assertFalse(
2054            'Base Data (account deactivated)' in self.browser.contents)
2055        self.assertFalse(self.student.suspended)
2056        # History messages have been added ...
2057        self.browser.getLink("History").click()
2058        self.assertTrue(
2059            'Student account deactivated by Manager<br />' in self.browser.contents)
2060        self.assertTrue(
2061            'Student account activated by Manager<br />' in self.browser.contents)
2062        # ... and actions have been logged.
2063        logfile = os.path.join(
2064            self.app['datacenter'].storage, 'logs', 'students.log')
2065        logcontent = open(logfile).read()
2066        self.assertTrue('zope.mgr - students.browser.StudentDeactivatePage - '
2067                        'K1000000 - account deactivated' in logcontent)
2068        self.assertTrue('zope.mgr - students.browser.StudentActivatePage - '
2069                        'K1000000 - account activated' in logcontent)
2070
2071    def test_student_locked_level_forms(self):
2072
2073        # Add two study levels, one current and one previous
2074        studylevel = createObject(u'waeup.StudentStudyLevel')
2075        studylevel.level = 100
2076        self.student['studycourse'].addStudentStudyLevel(
2077            self.certificate, studylevel)
2078        studylevel = createObject(u'waeup.StudentStudyLevel')
2079        studylevel.level = 200
2080        self.student['studycourse'].addStudentStudyLevel(
2081            self.certificate, studylevel)
2082        IWorkflowState(self.student).setState('school fee paid')
2083        self.student['studycourse'].current_level = 200
2084
2085        self.browser.open(self.login_path)
2086        self.browser.getControl(name="form.login").value = self.student_id
2087        self.browser.getControl(name="form.password").value = 'spwd'
2088        self.browser.getControl("Login").click()
2089
2090        self.browser.open(self.student_path + '/studycourse/200/edit')
2091        self.assertFalse('The requested form is locked' in self.browser.contents)
2092        self.browser.open(self.student_path + '/studycourse/100/edit')
2093        self.assertTrue('The requested form is locked' in self.browser.contents)
2094
2095        self.browser.open(self.student_path + '/studycourse/200/ctadd')
2096        self.assertFalse('The requested form is locked' in self.browser.contents)
2097        self.browser.open(self.student_path + '/studycourse/100/ctadd')
2098        self.assertTrue('The requested form is locked' in self.browser.contents)
2099
2100        IWorkflowState(self.student).setState('courses registered')
2101        self.browser.open(self.student_path + '/studycourse/200/edit')
2102        self.assertTrue('The requested form is locked' in self.browser.contents)
2103        self.browser.open(self.student_path + '/studycourse/200/ctadd')
2104        self.assertTrue('The requested form is locked' in self.browser.contents)
2105
2106
2107    def test_manage_student_transfer(self):
2108        # Add second certificate
2109        self.certificate2 = createObject('waeup.Certificate')
2110        self.certificate2.code = u'CERT2'
2111        self.certificate2.study_mode = 'ug_ft'
2112        self.certificate2.start_level = 999
2113        self.certificate2.end_level = 999
2114        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
2115            self.certificate2)
2116
2117        # Add study level to old study course
2118        studylevel = createObject(u'waeup.StudentStudyLevel')
2119        studylevel.level = 200
2120        self.student['studycourse'].addStudentStudyLevel(
2121            self.certificate, studylevel)
2122        studylevel = createObject(u'waeup.StudentStudyLevel')
2123        studylevel.level = 999
2124        self.student['studycourse'].addStudentStudyLevel(
2125            self.certificate, studylevel)
2126
2127        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2128        self.browser.open(self.student_path)
2129        self.browser.getLink("Transfer").click()
2130        self.browser.getControl(name="form.certificate").value = ['CERT2']
2131        self.browser.getControl(name="form.current_session").value = ['2011']
2132        self.browser.getControl(name="form.current_level").value = ['200']
2133        self.browser.getControl("Transfer").click()
2134        self.assertTrue(
2135            'Current level does not match certificate levels'
2136            in self.browser.contents)
2137        self.browser.getControl(name="form.current_level").value = ['999']
2138        self.browser.getControl("Transfer").click()
2139        self.assertTrue('Successfully transferred' in self.browser.contents)
2140        # The catalog has been updated
2141        cat = queryUtility(ICatalog, name='students_catalog')
2142        results = list(
2143            cat.searchResults(
2144            certcode=('CERT2', 'CERT2')))
2145        self.assertTrue(results[0] is self.student)
2146        results = list(
2147            cat.searchResults(
2148            current_session=(2011, 2011)))
2149        self.assertTrue(results[0] is self.student)
2150        # Add study level to new study course
2151        studylevel = createObject(u'waeup.StudentStudyLevel')
2152        studylevel.level = 999
2153        self.student['studycourse'].addStudentStudyLevel(
2154            self.certificate, studylevel)
2155
2156        # Edit and add pages are locked for old study courses
2157        self.browser.open(self.student_path + '/studycourse/manage')
2158        self.assertFalse('The requested form is locked' in self.browser.contents)
2159        self.browser.open(self.student_path + '/studycourse_1/manage')
2160        self.assertTrue('The requested form is locked' in self.browser.contents)
2161
2162        self.browser.open(self.student_path + '/studycourse/start_session')
2163        self.assertFalse('The requested form is locked' in self.browser.contents)
2164        self.browser.open(self.student_path + '/studycourse_1/start_session')
2165        self.assertTrue('The requested form is locked' in self.browser.contents)
2166
2167        IWorkflowState(self.student).setState('school fee paid')
2168        self.browser.open(self.student_path + '/studycourse/add')
2169        self.assertFalse('The requested form is locked' in self.browser.contents)
2170        self.browser.open(self.student_path + '/studycourse_1/add')
2171        self.assertTrue('The requested form is locked' in self.browser.contents)
2172
2173        self.browser.open(self.student_path + '/studycourse/999/manage')
2174        self.assertFalse('The requested form is locked' in self.browser.contents)
2175        self.browser.open(self.student_path + '/studycourse_1/999/manage')
2176        self.assertTrue('The requested form is locked' in self.browser.contents)
2177
2178        self.browser.open(self.student_path + '/studycourse/999/validate_courses')
2179        self.assertFalse('The requested form is locked' in self.browser.contents)
2180        self.browser.open(self.student_path + '/studycourse_1/999/validate_courses')
2181        self.assertTrue('The requested form is locked' in self.browser.contents)
2182
2183        self.browser.open(self.student_path + '/studycourse/999/reject_courses')
2184        self.assertFalse('The requested form is locked' in self.browser.contents)
2185        self.browser.open(self.student_path + '/studycourse_1/999/reject_courses')
2186        self.assertTrue('The requested form is locked' in self.browser.contents)
2187
2188        self.browser.open(self.student_path + '/studycourse/999/add')
2189        self.assertFalse('The requested form is locked' in self.browser.contents)
2190        self.browser.open(self.student_path + '/studycourse_1/999/add')
2191        self.assertTrue('The requested form is locked' in self.browser.contents)
2192
2193        self.browser.open(self.student_path + '/studycourse/999/edit')
2194        self.assertFalse('The requested form is locked' in self.browser.contents)
2195        self.browser.open(self.student_path + '/studycourse_1/999/edit')
2196        self.assertTrue('The requested form is locked' in self.browser.contents)
2197
2198class StudentRequestPWTests(StudentsFullSetup):
2199    # Tests for student registration
2200
2201    layer = FunctionalLayer
2202
2203    def test_request_pw(self):
2204        # Student with wrong number can't be found.
2205        self.browser.open('http://localhost/app/requestpw')
2206        self.browser.getControl(name="form.firstname").value = 'Anna'
2207        self.browser.getControl(name="form.number").value = 'anynumber'
2208        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
2209        self.browser.getControl("Send login credentials").click()
2210        self.assertTrue('No student record found.'
2211            in self.browser.contents)
2212        # Anonymous is not informed that firstname verification failed.
2213        # It seems that the record doesn't exist.
2214        self.browser.open('http://localhost/app/requestpw')
2215        self.browser.getControl(name="form.firstname").value = 'Johnny'
2216        self.browser.getControl(name="form.number").value = '123'
2217        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
2218        self.browser.getControl("Send login credentials").click()
2219        self.assertTrue('No student record found.'
2220            in self.browser.contents)
2221        # Even with the correct firstname we can't register if a
2222        # password has been set and used.
2223        self.browser.getControl(name="form.firstname").value = 'Anna'
2224        self.browser.getControl(name="form.number").value = '123'
2225        self.browser.getControl("Send login credentials").click()
2226        self.assertTrue('Your password has already been set and used.'
2227            in self.browser.contents)
2228        self.browser.open('http://localhost/app/requestpw')
2229        self.app['students'][self.student_id].password = None
2230        # The firstname field, used for verification, is not case-sensitive.
2231        self.browser.getControl(name="form.firstname").value = 'aNNa'
2232        self.browser.getControl(name="form.number").value = '123'
2233        self.browser.getControl(name="form.email").value = 'new@yy.zz'
2234        self.browser.getControl("Send login credentials").click()
2235        # Yeah, we succeded ...
2236        self.assertTrue('Your password request was successful.'
2237            in self.browser.contents)
2238        # We can also use the matric_number instead.
2239        self.browser.open('http://localhost/app/requestpw')
2240        self.browser.getControl(name="form.firstname").value = 'aNNa'
2241        self.browser.getControl(name="form.number").value = '234'
2242        self.browser.getControl(name="form.email").value = 'new@yy.zz'
2243        self.browser.getControl("Send login credentials").click()
2244        self.assertTrue('Your password request was successful.'
2245            in self.browser.contents)
2246        # ... and  student can be found in the catalog via the email address
2247        cat = queryUtility(ICatalog, name='students_catalog')
2248        results = list(
2249            cat.searchResults(
2250            email=('new@yy.zz', 'new@yy.zz')))
2251        self.assertEqual(self.student,results[0])
2252        logfile = os.path.join(
2253            self.app['datacenter'].storage, 'logs', 'main.log')
2254        logcontent = open(logfile).read()
2255        self.assertTrue('zope.anybody - students.browser.StudentRequestPasswordPage - '
2256                        '234 (K1000000) - new@yy.zz' in logcontent)
2257        return
Note: See TracBrowser for help on using the repository browser.