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

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

Add another test which checks the xml response.

File size: 4.7 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
9from waeup.kofa.students.tests.test_browser import StudentsFullSetup
10from waeup.kofa.students.studylevel import StudentStudyLevel, CourseTicket
11
12class XMLRPCTests(StudentsFullSetup):
13    # check XMLRPC services for university portal
14
15    layer = FunctionalLayer
16
17    def setup_student(self, student):
18        study_level = StudentStudyLevel()
19        study_level.level_session = 2012
20        study_level.level_verdict = "A"
21        study_level.level = 100
22        student['studycourse'].addStudentStudyLevel(
23            self.certificate, study_level)
24
25        ticket = CourseTicket()
26        ticket.automatic = True
27        ticket.carry_over = True
28        ticket.code = u'CRS1'
29        ticket.title = u'Course 1'
30        ticket.fcode = u'FAC1'
31        ticket.dcode = u'DEP1'
32        ticket.credits = 100
33        ticket.passmark = 100
34        ticket.semester = 2
35        study_level[ticket.code] = ticket
36
37    def test_get_student_id_no_match(self):
38        # w/o any students we get none
39        server = ServerProxy('http://mgr:mgrpw@localhost/app')
40        result = server.get_student_id('Nonsense')
41        self.assertTrue(result is None)
42        return
43
44    def test_get_student_id_regno_exists(self):
45        # we can get the id of an existing student with matching reg_no
46        server = ServerProxy('http://mgr:mgrpw@localhost/app')
47        result = server.get_student_id('123')
48        self.assertEqual(result, 'K1000000')
49        self.assertEqual(self.student_id, result)
50        return
51
52    def test_get_student_id_block_unauthorized(self):
53        # requests from unauthorized users are blocked
54        # no username nor password
55        server = ServerProxy('http://localhost/app')
56        self.assertRaises(
57            xmlrpclib.ProtocolError, server.get_student_id, '123')
58        # wrong password
59        server = ServerProxy('http://mgr:WRONGPW@localhost/app')
60        self.assertRaises(
61            xmlrpclib.ProtocolError, server.get_student_id, '123')
62        # wrong username
63        server = ServerProxy('http://WRONGUSER:mgrpw@localhost/app')
64        self.assertRaises(
65            xmlrpclib.ProtocolError, server.get_student_id, '123')
66        return
67
68    def test_XMLRPC_post_1(self):
69        REQUEST_XML="""\
70<?xml version="1.0"?>
71<methodCall>
72<methodName>get_student_id</methodName>
73<params>
74<param>
75<value><string>123</string></value>
76</param>
77</params>
78</methodCall>"""
79        RESPONSE_XML="""\
80<?xml version='1.0'?>
81<methodResponse>
82<params>
83<param>
84<value><string>K1000000</string></value>
85</param>
86</params>
87</methodResponse>
88"""
89        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
90        self.browser.addHeader('Content-Length', len(REQUEST_XML))
91        self.browser.post('http://localhost/app', REQUEST_XML,
92            'text/xml; charset=utf-8')
93        self.assertEqual(self.browser.contents, RESPONSE_XML)
94        return
95
96    def test_get_courses_by_session(self):
97        server = ServerProxy('http://mgr:mgrpw@localhost/app')
98        result = server.get_courses_by_session('K1000000')
99        self.assertEqual(result, None)
100        self.setup_student(self.student)
101        result = server.get_courses_by_session('K1000000', '2010')
102        self.assertEqual(result, None)
103        result = server.get_courses_by_session('K1000000', '2012')
104        self.assertEqual(result,
105            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
106        result = server.get_courses_by_session('K1000000')
107        self.assertEqual(result,
108            {'100|CRS1': 'Course 1', '100|COURSE1': 'Unnamed Course'})
109        return
110
111    def test_XMLRPC_post_2(self):
112        self.setup_student(self.student)
113        REQUEST_XML="""\
114<?xml version="1.0"?>
115<methodCall>
116<methodName>get_courses_by_session</methodName>
117<params>
118<param>
119<value><string>K1000000</string></value>
120</param>
121</params>
122</methodCall>"""
123        RESPONSE_XML="""\
124<?xml version='1.0'?>
125<methodResponse>
126<params>
127<param>
128<value><struct>
129<member>
130<name>100|CRS1</name>
131<value><string>Course 1</string></value>
132</member>
133<member>
134<name>100|COURSE1</name>
135<value><string>Unnamed Course</string></value>
136</member>
137</struct></value>
138</param>
139</params>
140</methodResponse>
141"""
142        self.browser.addHeader('Authorization', 'Basic mgr:mgrpw')
143        self.browser.addHeader('Content-Length', len(REQUEST_XML))
144        self.browser.post('http://localhost/app', REQUEST_XML,
145            'text/xml; charset=utf-8')
146        self.assertEqual(self.browser.contents, RESPONSE_XML)
147        return
Note: See TracBrowser for help on using the repository browser.