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 createObject, getUtility |
---|
7 | from zope.component.hooks import setSite |
---|
8 | from zope.testbrowser.testing import Browser |
---|
9 | from waeup.kofa.app import University |
---|
10 | from waeup.kofa.interfaces import IExtFileStore, IFileStoreNameChooser |
---|
11 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
12 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
13 | from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket |
---|
14 | |
---|
15 | class XMLRPCTests(StudentsFullSetup): |
---|
16 | # check XMLRPC services for university portal |
---|
17 | |
---|
18 | layer = FunctionalLayer |
---|
19 | |
---|
20 | def setup_student(self, student): |
---|
21 | study_level = StudentStudyLevel() |
---|
22 | study_level.level_session = 2012 |
---|
23 | study_level.level_verdict = "A" |
---|
24 | study_level.level = 100 |
---|
25 | study_level.validated_by = u"my adviser" |
---|
26 | student['studycourse'].addStudentStudyLevel( |
---|
27 | self.certificate, study_level) |
---|
28 | |
---|
29 | ticket = CourseTicket() |
---|
30 | ticket.automatic = True |
---|
31 | ticket.carry_over = True |
---|
32 | ticket.code = u'CRS1' |
---|
33 | ticket.title = u'Course 1' |
---|
34 | ticket.fcode = u'FAC1' |
---|
35 | ticket.dcode = u'DEP1' |
---|
36 | ticket.credits = 100 |
---|
37 | ticket.passmark = 100 |
---|
38 | ticket.semester = 2 |
---|
39 | study_level[ticket.code] = ticket |
---|
40 | |
---|
41 | def create_passport_img(self, student): |
---|
42 | # create some passport file for `student` |
---|
43 | storage = getUtility(IExtFileStore) |
---|
44 | image_path = os.path.join(os.path.dirname(__file__), 'test_image.jpg') |
---|
45 | self.image_contents = open(image_path, 'rb').read() |
---|
46 | file_id = IFileStoreNameChooser(student).chooseName( |
---|
47 | attr='passport.jpg') |
---|
48 | storage.createFile(file_id, StringIO(self.image_contents)) |
---|
49 | |
---|
50 | def XMLRPC_post(self, body): |
---|
51 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
52 | self.browser.addHeader('Content-Length', len(body)) |
---|
53 | self.browser.post('http://localhost/app', body, |
---|
54 | 'text/xml; charset=utf-8') |
---|
55 | return self.browser.contents |
---|
56 | |
---|
57 | def test_get_student_id_no_match(self): |
---|
58 | # w/o any students we get none |
---|
59 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
60 | result = server.get_student_id('Nonsense') |
---|
61 | self.assertTrue(result is None) |
---|
62 | return |
---|
63 | |
---|
64 | def test_get_student_id_regno_exists(self): |
---|
65 | # we can get the id of an existing student with matching reg_no |
---|
66 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
67 | result = server.get_student_id('123') |
---|
68 | self.assertEqual(result, 'K1000000') |
---|
69 | self.assertEqual(self.student_id, result) |
---|
70 | REQUEST_XML="""\ |
---|
71 | <?xml version="1.0"?> |
---|
72 | <methodCall> |
---|
73 | <methodName>get_student_id</methodName> |
---|
74 | <params> |
---|
75 | <param> |
---|
76 | <value><string>123</string></value> |
---|
77 | </param> |
---|
78 | </params> |
---|
79 | </methodCall>""" |
---|
80 | RESPONSE_XML="""\ |
---|
81 | <?xml version='1.0'?> |
---|
82 | <methodResponse> |
---|
83 | <params> |
---|
84 | <param> |
---|
85 | <value><string>K1000000</string></value> |
---|
86 | </param> |
---|
87 | </params> |
---|
88 | </methodResponse> |
---|
89 | """ |
---|
90 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
91 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
92 | return |
---|
93 | |
---|
94 | def test_get_student_id_block_unauthorized(self): |
---|
95 | # requests from unauthorized users are blocked |
---|
96 | # no username nor password |
---|
97 | server = ServerProxy('http://localhost/app') |
---|
98 | self.assertRaises( |
---|
99 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
100 | # wrong password |
---|
101 | server = ServerProxy('http://mgr:WRONGPW@localhost/app') |
---|
102 | self.assertRaises( |
---|
103 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
104 | # wrong username |
---|
105 | server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app') |
---|
106 | self.assertRaises( |
---|
107 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
108 | return |
---|
109 | |
---|
110 | def test_get_courses_by_session(self): |
---|
111 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
112 | result = server.get_courses_by_session('K1000000') |
---|
113 | self.assertEqual(result, None) |
---|
114 | self.setup_student(self.student) |
---|
115 | result = server.get_courses_by_session('K1000000', '2010') |
---|
116 | self.assertEqual(result, None) |
---|
117 | result = server.get_courses_by_session('K1000000', '2012') |
---|
118 | self.assertEqual(result, |
---|
119 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
120 | result = server.get_courses_by_session('K1000000') |
---|
121 | self.assertEqual(result, |
---|
122 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
123 | # Also matric_number ... |
---|
124 | result = server.get_courses_by_session('234') |
---|
125 | self.assertEqual(result, |
---|
126 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
127 | # ... or reg_number can be used. |
---|
128 | result = server.get_courses_by_session('123') |
---|
129 | self.assertEqual(result, |
---|
130 | {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'}) |
---|
131 | result = server.get_courses_by_session('Nonsense') |
---|
132 | self.assertEqual(result, None) |
---|
133 | REQUEST_XML="""\ |
---|
134 | <?xml version="1.0"?> |
---|
135 | <methodCall> |
---|
136 | <methodName>get_courses_by_session</methodName> |
---|
137 | <params> |
---|
138 | <param> |
---|
139 | <value><string>K1000000</string></value> |
---|
140 | </param> |
---|
141 | </params> |
---|
142 | </methodCall>""" |
---|
143 | RESPONSE_XML="""\ |
---|
144 | <?xml version='1.0'?> |
---|
145 | <methodResponse> |
---|
146 | <params> |
---|
147 | <param> |
---|
148 | <value><struct> |
---|
149 | <member> |
---|
150 | <name>100|CRS1</name> |
---|
151 | <value><string>Course 1</string></value> |
---|
152 | </member> |
---|
153 | <member> |
---|
154 | <name>100|COURSE1</name> |
---|
155 | <value><string>Unnamed Course</string></value> |
---|
156 | </member> |
---|
157 | </struct></value> |
---|
158 | </param> |
---|
159 | </params> |
---|
160 | </methodResponse> |
---|
161 | """ |
---|
162 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
163 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
164 | return |
---|
165 | |
---|
166 | def test_get_students_by_course(self): |
---|
167 | self.setup_student(self.student) |
---|
168 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
169 | result = server.get_students_by_course('CRS1', '2010') |
---|
170 | self.assertEqual(result, None) |
---|
171 | result = server.get_students_by_course('CRS1', '2012') |
---|
172 | self.assertEqual(result, [['K1000000', '234', 'my adviser'],]) |
---|
173 | result = server.get_students_by_course('CRS1') |
---|
174 | self.assertEqual(result, [['K1000000', '234', 'my adviser'],]) |
---|
175 | REQUEST_XML="""\ |
---|
176 | <?xml version="1.0"?> |
---|
177 | <methodCall> |
---|
178 | <methodName>get_students_by_course</methodName> |
---|
179 | <params> |
---|
180 | <param> |
---|
181 | <value><string>CRS1</string></value> |
---|
182 | <value><string>2012</string></value> |
---|
183 | </param> |
---|
184 | </params> |
---|
185 | </methodCall>""" |
---|
186 | RESPONSE_XML="""\ |
---|
187 | <?xml version='1.0'?> |
---|
188 | <methodResponse> |
---|
189 | <params> |
---|
190 | <param> |
---|
191 | <value><array><data> |
---|
192 | <value><array><data> |
---|
193 | <value><string>K1000000</string></value> |
---|
194 | <value><string>234</string></value> |
---|
195 | <value><string>my adviser</string></value> |
---|
196 | </data></array></value> |
---|
197 | </data></array></value> |
---|
198 | </param> |
---|
199 | </params> |
---|
200 | </methodResponse> |
---|
201 | """ |
---|
202 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
203 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
204 | return |
---|
205 | |
---|
206 | def test_get_student_info(self): |
---|
207 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
208 | self.setup_student(self.student) |
---|
209 | result = server.get_student_info('123') |
---|
210 | self.assertEqual(result, |
---|
211 | ['Anna Tester', 'CERT1', '1234', 'aa@aa.ng']) |
---|
212 | REQUEST_XML="""\ |
---|
213 | <?xml version="1.0"?> |
---|
214 | <methodCall> |
---|
215 | <methodName>get_student_info</methodName> |
---|
216 | <params> |
---|
217 | <param> |
---|
218 | <value><string>K1000000</string></value> |
---|
219 | </param> |
---|
220 | </params> |
---|
221 | </methodCall>""" |
---|
222 | RESPONSE_XML="""\ |
---|
223 | <?xml version='1.0'?> |
---|
224 | <methodResponse> |
---|
225 | <params> |
---|
226 | <param> |
---|
227 | <value><array><data> |
---|
228 | <value><string>Anna Tester</string></value> |
---|
229 | <value><string>CERT1</string></value> |
---|
230 | <value><string>1234</string></value> |
---|
231 | <value><string>aa@aa.ng</string></value> |
---|
232 | </data></array></value> |
---|
233 | </param> |
---|
234 | </params> |
---|
235 | </methodResponse> |
---|
236 | """ |
---|
237 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
238 | self.assertEqual(xmlout, RESPONSE_XML) |
---|
239 | return |
---|
240 | |
---|
241 | def test_get_student_passport(self): |
---|
242 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
243 | self.setup_student(self.student) |
---|
244 | self.create_passport_img(self.student) |
---|
245 | result = server.get_student_passport('123') |
---|
246 | img = getUtility(IExtFileStore).getFileByContext( |
---|
247 | self.student, attr='passport.jpg') |
---|
248 | binary = img.read() |
---|
249 | self.assertEqual(binary, result) |
---|
250 | REQUEST_XML="""\ |
---|
251 | <?xml version="1.0"?> |
---|
252 | <methodCall> |
---|
253 | <methodName>get_student_passport</methodName> |
---|
254 | <params> |
---|
255 | <param> |
---|
256 | <value><string>K1000000</string></value> |
---|
257 | </param> |
---|
258 | </params> |
---|
259 | </methodCall>""" |
---|
260 | RESPONSE_XML="""\ |
---|
261 | <?xml version='1.0'?> |
---|
262 | <methodResponse> |
---|
263 | <params> |
---|
264 | <param> |
---|
265 | <value><base64> |
---|
266 | /9j/4AAQSkZJRgABAgAAZABkAAD/7AARRHVja3kAAQAEAAAAPAAA/+4ADkFkb2JlAGTAAAAAAf/b |
---|
267 | """ |
---|
268 | xmlout = self.XMLRPC_post(REQUEST_XML) |
---|
269 | self.assertTrue(xmlout.startswith(RESPONSE_XML)) |
---|