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

Last change on this file since 9798 was 9632, checked in by uli, 12 years ago

Provide XMLRPC services. We start with a student_id service.

File size: 2.3 KB
Line 
1# Tests for webservices
2import xmlrpclib
3from zope.app.testing.xmlrpc import ServerProxy
4from zope.component import createObject
5from zope.component.hooks import setSite
6from zope.testbrowser.testing import Browser
7from waeup.kofa.app import University
8from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
9
10class XMLRPCTests(FunctionalTestCase):
11    # check XMLRPC services for university portal
12
13    layer = FunctionalLayer
14
15    def setUp(self):
16        super(XMLRPCTests, self).setUp()
17        # create a site
18        self.root = self.getRootFolder()
19        self.app = University()
20        self.root['app'] = self.app
21        return
22
23    def create_student(self, reg_no=u'REG123'):
24        # create a student in site
25        setSite(self.app) # needed to create a student
26        stud = createObject('waeup.Student')
27        stud.reg_number = reg_no
28        self.app['students'].addStudent(stud)
29        setSite(None) # make sure no site is set when returning
30        return stud
31
32    def test_get_student_id_no_match(self):
33        # w/o any students we get none
34        server = ServerProxy('http://mgr:mgrpw@localhost/app')
35        result = server.get_student_id('R123')
36        self.assertTrue(result is None)
37        return
38
39    def test_get_student_id_regno_exists(self):
40        # we can get the id of an existing student with matching reg_no
41        stud1 = self.create_student(reg_no=u'R123')
42        server = ServerProxy('http://mgr:mgrpw@localhost/app')
43        result = server.get_student_id('R123')
44        self.assertEqual(result, 'K1000000')
45        self.assertEqual(stud1.student_id, result)
46        return
47
48    def test_get_student_id_block_unauthorized(self):
49        # requests from unauthorized users are blocked
50        # no username nor password
51        server = ServerProxy('http://localhost/app')
52        self.assertRaises(
53            xmlrpclib.ProtocolError, server.get_student_id, 'R123')
54        # wrong password
55        server = ServerProxy('http://mgr:WRONGPW@localhost/app')
56        self.assertRaises(
57            xmlrpclib.ProtocolError, server.get_student_id, 'R123')
58        # wrong username
59        server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app')
60        self.assertRaises(
61            xmlrpclib.ProtocolError, server.get_student_id, 'R123')
62        return
Note: See TracBrowser for help on using the repository browser.