1 | # Tests for webservices |
---|
2 | import xmlrpclib |
---|
3 | import httplib |
---|
4 | import urllib |
---|
5 | import base64 |
---|
6 | from zope.app.testing.xmlrpc import ServerProxy |
---|
7 | from zope.component import createObject |
---|
8 | from zope.component.hooks import setSite |
---|
9 | from zope.testbrowser.testing import Browser |
---|
10 | from waeup.kofa.app import University |
---|
11 | from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase |
---|
12 | from waeup.kofa.students.tests.test_browser import StudentsFullSetup |
---|
13 | |
---|
14 | class XMLRPCTests(StudentsFullSetup): |
---|
15 | # check XMLRPC services for university portal |
---|
16 | |
---|
17 | layer = FunctionalLayer |
---|
18 | |
---|
19 | def test_get_student_id_no_match(self): |
---|
20 | # w/o any students we get none |
---|
21 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
22 | result = server.get_student_id('Nonsense') |
---|
23 | self.assertTrue(result is None) |
---|
24 | return |
---|
25 | |
---|
26 | def test_get_student_id_regno_exists(self): |
---|
27 | # we can get the id of an existing student with matching reg_no |
---|
28 | server = ServerProxy('http://mgr:mgrpw@localhost/app') |
---|
29 | result = server.get_student_id('123') |
---|
30 | self.assertEqual(result, 'K1000000') |
---|
31 | self.assertEqual(self.student_id, result) |
---|
32 | return |
---|
33 | |
---|
34 | def test_get_student_id_block_unauthorized(self): |
---|
35 | # requests from unauthorized users are blocked |
---|
36 | # no username nor password |
---|
37 | server = ServerProxy('http://localhost/app') |
---|
38 | self.assertRaises( |
---|
39 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
40 | # wrong password |
---|
41 | server = ServerProxy('http://mgr:WRONGPW@localhost/app') |
---|
42 | self.assertRaises( |
---|
43 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
44 | # wrong username |
---|
45 | server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app') |
---|
46 | self.assertRaises( |
---|
47 | xmlrpclib.ProtocolError, server.get_student_id, '123') |
---|
48 | return |
---|
49 | |
---|
50 | REQUEST_XML="""\ |
---|
51 | <?xml version="1.0"?> |
---|
52 | <methodCall> |
---|
53 | <methodName>get_student_id</methodName> |
---|
54 | <params> |
---|
55 | <param> |
---|
56 | <value><string>123</string></value> |
---|
57 | </param> |
---|
58 | </params> |
---|
59 | </methodCall>""" |
---|
60 | |
---|
61 | RESPONSE_XML="""\ |
---|
62 | <?xml version='1.0'?> |
---|
63 | <methodResponse> |
---|
64 | <params> |
---|
65 | <param> |
---|
66 | <value><string>K1000000</string></value> |
---|
67 | </param> |
---|
68 | </params> |
---|
69 | </methodResponse> |
---|
70 | """ |
---|
71 | |
---|
72 | def test_XMLRPC_post(self): |
---|
73 | self.browser.addHeader('Authorization', 'Basic mgr:mgrpw') |
---|
74 | self.browser.addHeader('Content-Length', len(self.REQUEST_XML)) |
---|
75 | self.browser.post('http://localhost/app', self.REQUEST_XML, |
---|
76 | 'text/xml; charset=utf-8') |
---|
77 | self.assertEqual(self.browser.contents, self.RESPONSE_XML) |
---|