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

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

Remove temporary dirs in tests.

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