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

Last change on this file since 10549 was 10516, checked in by Henrik Bettermann, 11 years ago

Rename get_moodle_data method.

File size: 11.3 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 getUtility
7from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser
8from waeup.kofa.testing import FunctionalLayer
9from waeup.kofa.students.payments import StudentOnlinePayment
10from waeup.kofa.students.tests.test_browser import StudentsFullSetup
11from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
12
13
14class XMLRPCTests(StudentsFullSetup):
15    # check XMLRPC services for university portal
16
17    layer = FunctionalLayer
18
19    def setup_student(self, student):
20        study_level = StudentStudyLevel()
21        study_level.level_session = 2012
22        study_level.level_verdict = "A"
23        study_level.level = 100
24        study_level.validated_by = u"my adviser"
25        student['studycourse'].addStudentStudyLevel(
26            self.certificate, study_level)
27
28        ticket = CourseTicket()
29        ticket.automatic = True
30        ticket.carry_over = True
31        ticket.code = u'CRS1'
32        ticket.title = u'Course 1'
33        ticket.fcode = u'FAC1'
34        ticket.dcode = u'DEP1'
35        ticket.credits = 100
36        ticket.passmark = 100
37        ticket.semester = 2
38        study_level[ticket.code] = ticket
39
40    def create_passport_img(self, student):
41        # create some passport file for `student`
42        storage = getUtility(IExtFileStore)
43        image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg')
44        self.image_contents = open(image_path, 'rb').read()
45        file_id = IFileStoreNameChooser(student).chooseName(
46            attr='passport.jpg')
47        storage.createFile(file_id, StringIO(self.image_contents))
48
49    def XMLRPC_post(self, body):
50        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
51        self.browser.addHeader('Content-Length', len(body))
52        self.browser.post('http://localhost/app', body,
53            'text/xml; charset=utf-8')
54        return self.browser.contents
55
56    def test_get_student_id_no_match(self):
57        # w/o any students we get none
58        server = ServerProxy('http://mgr:mgrpw@localhost/app')
59        result = server.get_student_id('Nonsense')
60        self.assertTrue(result is None)
61        return
62
63    def test_get_student_id_regno_exists(self):
64        # we can get the id of an existing student with matching reg_no
65        server = ServerProxy('http://mgr:mgrpw@localhost/app')
66        result = server.get_student_id('123')
67        self.assertEqual(result, 'K1000000')
68        self.assertEqual(self.student_id, result)
69        REQUEST_XML = """\
70<?xml version="1.0"?>
71<methodCall>
72<methodName>get_student_id</methodName>
73<params>
74<param>
75<value><string>123</string></value>
76</param>
77</params>
78</methodCall>"""
79        RESPONSE_XML = """\
80<?xml version='1.0'?>
81<methodResponse>
82<params>
83<param>
84<value><string>K1000000</string></value>
85</param>
86</params>
87</methodResponse>
88"""
89        xmlout = self.XMLRPC_post(REQUEST_XML)
90        self.assertEqual(xmlout, RESPONSE_XML)
91        return
92
93    def test_get_student_id_block_unauthorized(self):
94        # requests from unauthorized users are blocked
95        # no username nor password
96        server = ServerProxy('http://localhost/app')
97        self.assertRaises(
98            xmlrpclib.ProtocolError, server.get_student_id, '123')
99        # wrong password
100        server = ServerProxy('http://mgr:WRONGPW@localhost/app')
101        self.assertRaises(
102            xmlrpclib.ProtocolError, server.get_student_id, '123')
103        # wrong username
104        server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app')
105        self.assertRaises(
106            xmlrpclib.ProtocolError, server.get_student_id, '123')
107        return
108
109    def test_get_courses_by_session(self):
110        server = ServerProxy('http://mgr:mgrpw@localhost/app')
111        result = server.get_courses_by_session('K1000000')
112        self.assertEqual(result, None)
113        self.setup_student(self.student)
114        result = server.get_courses_by_session('K1000000', '2010')
115        self.assertEqual(result, None)
116        result = server.get_courses_by_session('K1000000', '2012')
117        self.assertEqual(result,
118            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
119        result = server.get_courses_by_session('K1000000')
120        self.assertEqual(result,
121            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
122        # Also matric_number ...
123        result = server.get_courses_by_session('234')
124        self.assertEqual(result,
125            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
126        # ... or reg_number can be used.
127        result = server.get_courses_by_session('123')
128        self.assertEqual(result,
129            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
130        result = server.get_courses_by_session('Nonsense')
131        self.assertEqual(result, None)
132        REQUEST_XML = """\
133<?xml version="1.0"?>
134<methodCall>
135<methodName>get_courses_by_session</methodName>
136<params>
137<param>
138<value><string>K1000000</string></value>
139</param>
140</params>
141</methodCall>"""
142        RESPONSE_XML = """\
143<?xml version='1.0'?>
144<methodResponse>
145<params>
146<param>
147<value><struct>
148<member>
149<name>100|CRS1</name>
150<value><string>Course 1</string></value>
151</member>
152<member>
153<name>100|COURSE1</name>
154<value><string>Unnamed Course</string></value>
155</member>
156</struct></value>
157</param>
158</params>
159</methodResponse>
160"""
161        xmlout = self.XMLRPC_post(REQUEST_XML)
162        self.assertEqual(xmlout, RESPONSE_XML)
163        return
164
165    def test_get_students_by_course(self):
166        self.setup_student(self.student)
167        server = ServerProxy('http://mgr:mgrpw@localhost/app')
168        result = server.get_students_by_course('CRS1', '2010')
169        self.assertEqual(result, None)
170        result = server.get_students_by_course('CRS1', '2012')
171        self.assertEqual(result, [['K1000000', '234', 'my adviser', 0], ])
172        result = server.get_students_by_course('CRS1')
173        self.assertEqual(result, [['K1000000', '234', 'my adviser', 0], ])
174        payment = StudentOnlinePayment()
175        payment.p_id = 'my-id'
176        payment.p_session = 2012
177        payment.amount_auth = 12.12
178        payment.p_state = u'paid'
179        self.student['payments']['my-payment'] = payment
180        result = server.get_students_by_course('CRS1')
181        self.assertEqual(result, [['K1000000', '234', 'my adviser', 12.12], ])
182        REQUEST_XML = """\
183<?xml version="1.0"?>
184<methodCall>
185<methodName>get_students_by_course</methodName>
186<params>
187<param>
188<value><string>CRS1</string></value>
189<value><string>2012</string></value>
190</param>
191</params>
192</methodCall>"""
193        RESPONSE_XML = """\
194<?xml version='1.0'?>
195<methodResponse>
196<params>
197<param>
198<value><array><data>
199<value><array><data>
200<value><string>K1000000</string></value>
201<value><string>234</string></value>
202<value><string>my adviser</string></value>
203<value><double>12.12</double></value>
204</data></array></value>
205</data></array></value>
206</param>
207</params>
208</methodResponse>
209"""
210        xmlout = self.XMLRPC_post(REQUEST_XML)
211        self.assertEqual(xmlout, RESPONSE_XML)
212        return
213
214    def test_get_student_info(self):
215        server = ServerProxy('http://mgr:mgrpw@localhost/app')
216        self.setup_student(self.student)
217        result = server.get_student_info('123')
218        self.assertEqual(result,
219            ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng'])
220        REQUEST_XML = """\
221<?xml version="1.0"?>
222<methodCall>
223<methodName>get_student_info</methodName>
224<params>
225<param>
226<value><string>K1000000</string></value>
227</param>
228</params>
229</methodCall>"""
230        RESPONSE_XML = """\
231<?xml version='1.0'?>
232<methodResponse>
233<params>
234<param>
235<value><array><data>
236<value><string>Anna Tester</string></value>
237<value><string>CERT1</string></value>
238<value><string>1234</string></value>
239<value><string>aa@aa.ng</string></value>
240</data></array></value>
241</param>
242</params>
243</methodResponse>
244"""
245        xmlout = self.XMLRPC_post(REQUEST_XML)
246        self.assertEqual(xmlout, RESPONSE_XML)
247        return
248
249    def test_get_student_passport(self):
250        server = ServerProxy('http://mgr:mgrpw@localhost/app')
251        self.setup_student(self.student)
252        self.create_passport_img(self.student)
253        result = server.get_student_passport('123')
254        img = getUtility(IExtFileStore).getFileByContext(
255            self.student, attr='passport.jpg')
256        binary = img.read()
257        self.assertEqual(binary, result)
258        REQUEST_XML = """\
259<?xml version="1.0"?>
260<methodCall>
261<methodName>get_student_passport</methodName>
262<params>
263<param>
264<value><string>K1000000</string></value>
265</param>
266</params>
267</methodCall>"""
268        RESPONSE_XML = """\
269<?xml version='1.0'?>
270<methodResponse>
271<params>
272<param>
273<value><base64>
274/9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b
275"""
276        xmlout = self.XMLRPC_post(REQUEST_XML)
277        self.assertTrue(xmlout.startswith(RESPONSE_XML))
278
279    def test_get_paid_sessions(self):
280        server = ServerProxy('http://mgr:mgrpw@localhost/app')
281        self.setup_student(self.student)
282        payment = StudentOnlinePayment()
283        payment.p_id = 'my-id'
284        payment.p_session = 2009
285        payment.amount_auth = 12.12
286        payment.p_state = u'paid'
287        self.student['payments']['my-payment'] = payment
288        result = server.get_paid_sessions('123')
289        self.assertEqual(result, {'2009': 12.12})
290        REQUEST_XML = """\
291<?xml version="1.0"?>
292<methodCall>
293<methodName>get_paid_sessions</methodName>
294<params>
295<param>
296<value><string>K1000000</string></value>
297</param>
298</params>
299</methodCall>"""
300        RESPONSE_XML = """\
301<?xml version='1.0'?>
302<methodResponse>
303<params>
304<param>
305<value><struct>
306<member>
307<name>2009</name>
308<value><double>12.12</double></value>
309</member>
310</struct></value>
311</param>
312</params>
313</methodResponse>
314"""
315        xmlout = self.XMLRPC_post(REQUEST_XML)
316        self.assertEqual(xmlout, RESPONSE_XML)
317        return
318
319    def test_check_student_credentials(self):
320        # make sure we can get student infos providing valid creds
321        server = ServerProxy('http://mgr:mgrpw@localhost/app')
322        self.setup_student(self.student)
323        stud_id = self.student.student_id
324        result = server.check_student_credentials(stud_id, 'spwd')
325        self.assertEqual(
326            result, {
327                'description': 'Anna Tester',
328                'email': 'aa@aa.ng',
329                'id': 'K1000000',
330                'type': 'student'}
331            )
332        return
333
334    def test_get_student_moodle_data(self):
335        server = ServerProxy('http://mgr:mgrpw@localhost/app')
336        self.setup_student(self.student)
337        result = server.get_student_moodle_data(self.student.student_id)
338        self.assertEqual(result,
339            {'lastname': 'Tester', 'email': 'aa@aa.ng', 'firstname': 'Anna'})
340        REQUEST_XML = """\
341<?xml version="1.0"?>
342<methodCall>
343<methodName>get_student_moodle_data</methodName>
344<params>
345<param>
346<value><string>K1000000</string></value>
347</param>
348</params>
349</methodCall>"""
350        RESPONSE_XML = """\
351<?xml version='1.0'?>
352<methodResponse>
353<params>
354<param>
355<value><struct>
356<member>
357<name>lastname</name>
358<value><string>Tester</string></value>
359</member>
360<member>
361<name>email</name>
362<value><string>aa@aa.ng</string></value>
363</member>
364<member>
365<name>firstname</name>
366<value><string>Anna</string></value>
367</member>
368</struct></value>
369</param>
370</params>
371</methodResponse>
372"""
373        xmlout = self.XMLRPC_post(REQUEST_XML)
374        self.assertEqual(xmlout, RESPONSE_XML)
375        return
Note: See TracBrowser for help on using the repository browser.