Changeset 10035


Ignore:
Timestamp:
18 Mar 2013, 09:05:35 (12 years ago)
Author:
Henrik Bettermann
Message:

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

Add test which posts an xml body.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/students/tests/test_webservices.py

    r10034 r10035  
    11# Tests for webservices
    22import xmlrpclib
     3import httplib
     4import urllib
     5import base64
    36from zope.app.testing.xmlrpc import ServerProxy
    47from zope.component import createObject
     
    710from waeup.kofa.app import University
    811from waeup.kofa.testing import FunctionalLayer, FunctionalTestCase
     12from waeup.kofa.students.tests.test_browser import StudentsFullSetup
    913
    10 class XMLRPCTests(FunctionalTestCase):
     14class XMLRPCTests(StudentsFullSetup):
    1115    # check XMLRPC services for university portal
    1216
    1317    layer = FunctionalLayer
    1418
    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 
    3219    def test_get_student_id_no_match(self):
    3320        # w/o any students we get none
    3421        server = ServerProxy('http://mgr:mgrpw@localhost/app')
    35         result = server.get_student_id('R123')
     22        result = server.get_student_id('Nonsense')
    3623        self.assertTrue(result is None)
    3724        return
     
    3926    def test_get_student_id_regno_exists(self):
    4027        # we can get the id of an existing student with matching reg_no
    41         stud1 = self.create_student(reg_no=u'R123')
    4228        server = ServerProxy('http://mgr:mgrpw@localhost/app')
    43         result = server.get_student_id('R123')
     29        result = server.get_student_id('123')
    4430        self.assertEqual(result, 'K1000000')
    45         self.assertEqual(stud1.student_id, result)
     31        self.assertEqual(self.student_id, result)
    4632        return
    4733
     
    5137        server = ServerProxy('http://localhost/app')
    5238        self.assertRaises(
    53             xmlrpclib.ProtocolError, server.get_student_id, 'R123')
     39            xmlrpclib.ProtocolError, server.get_student_id, '123')
    5440        # wrong password
    5541        server = ServerProxy('http://mgr:WRONGPW@localhost/app')
    5642        self.assertRaises(
    57             xmlrpclib.ProtocolError, server.get_student_id, 'R123')
     43            xmlrpclib.ProtocolError, server.get_student_id, '123')
    5844        # wrong username
    5945        server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app')
    6046        self.assertRaises(
    61             xmlrpclib.ProtocolError, server.get_student_id, 'R123')
     47            xmlrpclib.ProtocolError, server.get_student_id, '123')
    6248        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 TracChangeset for help on using the changeset viewer.