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

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

Enable sending emails to several email addresses all the with the same rcpt_name (Import Manager, Clearance Officer ...).

  • Property svn:keywords set to Id
File size: 115.3 KB
Line 
1## $Id: test_browser.py 9306 2012-10-07 08:32:45Z 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        # Student cant login if their password is not set
1164        self.student.password = None
1165        self.browser.open(self.login_path)
1166        self.browser.getControl(name="form.login").value = self.student_id
1167        self.browser.getControl(name="form.password").value = 'spwd'
1168        self.browser.getControl("Login").click()
1169        self.assertTrue(
1170            'You entered invalid credentials.' in self.browser.contents)
1171        # We set the password again
1172        IUserAccount(
1173            self.app['students'][self.student_id]).setPassword('spwd')
1174        IWorkflowInfo(self.student).fireTransition('admit')
1175        # Students can't login if their account is suspended/deactivated
1176        self.student.suspended = True
1177        self.browser.open(self.login_path)
1178        self.browser.getControl(name="form.login").value = self.student_id
1179        self.browser.getControl(name="form.password").value = 'spwd'
1180        self.browser.getControl("Login").click()
1181        self.assertTrue(
1182            'Your account has been deactivated.' in self.browser.contents)
1183        self.student.suspended = False
1184        self.browser.getControl("Login").click()
1185        self.assertTrue(
1186            'You logged in.' in self.browser.contents)
1187        # Admitted student can upload a passport picture
1188        self.browser.open(self.student_path + '/change_portrait')
1189        ctrl = self.browser.getControl(name='passportuploadedit')
1190        file_obj = open(SAMPLE_IMAGE, 'rb')
1191        file_ctrl = ctrl.mech_control
1192        file_ctrl.add_file(file_obj, filename='my_photo.jpg')
1193        self.browser.getControl(
1194            name='upload_passportuploadedit').click()
1195        self.assertTrue(
1196            '<img align="middle" height="125px" src="passport.jpg" />'
1197            in self.browser.contents)
1198        # Students can open admission letter
1199        self.browser.getLink("Base Data").click()
1200        self.browser.getLink("Download admission letter").click()
1201        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1202        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1203        # Student can view the clearance data
1204        self.browser.open(self.student_path)
1205        self.browser.getLink("Clearance Data").click()
1206        # Student can't open clearance edit form before starting clearance
1207        self.browser.open(self.student_path + '/cedit')
1208        self.assertMatches('...The requested form is locked...',
1209                           self.browser.contents)
1210        self.browser.getLink("Clearance Data").click()
1211        self.browser.getLink("Start clearance").click()
1212        self.student.email = None
1213        # Uups, we forgot to fill the email fields
1214        self.browser.getControl("Start clearance").click()
1215        self.assertMatches('...Not all required fields filled...',
1216                           self.browser.contents)
1217        self.browser.open(self.student_path + '/edit_base')
1218        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
1219        self.browser.getControl("Save").click()
1220        self.browser.open(self.student_path + '/start_clearance')
1221        self.browser.getControl(name="ac_series").value = '3'
1222        self.browser.getControl(name="ac_number").value = '4444444'
1223        self.browser.getControl("Start clearance now").click()
1224        self.assertMatches('...Activation code is invalid...',
1225                           self.browser.contents)
1226        self.browser.getControl(name="ac_series").value = self.existing_clrseries
1227        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
1228        # Owner is Hans Wurst, AC can't be invalidated
1229        self.browser.getControl("Start clearance now").click()
1230        self.assertMatches('...You are not the owner of this access code...',
1231                           self.browser.contents)
1232        # Set the correct owner
1233        self.existing_clrac.owner = self.student_id
1234        # clr_code might be set (and thus returns None) due importing
1235        # an empty clr_code column.
1236        self.student.clr_code = None
1237        self.browser.getControl("Start clearance now").click()
1238        self.assertMatches('...Clearance process has been started...',
1239                           self.browser.contents)
1240        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
1241        self.browser.getControl("Save", index=0).click()
1242        # Student can view the clearance data
1243        self.browser.getLink("Clearance Data").click()
1244        # and go back to the edit form
1245        self.browser.getLink("Edit").click()
1246        # Students can upload documents
1247        ctrl = self.browser.getControl(name='birthcertificateupload')
1248        file_obj = open(SAMPLE_IMAGE, 'rb')
1249        file_ctrl = ctrl.mech_control
1250        file_ctrl.add_file(file_obj, filename='my_birth_certificate.jpg')
1251        self.browser.getControl(
1252            name='upload_birthcertificateupload').click()
1253        self.assertTrue(
1254            '<a target="image" href="birth_certificate">Birth Certificate Scan</a>'
1255            in self.browser.contents)
1256        # Students can open clearance slip
1257        self.browser.getLink("View").click()
1258        self.browser.getLink("Download clearance slip").click()
1259        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1260        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1261        # Students can request clearance
1262        self.browser.open(self.edit_clearance_path)
1263        self.browser.getControl("Save and request clearance").click()
1264        self.browser.getControl(name="ac_series").value = self.existing_clrseries
1265        self.browser.getControl(name="ac_number").value = self.existing_clrnumber
1266        self.browser.getControl("Request clearance now").click()
1267        self.assertMatches('...Clearance has been requested...',
1268                           self.browser.contents)
1269        # Student can't reopen clearance form after requesting clearance
1270        self.browser.open(self.student_path + '/cedit')
1271        self.assertMatches('...The requested form is locked...',
1272                           self.browser.contents)
1273        # Student can't add study level if not in state 'school fee paid'
1274        self.browser.open(self.student_path + '/studycourse/add')
1275        self.assertMatches('...The requested form is locked...',
1276                           self.browser.contents)
1277        # ... and must be transferred first
1278        IWorkflowInfo(self.student).fireTransition('clear')
1279        IWorkflowInfo(self.student).fireTransition('pay_first_school_fee')
1280        # Now students can add the current study level
1281        self.browser.getLink("Study Course").click()
1282        self.browser.getLink("Add course list").click()
1283        self.assertMatches('...Add current level 100 (Year 1)...',
1284                           self.browser.contents)
1285        self.browser.getControl("Create course list now").click()
1286        # A level with one course ticket was created
1287        self.assertEqual(self.student['studycourse']['100'].number_of_tickets, 1)
1288        self.browser.getLink("100").click()
1289        self.browser.getLink("Edit course list").click()
1290        self.browser.getControl("Add course ticket").click()
1291        self.browser.getControl(name="form.course").value = ['COURSE1']
1292        self.browser.getControl("Add course ticket").click()
1293        self.assertMatches('...The ticket exists...',
1294                           self.browser.contents)
1295        self.student['studycourse'].current_level = 200
1296        self.browser.getLink("Study Course").click()
1297        self.browser.getLink("Add course list").click()
1298        self.assertMatches('...Add current level 200 (Year 2)...',
1299                           self.browser.contents)
1300        self.browser.getControl("Create course list now").click()
1301        self.browser.getLink("200").click()
1302        self.browser.getLink("Edit course list").click()
1303        self.browser.getControl("Add course ticket").click()
1304        self.browser.getControl(name="form.course").value = ['COURSE1']
1305        self.browser.getControl("Add course ticket").click()
1306        self.assertMatches('...The ticket exists...',
1307                           self.browser.contents)
1308        # Indeed the ticket exists as carry-over course from level 100
1309        # since its score was 0
1310        self.assertTrue(
1311            self.student['studycourse']['200']['COURSE1'].carry_over is True)
1312        # Students can open the pdf course registration slip
1313        self.browser.open(self.student_path + '/studycourse/200')
1314        self.browser.getLink("Download course registration slip").click()
1315        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1316        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1317        # Students can remove course tickets
1318        self.browser.open(self.student_path + '/studycourse/200/edit')
1319        self.browser.getControl("Remove selected", index=0).click()
1320        self.assertTrue('No ticket selected' in self.browser.contents)
1321        # No ticket can be selected since the carry-over course is a core course
1322        self.assertRaises(
1323            LookupError, self.browser.getControl, name='val_id')
1324        self.student['studycourse']['200']['COURSE1'].mandatory = False
1325        self.browser.open(self.student_path + '/studycourse/200/edit')
1326        # Course list can't be registered if total_credits exceeds max_credits
1327        self.student['studycourse']['200']['COURSE1'].credits = 60
1328        self.browser.getControl("Register course list").click()
1329        self.assertTrue('Maximum credits of 50 exceeded' in self.browser.contents)
1330        # Student can now remove the ticket
1331        ctrl = self.browser.getControl(name='val_id')
1332        ctrl.getControl(value='COURSE1').selected = True
1333        self.browser.getControl("Remove selected", index=0).click()
1334        self.assertTrue('Successfully removed' in self.browser.contents)
1335        # Course list can be registered, even if it's empty
1336        self.browser.getControl("Register course list").click()
1337        self.assertTrue('Course list has been registered' in self.browser.contents)
1338        self.assertEqual(self.student.state, 'courses registered')
1339        return
1340
1341    def test_postgraduate_student_access(self):
1342        self.certificate.study_mode = 'pg_ft'
1343        self.certificate.start_level = 999
1344        self.certificate.end_level = 999
1345        self.student['studycourse'].current_level = 999
1346        IWorkflowState(self.student).setState('school fee paid')
1347        self.browser.open(self.login_path)
1348        self.browser.getControl(name="form.login").value = self.student_id
1349        self.browser.getControl(name="form.password").value = 'spwd'
1350        self.browser.getControl("Login").click()
1351        self.assertTrue(
1352            'You logged in.' in self.browser.contents)
1353        # Now students can add the current study level
1354        self.browser.getLink("Study Course").click()
1355        self.browser.getLink("Add course list").click()
1356        self.assertMatches('...Add current level Postgraduate Level...',
1357                           self.browser.contents)
1358        self.browser.getControl("Create course list now").click()
1359        # A level with one course ticket was created
1360        self.assertEqual(self.student['studycourse']['999'].number_of_tickets, 0)
1361        self.browser.getLink("999").click()
1362        self.browser.getLink("Edit course list").click()
1363        self.browser.getControl("Add course ticket").click()
1364        self.browser.getControl(name="form.course").value = ['COURSE1']
1365        self.browser.getControl("Add course ticket").click()
1366        self.assertMatches('...Successfully added COURSE1...',
1367                           self.browser.contents)
1368        # Postgraduate students can't register course lists
1369        self.browser.getControl("Register course list").click()
1370        self.assertTrue("your course list can't bee registered"
1371            in self.browser.contents)
1372        self.assertEqual(self.student.state, 'school fee paid')
1373        return
1374
1375    def test_student_clearance_wo_clrcode(self):
1376        IWorkflowState(self.student).setState('clearance started')
1377        self.browser.open(self.login_path)
1378        self.browser.getControl(name="form.login").value = self.student_id
1379        self.browser.getControl(name="form.password").value = 'spwd'
1380        self.browser.getControl("Login").click()
1381        self.student.clearance_locked = False
1382        self.browser.open(self.edit_clearance_path)
1383        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
1384        self.browser.getControl("Save and request clearance").click()
1385        self.assertMatches('...Clearance has been requested...',
1386                           self.browser.contents)
1387
1388    def test_manage_payments(self):
1389        # Managers can add online school fee payment tickets
1390        # if certain requirements are met
1391        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1392        self.browser.open(self.payments_path)
1393        IWorkflowState(self.student).setState('cleared')
1394        self.browser.getControl("Add online payment ticket").click()
1395        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1396        self.browser.getControl("Create ticket").click()
1397        self.assertMatches('...ticket created...',
1398                           self.browser.contents)
1399        ctrl = self.browser.getControl(name='val_id')
1400        value = ctrl.options[0]
1401        self.browser.getLink(value).click()
1402        self.assertMatches('...Amount Authorized...',
1403                           self.browser.contents)
1404        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
1405        payment_url = self.browser.url
1406
1407        # The pdf payment slip can't yet be opened
1408        #self.browser.open(payment_url + '/payment_slip.pdf')
1409        #self.assertMatches('...Ticket not yet paid...',
1410        #                   self.browser.contents)
1411
1412        # The same payment (with same p_item, p_session and p_category)
1413        # can be initialized a second time if the former ticket is not yet paid.
1414        self.browser.open(self.payments_path)
1415        self.browser.getControl("Add online payment ticket").click()
1416        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1417        self.browser.getControl("Create ticket").click()
1418        self.assertMatches('...Payment ticket created...',
1419                           self.browser.contents)
1420
1421        # Managers can approve the payment
1422        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1423        self.browser.open(payment_url)
1424        self.browser.getLink("Approve payment").click()
1425        self.assertMatches('...Payment approved...',
1426                          self.browser.contents)
1427
1428        # The authorized amount has been stored in the access code
1429        self.assertEqual(
1430            self.app['accesscodes']['SFE-0'].values()[0].cost,40000.0)
1431
1432        # Payments can't be approved twice
1433        self.browser.open(payment_url + '/approve')
1434        self.assertMatches('...This ticket has already been paid...',
1435                          self.browser.contents)
1436
1437        # Now the first ticket is paid and no more ticket of same type
1438        # (with same p_item, p_session and p_category) can be added
1439        self.browser.open(self.payments_path)
1440        self.browser.getControl("Add online payment ticket").click()
1441        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1442        self.browser.getControl("Create ticket").click()
1443        self.assertMatches(
1444            '...This type of payment has already been made...',
1445            self.browser.contents)
1446
1447        # Managers can open the pdf payment slip
1448        self.browser.open(payment_url)
1449        self.browser.getLink("Download payment slip").click()
1450        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1451        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1452
1453        # Managers can remove online school fee payment tickets
1454        self.browser.open(self.payments_path)
1455        self.browser.getControl("Remove selected").click()
1456        self.assertMatches('...No payment selected...', self.browser.contents)
1457        ctrl = self.browser.getControl(name='val_id')
1458        value = ctrl.options[0]
1459        ctrl.getControl(value=value).selected = True
1460        self.browser.getControl("Remove selected", index=0).click()
1461        self.assertTrue('Successfully removed' in self.browser.contents)
1462
1463        # Managers can add online clearance payment tickets
1464        self.browser.open(self.payments_path + '/addop')
1465        self.browser.getControl(name="form.p_category").value = ['clearance']
1466        self.browser.getControl("Create ticket").click()
1467        self.assertMatches('...ticket created...',
1468                           self.browser.contents)
1469
1470        # Managers can approve the payment
1471        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
1472        ctrl = self.browser.getControl(name='val_id')
1473        value = ctrl.options[1] # The clearance payment is the second in the table
1474        self.browser.getLink(value).click()
1475        self.browser.open(self.browser.url + '/approve')
1476        self.assertMatches('...Payment approved...',
1477                          self.browser.contents)
1478        expected = '''...
1479        <td>
1480          <span>Paid</span>
1481        </td>...'''
1482        self.assertMatches(expected,self.browser.contents)
1483        # The new CLR-0 pin has been created
1484        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
1485        pin = self.app['accesscodes']['CLR-0'].keys()[0]
1486        ac = self.app['accesscodes']['CLR-0'][pin]
1487        self.assertEqual(ac.owner, self.student_id)
1488        self.assertEqual(ac.cost, 3456.0)
1489        return
1490
1491    def test_student_payments(self):
1492        # Login
1493        self.browser.open(self.login_path)
1494        self.browser.getControl(name="form.login").value = self.student_id
1495        self.browser.getControl(name="form.password").value = 'spwd'
1496        self.browser.getControl("Login").click()
1497
1498        # Students can add online clearance payment tickets
1499        self.browser.open(self.payments_path + '/addop')
1500        self.browser.getControl(name="form.p_category").value = ['clearance']
1501        self.browser.getControl("Create ticket").click()
1502        self.assertMatches('...ticket created...',
1503                           self.browser.contents)
1504
1505        # Students can't approve the payment
1506        self.assertEqual(len(self.app['accesscodes']['CLR-0']),0)
1507        ctrl = self.browser.getControl(name='val_id')
1508        value = ctrl.options[0]
1509        self.browser.getLink(value).click()
1510        payment_url = self.browser.url
1511        self.assertRaises(
1512            Unauthorized, self.browser.open, payment_url + '/approve')
1513        # In the base package they can 'use' a fake approval view
1514        self.browser.open(payment_url + '/fake_approve')
1515        self.assertMatches('...Payment approved...',
1516                          self.browser.contents)
1517        expected = '''...
1518        <td>
1519          <span>Paid</span>
1520        </td>...'''
1521        expected = '''...
1522        <td>
1523          <span>Paid</span>
1524        </td>...'''
1525        self.assertMatches(expected,self.browser.contents)
1526        payment_id = self.student['payments'].keys()[0]
1527        payment = self.student['payments'][payment_id]
1528        self.assertEqual(payment.p_state, 'paid')
1529        self.assertEqual(payment.r_amount_approved, 3456.0)
1530        self.assertEqual(payment.r_code, 'AP')
1531        self.assertEqual(payment.r_desc, u'Payment approved by Anna Tester')
1532        # The new CLR-0 pin has been created
1533        self.assertEqual(len(self.app['accesscodes']['CLR-0']),1)
1534        pin = self.app['accesscodes']['CLR-0'].keys()[0]
1535        ac = self.app['accesscodes']['CLR-0'][pin]
1536        self.assertEqual(ac.owner, self.student_id)
1537        self.assertEqual(ac.cost, 3456.0)
1538
1539        # Students can open the pdf payment slip
1540        self.browser.open(payment_url + '/payment_slip.pdf')
1541        self.assertEqual(self.browser.headers['Status'], '200 Ok')
1542        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
1543
1544        # The new CLR-0 pin can be used for starting clearance
1545        # but they have to upload a passport picture first
1546        # which is only possible in state admitted
1547        self.browser.open(self.student_path + '/change_portrait')
1548        self.assertMatches('...form is locked...',
1549                          self.browser.contents)
1550        IWorkflowInfo(self.student).fireTransition('admit')
1551        self.browser.open(self.student_path + '/change_portrait')
1552        image = open(SAMPLE_IMAGE, 'rb')
1553        ctrl = self.browser.getControl(name='passportuploadedit')
1554        file_ctrl = ctrl.mech_control
1555        file_ctrl.add_file(image, filename='my_photo.jpg')
1556        self.browser.getControl(
1557            name='upload_passportuploadedit').click()
1558        self.browser.open(self.student_path + '/start_clearance')
1559        parts = pin.split('-')[1:]
1560        clrseries, clrnumber = parts
1561        self.browser.getControl(name="ac_series").value = clrseries
1562        self.browser.getControl(name="ac_number").value = clrnumber
1563        self.browser.getControl("Start clearance now").click()
1564        self.assertMatches('...Clearance process has been started...',
1565                           self.browser.contents)
1566
1567        # Students can add online school fee payment tickets.
1568        IWorkflowState(self.student).setState('returning')
1569        self.browser.open(self.payments_path)
1570        self.browser.getControl("Add online payment ticket").click()
1571        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1572        self.browser.getControl("Create ticket").click()
1573        self.assertMatches('...ticket created...',
1574                           self.browser.contents)
1575        ctrl = self.browser.getControl(name='val_id')
1576        value = ctrl.options[0]
1577        self.browser.getLink(value).click()
1578        self.assertMatches('...Amount Authorized...',
1579                           self.browser.contents)
1580        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
1581        # Payment session and will be calculated as defined
1582        # in w.k.students.utils because we set changed the state
1583        # to returning
1584        self.assertEqual(self.student['payments'][value].p_session, 2005)
1585        self.assertEqual(self.student['payments'][value].p_level, 200)
1586
1587        # Student is the payee of the payment ticket.
1588        webservice = IPaymentWebservice(self.student['payments'][value])
1589        self.assertEqual(webservice.display_fullname, 'Anna Tester')
1590        self.assertEqual(webservice.id, self.student_id)
1591        self.assertEqual(webservice.faculty, 'fac1')
1592        self.assertEqual(webservice.department, 'dep1')
1593
1594        # We simulate the approval
1595        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1596        self.browser.open(self.browser.url + '/fake_approve')
1597        self.assertMatches('...Payment approved...',
1598                          self.browser.contents)
1599
1600        # Students can remove only online payment tickets which have
1601        # not received a valid callback
1602        self.browser.open(self.payments_path)
1603        self.assertRaises(
1604            LookupError, self.browser.getControl, name='val_id')
1605        self.browser.open(self.payments_path + '/addop')
1606        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1607        self.browser.getControl("Create ticket").click()
1608        self.browser.open(self.payments_path)
1609        ctrl = self.browser.getControl(name='val_id')
1610        value = ctrl.options[0]
1611        ctrl.getControl(value=value).selected = True
1612        self.browser.getControl("Remove selected", index=0).click()
1613        self.assertTrue('Successfully removed' in self.browser.contents)
1614
1615        # The new SFE-0 pin can be used for starting new session
1616        self.browser.open(self.studycourse_path)
1617        self.browser.getLink('Start new session').click()
1618        pin = self.app['accesscodes']['SFE-0'].keys()[0]
1619        parts = pin.split('-')[1:]
1620        sfeseries, sfenumber = parts
1621        self.browser.getControl(name="ac_series").value = sfeseries
1622        self.browser.getControl(name="ac_number").value = sfenumber
1623        self.browser.getControl("Start now").click()
1624        self.assertMatches('...Session started...',
1625                           self.browser.contents)
1626        self.assertTrue(self.student.state == 'school fee paid')
1627        return
1628
1629    def test_student_previous_payments(self):
1630        configuration = createObject('waeup.SessionConfiguration')
1631        configuration.academic_session = 2000
1632        configuration.clearance_fee = 3456.0
1633        configuration.booking_fee = 123.4
1634        self.app['configuration'].addSessionConfiguration(configuration)
1635        configuration2 = createObject('waeup.SessionConfiguration')
1636        configuration2.academic_session = 2003
1637        configuration2.clearance_fee = 3456.0
1638        configuration2.booking_fee = 123.4
1639        self.app['configuration'].addSessionConfiguration(configuration2)
1640
1641        self.student['studycourse'].entry_session = 2002
1642
1643        # Login
1644        self.browser.open(self.login_path)
1645        self.browser.getControl(name="form.login").value = self.student_id
1646        self.browser.getControl(name="form.password").value = 'spwd'
1647        self.browser.getControl("Login").click()
1648
1649        # Students can add previous school fee payment tickets in any state.
1650        IWorkflowState(self.student).setState('courses registered')
1651        self.browser.open(self.payments_path)
1652        self.browser.getControl("Add online payment ticket").click()
1653        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1654        self.browser.getControl("Create ticket").click()
1655
1656        # Amount cannot be determined since the state is not
1657        # 'cleared' or 'returning'
1658        self.assertMatches('...Amount could not be determined...',
1659                           self.browser.contents)
1660        self.assertMatches('...Would you like to pay for a previous session?...',
1661                           self.browser.contents)
1662
1663        # Previous session payment form is provided
1664        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1665        self.browser.getControl(name="form.p_session").value = ['2000']
1666        self.browser.getControl(name="form.p_level").value = ['300']
1667        self.browser.getControl("Create ticket").click()
1668        self.assertMatches('...The previous session must not fall below...',
1669                           self.browser.contents)
1670        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1671        self.browser.getControl(name="form.p_session").value = ['2004']
1672        self.browser.getControl(name="form.p_level").value = ['300']
1673        self.browser.getControl("Create ticket").click()
1674        self.assertMatches('...This is not a previous session...',
1675                           self.browser.contents)
1676        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1677        self.browser.getControl(name="form.p_session").value = ['2003']
1678        self.browser.getControl(name="form.p_level").value = ['300']
1679        self.browser.getControl("Create ticket").click()
1680        self.assertMatches('...ticket created...',
1681                           self.browser.contents)
1682        ctrl = self.browser.getControl(name='val_id')
1683        value = ctrl.options[0]
1684        self.browser.getLink(value).click()
1685        self.assertMatches('...Amount Authorized...',
1686                           self.browser.contents)
1687        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
1688
1689        # Payment session is properly set
1690        self.assertEqual(self.student['payments'][value].p_session, 2003)
1691        self.assertEqual(self.student['payments'][value].p_level, 300)
1692
1693        # We simulate the approval
1694        self.browser.open(self.browser.url + '/fake_approve')
1695        self.assertMatches('...Payment approved...',
1696                          self.browser.contents)
1697
1698        # No AC has been created
1699        self.assertEqual(len(self.app['accesscodes']['SFE-0'].keys()), 0)
1700        self.assertTrue(self.student['payments'][value].ac is None)
1701
1702        # Current payment flag is set False
1703        self.assertFalse(self.student['payments'][value].p_current)
1704        return
1705
1706    def test_postgraduate_student_payments(self):
1707        self.certificate.study_mode = 'pg_ft'
1708        self.certificate.start_level = 999
1709        self.certificate.end_level = 999
1710        self.student['studycourse'].current_level = 999
1711        # Login
1712        self.browser.open(self.login_path)
1713        self.browser.getControl(name="form.login").value = self.student_id
1714        self.browser.getControl(name="form.password").value = 'spwd'
1715        self.browser.getControl("Login").click()
1716        # Students can add online school fee payment tickets.
1717        IWorkflowState(self.student).setState('cleared')
1718        self.browser.open(self.payments_path)
1719        self.browser.getControl("Add online payment ticket").click()
1720        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1721        self.browser.getControl("Create ticket").click()
1722        self.assertMatches('...ticket created...',
1723                           self.browser.contents)
1724        ctrl = self.browser.getControl(name='val_id')
1725        value = ctrl.options[0]
1726        self.browser.getLink(value).click()
1727        self.assertMatches('...Amount Authorized...',
1728                           self.browser.contents)
1729        # Payment session and level are current ones.
1730        # Postgrads have to pay school_fee_1.
1731        self.assertEqual(self.student['payments'][value].amount_auth, 40000.0)
1732        self.assertEqual(self.student['payments'][value].p_session, 2004)
1733        self.assertEqual(self.student['payments'][value].p_level, 999)
1734
1735        # We simulate the approval
1736        self.assertEqual(len(self.app['accesscodes']['SFE-0']),0)
1737        self.browser.open(self.browser.url + '/fake_approve')
1738        self.assertMatches('...Payment approved...',
1739                          self.browser.contents)
1740
1741        # The new SFE-0 pin can be used for starting session
1742        self.browser.open(self.studycourse_path)
1743        self.browser.getLink('Start new session').click()
1744        pin = self.app['accesscodes']['SFE-0'].keys()[0]
1745        parts = pin.split('-')[1:]
1746        sfeseries, sfenumber = parts
1747        self.browser.getControl(name="ac_series").value = sfeseries
1748        self.browser.getControl(name="ac_number").value = sfenumber
1749        self.browser.getControl("Start now").click()
1750        self.assertMatches('...Session started...',
1751                           self.browser.contents)
1752        self.assertTrue(self.student.state == 'school fee paid')
1753
1754        # Postgrad students do not need to register courses the
1755        # can just pay for the next session.
1756        self.browser.open(self.payments_path)
1757        # Remove first payment to be sure that we access the right ticket
1758        del self.student['payments'][value]
1759        self.browser.getControl("Add online payment ticket").click()
1760        self.browser.getControl(name="form.p_category").value = ['schoolfee']
1761        self.browser.getControl("Create ticket").click()
1762        ctrl = self.browser.getControl(name='val_id')
1763        value = ctrl.options[0]
1764        self.browser.getLink(value).click()
1765        # Payment session has increased by one, payment level remains the same.
1766        # Returning Postgraduates have to pay school_fee_2.
1767        self.assertEqual(self.student['payments'][value].amount_auth, 20000.0)
1768        self.assertEqual(self.student['payments'][value].p_session, 2005)
1769        self.assertEqual(self.student['payments'][value].p_level, 999)
1770
1771        # Student is still in old session
1772        self.assertEqual(self.student.current_session, 2004)
1773
1774        # We do not need to pay the ticket if any other
1775        # SFE pin is provided
1776        pin_container = self.app['accesscodes']
1777        pin_container.createBatch(
1778            datetime.utcnow(), 'some_userid', 'SFE', 9.99, 1)
1779        pin = pin_container['SFE-1'].values()[0].representation
1780        sfeseries, sfenumber = pin.split('-')[1:]
1781        # The new SFE-1 pin can be used for starting new session
1782        self.browser.open(self.studycourse_path)
1783        self.browser.getLink('Start new session').click()
1784        self.browser.getControl(name="ac_series").value = sfeseries
1785        self.browser.getControl(name="ac_number").value = sfenumber
1786        self.browser.getControl("Start now").click()
1787        self.assertMatches('...Session started...',
1788                           self.browser.contents)
1789        self.assertTrue(self.student.state == 'school fee paid')
1790        # Student is in new session
1791        self.assertEqual(self.student.current_session, 2005)
1792        self.assertEqual(self.student['studycourse'].current_level, 999)
1793        return
1794
1795    def test_manage_accommodation(self):
1796        # Managers can add online booking fee payment tickets and open the
1797        # callback view (see test_manage_payments)
1798        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
1799        self.browser.open(self.payments_path)
1800        self.browser.getControl("Add online payment ticket").click()
1801        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1802        # If student is not in accommodation session, payment cannot be processed
1803        self.app['hostels'].accommodation_session = 2011
1804        self.browser.getControl("Create ticket").click()
1805        self.assertMatches('...Your current session does not match...',
1806                           self.browser.contents)
1807        self.app['hostels'].accommodation_session = 2004
1808        self.browser.getControl("Add online payment ticket").click()
1809        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1810        self.browser.getControl("Create ticket").click()
1811        ctrl = self.browser.getControl(name='val_id')
1812        value = ctrl.options[0]
1813        self.browser.getLink(value).click()
1814        self.browser.open(self.browser.url + '/approve')
1815        # The new HOS-0 pin has been created
1816        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
1817        pin = self.app['accesscodes']['HOS-0'].keys()[0]
1818        ac = self.app['accesscodes']['HOS-0'][pin]
1819        self.assertEqual(ac.owner, self.student_id)
1820        parts = pin.split('-')[1:]
1821        sfeseries, sfenumber = parts
1822        # Managers can use HOS code and book a bed space with it
1823        self.browser.open(self.acco_path)
1824        self.browser.getLink("Book accommodation").click()
1825        self.assertMatches('...You are in the wrong...',
1826                           self.browser.contents)
1827        IWorkflowInfo(self.student).fireTransition('admit')
1828        # An existing HOS code can only be used if students
1829        # are in accommodation session
1830        self.student['studycourse'].current_session = 2003
1831        self.browser.getLink("Book accommodation").click()
1832        self.assertMatches('...Your current session does not match...',
1833                           self.browser.contents)
1834        self.student['studycourse'].current_session = 2004
1835        # All requirements are met and ticket can be created
1836        self.browser.getLink("Book accommodation").click()
1837        self.assertMatches('...Activation Code:...',
1838                           self.browser.contents)
1839        self.browser.getControl(name="ac_series").value = sfeseries
1840        self.browser.getControl(name="ac_number").value = sfenumber
1841        self.browser.getControl("Create bed ticket").click()
1842        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1843                           self.browser.contents)
1844        # Bed has been allocated
1845        bed1 = self.app['hostels']['hall-1']['hall-1_A_101_A']
1846        self.assertTrue(bed1.owner == self.student_id)
1847        # BedTicketAddPage is now blocked
1848        self.browser.getLink("Book accommodation").click()
1849        self.assertMatches('...You already booked a bed space...',
1850            self.browser.contents)
1851        # The bed ticket displays the data correctly
1852        self.browser.open(self.acco_path + '/2004')
1853        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1854                           self.browser.contents)
1855        self.assertMatches('...2004/2005...', self.browser.contents)
1856        self.assertMatches('...regular_male_fr...', self.browser.contents)
1857        self.assertMatches('...%s...' % pin, self.browser.contents)
1858        # Managers can relocate students if the student's bed_type has changed
1859        self.browser.getLink("Relocate student").click()
1860        self.assertMatches(
1861            "...Student can't be relocated...", self.browser.contents)
1862        self.student.sex = u'f'
1863        self.browser.getLink("Relocate student").click()
1864        self.assertMatches(
1865            "...Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1866        self.assertTrue(bed1.owner == NOT_OCCUPIED)
1867        bed2 = self.app['hostels']['hall-1']['hall-1_A_101_B']
1868        self.assertTrue(bed2.owner == self.student_id)
1869        self.assertTrue(self.student['accommodation'][
1870            '2004'].bed_type == u'regular_female_fr')
1871        # The payment object still shows the original payment item
1872        payment_id = self.student['payments'].keys()[0]
1873        payment = self.student['payments'][payment_id]
1874        self.assertTrue(payment.p_item == u'regular_male_fr')
1875        # Managers can relocate students if the bed's bed_type has changed
1876        bed1.bed_type = u'regular_female_fr'
1877        bed2.bed_type = u'regular_male_fr'
1878        notify(grok.ObjectModifiedEvent(bed1))
1879        notify(grok.ObjectModifiedEvent(bed2))
1880        self.browser.getLink("Relocate student").click()
1881        self.assertMatches(
1882            "...Student relocated...", self.browser.contents)
1883        self.assertMatches(
1884            "... Hall 1, Block A, Room 101, Bed A...", self.browser.contents)
1885        self.assertMatches(bed1.owner, self.student_id)
1886        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1887        # Managers can't relocate students if bed is reserved
1888        self.student.sex = u'm'
1889        bed1.bed_type = u'regular_female_reserved'
1890        notify(grok.ObjectModifiedEvent(bed1))
1891        self.browser.getLink("Relocate student").click()
1892        self.assertMatches(
1893            "...Students in reserved beds can't be relocated...",
1894            self.browser.contents)
1895        # Managers can relocate students if booking has been cancelled but
1896        # other bed space has been manually allocated after cancellation
1897        old_owner = bed1.releaseBed()
1898        self.assertMatches(old_owner, self.student_id)
1899        bed2.owner = self.student_id
1900        self.browser.open(self.acco_path + '/2004')
1901        self.assertMatches(
1902            "...booking cancelled...", self.browser.contents)
1903        self.browser.getLink("Relocate student").click()
1904        # We didn't informed the catalog therefore the new owner is not found
1905        self.assertMatches(
1906            "...There is no free bed in your category regular_male_fr...",
1907            self.browser.contents)
1908        # Now we fire the event properly
1909        notify(grok.ObjectModifiedEvent(bed2))
1910        self.browser.getLink("Relocate student").click()
1911        self.assertMatches(
1912            "...Student relocated...", self.browser.contents)
1913        self.assertMatches(
1914            "... Hall 1, Block A, Room 101, Bed B...", self.browser.contents)
1915          # Managers can delete bed tickets
1916        self.browser.open(self.acco_path)
1917        ctrl = self.browser.getControl(name='val_id')
1918        value = ctrl.options[0]
1919        ctrl.getControl(value=value).selected = True
1920        self.browser.getControl("Remove selected", index=0).click()
1921        self.assertMatches('...Successfully removed...', self.browser.contents)
1922        # The bed has been properly released by the event handler
1923        self.assertMatches(bed1.owner, NOT_OCCUPIED)
1924        self.assertMatches(bed2.owner, NOT_OCCUPIED)
1925        return
1926
1927    def test_student_accommodation(self):
1928        # Login
1929        self.browser.open(self.login_path)
1930        self.browser.getControl(name="form.login").value = self.student_id
1931        self.browser.getControl(name="form.password").value = 'spwd'
1932        self.browser.getControl("Login").click()
1933
1934        # Students can add online booking fee payment tickets and open the
1935        # callback view (see test_manage_payments)
1936        self.browser.getLink("Payments").click()
1937        self.browser.getControl("Add online payment ticket").click()
1938        self.browser.getControl(name="form.p_category").value = ['bed_allocation']
1939        self.browser.getControl("Create ticket").click()
1940        ctrl = self.browser.getControl(name='val_id')
1941        value = ctrl.options[0]
1942        self.browser.getLink(value).click()
1943        self.browser.open(self.browser.url + '/fake_approve')
1944        # The new HOS-0 pin has been created
1945        self.assertEqual(len(self.app['accesscodes']['HOS-0']),1)
1946        pin = self.app['accesscodes']['HOS-0'].keys()[0]
1947        ac = self.app['accesscodes']['HOS-0'][pin]
1948        parts = pin.split('-')[1:]
1949        sfeseries, sfenumber = parts
1950
1951        # Students can use HOS code and book a bed space with it ...
1952        self.browser.open(self.acco_path)
1953        # ... but not if booking period has expired ...
1954        self.app['hostels'].enddate = datetime.now(pytz.utc)
1955        self.browser.getLink("Book accommodation").click()
1956        self.assertMatches('...Outside booking period: ...',
1957                           self.browser.contents)
1958        self.app['hostels'].enddate = datetime.now(pytz.utc) + timedelta(days=10)
1959        # ... or student is not the an allowed state ...
1960        self.browser.getLink("Book accommodation").click()
1961        self.assertMatches('...You are in the wrong...',
1962                           self.browser.contents)
1963        IWorkflowInfo(self.student).fireTransition('admit')
1964        self.browser.getLink("Book accommodation").click()
1965        self.assertMatches('...Activation Code:...',
1966                           self.browser.contents)
1967        # Student can't used faked ACs ...
1968        self.browser.getControl(name="ac_series").value = u'nonsense'
1969        self.browser.getControl(name="ac_number").value = sfenumber
1970        self.browser.getControl("Create bed ticket").click()
1971        self.assertMatches('...Activation code is invalid...',
1972                           self.browser.contents)
1973        # ... or ACs owned by somebody else.
1974        ac.owner = u'Anybody'
1975        self.browser.getControl(name="ac_series").value = sfeseries
1976        self.browser.getControl(name="ac_number").value = sfenumber
1977        self.browser.getControl("Create bed ticket").click()
1978        self.assertMatches('...You are not the owner of this access code...',
1979                           self.browser.contents)
1980        ac.owner = self.student_id
1981        self.browser.getControl(name="ac_series").value = sfeseries
1982        self.browser.getControl(name="ac_number").value = sfenumber
1983        self.browser.getControl("Create bed ticket").click()
1984        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1985                           self.browser.contents)
1986
1987        # Bed has been allocated
1988        bed = self.app['hostels']['hall-1']['hall-1_A_101_A']
1989        self.assertTrue(bed.owner == self.student_id)
1990
1991        # BedTicketAddPage is now blocked
1992        self.browser.getLink("Book accommodation").click()
1993        self.assertMatches('...You already booked a bed space...',
1994            self.browser.contents)
1995
1996        # The bed ticket displays the data correctly
1997        self.browser.open(self.acco_path + '/2004')
1998        self.assertMatches('...Hall 1, Block A, Room 101, Bed A...',
1999                           self.browser.contents)
2000        self.assertMatches('...2004/2005...', self.browser.contents)
2001        self.assertMatches('...regular_male_fr...', self.browser.contents)
2002        self.assertMatches('...%s...' % pin, self.browser.contents)
2003
2004        # Students can open the pdf slip
2005        self.browser.open(self.browser.url + '/bed_allocation.pdf')
2006        self.assertEqual(self.browser.headers['Status'], '200 Ok')
2007        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
2008
2009        # Students can't relocate themselves
2010        self.assertFalse('Relocate' in self.browser.contents)
2011        relocate_path = self.acco_path + '/2004/relocate'
2012        self.assertRaises(
2013            Unauthorized, self.browser.open, relocate_path)
2014
2015        # Students can't the Remove button and check boxes
2016        self.browser.open(self.acco_path)
2017        self.assertFalse('Remove' in self.browser.contents)
2018        self.assertFalse('val_id' in self.browser.contents)
2019        return
2020
2021    def test_change_password_request(self):
2022        self.browser.open('http://localhost/app/changepw')
2023        self.browser.getControl(name="form.identifier").value = '123'
2024        self.browser.getControl(name="form.email").value = 'aa@aa.ng'
2025        self.browser.getControl("Send login credentials").click()
2026        self.assertTrue('An email with' in self.browser.contents)
2027
2028    def test_change_current_mode(self):
2029        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2030        self.browser.open(self.clearance_path)
2031        self.assertFalse('Employer' in self.browser.contents)
2032        self.browser.open(self.manage_clearance_path)
2033        self.assertFalse('Employer' in self.browser.contents)
2034        self.student.clearance_locked = False
2035        self.browser.open(self.edit_clearance_path)
2036        self.assertFalse('Employer' in self.browser.contents)
2037        # Now we change the study mode of the certificate and a different
2038        # interface is used by clearance views.
2039        self.certificate.study_mode = 'pg_ft'
2040        # Invariants are not being checked here?!
2041        self.certificate.end_level = 100
2042        self.browser.open(self.clearance_path)
2043        self.assertTrue('Employer' in self.browser.contents)
2044        self.browser.open(self.manage_clearance_path)
2045        self.assertTrue('Employer' in self.browser.contents)
2046        self.browser.open(self.edit_clearance_path)
2047        self.assertTrue('Employer' in self.browser.contents)
2048
2049    def test_activate_deactivate_buttons(self):
2050        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2051        self.browser.open(self.student_path)
2052        self.browser.getLink("Deactivate").click()
2053        self.assertTrue(
2054            'Student account has been deactivated.' in self.browser.contents)
2055        self.assertTrue(
2056            'Base Data (account deactivated)' in self.browser.contents)
2057        self.assertTrue(self.student.suspended)
2058        self.browser.getLink("Activate").click()
2059        self.assertTrue(
2060            'Student account has been activated.' in self.browser.contents)
2061        self.assertFalse(
2062            'Base Data (account deactivated)' in self.browser.contents)
2063        self.assertFalse(self.student.suspended)
2064        # History messages have been added ...
2065        self.browser.getLink("History").click()
2066        self.assertTrue(
2067            'Student account deactivated by Manager<br />' in self.browser.contents)
2068        self.assertTrue(
2069            'Student account activated by Manager<br />' in self.browser.contents)
2070        # ... and actions have been logged.
2071        logfile = os.path.join(
2072            self.app['datacenter'].storage, 'logs', 'students.log')
2073        logcontent = open(logfile).read()
2074        self.assertTrue('zope.mgr - students.browser.StudentDeactivatePage - '
2075                        'K1000000 - account deactivated' in logcontent)
2076        self.assertTrue('zope.mgr - students.browser.StudentActivatePage - '
2077                        'K1000000 - account activated' in logcontent)
2078
2079    def test_student_locked_level_forms(self):
2080
2081        # Add two study levels, one current and one previous
2082        studylevel = createObject(u'waeup.StudentStudyLevel')
2083        studylevel.level = 100
2084        self.student['studycourse'].addStudentStudyLevel(
2085            self.certificate, studylevel)
2086        studylevel = createObject(u'waeup.StudentStudyLevel')
2087        studylevel.level = 200
2088        self.student['studycourse'].addStudentStudyLevel(
2089            self.certificate, studylevel)
2090        IWorkflowState(self.student).setState('school fee paid')
2091        self.student['studycourse'].current_level = 200
2092
2093        self.browser.open(self.login_path)
2094        self.browser.getControl(name="form.login").value = self.student_id
2095        self.browser.getControl(name="form.password").value = 'spwd'
2096        self.browser.getControl("Login").click()
2097
2098        self.browser.open(self.student_path + '/studycourse/200/edit')
2099        self.assertFalse('The requested form is locked' in self.browser.contents)
2100        self.browser.open(self.student_path + '/studycourse/100/edit')
2101        self.assertTrue('The requested form is locked' in self.browser.contents)
2102
2103        self.browser.open(self.student_path + '/studycourse/200/ctadd')
2104        self.assertFalse('The requested form is locked' in self.browser.contents)
2105        self.browser.open(self.student_path + '/studycourse/100/ctadd')
2106        self.assertTrue('The requested form is locked' in self.browser.contents)
2107
2108        IWorkflowState(self.student).setState('courses registered')
2109        self.browser.open(self.student_path + '/studycourse/200/edit')
2110        self.assertTrue('The requested form is locked' in self.browser.contents)
2111        self.browser.open(self.student_path + '/studycourse/200/ctadd')
2112        self.assertTrue('The requested form is locked' in self.browser.contents)
2113
2114
2115    def test_manage_student_transfer(self):
2116        # Add second certificate
2117        self.certificate2 = createObject('waeup.Certificate')
2118        self.certificate2.code = u'CERT2'
2119        self.certificate2.study_mode = 'ug_ft'
2120        self.certificate2.start_level = 999
2121        self.certificate2.end_level = 999
2122        self.app['faculties']['fac1']['dep1'].certificates.addCertificate(
2123            self.certificate2)
2124
2125        # Add study level to old study course
2126        studylevel = createObject(u'waeup.StudentStudyLevel')
2127        studylevel.level = 200
2128        self.student['studycourse'].addStudentStudyLevel(
2129            self.certificate, studylevel)
2130        studylevel = createObject(u'waeup.StudentStudyLevel')
2131        studylevel.level = 999
2132        self.student['studycourse'].addStudentStudyLevel(
2133            self.certificate, studylevel)
2134
2135        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
2136        self.browser.open(self.student_path)
2137        self.browser.getLink("Transfer").click()
2138        self.browser.getControl(name="form.certificate").value = ['CERT2']
2139        self.browser.getControl(name="form.current_session").value = ['2011']
2140        self.browser.getControl(name="form.current_level").value = ['200']
2141        self.browser.getControl("Transfer").click()
2142        self.assertTrue(
2143            'Current level does not match certificate levels'
2144            in self.browser.contents)
2145        self.browser.getControl(name="form.current_level").value = ['999']
2146        self.browser.getControl("Transfer").click()
2147        self.assertTrue('Successfully transferred' in self.browser.contents)
2148        # The catalog has been updated
2149        cat = queryUtility(ICatalog, name='students_catalog')
2150        results = list(
2151            cat.searchResults(
2152            certcode=('CERT2', 'CERT2')))
2153        self.assertTrue(results[0] is self.student)
2154        results = list(
2155            cat.searchResults(
2156            current_session=(2011, 2011)))
2157        self.assertTrue(results[0] is self.student)
2158        # Add study level to new study course
2159        studylevel = createObject(u'waeup.StudentStudyLevel')
2160        studylevel.level = 999
2161        self.student['studycourse'].addStudentStudyLevel(
2162            self.certificate, studylevel)
2163
2164        # Edit and add pages are locked for old study courses
2165        self.browser.open(self.student_path + '/studycourse/manage')
2166        self.assertFalse('The requested form is locked' in self.browser.contents)
2167        self.browser.open(self.student_path + '/studycourse_1/manage')
2168        self.assertTrue('The requested form is locked' in self.browser.contents)
2169
2170        self.browser.open(self.student_path + '/studycourse/start_session')
2171        self.assertFalse('The requested form is locked' in self.browser.contents)
2172        self.browser.open(self.student_path + '/studycourse_1/start_session')
2173        self.assertTrue('The requested form is locked' in self.browser.contents)
2174
2175        IWorkflowState(self.student).setState('school fee paid')
2176        self.browser.open(self.student_path + '/studycourse/add')
2177        self.assertFalse('The requested form is locked' in self.browser.contents)
2178        self.browser.open(self.student_path + '/studycourse_1/add')
2179        self.assertTrue('The requested form is locked' in self.browser.contents)
2180
2181        self.browser.open(self.student_path + '/studycourse/999/manage')
2182        self.assertFalse('The requested form is locked' in self.browser.contents)
2183        self.browser.open(self.student_path + '/studycourse_1/999/manage')
2184        self.assertTrue('The requested form is locked' in self.browser.contents)
2185
2186        self.browser.open(self.student_path + '/studycourse/999/validate_courses')
2187        self.assertFalse('The requested form is locked' in self.browser.contents)
2188        self.browser.open(self.student_path + '/studycourse_1/999/validate_courses')
2189        self.assertTrue('The requested form is locked' in self.browser.contents)
2190
2191        self.browser.open(self.student_path + '/studycourse/999/reject_courses')
2192        self.assertFalse('The requested form is locked' in self.browser.contents)
2193        self.browser.open(self.student_path + '/studycourse_1/999/reject_courses')
2194        self.assertTrue('The requested form is locked' in self.browser.contents)
2195
2196        self.browser.open(self.student_path + '/studycourse/999/add')
2197        self.assertFalse('The requested form is locked' in self.browser.contents)
2198        self.browser.open(self.student_path + '/studycourse_1/999/add')
2199        self.assertTrue('The requested form is locked' in self.browser.contents)
2200
2201        self.browser.open(self.student_path + '/studycourse/999/edit')
2202        self.assertFalse('The requested form is locked' in self.browser.contents)
2203        self.browser.open(self.student_path + '/studycourse_1/999/edit')
2204        self.assertTrue('The requested form is locked' in self.browser.contents)
2205
2206class StudentRequestPWTests(StudentsFullSetup):
2207    # Tests for student registration
2208
2209    layer = FunctionalLayer
2210
2211    def test_request_pw(self):
2212        # Student with wrong number can't be found.
2213        self.browser.open('http://localhost/app/requestpw')
2214        self.browser.getControl(name="form.firstname").value = 'Anna'
2215        self.browser.getControl(name="form.number").value = 'anynumber'
2216        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
2217        self.browser.getControl("Send login credentials").click()
2218        self.assertTrue('No student record found.'
2219            in self.browser.contents)
2220        # Anonymous is not informed that firstname verification failed.
2221        # It seems that the record doesn't exist.
2222        self.browser.open('http://localhost/app/requestpw')
2223        self.browser.getControl(name="form.firstname").value = 'Johnny'
2224        self.browser.getControl(name="form.number").value = '123'
2225        self.browser.getControl(name="form.email").value = 'xx@yy.zz'
2226        self.browser.getControl("Send login credentials").click()
2227        self.assertTrue('No student record found.'
2228            in self.browser.contents)
2229        # Even with the correct firstname we can't register if a
2230        # password has been set and used.
2231        self.browser.getControl(name="form.firstname").value = 'Anna'
2232        self.browser.getControl(name="form.number").value = '123'
2233        self.browser.getControl("Send login credentials").click()
2234        self.assertTrue('Your password has already been set and used.'
2235            in self.browser.contents)
2236        self.browser.open('http://localhost/app/requestpw')
2237        self.app['students'][self.student_id].password = None
2238        # The firstname field, used for verification, is not case-sensitive.
2239        self.browser.getControl(name="form.firstname").value = 'aNNa'
2240        self.browser.getControl(name="form.number").value = '123'
2241        self.browser.getControl(name="form.email").value = 'new@yy.zz'
2242        self.browser.getControl("Send login credentials").click()
2243        # Yeah, we succeded ...
2244        self.assertTrue('Your password request was successful.'
2245            in self.browser.contents)
2246        # We can also use the matric_number instead.
2247        self.browser.open('http://localhost/app/requestpw')
2248        self.browser.getControl(name="form.firstname").value = 'aNNa'
2249        self.browser.getControl(name="form.number").value = '234'
2250        self.browser.getControl(name="form.email").value = 'new@yy.zz'
2251        self.browser.getControl("Send login credentials").click()
2252        self.assertTrue('Your password request was successful.'
2253            in self.browser.contents)
2254        # ... and  student can be found in the catalog via the email address
2255        cat = queryUtility(ICatalog, name='students_catalog')
2256        results = list(
2257            cat.searchResults(
2258            email=('new@yy.zz', 'new@yy.zz')))
2259        self.assertEqual(self.student,results[0])
2260        logfile = os.path.join(
2261            self.app['datacenter'].storage, 'logs', 'main.log')
2262        logcontent = open(logfile).read()
2263        self.assertTrue('zope.anybody - students.browser.StudentRequestPasswordPage - '
2264                        '234 (K1000000) - new@yy.zz' in logcontent)
2265        return
Note: See TracBrowser for help on using the repository browser.