From f58ff2921defef330d53e08e427a82ced7585c88 Mon Sep 17 00:00:00 2001 From: Jason Gerard DeRose Date: Tue, 13 Oct 2009 11:28:00 -0600 Subject: Giant webui patch take 2 --- tests/test_ipaserver/test_rpcserver.py | 63 ++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'tests/test_ipaserver') diff --git a/tests/test_ipaserver/test_rpcserver.py b/tests/test_ipaserver/test_rpcserver.py index c8cf7e05d..effac4b33 100644 --- a/tests/test_ipaserver/test_rpcserver.py +++ b/tests/test_ipaserver/test_rpcserver.py @@ -25,6 +25,7 @@ from tests.util import create_test_api, raises, PluginTester from tests.data import unicode_str from ipalib import errors, Command from ipaserver import rpcserver +import json def test_params_2_args_options(): @@ -50,3 +51,65 @@ class test_xmlserver(PluginTester): def test_marshaled_dispatch(self): (o, api, home) = self.instance('Backend', in_server=True) + + +class test_jsonserver(PluginTester): + """ + Test the `ipaserver.rpcserver.jsonserver` plugin. + """ + + _plugin = rpcserver.jsonserver + + def test_unmarshal(self): + """ + Test the `ipaserver.rpcserver.jsonserver.unmarshal` method. + """ + (o, api, home) = self.instance('Backend', in_server=True) + + # Test with invalid JSON-data: + e = raises(errors.JSONError, o.unmarshal, 'this wont work') + assert isinstance(e.error, ValueError) + assert str(e.error) == 'No JSON object could be decoded' + + # Test with non-dict type: + e = raises(errors.JSONError, o.unmarshal, json.dumps([1, 2, 3])) + assert str(e.error) == 'Request must be a dict' + + params = [[1, 2], dict(three=3, four=4)] + # Test with missing method: + d = dict(params=params, id=18) + e = raises(errors.JSONError, o.unmarshal, json.dumps(d)) + assert str(e.error) == 'Request is missing "method"' + + # Test with missing params: + d = dict(method='echo', id=18) + e = raises(errors.JSONError, o.unmarshal, json.dumps(d)) + assert str(e.error) == 'Request is missing "params"' + + # Test with non-list params: + for p in ('hello', dict(args=tuple(), options=dict())): + d = dict(method='echo', id=18, params=p) + e = raises(errors.JSONError, o.unmarshal, json.dumps(d)) + assert str(e.error) == 'params must be a list' + + # Test with other than 2 params: + for p in ([], [tuple()], [None, dict(), tuple()]): + d = dict(method='echo', id=18, params=p) + e = raises(errors.JSONError, o.unmarshal, json.dumps(d)) + assert str(e.error) == 'params must contain [args, options]' + + # Test when args is not a list: + d = dict(method='echo', id=18, params=['args', dict()]) + e = raises(errors.JSONError, o.unmarshal, json.dumps(d)) + assert str(e.error) == 'params[0] (aka args) must be a list' + + # Test when options is not a dict: + d = dict(method='echo', id=18, params=[('hello', 'world'), 'options']) + e = raises(errors.JSONError, o.unmarshal, json.dumps(d)) + assert str(e.error) == 'params[1] (aka options) must be a dict' + + # Test with valid values: + args = [u'jdoe'] + options = dict(givenname=u'John', sn='Doe') + d = dict(method=u'user_add', params=[args, options], id=18) + assert o.unmarshal(json.dumps(d)) == (u'user_add', args, options, 18) -- cgit