Commits

Olemis Lang committed 1e7bdb6

TracRpc: API v2: Restarted. Base TestCase + `test_suite` + all `api.py` test cases PASS

Comments (0)

Files changed (1)

t5437/t5437-protocol_api_v2-r7194.diff

 RPC Protocol API version 2
 
-diff -r 203e791a70bf trunk/setup.py
---- a/trunk/setup.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/setup.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/setup.py
+--- a/trunk/setup.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/setup.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -28,7 +28,7 @@
      url='http://trac-hacks.org/wiki/XmlRpcPlugin',
      description='RPC interface to Trac',
      tests_require = test_deps,
      packages=find_packages(exclude=['*.tests']),
      package_data={
-diff -r 203e791a70bf trunk/tracrpc/api.py
---- a/trunk/tracrpc/api.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/api.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/tracrpc/api.py
+--- a/trunk/tracrpc/api.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/api.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -21,13 +21,34 @@
      """ RPC Binary type. Currently == xmlrpclib.Binary. """
      pass
  
  class IXMLRPCHandler(Interface):
  
-diff -r 203e791a70bf trunk/tracrpc/json_rpc.py
---- a/trunk/tracrpc/json_rpc.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/json_rpc.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/tracrpc/json_rpc.py
+--- a/trunk/tracrpc/json_rpc.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/json_rpc.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -5,8 +5,9 @@
  (c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
  """
              return self._json_error(e, r_id=r_id)
  
      def _json_error(self, e, c=None, r_id=None):
-diff -r 203e791a70bf trunk/tracrpc/tests/__init__.py
---- a/trunk/tracrpc/tests/__init__.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/tests/__init__.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/tracrpc/tests/__init__.py
+--- a/trunk/tracrpc/tests/__init__.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/tests/__init__.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -35,6 +35,7 @@
              print "Enabling RPC plugin and permissions..."
              env.config.set('components', 'tracrpc.*', 'enabled')
 +                raise self.failureException, "Expected %s\n\nNothing raised" % excName
 +
 +        assertRaises = failUnlessRaises
-diff -r 203e791a70bf trunk/tracrpc/tests/api.py
---- a/trunk/tracrpc/tests/api.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/tests/api.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/tracrpc/tests/api.py
+--- a/trunk/tracrpc/tests/api.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/tests/api.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -9,14 +9,14 @@
  import unittest
  import urllib2
  
      def test_invalid_content_type(self):
          req = urllib2.Request(rpc_testenv.url_anon,
-@@ -47,13 +47,18 @@
+@@ -47,16 +47,18 @@
              "class DummyProvider(Component):\n"
              "    implements(IRPCProtocol)\n"
              "    def rpc_info(self):\n"
 +            "        req.send_error(None, template='', content_type=rpcreq['mimetype'],\n"
 +            "                       status=500, env=None, data='Test failure ')\n"
 +            "    def send_rpc_result(self, req, rpcreq, result):\n"
++            "        # raise KeyError('Here')\n"
              "        response = 'Got a result!'\n"
-             "        req.send_response(200)\n"
+-            "        req.send_response(200)\n"
 -            "        req.send_header('Content-Type', content_type)\n"
-+            "        req.send_header('Content-Type', rpcreq['mimetype'])\n"
-             "        req.send_header('Content-Length', len(response))\n"
-             "        req.end_headers()\n"
-             "        req.write(response)\n")
-@@ -80,10 +85,21 @@
+-            "        req.send_header('Content-Length', len(response))\n"
+-            "        req.end_headers()\n"
+-            "        req.write(response)\n")
++            "        req.send(response, rpcreq['mimetype'], 200)\n")
+         rpc_testenv.restart()
+         try:
+             req = urllib2.Request(rpc_testenv.url_anon,
+@@ -65,7 +67,7 @@
+             self.assertEquals(200, resp.code)
+             self.assertEquals("Got a result!", resp.read())
+             self.assertEquals(resp.headers['Content-Type'],
+-                                                'application/x-tracrpc-test')
++                                                'application/x-tracrpc-test;charset=utf-8')
+         finally:
+             # Clean up so that provider don't affect further tests
+             os.unlink(provider)
+@@ -80,10 +82,21 @@
              "class DummyProvider(Component):\n"
              "    implements(IRPCProtocol)\n"
              "    def rpc_info(self):\n"
              "        raise RPCError('No good.')")
          rpc_testenv.restart()
          # Make the request
-@@ -100,8 +116,8 @@
+@@ -100,8 +113,8 @@
              os.unlink(provider)
              rpc_testenv.restart()
  
  if __name__ == '__main__':
 -    unittest.main(defaultTest='suite')
 +    unittest.main(defaultTest='test_suite')
-diff -r 203e791a70bf trunk/tracrpc/tests/json_rpc.py
---- a/trunk/tracrpc/tests/json_rpc.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/tests/json_rpc.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/tracrpc/tests/json_rpc.py
+--- a/trunk/tracrpc/tests/json_rpc.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/tests/json_rpc.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -14,9 +14,9 @@
  from tracrpc.util import StringIO
  
          
          def _anon_req(self, data):
              req = urllib2.Request(rpc_testenv.url_anon,
-@@ -38,12 +38,6 @@
-             resp = urllib2.build_opener(handler).open(req)
-             return json.loads(resp.read())
- 
--        def setUp(self):
--            pass
--
--        def tearDown(self):
--            pass
--
-         def test_call(self):
-             result = self._anon_req(
-                     {'method': 'system.listMethods', 'params': [], 'id': 244})
-@@ -167,11 +161,11 @@
+@@ -167,11 +167,11 @@
              self.assertEquals(result['error']['message'],
                       'Wiki page "Test" does not exist at version 10')
  
  if __name__ == '__main__':
 -    unittest.main(defaultTest='suite')
 +    unittest.main(defaultTest='test_suite')
-diff -r 203e791a70bf trunk/tracrpc/tests/ticket.py
---- a/trunk/tracrpc/tests/ticket.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/tests/ticket.py	Wed Mar 17 16:05:57 2010 -0500
-@@ -12,11 +12,12 @@
+diff -r 8b6987624152 trunk/tracrpc/tests/ticket.py
+--- a/trunk/tracrpc/tests/ticket.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/tests/ticket.py	Thu Mar 18 12:50:41 2010 -0500
+@@ -12,9 +12,9 @@
  import shutil
  import time
  
 +class RpcTicketTestCase(TracRpcTestCase):
      
      def setUp(self):
-+        TracRpcTestCase.setUp(self)
          self.anon = xmlrpclib.ServerProxy(rpc_testenv.url_anon)
-         self.user = xmlrpclib.ServerProxy(rpc_testenv.url_user)
-         self.admin = xmlrpclib.ServerProxy(rpc_testenv.url_admin)
-@@ -124,8 +125,8 @@
+@@ -124,8 +124,8 @@
              self.admin.ticket.delete(tid1)
              self.admin.ticket.delete(tid2)
  
  if __name__ == '__main__':
 -    unittest.main(defaultTest='suite')
 +    unittest.main(defaultTest='test_suite')
-diff -r 203e791a70bf trunk/tracrpc/tests/wiki.py
---- a/trunk/tracrpc/tests/wiki.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/tests/wiki.py	Wed Mar 17 16:05:57 2010 -0500
-@@ -13,12 +13,13 @@
+diff -r 8b6987624152 trunk/tracrpc/tests/wiki.py
+--- a/trunk/tracrpc/tests/wiki.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/tests/wiki.py	Thu Mar 18 12:50:41 2010 -0500
+@@ -13,10 +13,10 @@
  
  from trac.util.compat import sorted
  
 +class RpcWikiTestCase(TracRpcTestCase):
      
      def setUp(self):
-+        TracRpcTestCase.setUp(self)
          self.anon = xmlrpclib.ServerProxy(rpc_testenv.url_anon)
-         self.user = xmlrpclib.ServerProxy(rpc_testenv.url_user)
-         self.admin = xmlrpclib.ServerProxy(rpc_testenv.url_admin)
-@@ -60,8 +61,8 @@
+@@ -60,8 +60,8 @@
          self.admin.wiki.deletePage('WikiOne')
          self.admin.wiki.deletePage('WikiTwo')
  
  if __name__ == '__main__':
 -    unittest.main(defaultTest='suite')
 +    unittest.main(defaultTest='test_suite')
-diff -r 203e791a70bf trunk/tracrpc/tests/xml_rpc.py
---- a/trunk/tracrpc/tests/xml_rpc.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/tests/xml_rpc.py	Wed Mar 17 16:05:57 2010 -0500
-@@ -9,11 +9,12 @@
+diff -r 8b6987624152 trunk/tracrpc/tests/xml_rpc.py
+--- a/trunk/tracrpc/tests/xml_rpc.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/tests/xml_rpc.py	Thu Mar 18 12:50:41 2010 -0500
+@@ -9,9 +9,9 @@
  
  import xmlrpclib
  
 +class RpcXmlTestCase(TracRpcTestCase):
      
      def setUp(self):
-+        TracRpcTestCase.setUp(self)
          self.anon = xmlrpclib.ServerProxy(rpc_testenv.url_anon)
-         self.user = xmlrpclib.ServerProxy(rpc_testenv.url_user)
-         self.admin = xmlrpclib.ServerProxy(rpc_testenv.url_admin)
-@@ -22,44 +23,48 @@
-         # Test returned response if not XML_RPC permission
-         rpc_testenv._tracadmin('permission', 'remove', 'anonymous',
-                                 'XML_RPC')
--        try:
-+
-+        def testcall():
-             self.anon.system.listMethods()
-             rpc_testenv._tracadmin('permission', 'add', 'anonymous',
-                                         'XML_RPC')
-             self.fail("Revoked permissions not taken effect???")
--        except xmlrpclib.Fault, e:
--            self.assertEquals(403, e.faultCode)
--            self.assertTrue('XML_RPC' in e.faultString)
--            rpc_testenv._tracadmin('permission', 'add', 'anonymous',
--                                        'XML_RPC')
-+
-+        e = self.assertRaises(xmlrpclib.Fault, testcall)
-+        self.assertEquals(403, e.faultCode)
-+        self.assertTrue('XML_RPC' in e.faultString)
-+        rpc_testenv._tracadmin('permission', 'add', 'anonymous',
-+                                    'XML_RPC')
- 
-     def test_method_not_found(self):
--        try:
-+        def testcall():
-             self.admin.system.doesNotExist()
-             self.fail("What? Method exists???")
--        except xmlrpclib.Fault, e:
--            self.assertEquals(-32601, e.faultCode)
--            self.assertTrue("not found" in e.faultString)
-+
-+        e = self.assertRaises(xmlrpclib.Fault, testcall)
-+        self.assertEquals(-32601, e.faultCode)
-+        self.assertTrue("not found" in e.faultString)
- 
-     def test_wrong_argspec(self):
--        try:
-+        def testcall():
-             self.admin.system.listMethods("hello")
-             self.fail("Oops. Wrong argspec accepted???")
--        except xmlrpclib.Fault, e:
--            self.assertEquals(1, e.faultCode)
--            self.assertTrue("listMethods() takes exactly 2 arguments" \
-+
-+        e = self.assertRaises(xmlrpclib.Fault, testcall)
-+        self.assertEquals(1, e.faultCode)
-+        self.assertTrue("listMethods() takes exactly 2 arguments" \
-                                     in e.faultString)
- 
-     def test_content_encoding(self):
-         test_string = "øæåØÆÅàéüoö"
-         # No encoding / encoding error
--        try:
-+        def testcall():
-             t_id = self.admin.ticket.create(test_string, test_string[::-1], {})
-             self.admin.ticket.delete(t_id)
-             self.fail("Expected ticket create to fail...")
--        except Exception, e:
--            self.assertTrue(isinstance(e, xmlrpclib.Fault))
--            self.assertEquals(-32700, e.faultCode)
-+        e = self.assertRaises(xmlrpclib.Fault, testcall)
-+        self.assertEquals(-32700, e.faultCode)
-+
-         # Unicode version (encodable)
-         from trac.util.text import to_unicode
-         test_string = to_unicode(test_string)
-@@ -81,7 +86,8 @@
-         xmlrpc_now = to_xmlrpc_datetime(now)
-         self.assertTrue(isinstance(xmlrpc_now, xmlrpclib.DateTime),
-                 "Expected xmlprc_now to be an xmlrpclib.DateTime")
--        self.assertEquals(xmlrpc_now.timetuple()[:6], now_timetuple)
-+        # self.assertEquals(xmlrpc_now.timetuple()[:6], now_timetuple)
-+        self.assertEquals(str(xmlrpc_now), now.strftime("%Y%m%dT%H:%M:%S"))
-         now_from_xmlrpc = from_xmlrpc_datetime(xmlrpc_now)
-         self.assertTrue(isinstance(now_from_xmlrpc, datetime),
-                 "Expected now_from_xmlrpc to be a datetime")
-@@ -90,22 +96,19 @@
- 
-     def test_resource_not_found(self):
-         # A Ticket resource
--        try:
--            self.admin.ticket.get(2147483647)
--        except Exception, e:
--            self.assertEquals(e.faultCode, 404)
--            self.assertEquals(e.faultString, 
--                    'Ticket 2147483647 does not exist.')
-+        e = self.assertRaises(xmlrpclib.Fault, lambda : self.admin.ticket.get(2147483647))
-+        self.assertEquals(e.faultCode, 404)
-+        self.assertEquals(e.faultString, 
-+                'Ticket 2147483647 does not exist.')
-         # A Wiki resource
--        try:
--            self.admin.wiki.getPage("Test", 10)
--        except Exception, e:
--            self.assertEquals(e.faultCode, 404)
--            self.assertEquals(e.faultString,
--                    'Wiki page "Test" does not exist at version 10')
-+        e = self.assertRaises(xmlrpclib.Fault, lambda : self.admin.wiki.getPage("Test", 10))
-+        self.assertEquals(e.faultCode, 404)
-+        self.assertEquals(e.faultString,
-+                'Wiki page "Test" does not exist at version 10')
+@@ -104,8 +104,8 @@
+             self.assertEquals(e.faultString,
+                     'Wiki page "Test" does not exist at version 10')
  
 -def suite():
 +def test_suite():
  if __name__ == '__main__':
 -    unittest.main(defaultTest='suite')
 +    unittest.main(defaultTest='test_suite')
-+
-diff -r 203e791a70bf trunk/tracrpc/web_ui.py
---- a/trunk/tracrpc/web_ui.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/web_ui.py	Wed Mar 17 16:05:57 2010 -0500
+diff -r 8b6987624152 trunk/tracrpc/web_ui.py
+--- a/trunk/tracrpc/web_ui.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/web_ui.py	Thu Mar 18 12:50:41 2010 -0500
 @@ -6,6 +6,10 @@
  (c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
  """
  
 +    def _rpc_process(self, req, protocol, content_type):
 +        """Process incoming RPC request and finalize response."""
-+        proto_id = tuple(protocol.rpc_info())[0]
++        req.perm.require('XML_RPC') # Need at least XML_RPC
++        proto_id = protocol.rpc_info()[0]
 +        rpcreq = {'mimetype': content_type}
 +        try :
 +            self.log.debug("RPC(%s) call by '%s'", proto_id, req.authname)
-+            req.perm.require('XML_RPC') # Need at least XML_RPC
 +            rpcreq = protocol.parse_rpc_request(req, content_type)
 +            rpcreq['mimetype'] = content_type
 +            method_name = rpcreq.get('method')
 +                result = (XMLRPCSystem(self.env).get_method(method_name)(req, args))[0]
 +                if isinstance(result, GeneratorType):
 +                    result = list(result)
-+            except (RPCError, PermissionError, ResourceNotFound):
++            except (RPCError, PermissionError, ResourceNotFound), e:
 +                raise
 +            except Exception:
 +                e, tb = sys.exc_info()[-2:]
      # ITemplateProvider methods
  
      def get_htdocs_dirs(self):
-diff -r 203e791a70bf trunk/tracrpc/xml_rpc.py
---- a/trunk/tracrpc/xml_rpc.py	Tue Mar 16 11:04:09 2010 -0500
-+++ b/trunk/tracrpc/xml_rpc.py	Wed Mar 17 16:05:57 2010 -0500
-@@ -17,9 +17,10 @@
- from trac.resource import ResourceNotFound
- from trac.util.datefmt import utc
+diff -r 8b6987624152 trunk/tracrpc/xml_rpc.py
+--- a/trunk/tracrpc/xml_rpc.py	Thu Mar 18 12:49:00 2010 -0500
++++ b/trunk/tracrpc/xml_rpc.py	Thu Mar 18 12:50:41 2010 -0500
+@@ -19,7 +19,7 @@
  from trac.util.text import to_unicode
-+from trac.web.api import RequestDone
  
  from tracrpc.api import XMLRPCSystem, IRPCProtocol, Binary, \
 -        RPCError, MethodNotFound
  from tracrpc.util import empty, prepare_docs
  
  __all__ = ['XmlRpcProtocol']
-@@ -81,41 +82,46 @@
+@@ -81,41 +81,46 @@
          yield ('xmlrpc', 'application/xml')
          yield ('xmlrpc', 'text/xml')
  
              self.log.error(e)
              import traceback
              from tracrpc.util import StringIO
-@@ -123,6 +129,7 @@
-             traceback.print_exc(file = out)
-             self.log.error(out.getvalue())
-             err_code = hasattr(e, 'code') and e.code or 1
-+            method = rpcreq.get('method')
-             self._send_response(req,
-                     xmlrpclib.dumps(
-                         xmlrpclib.Fault(err_code,
-@@ -137,6 +144,7 @@
-         req.send_header('Content-Length', len(response))
-         req.end_headers()
-         req.write(response)
-+        raise RequestDone()
- 
-     def _normalize_xml_input(self, args):
-         """ Normalizes arguments (at any level - traversing dicts and lists):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.