1 | ## $Id: test_browser.py 8328 2012-05-02 11:54:22Z 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 | import os |
---|
19 | import shutil |
---|
20 | import tempfile |
---|
21 | from hurry.workflow.interfaces import IWorkflowState |
---|
22 | from zope.component.hooks import setSite, clearSite |
---|
23 | from zope.component import getUtility, createObject |
---|
24 | from zope.interface import verify |
---|
25 | from waeup.kofa.app import University |
---|
26 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
27 | from waeup.kofa.testing import FunctionalTestCase |
---|
28 | from waeup.kofa.students.batching import StudentProcessor |
---|
29 | from waeup.kofa.students.interfaces import IStudentsUtils |
---|
30 | from waeup.uniben.students.batching import CustomStudentProcessor |
---|
31 | from waeup.uniben.testing import FunctionalLayer |
---|
32 | from waeup.uniben.students.utils import get_school_fee |
---|
33 | from waeup.uniben.students.interfaces import ( |
---|
34 | ICustomStudentStudyCourse, ICustomStudent, |
---|
35 | ICustomStudentStudyLevel, ICustomCourseTicket) |
---|
36 | |
---|
37 | |
---|
38 | STUDENT_SAMPLE_DATA = open( |
---|
39 | os.path.join(os.path.dirname(__file__), 'sample_student_data.csv'), |
---|
40 | 'rb').read() |
---|
41 | |
---|
42 | STUDENT_HEADER_FIELDS = STUDENT_SAMPLE_DATA.split( |
---|
43 | '\n')[0].split(',') |
---|
44 | |
---|
45 | class StudentProcessorTest(FunctionalTestCase): |
---|
46 | """Perform some batching tests. |
---|
47 | """ |
---|
48 | |
---|
49 | layer = FunctionalLayer |
---|
50 | |
---|
51 | def setUp(self): |
---|
52 | super(StudentProcessorTest, self).setUp() |
---|
53 | # Setup a sample site for each test |
---|
54 | app = University() |
---|
55 | self.dc_root = tempfile.mkdtemp() |
---|
56 | app['datacenter'].setStoragePath(self.dc_root) |
---|
57 | |
---|
58 | # Prepopulate the ZODB... |
---|
59 | self.getRootFolder()['app'] = app |
---|
60 | # we add the site immediately after creation to the |
---|
61 | # ZODB. Catalogs and other local utilities are not setup |
---|
62 | # before that step. |
---|
63 | self.app = self.getRootFolder()['app'] |
---|
64 | # Set site here. Some of the following setup code might need |
---|
65 | # to access grok.getSite() and should get our new app then |
---|
66 | setSite(app) |
---|
67 | |
---|
68 | self.processor_base = StudentProcessor() |
---|
69 | self.processor = CustomStudentProcessor() |
---|
70 | self.workdir = tempfile.mkdtemp() |
---|
71 | self.csv_file = os.path.join(self.workdir, 'sample_student_data.csv') |
---|
72 | open(self.csv_file, 'wb').write(STUDENT_SAMPLE_DATA) |
---|
73 | |
---|
74 | def tearDown(self): |
---|
75 | super(StudentProcessorTest, self).tearDown() |
---|
76 | shutil.rmtree(self.workdir) |
---|
77 | shutil.rmtree(self.dc_root) |
---|
78 | clearSite() |
---|
79 | return |
---|
80 | |
---|
81 | def test_import(self): |
---|
82 | # We have an empty column 'date_of_birth' in the import file. |
---|
83 | # The original processor will fail because 'date_of_birth' is required |
---|
84 | # in the base package. |
---|
85 | num, num_warns, fin_file, fail_file = self.processor_base.doImport( |
---|
86 | self.csv_file, STUDENT_HEADER_FIELDS) |
---|
87 | self.assertEqual(num_warns,3) |
---|
88 | assert len(self.app['students'].keys()) == 0 |
---|
89 | # The customized processor does not complain since 'date_of_birth' is |
---|
90 | # not required in the custom package. |
---|
91 | num, num_warns, fin_file, fail_file = self.processor.doImport( |
---|
92 | self.csv_file, STUDENT_HEADER_FIELDS) |
---|
93 | #print open(fail_file).read() |
---|
94 | self.assertEqual(num_warns,0) |
---|
95 | assert len(self.app['students'].keys()) == 3 |
---|
96 | shutil.rmtree(os.path.dirname(fin_file)) |
---|
97 | |
---|
98 | |
---|
99 | class StudentUITests(StudentsFullSetup): |
---|
100 | """Tests for customized student class views and pages |
---|
101 | """ |
---|
102 | |
---|
103 | layer = FunctionalLayer |
---|
104 | |
---|
105 | def test_classes(self): |
---|
106 | # Let's see if objects created in the customized |
---|
107 | # portal really implement the customized interfaces |
---|
108 | verify.verifyObject(ICustomStudent, self.student) |
---|
109 | verify.verifyObject( |
---|
110 | ICustomStudentStudyCourse, self.student['studycourse']) |
---|
111 | studylevel = createObject(u'waeup.StudentStudyLevel') |
---|
112 | verify.verifyObject(ICustomStudentStudyLevel, studylevel) |
---|
113 | ticket = createObject(u'waeup.CourseTicket') |
---|
114 | verify.verifyObject(ICustomCourseTicket, ticket) |
---|
115 | # Let's see if may_register works as expected |
---|
116 | # A, ug_ft, 100 |
---|
117 | self.assertTrue(self.student['studycourse'].may_register) |
---|
118 | # O, ug_ft, 100 |
---|
119 | self.student['studycourse'].current_verdict = 'O' |
---|
120 | self.assertTrue(self.student['studycourse'].may_register) |
---|
121 | # O, ug_ft, 200 |
---|
122 | self.student['studycourse'].current_level = 200 |
---|
123 | self.assertFalse(self.student['studycourse'].may_register) |
---|
124 | # O, de_ft, 200 |
---|
125 | self.student['studycourse'].certificate.study_mode = 'de_ft' |
---|
126 | self.assertTrue(self.student['studycourse'].may_register) |
---|
127 | # O, ph_ft, 300 |
---|
128 | self.student['studycourse'].certificate.study_mode = 'ph_ft' |
---|
129 | self.student['studycourse'].current_level = 300 |
---|
130 | self.assertTrue(self.student['studycourse'].may_register) |
---|
131 | # O, ph_ft, 400 |
---|
132 | self.student['studycourse'].current_level = 400 |
---|
133 | self.assertFalse(self.student['studycourse'].may_register) |
---|
134 | |
---|
135 | def test_manage_access(self): |
---|
136 | # Managers can access the pages of students |
---|
137 | # and can perform actions |
---|
138 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
139 | # The student created in the base package is an ug student |
---|
140 | self.browser.open(self.student_path) |
---|
141 | self.browser.getLink("Clearance Data").click() |
---|
142 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
143 | self.assertEqual(self.browser.url, self.clearance_path) |
---|
144 | self.browser.getLink("Manage").click() |
---|
145 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
146 | self.assertEqual(self.browser.url, self.manage_clearance_path) |
---|
147 | self.browser.getControl(name="form.date_of_birth").value = '09/10/1961' |
---|
148 | self.browser.getControl("Save").click() |
---|
149 | self.assertMatches('...Form has been saved...', |
---|
150 | self.browser.contents) |
---|
151 | self.assertMatches('...First Sitting Record...', |
---|
152 | self.browser.contents) |
---|
153 | # Managers can open clearance slip of ug students |
---|
154 | self.browser.open(self.student_path + '/clearance.pdf') |
---|
155 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
156 | self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') |
---|
157 | # There is no pg field in the clearance form |
---|
158 | self.assertFalse('Second Higher Education Record' |
---|
159 | in self.browser.contents) |
---|
160 | # Now we change the study mode ... |
---|
161 | self.certificate.study_mode = 'pg_ft' |
---|
162 | self.browser.open(self.clearance_path) |
---|
163 | # ... and additional pg clearance fields appear |
---|
164 | self.assertMatches('...Second Higher Education Record...', |
---|
165 | self.browser.contents) |
---|
166 | # But also fields from the ug form are displayed |
---|
167 | self.assertMatches('...First Sitting Record...', |
---|
168 | self.browser.contents) |
---|
169 | # Managers can open clearance slip of pg students |
---|
170 | self.browser.open(self.student_path + '/clearance.pdf') |
---|
171 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
172 | self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') |
---|
173 | |
---|
174 | def test_manage_payments(self): |
---|
175 | # Add missing configuration data |
---|
176 | self.app['configuration']['2004'].gown_fee = 150.0 |
---|
177 | self.app['configuration']['2004'].transfer_fee = 90.0 |
---|
178 | #self.app['configuration']['2004'].clearance_fee = 120.0 |
---|
179 | self.app['configuration']['2004'].booking_fee = 150.0 |
---|
180 | self.app['configuration']['2004'].maint_fee = 180.0 |
---|
181 | |
---|
182 | # Managers can add online payment tickets |
---|
183 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
184 | self.browser.open(self.payments_path) |
---|
185 | self.browser.getControl("Add online payment ticket").click() |
---|
186 | self.browser.getControl("Create ticket").click() |
---|
187 | self.assertMatches('...Amount could not be determined...', |
---|
188 | self.browser.contents) |
---|
189 | IWorkflowState(self.student).setState('cleared') |
---|
190 | self.browser.open(self.payments_path + '/addop') |
---|
191 | self.browser.getControl("Create ticket").click() |
---|
192 | self.assertMatches('...ticket created...', |
---|
193 | self.browser.contents) |
---|
194 | ctrl = self.browser.getControl(name='val_id') |
---|
195 | value = ctrl.options[0] |
---|
196 | self.browser.getLink(value).click() |
---|
197 | self.assertMatches('...Amount Authorized...', |
---|
198 | self.browser.contents) |
---|
199 | # Managers can open payment slip |
---|
200 | self.browser.getLink("Download payment slip").click() |
---|
201 | self.assertEqual(self.browser.headers['Status'], '200 Ok') |
---|
202 | self.assertEqual(self.browser.headers['Content-Type'], 'application/pdf') |
---|
203 | # Set ticket paid |
---|
204 | ticket = self.student['payments'].items()[0][1] |
---|
205 | ticket.p_state = 'paid' |
---|
206 | self.browser.open(self.payments_path + '/addop') |
---|
207 | self.browser.getControl("Create ticket").click() |
---|
208 | self.assertMatches('...This type of payment has already been made...', |
---|
209 | self.browser.contents) |
---|
210 | # Remove all payments so that we can add a school fee payment again |
---|
211 | keys = [i for i in self.student['payments'].keys()] |
---|
212 | for payment in keys: |
---|
213 | del self.student['payments'][payment] |
---|
214 | self.browser.open(self.payments_path + '/addop') |
---|
215 | self.browser.getControl("Create ticket").click() |
---|
216 | self.assertMatches('...ticket created...', |
---|
217 | self.browser.contents) |
---|
218 | self.browser.open(self.payments_path + '/addop') |
---|
219 | self.browser.getControl(name="form.p_category").value = ['gown'] |
---|
220 | self.browser.getControl("Create ticket").click() |
---|
221 | self.assertMatches('...ticket created...', |
---|
222 | self.browser.contents) |
---|
223 | self.browser.open(self.payments_path + '/addop') |
---|
224 | self.browser.getControl(name="form.p_category").value = ['transfer'] |
---|
225 | self.browser.getControl("Create ticket").click() |
---|
226 | self.assertMatches('...ticket created...', |
---|
227 | self.browser.contents) |
---|
228 | self.browser.open(self.payments_path + '/addop') |
---|
229 | self.browser.getControl( |
---|
230 | name="form.p_category").value = ['bed_allocation'] |
---|
231 | self.browser.getControl("Create ticket").click() |
---|
232 | self.assertMatches('...ticket created...', |
---|
233 | self.browser.contents) |
---|
234 | self.browser.open(self.payments_path + '/addop') |
---|
235 | self.browser.getControl( |
---|
236 | name="form.p_category").value = ['hostel_maintenance'] |
---|
237 | self.browser.getControl("Create ticket").click() |
---|
238 | self.assertMatches('...ticket created...', |
---|
239 | self.browser.contents) |
---|
240 | self.browser.open(self.payments_path + '/addop') |
---|
241 | self.browser.getControl(name="form.p_category").value = ['clearance'] |
---|
242 | self.browser.getControl("Create ticket").click() |
---|
243 | self.assertMatches('...ticket created...', |
---|
244 | self.browser.contents) |
---|
245 | self.browser.open(self.payments_path + '/addop') |
---|
246 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
247 | self.browser.getControl("Create ticket").click() |
---|
248 | self.assertMatches('...ticket created...', |
---|
249 | self.browser.contents) |
---|
250 | # In state returning we can add a new school fee ticket since |
---|
251 | # p_session and p_level is different |
---|
252 | IWorkflowState(self.student).setState('returning') |
---|
253 | self.browser.open(self.payments_path + '/addop') |
---|
254 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
255 | self.browser.getControl("Create ticket").click() |
---|
256 | self.assertMatches('...ticket created...', |
---|
257 | self.browser.contents) |
---|
258 | # In state admitted school fee can't be determined |
---|
259 | IWorkflowState(self.student).setState('admitted') |
---|
260 | self.browser.open(self.payments_path + '/addop') |
---|
261 | self.browser.getControl(name="form.p_category").value = ['schoolfee'] |
---|
262 | self.browser.getControl("Create ticket").click() |
---|
263 | self.assertMatches('...Amount could not be determined...', |
---|
264 | self.browser.contents) |
---|
265 | |
---|
266 | # If the session configuration doesn't exist an error message will |
---|
267 | # be shown. No other requirement is being checked. |
---|
268 | del self.app['configuration']['2004'] |
---|
269 | self.browser.open(self.payments_path) |
---|
270 | self.browser.getControl("Add online payment ticket").click() |
---|
271 | self.browser.getControl("Create ticket").click() |
---|
272 | self.assertMatches('...Session configuration object is not...', |
---|
273 | self.browser.contents) |
---|
274 | |
---|
275 | def test_student_access(self): |
---|
276 | # Students can edit clearance data |
---|
277 | IWorkflowState(self.student).setState('cleared') |
---|
278 | self.student.clearance_locked = False |
---|
279 | self.browser.open(self.login_path) |
---|
280 | self.browser.getControl(name="form.login").value = self.student_id |
---|
281 | self.browser.getControl(name="form.password").value = 'spwd' |
---|
282 | self.browser.getControl("Login").click() |
---|
283 | # Student can view and edit clearance data |
---|
284 | self.browser.getLink("Clearance Data").click() |
---|
285 | self.browser.getLink("Edit").click() |
---|
286 | self.assertTrue('Save' in self.browser.contents) |
---|
287 | |
---|
288 | def test_get_returning_data(self): |
---|
289 | # Student is in level 100, session 2004 with verdict A |
---|
290 | utils = getUtility(IStudentsUtils) |
---|
291 | self.assertEqual(utils.getReturningData(self.student),(2005, 200)) |
---|
292 | self.student['studycourse'].current_verdict = 'C' |
---|
293 | self.assertEqual(utils.getReturningData(self.student),(2005, 110)) |
---|
294 | self.student['studycourse'].current_verdict = 'D' |
---|
295 | self.assertEqual(utils.getReturningData(self.student),(2005, 100)) |
---|
296 | return |
---|
297 | |
---|
298 | def test_get_schoolfee(self): |
---|
299 | self.assertEqual(get_school_fee(self.student),0.0) |
---|
300 | IWorkflowState(self.student).setState('cleared') |
---|
301 | self.assertEqual(get_school_fee(self.student),40000.0) |
---|
302 | IWorkflowState(self.student).setState('returning') |
---|
303 | self.assertEqual(get_school_fee(self.student),20000.0) |
---|
304 | return |
---|
305 | |
---|
306 | def test_get_payment_details(self): |
---|
307 | self.app['configuration']['2004'].gown_fee = 150.0 |
---|
308 | self.app['configuration']['2004'].transfer_fee = 90.0 |
---|
309 | self.app['configuration']['2004'].booking_fee = 150.0 |
---|
310 | self.app['configuration']['2004'].maint_fee = 180.0 |
---|
311 | utils = getUtility(IStudentsUtils) |
---|
312 | self.assertEqual(utils.getPaymentDetails('schoolfee',self.student), |
---|
313 | {'p_level': 100, 'p_session': 2004, 'amount': 0.0, |
---|
314 | 'p_item': u'CERT1', 'error': u'Amount could not be determined.'} |
---|
315 | ) |
---|
316 | self.assertEqual(utils.getPaymentDetails('clearance',self.student), |
---|
317 | {'p_level': 100, 'p_session': 2004, 'amount': 34250.0, |
---|
318 | 'p_item': u'CERT1', 'error': u''} |
---|
319 | ) |
---|
320 | self.assertEqual(utils.getPaymentDetails('gown',self.student), |
---|
321 | {'p_level': 100, 'p_session': 2004, 'amount': 150.0, |
---|
322 | 'p_item': u'', 'error': u''} |
---|
323 | ) |
---|
324 | self.assertEqual(utils.getPaymentDetails('hostel_maintenance',self.student), |
---|
325 | {'p_level': 100, 'p_session': 2004, 'amount': 180.0, |
---|
326 | 'p_item': u'', 'error': u''} |
---|
327 | ) |
---|
328 | self.assertEqual(utils.getPaymentDetails('bed_allocation',self.student), |
---|
329 | {'p_level': 100, 'p_session': 2004, 'amount': 150.0, |
---|
330 | 'p_item': u'', 'error': u''} |
---|
331 | ) |
---|
332 | self.assertEqual(utils.getPaymentDetails('transfer',self.student), |
---|
333 | {'p_level': 100, 'p_session': 2004, 'amount': 90.0, |
---|
334 | 'p_item': u'', 'error': u''} |
---|
335 | ) |
---|
336 | return |
---|