New Ticket

TICKET_CREATE privileges are required to perform this operation. You don't have the required permissions



Login Close

File xml_rpc.py

bloodhound_rpc   tip

Last change on this file was 168:5bd64a1ea661, checked in by Olemis Lang <olemis+trac@…>, 7 months ago

BH RPC : Updated XML-RPC error codes

File size: 5.1 KB

Line 
1# -*- coding: utf-8 -*-
2"""
3License: BSD
4
5(c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
6"""
7
8from tracrpc.tests import unittest
9
10import xmlrpclib
11
12from tracrpc.tests import TracRpcTestCase
13
14class RpcXmlTestCase(TracRpcTestCase):
15   
16    def setUp(self):
17        TracRpcTestCase.setUp(self)
18        if not getattr(self._tester, 'xmlrpclib_compat', False):
19            raise unittest.SkipTest('Protocol not compatible with xmlrpclib')
20        self.anon = self._tester.server_proxy(self.url_anon)
21        self.user = self._tester.server_proxy(self.url_auth, user='user')
22        self.admin = self._tester.server_proxy(self.url_auth, user='admin')
23
24    def tearDown(self):
25        TracRpcTestCase.tearDown(self)
26
27    def test_xmlrpc_permission(self):
28        # Test returned response if not XML_RPC permission
29        self._testenv._tracadmin('permission', 'remove', 'anonymous',
30                                'XML_RPC', wait=True)
31        e = self.assertRaises(xmlrpclib.Fault,
32                    self.anon.system.listMethods)
33        self.assertEquals(403, e.faultCode)
34        self.assertTrue('XML_RPC' in e.faultString)
35        self._testenv._tracadmin('permission', 'add', 'anonymous',
36                                        'XML_RPC', wait=True)
37
38    def test_method_not_found(self):
39        def local_test():
40            self.admin.system.doesNotExist()
41            self.fail("What? Method exists???")
42        e = self.assertRaises(xmlrpclib.Fault, local_test)
43        self.assertEquals(-32601, e.faultCode)
44        self.assertTrue("not found" in e.faultString)
45
46    def test_wrong_argspec(self):
47        def local_test():
48            self.admin.system.listMethods("hello")
49            self.fail("Oops. Wrong argspec accepted???")
50        e = self.assertRaises(xmlrpclib.Fault, local_test)
51        self.assertEquals(-32602, e.faultCode)
52        self.assertTrue("listMethods() takes exactly 2 arguments" \
53                                    in e.faultString)
54
55    def test_content_encoding(self):
56        test_string = "øæåØÆÅàéüoö"
57        # No encoding / encoding error
58        def local_test():
59            t_id = self.admin.ticket.create(test_string, test_string[::-1], {})
60            self.admin.ticket.delete(t_id)
61            self.fail("Expected ticket create to fail...")
62        e = self.assertRaises(xmlrpclib.Fault, local_test)
63        self.assertTrue(isinstance(e, xmlrpclib.Fault))
64        self.assertEquals(-32700, e.faultCode)
65        # Unicode version (encodable)
66        from trac.util.text import to_unicode
67        test_string = to_unicode(test_string)
68        t_id = self.admin.ticket.create(test_string, test_string[::-1], {})
69        self.assertTrue(t_id > 0)
70        result = self.admin.ticket.get(t_id)
71        self.assertEquals(result[0], t_id)
72        self.assertEquals(result[3]['summary'], test_string)
73        self.assertEquals(result[3]['description'], test_string[::-1])
74        self.assertEquals(unicode, type(result[3]['summary']))
75        self.admin.ticket.delete(t_id)
76
77    def test_to_and_from_datetime(self):
78        from datetime import datetime
79        from trac.util.datefmt import to_datetime, utc
80        from tracrpc.xml_rpc import to_xmlrpc_datetime, from_xmlrpc_datetime
81        now = to_datetime(None, utc)
82        now_timetuple = now.timetuple()[:6]
83        xmlrpc_now = to_xmlrpc_datetime(now)
84        self.assertTrue(isinstance(xmlrpc_now, xmlrpclib.DateTime),
85                "Expected xmlprc_now to be an xmlrpclib.DateTime")
86        self.assertEquals(str(xmlrpc_now), now.strftime("%Y%m%dT%H:%M:%S"))
87        now_from_xmlrpc = from_xmlrpc_datetime(xmlrpc_now)
88        self.assertTrue(isinstance(now_from_xmlrpc, datetime),
89                "Expected now_from_xmlrpc to be a datetime")
90        self.assertEquals(now_from_xmlrpc.timetuple()[:6], now_timetuple)
91        self.assertEquals(now_from_xmlrpc.tzinfo, utc)
92
93    def test_resource_not_found(self):
94        # A Ticket resource
95        e = self.assertRaises(xmlrpclib.Fault, self.admin.ticket.get, 2147483647)
96        self.assertEquals(e.faultCode, 404)
97        self.assertEquals(e.faultString, 
98                'Ticket 2147483647 does not exist.')
99        # A Wiki resource (http://www.ecyrd.com/JSPWiki/wiki/WikiRPCInterface2)
100        e = self.assertRaises(xmlrpclib.Fault, self.admin.wiki.getPage, "Test", 10)
101        self.assertEquals(e.faultCode, 1)
102        self.assertEquals(e.faultString,
103                'Wiki page "Test" does not exist at version 10')
104
105    def test_xml_encoding_special_characters(self):
106        tid1 = self.admin.ticket.create(
107                                'One & Two < Four', 'Desc & ription', {})
108        ticket = self.admin.ticket.get(tid1)
109        self.assertEquals('One & Two < Four', ticket[3]['summary'])
110        self.assertEquals('Desc & ription', ticket[3]['description'])
111        self.admin.ticket.delete(tid1)
112
113def test_suite(suite=None):
114    if suite is None:
115        from tracrpc.tests import functionalSuite
116        suite = functionalSuite()
117    # FIXME: Improve
118    ts = unittest.makeSuite(RpcXmlTestCase)
119    for test in ts._tests:
120        suite.addTest(test)
121    return suite
122
123
124if __name__ == '__main__':
125    unittest.main(defaultTest='test_suite')

Note See TracBrowser for help on using the repository browser.