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

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

Move customized students and payments components to kofacustom.nigeria.

  • Property svn:keywords set to Id
File size: 9.4 KB
Line 
1## $Id: test_browser.py 8862 2012-07-01 09:56:50Z 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
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 StudentsFullSetup
28from waeup.kofa.testing import FunctionalTestCase
29from waeup.kofa.interfaces import (
30    IExtFileStore, IFileStoreNameChooser)
31from waeup.kofa.students.batching import StudentProcessor
32from waeup.kofa.students.interfaces import IStudentsUtils
33from kofacustom.nigeria.students.batching import CustomStudentProcessor
34from kofacustom.nigeria.testing import FunctionalLayer
35from kofacustom.nigeria.students.interfaces import (
36    ICustomStudentStudyCourse, ICustomStudent,
37    ICustomStudentStudyLevel, ICustomCourseTicket)
38
39
40STUDENT_SAMPLE_DATA = open(
41    os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'),
42    'rb').read()
43
44STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split(
45    '\n')[0].split(',')
46
47class StudentProcessorTest(FunctionalTestCase):
48    """Perform some batching tests.
49    """
50
51    layer = FunctionalLayer
52
53    def setUp(self):
54        super(StudentProcessorTest, self).setUp()
55        # Setup a sample site for each test
56        app = University()
57        self.dc_root = tempfile.mkdtemp()
58        app['datacenter'].setStoragePath(self.dc_root)
59
60        # Prepopulate the ZODB...
61        self.getRootFolder()['app'] = app
62        # we add the site immediately after creation to the
63        # ZODB. Catalogs and other local utilities are not setup
64        # before that step.
65        self.app = self.getRootFolder()['app']
66        # Set site here. Some of the following setup code might need
67        # to access grok.getSite() and should get our new app then
68        setSite(app)
69
70        self.processor_base = StudentProcessor()
71        self.processor = CustomStudentProcessor()
72        self.workdir = tempfile.mkdtemp()
73        self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv')
74        open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA)
75
76    def tearDown(self):
77        super(StudentProcessorTest, self).tearDown()
78        shutil.rmtree(self.workdir)
79        shutil.rmtree(self.dc_root)
80        clearSite()
81        return
82
83    def test_import(self):
84        # We have an empty column 'date_of_birth' in  the import file.
85        # The original processor will fail because 'date_of_birth' is required
86        # in the base package.
87        num, num_warns, fin_file, fail_file = self.processor_base.doImport(
88            self.csv_file, STUDENT_HEADER_FIELDS)
89        self.assertEqual(num_warns,3)
90        assert len(self.app['students'].keys()) == 0
91        # The customized processor does not complain since 'date_of_birth' is
92        # not required in the custom package.
93        num, num_warns, fin_file, fail_file = self.processor.doImport(
94            self.csv_file, STUDENT_HEADER_FIELDS)
95        #print open(fail_file).read()
96        self.assertEqual(num_warns,0)
97        assert len(self.app['students'].keys()) == 3
98        shutil.rmtree(os.path.dirname(fin_file))
99
100
101class StudentUITests(StudentsFullSetup):
102    """Tests for customized student class views and pages
103    """
104
105    layer = FunctionalLayer
106
107    def test_classes(self):
108        # Let's see if objects created in the customized
109        # portal really implement the customized interfaces
110        verify.verifyObject(ICustomStudent, self.student)
111        verify.verifyObject(
112            ICustomStudentStudyCourse, self.student['studycourse'])
113        studylevel = createObject(u'waeup.StudentStudyLevel')
114        verify.verifyObject(ICustomStudentStudyLevel, studylevel)
115        ticket = createObject(u'waeup.CourseTicket')
116        verify.verifyObject(ICustomCourseTicket, ticket)
117        IWorkflowState(self.student).setState('returning')
118        # Let's see if next_session_allowed works as expected
119        # A, ug_ft, 100
120        self.assertTrue(self.student['studycourse'].next_session_allowed)
121        # O, ug_ft, 100
122        self.student['studycourse'].current_verdict = 'O'
123        self.assertTrue(self.student['studycourse'].next_session_allowed)
124        # O, ug_ft, 200
125        self.student['studycourse'].current_level = 200
126        self.assertFalse(self.student['studycourse'].next_session_allowed)
127        # O, de_ft, 200
128        self.student['studycourse'].certificate.study_mode = 'de_ft'
129        self.assertTrue(self.student['studycourse'].next_session_allowed)
130        # O, ph_ft, 300
131        self.student['studycourse'].certificate.study_mode = 'ph_ft'
132        self.student['studycourse'].current_level = 300
133        self.assertTrue(self.student['studycourse'].next_session_allowed)
134        # O, ph_ft, 400
135        self.student['studycourse'].current_level = 400
136        self.assertFalse(self.student['studycourse'].next_session_allowed)
137
138        # Now we convert the certificate into a postgraduate certificate
139        IWorkflowState(self.student).setState('school fee paid')
140        self.certificate.study_mode = 'pg_ft'
141        # ... and voila next session registration is allowed
142        self.assertTrue(self.student['studycourse'].next_session_allowed)
143
144    def test_manage_access(self):
145        # Managers can access the pages of students
146        # and can perform actions
147        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
148        # The student created in the base package is an ug student
149        self.browser.open(self.student_path)
150        self.browser.getLink("Clearance Data").click()
151        self.assertEqual(self.browser.headers['Status'], '200 Ok')
152        self.assertEqual(self.browser.url, self.clearance_path)
153        self.browser.getLink("Manage").click()
154        self.assertEqual(self.browser.headers['Status'], '200 Ok')
155        self.assertEqual(self.browser.url, self.manage_clearance_path)
156        self.browser.getControl(name="form.date_of_birth").value = '09/10/1961'
157        self.browser.getControl("Save").click()
158        self.assertMatches('...Form has been saved...',
159                           self.browser.contents)
160        self.assertMatches('...First Sitting Record...',
161                           self.browser.contents)
162        # Managers can open clearance slip of ug students
163        self.browser.open(self.student_path + '/clearance.pdf')
164        self.assertEqual(self.browser.headers['Status'], '200 Ok')
165        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
166        # There is no pg field in the clearance form
167        self.assertFalse('Second Higher Education Record'
168            in self.browser.contents)
169        # Now we change the study mode ...
170        self.certificate.study_mode = 'pg_ft'
171        self.browser.open(self.clearance_path)
172        # ... and additional pg clearance fields appear
173        self.assertMatches('...Second Higher Education Record...',
174                           self.browser.contents)
175        # But also fields from the ug form are displayed
176        self.assertMatches('...First Sitting Record...',
177                           self.browser.contents)
178        # Managers can open clearance slip of pg students
179        self.browser.open(self.student_path + '/clearance.pdf')
180        self.assertEqual(self.browser.headers['Status'], '200 Ok')
181        self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf')
182
183    def test_student_access(self):
184        # Students can edit clearance data
185        IWorkflowState(self.student).setState('cleared')
186        self.student.clearance_locked = False
187        self.browser.open(self.login_path)
188        self.browser.getControl(name="form.login").value = self.student_id
189        self.browser.getControl(name="form.password").value = 'spwd'
190        self.browser.getControl("Login").click()
191        # Even in state admitted students can't change the portait if
192        # application slip exists.
193        IWorkflowState(self.student).setState('admitted')
194        self.browser.open(self.student_path)
195        self.assertTrue('Change portrait' in self.browser.contents)
196        file_store = getUtility(IExtFileStore)
197        applicant_slip = 'My application slip'
198        file_id = IFileStoreNameChooser(self.student).chooseName(
199            attr="application_slip.pdf")
200        file_store.createFile(file_id, StringIO(applicant_slip))
201        self.browser.open(self.student_path)
202        self.assertFalse('Change portrait' in self.browser.contents)
203        self.browser.open(self.student_path + '/change_portrait')
204        self.assertTrue('The requested form is locked' in self.browser.contents)
205        # Student can view and edit clearance data
206        self.browser.getLink("Clearance Data").click()
207        self.browser.getLink("Edit").click()
208        self.assertTrue('Save' in self.browser.contents)
Note: See TracBrowser for help on using the repository browser.