source: main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_webservices.py @ 16387

Last change on this file since 16387 was 16268, checked in by Henrik Bettermann, 4 years ago

Add p_session to get_bursary_data webservice parameters.

  • Property svn:keywords set to Id
File size: 31.6 KB
Line 
1# Tests for webservices
2import xmlrpclib
3import os
4from time import time
5from cStringIO import StringIO
6from zope.app.testing.xmlrpc import ServerProxy
7from zope.component import getUtility
8from waeup.kofa.interfaces import (
9    IExtFileStore, IFileStoreNameChooser, IKofaUtils)
10from waeup.kofa.testing import FunctionalLayer
11from waeup.kofa.utils.helpers import to_timezone
12from waeup.kofa.students.payments import StudentOnlinePayment
13from waeup.kofa.students.tests.test_browser import StudentsFullSetup
14from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
15
16
17class 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',2012)
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_payment(self):
804        payment1 = StudentOnlinePayment()
805        payment1.p_id = 'my-id1'
806        payment1.p_session = 2012
807        payment1.amount_auth = 12.12
808        payment1.p_state = u'failed'
809        payment1.p_category = u'schoolfee'
810        payment1.r_company = u'xyz'
811        self.student['payments']['my-payment1'] = payment1
812        server = ServerProxy('http://mgr:mgrpw@localhost/app', allow_none=True)
813        results = server.get_payment()
814        self.assertEqual(results, None)
815        results = server.get_payment('nonsense')
816        self.assertEqual(results, None)
817        results = server.get_payment('my-id1')
818        self.assertEqual(
819            results,
820                {'display_item': None,
821                'p_id': 'my-id1',
822                'p_category': 'schoolfee',
823                'amount_auth': 12.12, 'p_session': 2012,
824                'p_state': 'failed',
825                'r_company': 'xyz',
826                'fullname': 'Anna Tester',
827                'id': 'K1000000',
828                'matric_number': '234'}
829                )
830
831        REQUEST_XML = """\
832<?xml version="1.0"?>
833<methodCall>
834<methodName>get_payment</methodName>
835<params>
836<param>
837<value><string>my-id1</string></value>
838</param>
839</params>
840</methodCall>"""
841        RESPONSE_XML = """\
842<?xml version='1.0'?>
843<methodResponse>
844<params>
845<param>
846<value><struct>
847<member>
848<name>display_item</name>
849<value><nil/></value></member>
850<member>
851<name>amount_auth</name>
852<value><double>12.12</double></value>
853</member>
854<member>
855<name>p_id</name>
856<value><string>my-id1</string></value>
857</member>
858<member>
859<name>r_company</name>
860<value><string>xyz</string></value>
861</member>
862<member>
863<name>matric_number</name>
864<value><string>234</string></value>
865</member>
866<member>
867<name>p_category</name>
868<value><string>schoolfee</string></value>
869</member>
870<member>
871<name>fullname</name>
872<value><string>Anna Tester</string></value>
873</member>
874<member>
875<name>p_state</name>
876<value><string>failed</string></value>
877</member>
878<member>
879<name>p_session</name>
880<value><int>2012</int></value>
881</member>
882<member>
883<name>id</name>
884<value><string>K1000000</string></value>
885</member>
886</struct></value>
887</param>
888</params>
889</methodResponse>
890"""
891        xmlout = self.XMLRPC_post(REQUEST_XML)
892        self.assertEqual(xmlout, RESPONSE_XML)
893        return
894
895    def test_get_unpaid_payments(self):
896        timestamp1 = ("%d" % int((time()-10)*10000))[1:]
897        timestamp2 = ("%d" % int((time()-5)*10000))[1:]
898        timestamp3 = ("%d" % int((time()-(3*86400))*10000))[1:]
899        p_id_1 = ("p%s" % timestamp1)
900        p_id_2 = ("p%s" % timestamp2)
901        p_id_3 = ("p%s" % timestamp3)
902        payment1 = StudentOnlinePayment()
903        payment1.p_id = p_id_1
904        payment1.p_session = 2012
905        payment1.amount_auth = 12.12
906        payment1.p_state = u'failed'
907        payment1.p_category = u'schoolfee'
908        payment1.r_company = u'xyz'
909        self.student['payments']['my-payment1'] = payment1
910        payment2 = StudentOnlinePayment()
911        payment2.p_id = p_id_2
912        payment2.p_session = 2012
913        payment2.amount_auth = 12.12
914        payment2.p_state = u'paid'
915        payment2.p_category = u'clearance'
916        self.student['payments']['my-payment2'] = payment2
917        payment3 = StudentOnlinePayment()
918        payment3.p_id = p_id_3
919        payment3.p_session = 2012
920        payment3.amount_auth = 12.12
921        payment3.p_state = u'unpaid'
922        payment3.p_category = u'schoolfee'
923        self.student['payments']['my-payment3'] = payment3
924        server = ServerProxy('http://mgr:mgrpw@localhost/app', allow_none=True)
925        results = server.get_unpaid_payments(1)
926        self.assertEqual(
927            results,[
928                {'display_item': None,
929                'p_id': '%s' % p_id_1,
930                'p_category': 'schoolfee',
931                'amount_auth': 12.12, 'p_session': 2012,
932                'p_state': 'failed',
933                'r_company': 'xyz',
934                'fullname': 'Anna Tester',
935                'id': 'K1000000',
936                'matric_number': '234'}
937                ])
938        results = server.get_unpaid_payments(4)
939        self.assertEqual(
940            results,[
941                {'display_item': None,
942                  'p_id': '%s' % p_id_3,
943                  'p_category': 'schoolfee',
944                  'amount_auth': 12.12,
945                  'p_session': 2012,
946                  'p_state': 'unpaid',
947                  'r_company': None,
948                  'fullname': 'Anna Tester',
949                  'id': 'K1000000',
950                  'matric_number': '234'},
951                {'display_item': None,
952                  'p_id': '%s' % p_id_1,
953                  'p_category': 'schoolfee',
954                  'amount_auth': 12.12,
955                  'p_session': 2012,
956                  'p_state': 'failed',
957                  'r_company': 'xyz',
958                  'fullname': 'Anna Tester',
959                  'id': 'K1000000',
960                  'matric_number': '234'},
961                ])
962        results = server.get_unpaid_payments(4,'xyz')
963        self.assertEqual(
964            results,[
965                {'display_item': None,
966                  'p_id': '%s' % p_id_1,
967                  'p_category': 'schoolfee',
968                  'amount_auth': 12.12,
969                  'p_session': 2012,
970                  'p_state': 'failed',
971                  'r_company': 'xyz',
972                  'fullname': 'Anna Tester',
973                  'id': 'K1000000',
974                  'matric_number': '234'},
975                ])
976
977        REQUEST_XML = """\
978<?xml version="1.0"?>
979<methodCall>
980<methodName>get_unpaid_payments</methodName>
981<params>
982<param>
983<value><string>1</string></value>
984</param>
985</params>
986</methodCall>"""
987        RESPONSE_XML = """\
988<?xml version='1.0'?>
989<methodResponse>
990<params>
991<param>
992<value><array><data>
993<value><struct>
994<member>
995<name>display_item</name>
996<value><nil/></value></member>
997<member>
998<name>amount_auth</name>
999<value><double>12.12</double></value>
1000</member>
1001<member>
1002<name>p_id</name>
1003<value><string>%s</string></value>
1004</member>
1005<member>
1006<name>r_company</name>
1007<value><string>xyz</string></value>
1008</member>
1009<member>
1010<name>matric_number</name>
1011<value><string>234</string></value>
1012</member>
1013<member>
1014<name>p_category</name>
1015<value><string>schoolfee</string></value>
1016</member>
1017<member>
1018<name>fullname</name>
1019<value><string>Anna Tester</string></value>
1020</member>
1021<member>
1022<name>p_state</name>
1023<value><string>failed</string></value>
1024</member>
1025<member>
1026<name>p_session</name>
1027<value><int>2012</int></value>
1028</member>
1029<member>
1030<name>id</name>
1031<value><string>K1000000</string></value>
1032</member>
1033</struct></value>
1034</data></array></value>
1035</param>
1036</params>
1037</methodResponse>
1038""" % p_id_1
1039        xmlout = self.XMLRPC_post(REQUEST_XML)
1040        self.assertEqual(xmlout, RESPONSE_XML)
1041        return
Note: See TracBrowser for help on using the repository browser.