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 | 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_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_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 |
---|