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

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

Improve wevservice.

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