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

Last change on this file since 17847 was 17847, checked in by Henrik Bettermann, 4 months ago

Change order of parameters.

  • Property svn:keywords set to Id
File size: 37.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,2012,None,None,None,'dep1')
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>2012</string></value>
684<value><string></string></value>
685<value><string></string></value>
686<value><string></string></value>
687<value><string>dep1</string></value>
688</param>
689</params>
690</methodCall>"""
691        RESPONSE_XML = """\
692<?xml version='1.0'?>
693<methodResponse>
694<params>
695<param>
696<value><array><data>
697<value><struct>
698<member>
699<name>entry_mode</name>
700<value><nil/></value></member>
701<member>
702<name>reg_number</name>
703<value><string>123</string></value>
704</member>
705<member>
706<name>display_item</name>
707<value><nil/></value></member>
708<member>
709<name>firstname</name>
710<value><string>Anna</string></value>
711</member>
712<member>
713<name>payment_date</name>
714<value><string>None#</string></value>
715</member>
716<member>
717<name>middlename</name>
718<value><nil/></value></member>
719<member>
720<name>student_id</name>
721<value><string>K1000000</string></value>
722</member>
723<member>
724<name>p_id</name>
725<value><string>my-id1</string></value>
726</member>
727<member>
728<name>certcode</name>
729<value><string>CERT1</string></value>
730</member>
731<member>
732<name>creation_date</name>
733<value><string>%s</string></value>
734</member>
735<member>
736<name>matric_number</name>
737<value><string>234</string></value>
738</member>
739<member>
740<name>state</name>
741<value><string>created</string></value>
742</member>
743<member>
744<name>lastname</name>
745<value><string>Tester</string></value>
746</member>
747<member>
748<name>current_session</name>
749<value><int>2004</int></value>
750</member>
751<member>
752<name>faccode</name>
753<value><string>fac1</string></value>
754</member>
755<member>
756<name>entry_session</name>
757<value><int>2004</int></value>
758</member>
759<member>
760<name>p_category</name>
761<value><string>schoolfee</string></value>
762</member>
763<member>
764<name>amount_auth</name>
765<value><double>12.12</double></value>
766</member>
767<member>
768<name>p_session</name>
769<value><int>2012</int></value>
770</member>
771<member>
772<name>p_state</name>
773<value><string>paid</string></value>
774</member>
775<member>
776<name>depcode</name>
777<value><string>dep1</string></value>
778</member>
779</struct></value>
780<value><struct>
781<member>
782<name>entry_mode</name>
783<value><nil/></value></member>
784<member>
785<name>reg_number</name>
786<value><string>123</string></value>
787</member>
788<member>
789<name>display_item</name>
790<value><nil/></value></member>
791<member>
792<name>firstname</name>
793<value><string>Anna</string></value>
794</member>
795<member>
796<name>payment_date</name>
797<value><string>None#</string></value>
798</member>
799<member>
800<name>middlename</name>
801<value><nil/></value></member>
802<member>
803<name>student_id</name>
804<value><string>K1000000</string></value>
805</member>
806<member>
807<name>p_id</name>
808<value><string>my-id2</string></value>
809</member>
810<member>
811<name>certcode</name>
812<value><string>CERT1</string></value>
813</member>
814<member>
815<name>creation_date</name>
816<value><string>%s</string></value>
817</member>
818<member>
819<name>matric_number</name>
820<value><string>234</string></value>
821</member>
822<member>
823<name>state</name>
824<value><string>created</string></value>
825</member>
826<member>
827<name>lastname</name>
828<value><string>Tester</string></value>
829</member>
830<member>
831<name>current_session</name>
832<value><int>2004</int></value>
833</member>
834<member>
835<name>faccode</name>
836<value><string>fac1</string></value>
837</member>
838<member>
839<name>entry_session</name>
840<value><int>2004</int></value>
841</member>
842<member>
843<name>p_category</name>
844<value><string>clearance</string></value>
845</member>
846<member>
847<name>amount_auth</name>
848<value><double>12.12</double></value>
849</member>
850<member>
851<name>p_session</name>
852<value><int>2012</int></value>
853</member>
854<member>
855<name>p_state</name>
856<value><string>paid</string></value>
857</member>
858<member>
859<name>depcode</name>
860<value><string>dep1</string></value>
861</member>
862</struct></value>
863</data></array></value>
864</param>
865</params>
866</methodResponse>
867"""
868        xmlout = self.XMLRPC_post(REQUEST_XML)
869        response = RESPONSE_XML % (str('%s#' % to_timezone(payment1.creation_date, tz)),
870                                   str('%s#' % to_timezone(payment2.creation_date, tz))
871                                   )
872        self.assertEqual(xmlout, response)
873        return
874
875    def test_get_payment(self):
876        payment1 = StudentOnlinePayment()
877        payment1.p_id = 'my-id1'
878        payment1.p_session = 2012
879        payment1.amount_auth = 12.12
880        payment1.p_state = u'failed'
881        payment1.p_category = u'schoolfee'
882        payment1.r_company = u'xyz'
883        self.student['payments']['my-payment1'] = payment1
884        server = ServerProxy('http://mgr:mgrpw@localhost/app', allow_none=True)
885        results = server.get_payment()
886        self.assertEqual(results, None)
887        results = server.get_payment('nonsense')
888        self.assertEqual(results, None)
889        results = server.get_payment('my-id1')
890        self.assertEqual(
891            results,
892                {'display_item': None,
893                'p_id': 'my-id1',
894                'p_category': 'schoolfee',
895                'amount_auth': 12.12, 'p_session': 2012,
896                'p_state': 'failed',
897                'r_company': 'xyz',
898                'fullname': 'Anna Tester',
899                'id': 'K1000000',
900                'matric_number': '234'}
901                )
902
903        REQUEST_XML = """\
904<?xml version="1.0"?>
905<methodCall>
906<methodName>get_payment</methodName>
907<params>
908<param>
909<value><string>my-id1</string></value>
910</param>
911</params>
912</methodCall>"""
913        RESPONSE_XML = """\
914<?xml version='1.0'?>
915<methodResponse>
916<params>
917<param>
918<value><struct>
919<member>
920<name>display_item</name>
921<value><nil/></value></member>
922<member>
923<name>amount_auth</name>
924<value><double>12.12</double></value>
925</member>
926<member>
927<name>p_id</name>
928<value><string>my-id1</string></value>
929</member>
930<member>
931<name>r_company</name>
932<value><string>xyz</string></value>
933</member>
934<member>
935<name>matric_number</name>
936<value><string>234</string></value>
937</member>
938<member>
939<name>p_category</name>
940<value><string>schoolfee</string></value>
941</member>
942<member>
943<name>fullname</name>
944<value><string>Anna Tester</string></value>
945</member>
946<member>
947<name>p_state</name>
948<value><string>failed</string></value>
949</member>
950<member>
951<name>p_session</name>
952<value><int>2012</int></value>
953</member>
954<member>
955<name>id</name>
956<value><string>K1000000</string></value>
957</member>
958</struct></value>
959</param>
960</params>
961</methodResponse>
962"""
963        xmlout = self.XMLRPC_post(REQUEST_XML)
964        self.assertEqual(xmlout, RESPONSE_XML)
965        return
966
967    def test_get_unpaid_payments(self):
968        timestamp1 = ("%d" % int((time()-10)*10000))[1:]
969        timestamp2 = ("%d" % int((time()-5)*10000))[1:]
970        timestamp3 = ("%d" % int((time()-(3*86400))*10000))[1:]
971        p_id_1 = ("p%s" % timestamp1)
972        p_id_2 = ("p%s" % timestamp2)
973        p_id_3 = ("p%s" % timestamp3)
974        payment1 = StudentOnlinePayment()
975        payment1.p_id = p_id_1
976        payment1.p_session = 2012
977        payment1.amount_auth = 12.12
978        payment1.p_state = u'failed'
979        payment1.p_category = u'schoolfee'
980        payment1.r_company = u'xyz'
981        self.student['payments']['my-payment1'] = payment1
982        payment2 = StudentOnlinePayment()
983        payment2.p_id = p_id_2
984        payment2.p_session = 2012
985        payment2.amount_auth = 12.12
986        payment2.p_state = u'paid'
987        payment2.p_category = u'clearance'
988        self.student['payments']['my-payment2'] = payment2
989        payment3 = StudentOnlinePayment()
990        payment3.p_id = p_id_3
991        payment3.p_session = 2012
992        payment3.amount_auth = 12.12
993        payment3.p_state = u'unpaid'
994        payment3.p_category = u'schoolfee'
995        self.student['payments']['my-payment3'] = payment3
996        server = ServerProxy('http://mgr:mgrpw@localhost/app', allow_none=True)
997        results = server.get_unpaid_payments(1)
998        self.assertEqual(
999            results,[
1000                {'display_item': None,
1001                'p_id': '%s' % p_id_1,
1002                'p_category': 'schoolfee',
1003                'amount_auth': 12.12, 'p_session': 2012,
1004                'p_state': 'failed',
1005                'r_company': 'xyz',
1006                'fullname': 'Anna Tester',
1007                'id': 'K1000000',
1008                'matric_number': '234'}
1009                ])
1010        results = server.get_unpaid_payments(4)
1011        self.assertEqual(
1012            results,[
1013                {'display_item': None,
1014                  'p_id': '%s' % p_id_3,
1015                  'p_category': 'schoolfee',
1016                  'amount_auth': 12.12,
1017                  'p_session': 2012,
1018                  'p_state': 'unpaid',
1019                  'r_company': None,
1020                  'fullname': 'Anna Tester',
1021                  'id': 'K1000000',
1022                  'matric_number': '234'},
1023                {'display_item': None,
1024                  'p_id': '%s' % p_id_1,
1025                  'p_category': 'schoolfee',
1026                  'amount_auth': 12.12,
1027                  'p_session': 2012,
1028                  'p_state': 'failed',
1029                  'r_company': 'xyz',
1030                  'fullname': 'Anna Tester',
1031                  'id': 'K1000000',
1032                  'matric_number': '234'},
1033                ])
1034        results = server.get_unpaid_payments(4,'xyz')
1035        self.assertEqual(
1036            results,[
1037                {'display_item': None,
1038                  'p_id': '%s' % p_id_1,
1039                  'p_category': 'schoolfee',
1040                  'amount_auth': 12.12,
1041                  'p_session': 2012,
1042                  'p_state': 'failed',
1043                  'r_company': 'xyz',
1044                  'fullname': 'Anna Tester',
1045                  'id': 'K1000000',
1046                  'matric_number': '234'},
1047                ])
1048
1049        REQUEST_XML = """\
1050<?xml version="1.0"?>
1051<methodCall>
1052<methodName>get_unpaid_payments</methodName>
1053<params>
1054<param>
1055<value><string>1</string></value>
1056</param>
1057</params>
1058</methodCall>"""
1059        RESPONSE_XML = """\
1060<?xml version='1.0'?>
1061<methodResponse>
1062<params>
1063<param>
1064<value><array><data>
1065<value><struct>
1066<member>
1067<name>display_item</name>
1068<value><nil/></value></member>
1069<member>
1070<name>amount_auth</name>
1071<value><double>12.12</double></value>
1072</member>
1073<member>
1074<name>p_id</name>
1075<value><string>%s</string></value>
1076</member>
1077<member>
1078<name>r_company</name>
1079<value><string>xyz</string></value>
1080</member>
1081<member>
1082<name>matric_number</name>
1083<value><string>234</string></value>
1084</member>
1085<member>
1086<name>p_category</name>
1087<value><string>schoolfee</string></value>
1088</member>
1089<member>
1090<name>fullname</name>
1091<value><string>Anna Tester</string></value>
1092</member>
1093<member>
1094<name>p_state</name>
1095<value><string>failed</string></value>
1096</member>
1097<member>
1098<name>p_session</name>
1099<value><int>2012</int></value>
1100</member>
1101<member>
1102<name>id</name>
1103<value><string>K1000000</string></value>
1104</member>
1105</struct></value>
1106</data></array></value>
1107</param>
1108</params>
1109</methodResponse>
1110""" % p_id_1
1111        xmlout = self.XMLRPC_post(REQUEST_XML)
1112        self.assertEqual(xmlout, RESPONSE_XML)
1113        return
1114
1115    def test_get_student_info(self):
1116        server = ServerProxy('http://mgr:mgrpw@localhost/app')
1117        self.setup_student(self.student)
1118        result = server.get_student_info('123')
1119        self.assertEqual(result,
1120            ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng'])
1121        REQUEST_XML = """\
1122<?xml version="1.0"?>
1123<methodCall>
1124<methodName>get_student_info</methodName>
1125<params>
1126<param>
1127<value><string>K1000000</string></value>
1128</param>
1129</params>
1130</methodCall>"""
1131        RESPONSE_XML = """\
1132<?xml version='1.0'?>
1133<methodResponse>
1134<params>
1135<param>
1136<value><array><data>
1137<value><string>Anna Tester</string></value>
1138<value><string>CERT1</string></value>
1139<value><string>1234</string></value>
1140<value><string>aa@aa.ng</string></value>
1141</data></array></value>
1142</param>
1143</params>
1144</methodResponse>
1145"""
1146        xmlout = self.XMLRPC_post(REQUEST_XML)
1147        self.assertEqual(xmlout, RESPONSE_XML)
1148        return
1149
1150    def test_get_student_base_data(self):
1151        server = ServerProxy('http://mgr:mgrpw@localhost/app')
1152        result = server.get_student_base_data()
1153        self.assertEqual(result,
1154            [{'entry_mode': None,
1155              'reg_number': '123',
1156              'firstname': 'Anna',
1157              'middlename': None,
1158              'current_session': 2004,
1159              'entry_session': 2004,
1160              'certcode': 'CERT1',
1161              'state': 'created',
1162              'student_id': 'K1000000',
1163              'faccode': 'fac1',
1164              'lastname': 'Tester',
1165              'matric_number': '234',
1166              'depcode': 'dep1',
1167              'sex': 'm'}])
1168        REQUEST_XML = """\
1169<?xml version="1.0"?>
1170<methodCall>
1171<methodName>get_student_base_data</methodName>
1172<params>
1173</params>
1174</methodCall>"""
1175        RESPONSE_XML = """\
1176<?xml version='1.0'?>
1177<methodResponse>
1178<params>
1179<param>
1180<value><array><data>
1181<value><struct>
1182<member>
1183<name>entry_mode</name>
1184<value><nil/></value></member>
1185<member>
1186<name>reg_number</name>
1187<value><string>123</string></value>
1188</member>
1189<member>
1190<name>firstname</name>
1191<value><string>Anna</string></value>
1192</member>
1193<member>
1194<name>middlename</name>
1195<value><nil/></value></member>
1196<member>
1197<name>current_session</name>
1198<value><int>2004</int></value>
1199</member>
1200<member>
1201<name>entry_session</name>
1202<value><int>2004</int></value>
1203</member>
1204<member>
1205<name>certcode</name>
1206<value><string>CERT1</string></value>
1207</member>
1208<member>
1209<name>sex</name>
1210<value><string>m</string></value>
1211</member>
1212<member>
1213<name>matric_number</name>
1214<value><string>234</string></value>
1215</member>
1216<member>
1217<name>state</name>
1218<value><string>created</string></value>
1219</member>
1220<member>
1221<name>faccode</name>
1222<value><string>fac1</string></value>
1223</member>
1224<member>
1225<name>lastname</name>
1226<value><string>Tester</string></value>
1227</member>
1228<member>
1229<name>student_id</name>
1230<value><string>K1000000</string></value>
1231</member>
1232<member>
1233<name>depcode</name>
1234<value><string>dep1</string></value>
1235</member>
1236</struct></value>
1237</data></array></value>
1238</param>
1239</params>
1240</methodResponse>
1241"""
1242        xmlout = self.XMLRPC_post(REQUEST_XML)
1243        self.assertEqual(xmlout, RESPONSE_XML)
1244        return
Note: See TracBrowser for help on using the repository browser.