agentultra avatar agentultra committed deaa5c3

Added new unit tests and basic parameter check to raise invalid params code with wrong number of parameters

Comments (0)

Files changed (2)

pylons/controllers/jsonrpc.py

             err = jsonrpc_error(self._req_id, 'JSONRPC_METHOD_NOT_FOUND')
             return err(environ, start_response)
 
-        # now that we have a method, add self._req_params to
-        # self.kargs and dispatch control to WGIController
+        # now that we have a method, make sure we have enough
+        # parameters and pass off control to the controller.
         arglist = inspect.getargspec(self._func)[0][1:]
+        if len(self._req_params) < len(arglist):
+            err = jsonrpc_error(self._req_id, 'JSONRPC_INVALID_PARAMS')
+            return err(environ, start_response)
         kargs = dict(zip(arglist, self._req_params))
         kargs['action'], kargs['environ'] = self._req_method, environ
         kargs['start_response'] = start_response
             self._error = application_error(e)
         except Exception as e:
             log.debug('Encountered unhandled exception: %s', repr(e))
+            err = _reserved_errors['JSONRPC_INTERNAL_ERROR']
             self._error = application_error(
-                _reserved_errors['JSONRPC_INTERNAL_ERROR'])
+                JSONRPCError(err['code'], err['message']))
 
         if self._error is not None:
             response = dict(

tests/test_units/test_jsonrpc.py

         def return_garbage(self):
             return JSONRPCController
 
+        def subtract(self, x, y):
+            if not isinstance(x, int) and not isinstance(y, int):
+                raise JSONRPCError(1, 'That is not an integer')
+            else:
+                return x - y
+
         def _private(self):
             return 'private method'
 
         self.assertRaises(exc.HTTPLengthRequired,
                           lambda: self.app.post('/', extra_environ=\
                                                     dict(CONTENT_LENGTH='0')))
+
+    def test_positional_params(self):
+        response = self.jsonreq('subtract', args=[4, 2])
+        assert dict(jsonrpc='2.0',
+                    id='test',
+                    result=2) == response
+
+    def test_missing_positional_param(self):
+        response = self.jsonreq('subtract', args=[1])
+        assert dict(jsonrpc='2.0',
+                    id='test',
+                    error={'code': -32602,
+                           'message': "Invalid params"}) == response
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.