1 | # Tests for webservices |
---|
2 | import xmlrpclib |
---|
3 | import os |
---|
4 | from cStringIO import StringIO |
---|
5 | from zope.app.testing.xmlrpc import ServerProxy |
---|
6 | from zope.component import getUtility |
---|
7 | from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser |
---|
8 | from waeup.kofa.testing import FunctionalLayer |
---|
9 | from waeup.kofa.students.payments import StudentOnlinePayment |
---|
10 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
11 | from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket |
---|
12 | |
---|
13 | |
---|
14 | class 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 | payment.p_category = u'schoolfee' |
---|
180 | self.student['payments']['my-payment'] = payment |
---|
181 | result = server.get_students_by_course('CRS1') |
---|
182 | self.assertEqual(result, [['K1000000', '234', 'my adviser', 12.12], ]) |
---|
183 | REQUEST_XML = """\ |
---|
184 | <?xml version="1.0"?> |
---|
185 | <methodCall> |
---|
186 | <methodName>get_students_by_course</methodName> |
---|
187 | <params> |
---|
188 | <param> |
---|
189 | <value><string>CRS1</string></value> |
---|
190 | <value><string>2012</string></value> |
---|
191 | </param> |
---|
192 | </params> |
---|
193 | </methodCall>""" |
---|
194 | RESPONSE_XML = """\ |
---|
195 | <?xml version='1.0'?> |
---|
196 | <methodResponse> |
---|
197 | <params> |
---|
198 | <param> |
---|
199 | <value><array><data> |
---|
200 | <value><array><data> |
---|
201 | <value><string>K1000000</string></value> |
---|
202 | <value><string>234</string></value> |
---|
203 | <value><string>my adviser</string></value> |
---|
204 | <value><double>12.12</double></value> |
---|
205 | </data></array></value> |
---|
206 | </data></array></value> |
---|
207 | </param> |
---|
208 | </params> |
---|
209 | </methodResponse> |
---|
210 | """ |
---|
211 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
212 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
213 | return |
---|
214 | |
---|
215 | def test_get_student_info(self): |
---|
216 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
217 | self.setup_student(self.student) |
---|
218 | result = server.get_student_info('123') |
---|
219 | self.assertEqual(result, |
---|
220 | ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng']) |
---|
221 | REQUEST_XML = """\ |
---|
222 | <?xml version="1.0"?> |
---|
223 | <methodCall> |
---|
224 | <methodName>get_student_info</methodName> |
---|
225 | <params> |
---|
226 | <param> |
---|
227 | <value><string>K1000000</string></value> |
---|
228 | </param> |
---|
229 | </params> |
---|
230 | </methodCall>""" |
---|
231 | RESPONSE_XML = """\ |
---|
232 | <?xml version='1.0'?> |
---|
233 | <methodResponse> |
---|
234 | <params> |
---|
235 | <param> |
---|
236 | <value><array><data> |
---|
237 | <value><string>Anna Tester</string></value> |
---|
238 | <value><string>CERT1</string></value> |
---|
239 | <value><string>1234</string></value> |
---|
240 | <value><string>aa@aa.ng</string></value> |
---|
241 | </data></array></value> |
---|
242 | </param> |
---|
243 | </params> |
---|
244 | </methodResponse> |
---|
245 | """ |
---|
246 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
247 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
248 | return |
---|
249 | |
---|
250 | def test_get_student_passport(self): |
---|
251 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
252 | self.setup_student(self.student) |
---|
253 | self.create_passport_img(self.student) |
---|
254 | result = server.get_student_passport('123') |
---|
255 | img = getUtility(IExtFileStore).getFileByContext( |
---|
256 | self.student, attr='passport.jpg') |
---|
257 | binary = img.read() |
---|
258 | self.assertEqual(binary, result) |
---|
259 | REQUEST_XML = """\ |
---|
260 | <?xml version="1.0"?> |
---|
261 | <methodCall> |
---|
262 | <methodName>get_student_passport</methodName> |
---|
263 | <params> |
---|
264 | <param> |
---|
265 | <value><string>K1000000</string></value> |
---|
266 | </param> |
---|
267 | </params> |
---|
268 | </methodCall>""" |
---|
269 | RESPONSE_XML = """\ |
---|
270 | <?xml version='1.0'?> |
---|
271 | <methodResponse> |
---|
272 | <params> |
---|
273 | <param> |
---|
274 | <value><base64> |
---|
275 | /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b |
---|
276 | """ |
---|
277 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
278 | self.assertTrue(xmlout.startswith(RESPONSE_XML)) |
---|
279 | |
---|
280 | def test_get_paid_sessions(self): |
---|
281 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
282 | self.setup_student(self.student) |
---|
283 | payment = StudentOnlinePayment() |
---|
284 | payment.p_id = 'my-id' |
---|
285 | payment.p_session = 2009 |
---|
286 | payment.amount_auth = 12.12 |
---|
287 | payment.p_state = u'paid' |
---|
288 | payment.p_category = u'schoolfee' |
---|
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 |
---|
320 | |
---|
321 | def test_check_student_credentials(self): |
---|
322 | # make sure we can get student infos providing valid creds |
---|
323 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
324 | self.setup_student(self.student) |
---|
325 | stud_id = self.student.student_id |
---|
326 | result = server.check_student_credentials(stud_id, 'spwd') |
---|
327 | self.assertEqual( |
---|
328 | result, { |
---|
329 | 'description': 'Anna Tester', |
---|
330 | 'email': 'aa@aa.ng', |
---|
331 | 'id': 'K1000000', |
---|
332 | 'type': 'student'} |
---|
333 | ) |
---|
334 | return |
---|
335 | |
---|
336 | def test_get_student_moodle_data(self): |
---|
337 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
338 | self.setup_student(self.student) |
---|
339 | result = server.get_student_moodle_data(self.student.student_id) |
---|
340 | self.assertEqual(result, |
---|
341 | {'lastname': 'Tester', 'email': 'aa@aa.ng', 'firstname': 'Anna'}) |
---|
342 | REQUEST_XML = """\ |
---|
343 | <?xml version="1.0"?> |
---|
344 | <methodCall> |
---|
345 | <methodName>get_student_moodle_data</methodName> |
---|
346 | <params> |
---|
347 | <param> |
---|
348 | <value><string>K1000000</string></value> |
---|
349 | </param> |
---|
350 | </params> |
---|
351 | </methodCall>""" |
---|
352 | RESPONSE_XML = """\ |
---|
353 | <?xml version='1.0'?> |
---|
354 | <methodResponse> |
---|
355 | <params> |
---|
356 | <param> |
---|
357 | <value><struct> |
---|
358 | <member> |
---|
359 | <name>lastname</name> |
---|
360 | <value><string>Tester</string></value> |
---|
361 | </member> |
---|
362 | <member> |
---|
363 | <name>email</name> |
---|
364 | <value><string>aa@aa.ng</string></value> |
---|
365 | </member> |
---|
366 | <member> |
---|
367 | <name>firstname</name> |
---|
368 | <value><string>Anna</string></value> |
---|
369 | </member> |
---|
370 | </struct></value> |
---|
371 | </param> |
---|
372 | </params> |
---|
373 | </methodResponse> |
---|
374 | """ |
---|
375 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
376 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
377 | return |
---|