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

Last change on this file since 10322 was 10044, checked in by Henrik Bettermann, 12 years ago

Extend get_students_by_course and return school fee paid in that session.

File size: 9.9 KB
Line 
1# Tests for webservices
2import xmlrpclib
3import os
4from cStringIO import StringIO
5from zope.app.testing.xmlrpc import ServerProxy
6from zope.component import createObject, getUtility
7from zope.component.hooks import setSite
8from zope.testbrowser.testing import Browser
9from waeup.kofa.app import University
10from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
11from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
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
16class XMLRPCTests(StudentsFullSetup):
17    # check XMLRPC services for university portal
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 XMLRPC_post(self, body):
52        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
53        self.browser.addHeader('Content-Length', len(body))
54        self.browser.post('http://localhost/app', body,
55            'text/xml; charset=utf-8')
56        return self.browser.contents
57
58    def test_get_student_id_no_match(self):
59        # w/o any students we get none
60        server = ServerProxy('http://mgr:mgrpw@localhost/app')
61        result = server.get_student_id('Nonsense')
62        self.assertTrue(result is None)
63        return
64
65    def test_get_student_id_regno_exists(self):
66        # we can get the id of an existing student with matching reg_no
67        server = ServerProxy('http://mgr:mgrpw@localhost/app')
68        result = server.get_student_id('123')
69        self.assertEqual(result, 'K1000000')
70        self.assertEqual(self.student_id, result)
71        REQUEST_XML="""\
72<?xml version="1.0"?>
73<methodCall>
74<methodName>get_student_id</methodName>
75<params>
76<param>
77<value><string>123</string></value>
78</param>
79</params>
80</methodCall>"""
81        RESPONSE_XML="""\
82<?xml version='1.0'?>
83<methodResponse>
84<params>
85<param>
86<value><string>K1000000</string></value>
87</param>
88</params>
89</methodResponse>
90"""
91        xmlout = self.XMLRPC_post(REQUEST_XML)
92        self.assertEqual(xmlout, RESPONSE_XML)
93        return
94
95    def test_get_student_id_block_unauthorized(self):
96        # requests from unauthorized users are blocked
97        # no username nor password
98        server = ServerProxy('http://localhost/app')
99        self.assertRaises(
100            xmlrpclib.ProtocolError, server.get_student_id, '123')
101        # wrong password
102        server = ServerProxy('http://mgr:WRONGPW@localhost/app')
103        self.assertRaises(
104            xmlrpclib.ProtocolError, server.get_student_id, '123')
105        # wrong username
106        server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app')
107        self.assertRaises(
108            xmlrpclib.ProtocolError, server.get_student_id, '123')
109        return
110
111    def test_get_courses_by_session(self):
112        server = ServerProxy('http://mgr:mgrpw@localhost/app')
113        result = server.get_courses_by_session('K1000000')
114        self.assertEqual(result, None)
115        self.setup_student(self.student)
116        result = server.get_courses_by_session('K1000000', '2010')
117        self.assertEqual(result, None)
118        result = server.get_courses_by_session('K1000000', '2012')
119        self.assertEqual(result,
120            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
121        result = server.get_courses_by_session('K1000000')
122        self.assertEqual(result,
123            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
124        # Also matric_number ...
125        result = server.get_courses_by_session('234')
126        self.assertEqual(result,
127            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
128        # ... or reg_number can be used.
129        result = server.get_courses_by_session('123')
130        self.assertEqual(result,
131            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
132        result = server.get_courses_by_session('Nonsense')
133        self.assertEqual(result, None)
134        REQUEST_XML="""\
135<?xml version="1.0"?>
136<methodCall>
137<methodName>get_courses_by_session</methodName>
138<params>
139<param>
140<value><string>K1000000</string></value>
141</param>
142</params>
143</methodCall>"""
144        RESPONSE_XML="""\
145<?xml version='1.0'?>
146<methodResponse>
147<params>
148<param>
149<value><struct>
150<member>
151<name>100|CRS1</name>
152<value><string>Course 1</string></value>
153</member>
154<member>
155<name>100|COURSE1</name>
156<value><string>Unnamed Course</string></value>
157</member>
158</struct></value>
159</param>
160</params>
161</methodResponse>
162"""
163        xmlout = self.XMLRPC_post(REQUEST_XML)
164        self.assertEqual(xmlout, RESPONSE_XML)
165        return
166
167    def test_get_students_by_course(self):
168        self.setup_student(self.student)
169        server = ServerProxy('http://mgr:mgrpw@localhost/app')
170        result = server.get_students_by_course('CRS1', '2010')
171        self.assertEqual(result, None)
172        result = server.get_students_by_course('CRS1', '2012')
173        self.assertEqual(result, [['K1000000', '234', 'my adviser', 0],])
174        result = server.get_students_by_course('CRS1')
175        self.assertEqual(result, [['K1000000', '234', 'my adviser', 0],])
176        payment = StudentOnlinePayment()
177        payment.p_id = 'my-id'
178        payment.p_session = 2012
179        payment.amount_auth = 12.12
180        payment.p_state = u'paid'
181        self.student['payments']['my-payment'] = payment
182        result = server.get_students_by_course('CRS1')
183        self.assertEqual(result, [['K1000000', '234', 'my adviser', 12.12],])
184        REQUEST_XML="""\
185<?xml version="1.0"?>
186<methodCall>
187<methodName>get_students_by_course</methodName>
188<params>
189<param>
190<value><string>CRS1</string></value>
191<value><string>2012</string></value>
192</param>
193</params>
194</methodCall>"""
195        RESPONSE_XML="""\
196<?xml version='1.0'?>
197<methodResponse>
198<params>
199<param>
200<value><array><data>
201<value><array><data>
202<value><string>K1000000</string></value>
203<value><string>234</string></value>
204<value><string>my adviser</string></value>
205<value><double>12.12</double></value>
206</data></array></value>
207</data></array></value>
208</param>
209</params>
210</methodResponse>
211"""
212        xmlout = self.XMLRPC_post(REQUEST_XML)
213        self.assertEqual(xmlout, RESPONSE_XML)
214        return
215
216    def test_get_student_info(self):
217        server = ServerProxy('http://mgr:mgrpw@localhost/app')
218        self.setup_student(self.student)
219        result = server.get_student_info('123')
220        self.assertEqual(result,
221            ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng'])
222        REQUEST_XML="""\
223<?xml version="1.0"?>
224<methodCall>
225<methodName>get_student_info</methodName>
226<params>
227<param>
228<value><string>K1000000</string></value>
229</param>
230</params>
231</methodCall>"""
232        RESPONSE_XML="""\
233<?xml version='1.0'?>
234<methodResponse>
235<params>
236<param>
237<value><array><data>
238<value><string>Anna Tester</string></value>
239<value><string>CERT1</string></value>
240<value><string>1234</string></value>
241<value><string>aa@aa.ng</string></value>
242</data></array></value>
243</param>
244</params>
245</methodResponse>
246"""
247        xmlout = self.XMLRPC_post(REQUEST_XML)
248        self.assertEqual(xmlout, RESPONSE_XML)
249        return
250
251    def test_get_student_passport(self):
252        server = ServerProxy('http://mgr:mgrpw@localhost/app')
253        self.setup_student(self.student)
254        self.create_passport_img(self.student)
255        result = server.get_student_passport('123')
256        img = getUtility(IExtFileStore).getFileByContext(
257            self.student, attr='passport.jpg')
258        binary = img.read()
259        self.assertEqual(binary, result)
260        REQUEST_XML="""\
261<?xml version="1.0"?>
262<methodCall>
263<methodName>get_student_passport</methodName>
264<params>
265<param>
266<value><string>K1000000</string></value>
267</param>
268</params>
269</methodCall>"""
270        RESPONSE_XML="""\
271<?xml version='1.0'?>
272<methodResponse>
273<params>
274<param>
275<value><base64>
276/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b
277"""
278        xmlout = self.XMLRPC_post(REQUEST_XML)
279        self.assertTrue(xmlout.startswith(RESPONSE_XML))
280
281    def test_get_paid_sessions(self):
282        server = ServerProxy('http://mgr:mgrpw@localhost/app')
283        self.setup_student(self.student)
284        payment = StudentOnlinePayment()
285        payment.p_id = 'my-id'
286        payment.p_session = 2009
287        payment.amount_auth = 12.12
288        payment.p_state = u'paid'
289        self.student['payments']['my-payment'] = payment
290        result = server.get_paid_sessions('123')
291        self.assertEqual(result, {'2009': 12.12})
292        REQUEST_XML="""\
293<?xml version="1.0"?>
294<methodCall>
295<methodName>get_paid_sessions</methodName>
296<params>
297<param>
298<value><string>K1000000</string></value>
299</param>
300</params>
301</methodCall>"""
302        RESPONSE_XML="""\
303<?xml version='1.0'?>
304<methodResponse>
305<params>
306<param>
307<value><struct>
308<member>
309<name>2009</name>
310<value><double>12.12</double></value>
311</member>
312</struct></value>
313</param>
314</params>
315</methodResponse>
316"""
317        xmlout = self.XMLRPC_post(REQUEST_XML)
318        self.assertEqual(xmlout, RESPONSE_XML)
319        return
Note: See TracBrowser for help on using the repository browser.