source: main/kofacustom.nigeria/trunk/src/kofacustom/nigeria/students/tests/test_browser.py @ 9356

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

Capital O was mixed up with Zero. Capital O is an accepted verdict for next session transition. Therefore the test passed and did not test what it was expected to test.

  • Property svn:keywords set to Id
File size: 12.9 KB
Line 
1## $Id: test_browser.py 9356 2012-10-19 16:26:27Z 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##
18import os
19import shutil
20import tempfile
21from StringIO import StringIO
22from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
23from zope.component.hooks import setSite, clearSite
24from zope.component import getUtility, createObject
25from zope.interface import verify
26from waeup.kofa.app import University
27from waeup.kofa.students.tests.test_browser import (
28    StudentsFullSetup, SAMPLE_IMAGE)
29from waeup.kofa.testing import FunctionalTestCase
30from waeup.kofa.interfaces import (
31    IExtFileStore, IFileStoreNameChooser)
32from waeup.kofa.students.batching import StudentProcessor
33from waeup.kofa.students.interfaces import IStudentsUtils
34from kofacustom.nigeria.students.batching import NigeriaStudentProcessor
35from kofacustom.nigeria.testing import FunctionalLayer
36from kofacustom.nigeria.students.interfaces import (
37    INigeriaStudentStudyCourse, INigeriaStudent,
38    INigeriaStudentStudyLevel, INigeriaCourseTicket)
39
40
41STUDENT_SAMPLE_DATA = open(
42    os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'),
43    'rb').read()
44
45STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split(
46    '\n')[0].split(',')
47
48class StudentProcessorTest(FunctionalTestCase):
49    """Perform some batching tests.
50    """
51
52    layer = FunctionalLayer
53
54    def setUp(self):
55        super(StudentProcessorTest, self).setUp()
56        # Setup a sample site for each test
57        app = University()
58        self.dc_root = tempfile.mkdtemp()
59        app['datacenter'].setStoragePath(self.dc_root)
60
61        # Prepopulate the ZODB...
62        self.getRootFolder()['app'] = app
63        # we add the site immediately after creation to the
64        # ZODB. Catalogs and other local utilities are not setup
65        # before that step.
66        self.app = self.getRootFolder()['app']
67        # Set site here. Some of the following setup code might need
68        # to access grok.getSite() and should get our new app then
69        setSite(app)
70
71        self.processor_base = StudentProcessor()
72        self.processor = NigeriaStudentProcessor()
73        self.workdir = tempfile.mkdtemp()
74        self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv')
75        open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA)
76
77    def tearDown(self):
78        super(StudentProcessorTest, self).tearDown()
79        shutil.rmtree(self.workdir)
80        shutil.rmtree(self.dc_root)
81        clearSite()
82        return
83
84    def test_import(self):
85        # We have an empty column 'date_of_birth' in  the import file.
86        # The original processor will fail because 'date_of_birth' is required
87        # in the base package.
88        num, num_warns, fin_file, fail_file = self.processor_base.doImport(
89            self.csv_file, STUDENT_HEADER_FIELDS)
90        self.assertEqual(num_warns,3)
91        assert len(self.app['students'].keys()) == 0
92        # The customized processor does not complain since 'date_of_birth' is
93        # not required in the custom package.
94        num, num_warns, fin_file, fail_file = self.processor.doImport(
95            self.csv_file, STUDENT_HEADER_FIELDS)
96        #print open(fail_file).read()
97        self.assertEqual(num_warns,0)
98        assert len(self.app['students'].keys()) == 3
99        shutil.rmtree(os.path.dirname(fin_file))
100
101
102class StudentUITests(StudentsFullSetup):
103    """Tests for customized student class views and pages
104    """
105
106    layer = FunctionalLayer
107
108    def setUp(self):
109        super(StudentUITests, self).setUp()
110        self.student.nationality = u'DE'
111
112    def test_classes(self):
113        # Let's see if objects created in the customized
114        # portal really implement the customized interfaces
115        verify.verifyObject(INigeriaStudent, self.student)
116        verify.verifyObject(
117            INigeriaStudentStudyCourse, self.student['studycourse'])
118        studylevel = createObject(u'waeup.StudentStudyLevel')
119        verify.verifyObject(INigeriaStudentStudyLevel, studylevel)
120        ticket = createObject(u'waeup.CourseTicket')
121        verify.verifyObject(INigeriaCourseTicket, ticket)
122        IWorkflowState(self.student).setState('returning')
123        # Let's see if next_session_allowed works as expected
124        # A, ug_ft, 100
125        self.assertTrue(self.student['studycourse'].next_session_allowed)
126        # Zero, ug_ft, 100
127        self.student['studycourse'].current_verdict = '0' # Zero!
128        self.assertTrue(self.student['studycourse'].next_session_allowed)
129        # Zero, ug_ft, 200
130        self.student['studycourse'].current_level = 200
131        self.assertFalse(self.student['studycourse'].next_session_allowed)
132        # Zero, de_ft, 200
133        self.student['studycourse'].certificate.study_mode = 'de_ft'
134        self.assertTrue(self.student['studycourse'].next_session_allowed)
135        # Zero, ph_ft, 300
136        self.student['studycourse'].certificate.study_mode = 'ph_ft'
137        self.student['studycourse'].current_level = 300
138        self.assertTrue(self.student['studycourse'].next_session_allowed)
139        # Zero, ph_ft, 400
140        self.student['studycourse'].current_level = 400
141        self.assertFalse(self.student['studycourse'].next_session_allowed)
142
143        # Now we convert the certificate into a postgraduate certificate
144        IWorkflowState(self.student).setState('school fee paid')
145        self.certificate.study_mode = 'pg_ft'
146        # ... and voila next session registration is allowed
147        self.assertTrue(self.student['studycourse'].next_session_allowed)
148
149    def test_manage_access(self):
150        # Managers can access the pages of students
151        # and can perform actions
152        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
153        # The student created in the base package is an ug student
154        self.browser.open(self.student_path)
155        self.browser.getLink("Clearance Data").click()
156        self.assertEqual(self.browser.headers['Status'], '200 Ok')
157        self.assertEqual(self.browser.url, self.clearance_path)
158        self.browser.getLink("Manage").click()
159        self.assertEqual(self.browser.headers['Status'], '200 Ok')
160        self.assertEqual(self.browser.url, self.manage_clearance_path)
161        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
162        self.browser.getControl("Save").click()
163        self.assertMatches('...Form has been saved...',
164                           self.browser.contents)
165        self.assertMatches('...First Sitting Record...',
166                           self.browser.contents)
167        # Managers can open clearance slip of ug students
168        self.browser.open(self.student_path + '/clearance.pdf')
169        self.assertEqual(self.browser.headers['Status'], '200 Ok')
170        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
171        # There is no pg field in the clearance form
172        self.assertFalse('Second Higher Education Record'
173            in self.browser.contents)
174        # Now we change the study mode ...
175        self.certificate.study_mode = 'pg_ft'
176        self.browser.open(self.clearance_path)
177        # ... and additional pg clearance fields appear
178        self.assertMatches('...Second Higher Education Record...',
179                           self.browser.contents)
180        # But also fields from the ug form are displayed
181        self.assertMatches('...First Sitting Record...',
182                           self.browser.contents)
183        # Managers can open clearance slip of pg students
184        self.browser.open(self.student_path + '/clearance.pdf')
185        self.assertEqual(self.browser.headers['Status'], '200 Ok')
186        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
187
188    def test_student_access(self):
189        # Students can edit clearance data
190        IWorkflowState(self.student).setState('cleared')
191        self.browser.open(self.login_path)
192        self.browser.getControl(name="form.login").value = self.student_id
193        self.browser.getControl(name="form.password").value = 'spwd'
194        self.browser.getControl("Login").click()
195        # Even in state admitted students can't change the portait if
196        # application slip exists.
197        IWorkflowState(self.student).setState('admitted')
198        self.browser.open(self.student_path)
199        self.assertTrue('Change portrait' in self.browser.contents)
200        file_store = getUtility(IExtFileStore)
201        applicant_slip = 'My application slip'
202        file_id = IFileStoreNameChooser(self.student).chooseName(
203            attr="application_slip.pdf")
204        file_store.createFile(file_id, StringIO(applicant_slip))
205        self.browser.open(self.student_path)
206        self.assertFalse('Change portrait' in self.browser.contents)
207        self.browser.open(self.student_path + '/change_portrait')
208        self.assertTrue('The requested form is locked' in self.browser.contents)
209
210        # Student can view and edit clearance data if clearance has started
211        IWorkflowInfo(self.student).fireTransition('start_clearance')
212        self.browser.getLink("Clearance Data").click()
213        self.browser.getLink("Edit").click()
214        self.assertTrue('Save' in self.browser.contents)
215
216        # Clearance can only be requested if all required documents
217        # have been uploaded.
218        self.browser.getControl("Save and request clearance").click()
219        self.assertTrue('No birth certificate uploaded' in self.browser.contents)
220        birth_certificate = 'My birth certificate'
221        file_id = IFileStoreNameChooser(self.student).chooseName(
222            attr="birth_certificate.jpg")
223        file_store.createFile(file_id, StringIO(birth_certificate))
224        self.browser.open(self.edit_clearance_path)
225        self.browser.getControl("Save and request clearance").click()
226
227        self.assertTrue('No referee letter uploaded' in self.browser.contents)
228        ref_let = 'My ref let'
229        file_id = IFileStoreNameChooser(self.student).chooseName(
230            attr="ref_let.jpg")
231        file_store.createFile(file_id, StringIO(ref_let))
232        self.browser.open(self.edit_clearance_path)
233        self.browser.getControl("Save and request clearance").click()
234
235        self.assertTrue('No acceptance letter uploaded' in self.browser.contents)
236        acc_let = 'My acc let'
237        file_id = IFileStoreNameChooser(self.student).chooseName(
238            attr="acc_let.jpg")
239        file_store.createFile(file_id, StringIO(acc_let))
240        self.browser.open(self.edit_clearance_path)
241        self.browser.getControl("Save and request clearance").click()
242
243        self.assertTrue('No first sitting result uploaded' in self.browser.contents)
244        fst_sit_scan = 'My first sitting result'
245        file_id = IFileStoreNameChooser(self.student).chooseName(
246            attr="fst_sit_scan.jpg")
247        file_store.createFile(file_id, StringIO(fst_sit_scan))
248        self.browser.open(self.edit_clearance_path)
249        self.browser.getControl("Save and request clearance").click()
250        self.assertTrue('Clearance has been requested'
251            in self.browser.contents)
252
253    def test_manage_upload_file(self):
254        # Managers can upload a file via the StudentClearanceManageFormPage
255        # The image is stored even if form has errors
256        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
257        self.browser.open(self.manage_clearance_path)
258        # Managers can add and delete a file
259        self.browser.open(self.manage_clearance_path)
260        image = open(SAMPLE_IMAGE, 'rb')
261        ctrl = self.browser.getControl(name='birthcertificateupload')
262        file_ctrl = ctrl.mech_control
263        file_ctrl.add_file(image, filename='my_acceptance_letter.jpg')
264        self.browser.getControl(
265            name='upload_acceptanceletterupload').click()
266        # Uups, we used the wrong 'Browse' field
267        self.assertFalse(
268            '<a target="image" href="acc_let">'
269            in self.browser.contents)
270        ctrl = self.browser.getControl(name='acceptanceletterupload')
271        file_ctrl = ctrl.mech_control
272        image.seek(0)
273        file_ctrl.add_file(image, filename='my_acceptance_letter.jpg')
274        self.browser.getControl(
275            name='upload_acceptanceletterupload').click()
276        self.assertTrue(
277            '<a target="image" href="acc_let">'
278            in self.browser.contents)
279        self.browser.getControl(
280            name='delete_acceptanceletterupload').click()
281        self.assertTrue(
282            'acc_let deleted'
283            in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.