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

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

Add web service method which gets the ids and matric numbers of students registered for a certain course in a certain session.

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