1 | # Tests for webservices |
---|
2 | import xmlrpclib |
---|
3 | import os |
---|
4 | from time import time |
---|
5 | from cStringIO import StringIO |
---|
6 | from zope.app.testing.xmlrpc import ServerProxy |
---|
7 | from zope.component import getUtility |
---|
8 | from waeup.kofa.interfaces import ( |
---|
9 | IExtFileStore, IFileStoreNameChooser, IKofaUtils) |
---|
10 | from waeup.kofa.testing import FunctionalLayer |
---|
11 | from waeup.kofa.utils.helpers import to_timezone |
---|
12 | from waeup.kofa.students.payments import StudentOnlinePayment |
---|
13 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
14 | from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket |
---|
15 | |
---|
16 | |
---|
17 | class XMLRPCTests(StudentsFullSetup): |
---|
18 | # check XMLRPC services for university portal |
---|
19 | |
---|
20 | layer = FunctionalLayer |
---|
21 | |
---|
22 | def setup_student(self, student): |
---|
23 | study_level = StudentStudyLevel() |
---|
24 | study_level.level_session = 2012 |
---|
25 | study_level.level_verdict = "A" |
---|
26 | study_level.level = 100 |
---|
27 | study_level.validated_by = u"my adviser" |
---|
28 | student['studycourse'].addStudentStudyLevel( |
---|
29 | self.certificate, study_level) |
---|
30 | |
---|
31 | ticket = CourseTicket() |
---|
32 | ticket.automatic = True |
---|
33 | ticket.carry_over = True |
---|
34 | ticket.code = u'CRS1' |
---|
35 | ticket.title = u'Course 1' |
---|
36 | ticket.fcode = u'FAC1' |
---|
37 | ticket.dcode = u'DEP1' |
---|
38 | ticket.credits = 100 |
---|
39 | ticket.passmark = 100 |
---|
40 | ticket.semester = 2 |
---|
41 | study_level[ticket.code] = ticket |
---|
42 | |
---|
43 | def create_passport_img(self, student): |
---|
44 | # create some passport file for `student` |
---|
45 | storage = getUtility(IExtFileStore) |
---|
46 | image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg') |
---|
47 | self.image_contents = open(image_path, 'rb').read() |
---|
48 | file_id = IFileStoreNameChooser(student).chooseName( |
---|
49 | attr='passport.jpg') |
---|
50 | storage.createFile(file_id, StringIO(self.image_contents)) |
---|
51 | |
---|
52 | def create_fpm_file(self, student, finger_num): |
---|
53 | # create some .fpm file for `student` finger `finger_num` |
---|
54 | storage = getUtility(IExtFileStore) |
---|
55 | file_id = IFileStoreNameChooser(student).chooseName( |
---|
56 | attr='%s.fpm' % finger_num) |
---|
57 | storage.createFile(file_id, StringIO('FP1FakedMintiaeFile1')) |
---|
58 | |
---|
59 | def XMLRPC_post(self, body): |
---|
60 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
61 | self.browser.addHeader('Content-Length', len(body)) |
---|
62 | self.browser.post('http://localhost/app', body, |
---|
63 | 'text/xml; charset=utf-8') |
---|
64 | return self.browser.contents |
---|
65 | |
---|
66 | def test_get_student_id_no_match(self): |
---|
67 | # w/o any students we get none |
---|
68 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
69 | result = server.get_student_id('Nonsense') |
---|
70 | self.assertTrue(result is None) |
---|
71 | return |
---|
72 | |
---|
73 | def test_get_student_id_regno_exists(self): |
---|
74 | # we can get the id of an existing student with matching reg_no |
---|
75 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
76 | result = server.get_student_id('123') |
---|
77 | self.assertEqual(result, 'K1000000') |
---|
78 | self.assertEqual(self.student_id, result) |
---|
79 | REQUEST_XML = """\ |
---|
80 | <?xml version="1.0"?> |
---|
81 | <methodCall> |
---|
82 | <methodName>get_student_id</methodName> |
---|
83 | <params> |
---|
84 | <param> |
---|
85 | <value><string>123</string></value> |
---|
86 | </param> |
---|
87 | </params> |
---|
88 | </methodCall>""" |
---|
89 | RESPONSE_XML = """\ |
---|
90 | <?xml version='1.0'?> |
---|
91 | <methodResponse> |
---|
92 | <params> |
---|
93 | <param> |
---|
94 | <value><string>K1000000</string></value> |
---|
95 | </param> |
---|
96 | </params> |
---|
97 | </methodResponse> |
---|
98 | """ |
---|
99 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
100 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
101 | return |
---|
102 | |
---|
103 | def test_get_student_id_block_unauthorized(self): |
---|
104 | # requests from unauthorized users are blocked |
---|
105 | # no username nor password |
---|
106 | server = ServerProxy('http://localhost/app') |
---|
107 | self.assertRaises( |
---|
108 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
109 | # wrong password |
---|
110 | server = ServerProxy('http://mgr:WRONGPW@localhost/app') |
---|
111 | self.assertRaises( |
---|
112 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
113 | # wrong username |
---|
114 | server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app') |
---|
115 | self.assertRaises( |
---|
116 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
117 | return |
---|
118 | |
---|
119 | def test_get_courses_by_session(self): |
---|
120 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
121 | result = server.get_courses_by_session('K1000000') |
---|
122 | self.assertEqual(result, None) |
---|
123 | self.setup_student(self.student) |
---|
124 | result = server.get_courses_by_session('K1000000', '2010') |
---|
125 | self.assertEqual(result, None) |
---|
126 | result = server.get_courses_by_session('K1000000', '2012') |
---|
127 | self.assertEqual(result, |
---|
128 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
129 | result = server.get_courses_by_session('K1000000') |
---|
130 | self.assertEqual(result, |
---|
131 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
132 | # Also matric_number ... |
---|
133 | result = server.get_courses_by_session('234') |
---|
134 | self.assertEqual(result, |
---|
135 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
136 | # ... or reg_number can be used. |
---|
137 | result = server.get_courses_by_session('123') |
---|
138 | self.assertEqual(result, |
---|
139 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
140 | result = server.get_courses_by_session('Nonsense') |
---|
141 | self.assertEqual(result, None) |
---|
142 | REQUEST_XML = """\ |
---|
143 | <?xml version="1.0"?> |
---|
144 | <methodCall> |
---|
145 | <methodName>get_courses_by_session</methodName> |
---|
146 | <params> |
---|
147 | <param> |
---|
148 | <value><string>K1000000</string></value> |
---|
149 | </param> |
---|
150 | </params> |
---|
151 | </methodCall>""" |
---|
152 | RESPONSE_XML = """\ |
---|
153 | <?xml version='1.0'?> |
---|
154 | <methodResponse> |
---|
155 | <params> |
---|
156 | <param> |
---|
157 | <value><struct> |
---|
158 | <member> |
---|
159 | <name>100|CRS1</name> |
---|
160 | <value><string>Course 1</string></value> |
---|
161 | </member> |
---|
162 | <member> |
---|
163 | <name>100|COURSE1</name> |
---|
164 | <value><string>Unnamed Course</string></value> |
---|
165 | </member> |
---|
166 | </struct></value> |
---|
167 | </param> |
---|
168 | </params> |
---|
169 | </methodResponse> |
---|
170 | """ |
---|
171 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
172 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
173 | return |
---|
174 | |
---|
175 | def test_get_students_by_course(self): |
---|
176 | self.setup_student(self.student) |
---|
177 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
178 | result = server.get_students_by_course('CRS1', '2010') |
---|
179 | self.assertEqual(result, None) |
---|
180 | result = server.get_students_by_course('CRS1', '2012') |
---|
181 | self.assertEqual(result, [['K1000000', '234', 'my adviser', 0], ]) |
---|
182 | result = server.get_students_by_course('CRS1') |
---|
183 | self.assertEqual(result, [['K1000000', '234', 'my adviser', 0], ]) |
---|
184 | payment = StudentOnlinePayment() |
---|
185 | payment.p_id = 'my-id' |
---|
186 | payment.p_session = 2012 |
---|
187 | payment.amount_auth = 12.12 |
---|
188 | payment.p_state = u'paid' |
---|
189 | payment.p_category = u'schoolfee' |
---|
190 | self.student['payments']['my-payment'] = payment |
---|
191 | result = server.get_students_by_course('CRS1') |
---|
192 | self.assertEqual(result, [['K1000000', '234', 'my adviser', 12.12], ]) |
---|
193 | REQUEST_XML = """\ |
---|
194 | <?xml version="1.0"?> |
---|
195 | <methodCall> |
---|
196 | <methodName>get_students_by_course</methodName> |
---|
197 | <params> |
---|
198 | <param> |
---|
199 | <value><string>CRS1</string></value> |
---|
200 | <value><string>2012</string></value> |
---|
201 | </param> |
---|
202 | </params> |
---|
203 | </methodCall>""" |
---|
204 | RESPONSE_XML = """\ |
---|
205 | <?xml version='1.0'?> |
---|
206 | <methodResponse> |
---|
207 | <params> |
---|
208 | <param> |
---|
209 | <value><array><data> |
---|
210 | <value><array><data> |
---|
211 | <value><string>K1000000</string></value> |
---|
212 | <value><string>234</string></value> |
---|
213 | <value><string>my adviser</string></value> |
---|
214 | <value><double>12.12</double></value> |
---|
215 | </data></array></value> |
---|
216 | </data></array></value> |
---|
217 | </param> |
---|
218 | </params> |
---|
219 | </methodResponse> |
---|
220 | """ |
---|
221 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
222 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
223 | return |
---|
224 | |
---|
225 | def test_get_student_info(self): |
---|
226 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
227 | self.setup_student(self.student) |
---|
228 | result = server.get_student_info('123') |
---|
229 | self.assertEqual(result, |
---|
230 | ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng']) |
---|
231 | REQUEST_XML = """\ |
---|
232 | <?xml version="1.0"?> |
---|
233 | <methodCall> |
---|
234 | <methodName>get_student_info</methodName> |
---|
235 | <params> |
---|
236 | <param> |
---|
237 | <value><string>K1000000</string></value> |
---|
238 | </param> |
---|
239 | </params> |
---|
240 | </methodCall>""" |
---|
241 | RESPONSE_XML = """\ |
---|
242 | <?xml version='1.0'?> |
---|
243 | <methodResponse> |
---|
244 | <params> |
---|
245 | <param> |
---|
246 | <value><array><data> |
---|
247 | <value><string>Anna Tester</string></value> |
---|
248 | <value><string>CERT1</string></value> |
---|
249 | <value><string>1234</string></value> |
---|
250 | <value><string>aa@aa.ng</string></value> |
---|
251 | </data></array></value> |
---|
252 | </param> |
---|
253 | </params> |
---|
254 | </methodResponse> |
---|
255 | """ |
---|
256 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
257 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
258 | return |
---|
259 | |
---|
260 | def test_get_grad_student(self): |
---|
261 | server = ServerProxy('http://localhost/app') |
---|
262 | self.setup_student(self.student) |
---|
263 | result = server.get_grad_student('123', 'aa@aa.ng') |
---|
264 | self.assertEqual(result, |
---|
265 | [True, False, False]) |
---|
266 | REQUEST_XML = """\ |
---|
267 | <?xml version="1.0"?> |
---|
268 | <methodCall> |
---|
269 | <methodName>get_grad_student</methodName> |
---|
270 | <params> |
---|
271 | <param> |
---|
272 | <value><string>K1000000</string></value> |
---|
273 | </param> |
---|
274 | <param> |
---|
275 | <value><string>aa@aa.ng</string></value> |
---|
276 | </param> |
---|
277 | </params> |
---|
278 | </methodCall>""" |
---|
279 | RESPONSE_XML = """\ |
---|
280 | <?xml version='1.0'?> |
---|
281 | <methodResponse> |
---|
282 | <params> |
---|
283 | <param> |
---|
284 | <value><array><data> |
---|
285 | <value><boolean>1</boolean></value> |
---|
286 | <value><boolean>0</boolean></value> |
---|
287 | <value><boolean>0</boolean></value> |
---|
288 | </data></array></value> |
---|
289 | </param> |
---|
290 | </params> |
---|
291 | </methodResponse> |
---|
292 | """ |
---|
293 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
294 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
295 | return |
---|
296 | |
---|
297 | def test_get_student_passport(self): |
---|
298 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
299 | self.setup_student(self.student) |
---|
300 | self.create_passport_img(self.student) |
---|
301 | result = server.get_student_passport('123') |
---|
302 | img = getUtility(IExtFileStore).getFileByContext( |
---|
303 | self.student, attr='passport.jpg') |
---|
304 | binary = img.read() |
---|
305 | self.assertEqual(binary, result) |
---|
306 | REQUEST_XML = """\ |
---|
307 | <?xml version="1.0"?> |
---|
308 | <methodCall> |
---|
309 | <methodName>get_student_passport</methodName> |
---|
310 | <params> |
---|
311 | <param> |
---|
312 | <value><string>K1000000</string></value> |
---|
313 | </param> |
---|
314 | </params> |
---|
315 | </methodCall>""" |
---|
316 | RESPONSE_XML = """\ |
---|
317 | <?xml version='1.0'?> |
---|
318 | <methodResponse> |
---|
319 | <params> |
---|
320 | <param> |
---|
321 | <value><base64> |
---|
322 | /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b |
---|
323 | """ |
---|
324 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
325 | self.assertTrue(xmlout.startswith(RESPONSE_XML)) |
---|
326 | |
---|
327 | def test_get_paid_sessions(self): |
---|
328 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
329 | self.setup_student(self.student) |
---|
330 | payment = StudentOnlinePayment() |
---|
331 | payment.p_id = 'my-id' |
---|
332 | payment.p_session = 2009 |
---|
333 | payment.amount_auth = 12.12 |
---|
334 | payment.p_state = u'paid' |
---|
335 | payment.p_category = u'schoolfee' |
---|
336 | self.student['payments']['my-payment'] = payment |
---|
337 | result = server.get_paid_sessions('123') |
---|
338 | self.assertEqual(result, {'2009': 12.12}) |
---|
339 | REQUEST_XML = """\ |
---|
340 | <?xml version="1.0"?> |
---|
341 | <methodCall> |
---|
342 | <methodName>get_paid_sessions</methodName> |
---|
343 | <params> |
---|
344 | <param> |
---|
345 | <value><string>K1000000</string></value> |
---|
346 | </param> |
---|
347 | </params> |
---|
348 | </methodCall>""" |
---|
349 | RESPONSE_XML = """\ |
---|
350 | <?xml version='1.0'?> |
---|
351 | <methodResponse> |
---|
352 | <params> |
---|
353 | <param> |
---|
354 | <value><struct> |
---|
355 | <member> |
---|
356 | <name>2009</name> |
---|
357 | <value><double>12.12</double></value> |
---|
358 | </member> |
---|
359 | </struct></value> |
---|
360 | </param> |
---|
361 | </params> |
---|
362 | </methodResponse> |
---|
363 | """ |
---|
364 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
365 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
366 | return |
---|
367 | |
---|
368 | def test_check_student_credentials(self): |
---|
369 | # make sure we can get student infos providing valid creds |
---|
370 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
371 | self.setup_student(self.student) |
---|
372 | stud_id = self.student.student_id |
---|
373 | result = server.check_student_credentials(stud_id, 'spwd') |
---|
374 | self.assertEqual( |
---|
375 | result, { |
---|
376 | 'description': 'Anna Tester', |
---|
377 | 'email': 'aa@aa.ng', |
---|
378 | 'id': 'K1000000', |
---|
379 | 'type': 'student'} |
---|
380 | ) |
---|
381 | return |
---|
382 | |
---|
383 | def test_get_student_moodle_data(self): |
---|
384 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
385 | self.setup_student(self.student) |
---|
386 | result = server.get_student_moodle_data(self.student.student_id) |
---|
387 | self.assertEqual(result, |
---|
388 | {'lastname': 'Tester', 'email': 'aa@aa.ng', 'firstname': 'Anna'}) |
---|
389 | REQUEST_XML = """\ |
---|
390 | <?xml version="1.0"?> |
---|
391 | <methodCall> |
---|
392 | <methodName>get_student_moodle_data</methodName> |
---|
393 | <params> |
---|
394 | <param> |
---|
395 | <value><string>K1000000</string></value> |
---|
396 | </param> |
---|
397 | </params> |
---|
398 | </methodCall>""" |
---|
399 | RESPONSE_XML = """\ |
---|
400 | <?xml version='1.0'?> |
---|
401 | <methodResponse> |
---|
402 | <params> |
---|
403 | <param> |
---|
404 | <value><struct> |
---|
405 | <member> |
---|
406 | <name>lastname</name> |
---|
407 | <value><string>Tester</string></value> |
---|
408 | </member> |
---|
409 | <member> |
---|
410 | <name>email</name> |
---|
411 | <value><string>aa@aa.ng</string></value> |
---|
412 | </member> |
---|
413 | <member> |
---|
414 | <name>firstname</name> |
---|
415 | <value><string>Anna</string></value> |
---|
416 | </member> |
---|
417 | </struct></value> |
---|
418 | </param> |
---|
419 | </params> |
---|
420 | </methodResponse> |
---|
421 | """ |
---|
422 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
423 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
424 | return |
---|
425 | |
---|
426 | def test_put_student_fingerprints_no_stud(self): |
---|
427 | # invalid student ids will result in `False` |
---|
428 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
429 | self.assertRaises( |
---|
430 | xmlrpclib.Fault, server.put_student_fingerprints, |
---|
431 | 'invalid id', {}) |
---|
432 | |
---|
433 | def test_put_student_fingerprints_non_dict(self): |
---|
434 | # fingerprints must be passed in in a dict |
---|
435 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
436 | self.setup_student(self.student) |
---|
437 | self.assertRaises( |
---|
438 | xmlrpclib.Fault, server.put_student_fingerprints, |
---|
439 | self.student.student_id, 'not-a-dict') |
---|
440 | |
---|
441 | def test_put_student_fingerprints_non_num_keys_ignored(self): |
---|
442 | # non-numeric keys in fingerprint dict are ignored |
---|
443 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
444 | self.setup_student(self.student) |
---|
445 | result = server.put_student_fingerprints( |
---|
446 | self.student.student_id, {'not-a-num': 'foo', |
---|
447 | '12.2': 'bar', |
---|
448 | '123': 'baz'}) |
---|
449 | self.assertEqual(result, False) |
---|
450 | |
---|
451 | def test_put_student_fingerprints_non_fpm_data(self): |
---|
452 | # we cannot pass non-.fpm files as values |
---|
453 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
454 | self.setup_student(self.student) |
---|
455 | self.assertRaises( |
---|
456 | xmlrpclib.Fault, server.put_student_fingerprints, |
---|
457 | self.student.student_id, {'1': 'not-a-fingerprint'}) |
---|
458 | |
---|
459 | def test_put_student_fingerprints_invalid_file_format(self): |
---|
460 | # invalid files will result in `False` |
---|
461 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
462 | self.setup_student(self.student) |
---|
463 | invalid_fpm = xmlrpclib.Binary('invalid file') |
---|
464 | self.assertRaises( |
---|
465 | xmlrpclib.Fault, server.put_student_fingerprints, |
---|
466 | self.student.student_id, {'1': invalid_fpm}) |
---|
467 | |
---|
468 | def test_put_student_fingerprints(self): |
---|
469 | # we can store fingerprints |
---|
470 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
471 | self.setup_student(self.student) |
---|
472 | fpm = xmlrpclib.Binary('FP1faked_fpm') |
---|
473 | result = server.put_student_fingerprints( |
---|
474 | self.student.student_id, {'1': fpm}) |
---|
475 | self.assertEqual(result, True) |
---|
476 | stored_file = getUtility(IExtFileStore).getFileByContext( |
---|
477 | self.student, attr="finger1.fpm") |
---|
478 | self.assertEqual(stored_file.read(), 'FP1faked_fpm') |
---|
479 | # storing is logged |
---|
480 | logfile = os.path.join( |
---|
481 | self.app['datacenter'].storage, 'logs', 'students.log') |
---|
482 | logcontent = open(logfile).read() |
---|
483 | self.assertTrue( |
---|
484 | 'zope.mgr - grok.meta.StudentsXMLRPC ' |
---|
485 | '- K1000000 - fingerprint stored' in logcontent) |
---|
486 | |
---|
487 | def test_put_student_fingerprints_existing(self): |
---|
488 | # existing fingerprints are overwritten |
---|
489 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
490 | self.setup_student(self.student) |
---|
491 | self.create_fpm_file(self.student, '1') |
---|
492 | fpm1 = xmlrpclib.Binary('FP1faked_fpm1') |
---|
493 | fpm2 = xmlrpclib.Binary('FP1faked_fpm2') |
---|
494 | result = server.put_student_fingerprints( |
---|
495 | self.student.student_id, {'1': fpm1, '3': fpm2}) |
---|
496 | self.assertEqual(result, True) |
---|
497 | stored_file1 = getUtility(IExtFileStore).getFileByContext( |
---|
498 | self.student, attr="finger1.fpm") |
---|
499 | stored_file2 = getUtility(IExtFileStore).getFileByContext( |
---|
500 | self.student, attr="finger3.fpm") |
---|
501 | self.assertEqual(stored_file1.read(), 'FP1faked_fpm1') |
---|
502 | self.assertEqual(stored_file2.read(), 'FP1faked_fpm2') |
---|
503 | |
---|
504 | def test_get_student_fingerprints_no_stud(self): |
---|
505 | # invalid student ids result in empty dict |
---|
506 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
507 | result = server.get_student_fingerprints('invalid id') |
---|
508 | self.assertEqual(result, {}) |
---|
509 | |
---|
510 | def test_get_student_fingerprints_no_files(self): |
---|
511 | # we get student data, but no fingerprints if not stored before |
---|
512 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
513 | self.setup_student(self.student) |
---|
514 | result = server.get_student_fingerprints(self.student.student_id) |
---|
515 | self.assertEqual( |
---|
516 | result, |
---|
517 | {'lastname': 'Tester', |
---|
518 | 'email': 'aa@aa.ng', |
---|
519 | 'firstname': 'Anna', |
---|
520 | 'fingerprints': {}, |
---|
521 | 'img': None, |
---|
522 | 'img_name': None, |
---|
523 | }) |
---|
524 | |
---|
525 | def test_get_student_fingerprints_passport(self): |
---|
526 | # we get a photograph of the student if avail. |
---|
527 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
528 | self.setup_student(self.student) |
---|
529 | self.create_passport_img(self.student) |
---|
530 | result = server.get_student_fingerprints(self.student.student_id) |
---|
531 | self.assertTrue( |
---|
532 | isinstance(result['img'], xmlrpclib.Binary)) |
---|
533 | self.assertEqual(result['img_name'], 'passport_K1000000.jpg') |
---|
534 | |
---|
535 | def test_get_student_fingerprints_fpm(self): |
---|
536 | # we get minutiae files if any are avail. |
---|
537 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
538 | self.setup_student(self.student) |
---|
539 | self.create_fpm_file(self.student, 'finger1') |
---|
540 | result = server.get_student_fingerprints(self.student.student_id) |
---|
541 | self.assertTrue('1' in result['fingerprints'].keys()) |
---|
542 | self.assertTrue( |
---|
543 | isinstance(result['fingerprints']['1'], xmlrpclib.Binary)) |
---|
544 | |
---|
545 | def test_get_student_fingerprints_block_unauthorized(self): |
---|
546 | # requests from unauthorized users are blocked |
---|
547 | # no username nor password |
---|
548 | server = ServerProxy('http://localhost/app') |
---|
549 | self.setup_student(self.student) |
---|
550 | stud_id = self.student.student_id |
---|
551 | self.assertRaises( |
---|
552 | xmlrpclib.ProtocolError, server.get_student_id, stud_id) |
---|
553 | # wrong password |
---|
554 | server = ServerProxy('http://mgr:WRONGPW@localhost/app') |
---|
555 | self.assertRaises( |
---|
556 | xmlrpclib.ProtocolError, server.get_student_id, stud_id) |
---|
557 | # wrong username |
---|
558 | server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app') |
---|
559 | self.assertRaises( |
---|
560 | xmlrpclib.ProtocolError, server.get_student_id, stud_id) |
---|
561 | return |
---|
562 | |
---|
563 | def test_get_bursary_data(self): |
---|
564 | tz = getUtility(IKofaUtils).tzinfo |
---|
565 | payment1 = StudentOnlinePayment() |
---|
566 | payment1.p_id = 'my-id1' |
---|
567 | payment1.p_session = 2012 |
---|
568 | payment1.amount_auth = 12.12 |
---|
569 | payment1.p_state = u'paid' |
---|
570 | payment1.p_category = u'schoolfee' |
---|
571 | self.student['payments']['my-payment1'] = payment1 |
---|
572 | payment2 = StudentOnlinePayment() |
---|
573 | payment2.p_id = 'my-id2' |
---|
574 | payment2.p_session = 2012 |
---|
575 | payment2.amount_auth = 12.12 |
---|
576 | payment2.p_state = u'paid' |
---|
577 | payment2.p_category = u'clearance' |
---|
578 | self.student['payments']['my-payment2'] = payment2 |
---|
579 | server = ServerProxy('http://mgr:mgrpw@localhost/app', allow_none=True) |
---|
580 | result = server.get_bursary_data(None,None,None,None,'dep1') |
---|
581 | self.assertEqual( |
---|
582 | result,[ |
---|
583 | {'entry_mode': None, 'reg_number': '123', 'display_item': None, |
---|
584 | 'firstname': 'Anna', 'payment_date': 'None#', |
---|
585 | 'middlename': None, 'student_id': 'K1000000', 'p_id': |
---|
586 | 'my-id1', 'certcode': 'CERT1', 'entry_session': 2004, |
---|
587 | 'creation_date': str('%s#' % to_timezone(payment1.creation_date, tz)), |
---|
588 | 'state': 'created', 'current_session': 2004, |
---|
589 | 'faccode': 'fac1', 'lastname': 'Tester', |
---|
590 | 'p_category': 'schoolfee', 'amount_auth': 12.12, |
---|
591 | 'p_state': 'paid', 'p_session': 2012, 'matric_number': '234', |
---|
592 | 'depcode': 'dep1'}, |
---|
593 | {'entry_mode': None, 'reg_number': '123', 'display_item': None, |
---|
594 | 'firstname': 'Anna', 'payment_date': 'None#', |
---|
595 | 'middlename': None, 'student_id': 'K1000000', |
---|
596 | 'p_id': 'my-id2', 'certcode': 'CERT1', 'entry_session': 2004, |
---|
597 | 'creation_date': str('%s#' % to_timezone(payment2.creation_date, tz)), |
---|
598 | 'state': 'created', 'current_session': 2004, 'faccode': 'fac1', |
---|
599 | 'lastname': 'Tester', 'p_category': 'clearance', |
---|
600 | 'amount_auth': 12.12, 'p_state': 'paid', 'p_session': 2012, |
---|
601 | 'matric_number': '234', 'depcode': 'dep1'} |
---|
602 | ] |
---|
603 | ) |
---|
604 | |
---|
605 | REQUEST_XML = """\ |
---|
606 | <?xml version="1.0"?> |
---|
607 | <methodCall> |
---|
608 | <methodName>get_bursary_data</methodName> |
---|
609 | <params> |
---|
610 | <param> |
---|
611 | <value><string></string></value> |
---|
612 | <value><string></string></value> |
---|
613 | <value><string></string></value> |
---|
614 | <value><string></string></value> |
---|
615 | <value><string>dep1</string></value> |
---|
616 | </param> |
---|
617 | </params> |
---|
618 | </methodCall>""" |
---|
619 | RESPONSE_XML = """\ |
---|
620 | <?xml version='1.0'?> |
---|
621 | <methodResponse> |
---|
622 | <params> |
---|
623 | <param> |
---|
624 | <value><array><data> |
---|
625 | <value><struct> |
---|
626 | <member> |
---|
627 | <name>entry_mode</name> |
---|
628 | <value><nil/></value></member> |
---|
629 | <member> |
---|
630 | <name>reg_number</name> |
---|
631 | <value><string>123</string></value> |
---|
632 | </member> |
---|
633 | <member> |
---|
634 | <name>display_item</name> |
---|
635 | <value><nil/></value></member> |
---|
636 | <member> |
---|
637 | <name>firstname</name> |
---|
638 | <value><string>Anna</string></value> |
---|
639 | </member> |
---|
640 | <member> |
---|
641 | <name>payment_date</name> |
---|
642 | <value><string>None#</string></value> |
---|
643 | </member> |
---|
644 | <member> |
---|
645 | <name>middlename</name> |
---|
646 | <value><nil/></value></member> |
---|
647 | <member> |
---|
648 | <name>student_id</name> |
---|
649 | <value><string>K1000000</string></value> |
---|
650 | </member> |
---|
651 | <member> |
---|
652 | <name>p_id</name> |
---|
653 | <value><string>my-id1</string></value> |
---|
654 | </member> |
---|
655 | <member> |
---|
656 | <name>certcode</name> |
---|
657 | <value><string>CERT1</string></value> |
---|
658 | </member> |
---|
659 | <member> |
---|
660 | <name>creation_date</name> |
---|
661 | <value><string>%s</string></value> |
---|
662 | </member> |
---|
663 | <member> |
---|
664 | <name>matric_number</name> |
---|
665 | <value><string>234</string></value> |
---|
666 | </member> |
---|
667 | <member> |
---|
668 | <name>state</name> |
---|
669 | <value><string>created</string></value> |
---|
670 | </member> |
---|
671 | <member> |
---|
672 | <name>lastname</name> |
---|
673 | <value><string>Tester</string></value> |
---|
674 | </member> |
---|
675 | <member> |
---|
676 | <name>current_session</name> |
---|
677 | <value><int>2004</int></value> |
---|
678 | </member> |
---|
679 | <member> |
---|
680 | <name>faccode</name> |
---|
681 | <value><string>fac1</string></value> |
---|
682 | </member> |
---|
683 | <member> |
---|
684 | <name>entry_session</name> |
---|
685 | <value><int>2004</int></value> |
---|
686 | </member> |
---|
687 | <member> |
---|
688 | <name>p_category</name> |
---|
689 | <value><string>schoolfee</string></value> |
---|
690 | </member> |
---|
691 | <member> |
---|
692 | <name>amount_auth</name> |
---|
693 | <value><double>12.12</double></value> |
---|
694 | </member> |
---|
695 | <member> |
---|
696 | <name>p_session</name> |
---|
697 | <value><int>2012</int></value> |
---|
698 | </member> |
---|
699 | <member> |
---|
700 | <name>p_state</name> |
---|
701 | <value><string>paid</string></value> |
---|
702 | </member> |
---|
703 | <member> |
---|
704 | <name>depcode</name> |
---|
705 | <value><string>dep1</string></value> |
---|
706 | </member> |
---|
707 | </struct></value> |
---|
708 | <value><struct> |
---|
709 | <member> |
---|
710 | <name>entry_mode</name> |
---|
711 | <value><nil/></value></member> |
---|
712 | <member> |
---|
713 | <name>reg_number</name> |
---|
714 | <value><string>123</string></value> |
---|
715 | </member> |
---|
716 | <member> |
---|
717 | <name>display_item</name> |
---|
718 | <value><nil/></value></member> |
---|
719 | <member> |
---|
720 | <name>firstname</name> |
---|
721 | <value><string>Anna</string></value> |
---|
722 | </member> |
---|
723 | <member> |
---|
724 | <name>payment_date</name> |
---|
725 | <value><string>None#</string></value> |
---|
726 | </member> |
---|
727 | <member> |
---|
728 | <name>middlename</name> |
---|
729 | <value><nil/></value></member> |
---|
730 | <member> |
---|
731 | <name>student_id</name> |
---|
732 | <value><string>K1000000</string></value> |
---|
733 | </member> |
---|
734 | <member> |
---|
735 | <name>p_id</name> |
---|
736 | <value><string>my-id2</string></value> |
---|
737 | </member> |
---|
738 | <member> |
---|
739 | <name>certcode</name> |
---|
740 | <value><string>CERT1</string></value> |
---|
741 | </member> |
---|
742 | <member> |
---|
743 | <name>creation_date</name> |
---|
744 | <value><string>%s</string></value> |
---|
745 | </member> |
---|
746 | <member> |
---|
747 | <name>matric_number</name> |
---|
748 | <value><string>234</string></value> |
---|
749 | </member> |
---|
750 | <member> |
---|
751 | <name>state</name> |
---|
752 | <value><string>created</string></value> |
---|
753 | </member> |
---|
754 | <member> |
---|
755 | <name>lastname</name> |
---|
756 | <value><string>Tester</string></value> |
---|
757 | </member> |
---|
758 | <member> |
---|
759 | <name>current_session</name> |
---|
760 | <value><int>2004</int></value> |
---|
761 | </member> |
---|
762 | <member> |
---|
763 | <name>faccode</name> |
---|
764 | <value><string>fac1</string></value> |
---|
765 | </member> |
---|
766 | <member> |
---|
767 | <name>entry_session</name> |
---|
768 | <value><int>2004</int></value> |
---|
769 | </member> |
---|
770 | <member> |
---|
771 | <name>p_category</name> |
---|
772 | <value><string>clearance</string></value> |
---|
773 | </member> |
---|
774 | <member> |
---|
775 | <name>amount_auth</name> |
---|
776 | <value><double>12.12</double></value> |
---|
777 | </member> |
---|
778 | <member> |
---|
779 | <name>p_session</name> |
---|
780 | <value><int>2012</int></value> |
---|
781 | </member> |
---|
782 | <member> |
---|
783 | <name>p_state</name> |
---|
784 | <value><string>paid</string></value> |
---|
785 | </member> |
---|
786 | <member> |
---|
787 | <name>depcode</name> |
---|
788 | <value><string>dep1</string></value> |
---|
789 | </member> |
---|
790 | </struct></value> |
---|
791 | </data></array></value> |
---|
792 | </param> |
---|
793 | </params> |
---|
794 | </methodResponse> |
---|
795 | """ |
---|
796 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
797 | response = RESPONSE_XML % (str('%s#' % to_timezone(payment1.creation_date, tz)), |
---|
798 | str('%s#' % to_timezone(payment2.creation_date, tz)) |
---|
799 | ) |
---|
800 | self.assertEqual(xmlout, response) |
---|
801 | return |
---|
802 | |
---|
803 | def test_get_unpaid_payments(self): |
---|
804 | timestamp1 = ("%d" % int((time()-10)*10000))[1:] |
---|
805 | timestamp2 = ("%d" % int((time()-5)*10000))[1:] |
---|
806 | timestamp3 = ("%d" % int((time()-(3*86400))*10000))[1:] |
---|
807 | p_id_1 = ("p%s" % timestamp1) |
---|
808 | p_id_2 = ("p%s" % timestamp2) |
---|
809 | p_id_3 = ("p%s" % timestamp3) |
---|
810 | payment1 = StudentOnlinePayment() |
---|
811 | payment1.p_id = p_id_1 |
---|
812 | payment1.p_session = 2012 |
---|
813 | payment1.amount_auth = 12.12 |
---|
814 | payment1.p_state = u'unpaid' |
---|
815 | payment1.p_category = u'schoolfee' |
---|
816 | payment1.r_company = u'xyz' |
---|
817 | self.student['payments']['my-payment1'] = payment1 |
---|
818 | payment2 = StudentOnlinePayment() |
---|
819 | payment2.p_id = p_id_2 |
---|
820 | payment2.p_session = 2012 |
---|
821 | payment2.amount_auth = 12.12 |
---|
822 | payment2.p_state = u'paid' |
---|
823 | payment2.p_category = u'clearance' |
---|
824 | self.student['payments']['my-payment2'] = payment2 |
---|
825 | payment3 = StudentOnlinePayment() |
---|
826 | payment3.p_id = p_id_3 |
---|
827 | payment3.p_session = 2012 |
---|
828 | payment3.amount_auth = 12.12 |
---|
829 | payment3.p_state = u'unpaid' |
---|
830 | payment3.p_category = u'schoolfee' |
---|
831 | self.student['payments']['my-payment3'] = payment3 |
---|
832 | server = ServerProxy('http://mgr:mgrpw@localhost/app', allow_none=True) |
---|
833 | results = server.get_unpaid_payments(1) |
---|
834 | self.assertEqual( |
---|
835 | results,[ |
---|
836 | {'display_item': None, |
---|
837 | 'p_id': '%s' % p_id_1, |
---|
838 | 'p_category': 'schoolfee', |
---|
839 | 'amount_auth': 12.12, 'p_session': 2012, |
---|
840 | 'p_state': 'unpaid', |
---|
841 | 'r_company': 'xyz'} |
---|
842 | ]) |
---|
843 | results = server.get_unpaid_payments(4) |
---|
844 | self.assertEqual( |
---|
845 | results,[ |
---|
846 | {'display_item': None, |
---|
847 | 'p_id': '%s' % p_id_1, |
---|
848 | 'p_category': 'schoolfee', |
---|
849 | 'amount_auth': 12.12, |
---|
850 | 'p_session': 2012, |
---|
851 | 'p_state': 'unpaid', |
---|
852 | 'r_company': 'xyz'}, |
---|
853 | {'display_item': None, |
---|
854 | 'p_id': '%s' % p_id_3, |
---|
855 | 'p_category': 'schoolfee', |
---|
856 | 'amount_auth': 12.12, |
---|
857 | 'p_session': 2012, |
---|
858 | 'p_state': 'unpaid', |
---|
859 | 'r_company': None} |
---|
860 | ]) |
---|
861 | results = server.get_unpaid_payments(4,'xyz') |
---|
862 | self.assertEqual( |
---|
863 | results,[ |
---|
864 | {'display_item': None, |
---|
865 | 'p_id': '%s' % p_id_1, |
---|
866 | 'p_category': 'schoolfee', |
---|
867 | 'amount_auth': 12.12, |
---|
868 | 'p_session': 2012, |
---|
869 | 'p_state': 'unpaid', |
---|
870 | 'r_company': 'xyz'}, |
---|
871 | ]) |
---|
872 | |
---|
873 | REQUEST_XML = """\ |
---|
874 | <?xml version="1.0"?> |
---|
875 | <methodCall> |
---|
876 | <methodName>get_unpaid_payments</methodName> |
---|
877 | <params> |
---|
878 | <param> |
---|
879 | <value><string>1</string></value> |
---|
880 | </param> |
---|
881 | </params> |
---|
882 | </methodCall>""" |
---|
883 | RESPONSE_XML = """\ |
---|
884 | <?xml version='1.0'?> |
---|
885 | <methodResponse> |
---|
886 | <params> |
---|
887 | <param> |
---|
888 | <value><array><data> |
---|
889 | <value><struct> |
---|
890 | <member> |
---|
891 | <name>display_item</name> |
---|
892 | <value><nil/></value></member> |
---|
893 | <member> |
---|
894 | <name>p_id</name> |
---|
895 | <value><string>%s</string></value> |
---|
896 | </member> |
---|
897 | <member> |
---|
898 | <name>r_company</name> |
---|
899 | <value><string>xyz</string></value> |
---|
900 | </member> |
---|
901 | <member> |
---|
902 | <name>p_category</name> |
---|
903 | <value><string>schoolfee</string></value> |
---|
904 | </member> |
---|
905 | <member> |
---|
906 | <name>amount_auth</name> |
---|
907 | <value><double>12.12</double></value> |
---|
908 | </member> |
---|
909 | <member> |
---|
910 | <name>p_session</name> |
---|
911 | <value><int>2012</int></value> |
---|
912 | </member> |
---|
913 | <member> |
---|
914 | <name>p_state</name> |
---|
915 | <value><string>unpaid</string></value> |
---|
916 | </member> |
---|
917 | </struct></value> |
---|
918 | </data></array></value> |
---|
919 | </param> |
---|
920 | </params> |
---|
921 | </methodResponse> |
---|
922 | """ % p_id_1 |
---|
923 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
924 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
925 | return |
---|