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

Last change on this file since 10035 was 10035, checked in by Henrik Bettermann, 12 years ago

Inherit test class from StudentsFullSetup?. We need this for all the upcoming web service methods.

Add test which posts an xml body.

File size: 2.5 KB
Line 
1# Tests for webservices
2import xmlrpclib
3import httplib
4import urllib
5import base64
6from zope.app.testing.xmlrpc import ServerProxy
7from zope.component import createObject
8from zope.component.hooks import setSite
9from zope.testbrowser.testing import Browser
10from waeup.kofa.app import University
11from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
12from waeup.kofa.students.tests.test_browser import StudentsFullSetup
13
14class 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)
Note: See TracBrowser for help on using the repository browser.