summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJason Gerard DeRose <jderose@redhat.com>2009-01-19 21:10:42 -0700
committerJason Gerard DeRose <jderose@redhat.com>2009-01-19 21:10:42 -0700
commit55fba5420d8ea57931937728102094492ca73d86 (patch)
tree65593f7af4809a6ae74c3533860c7840181fb76d /tests
parentbae9dd7c073a8a23f71b1df0fa4cb6d90b00a337 (diff)
downloadfreeipa-55fba5420d8ea57931937728102094492ca73d86.tar.gz
freeipa-55fba5420d8ea57931937728102094492ca73d86.tar.xz
freeipa-55fba5420d8ea57931937728102094492ca73d86.zip
Added rpc.xmlclient backend plugin for forwarding; added corresponding unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_ipalib/test_rpc.py81
-rw-r--r--tests/util.py45
2 files changed, 124 insertions, 2 deletions
diff --git a/tests/test_ipalib/test_rpc.py b/tests/test_ipalib/test_rpc.py
index eba35261b..296e9bc1c 100644
--- a/tests/test_ipalib/test_rpc.py
+++ b/tests/test_ipalib/test_rpc.py
@@ -21,10 +21,16 @@
Test the `ipalib.rpc` module.
"""
+import threading
from xmlrpclib import Binary, Fault, dumps, loads
-from tests.util import raises, assert_equal
+from tests.util import raises, assert_equal, PluginTester, DummyClass
from tests.data import binary_bytes, utf8_bytes, unicode_str
-from ipalib import rpc
+from ipalib.frontend import Command
+from ipalib.request import context
+from ipalib import rpc, errors2
+
+
+std_compound = (binary_bytes, utf8_bytes, unicode_str)
def dump_n_load(value):
@@ -170,3 +176,74 @@ def test_xml_loads():
e = raises(Fault, f, data)
assert e.faultCode == 69
assert_equal(e.faultString, unicode_str)
+
+
+class test_xmlclient(PluginTester):
+ """
+ Test the `ipalib.rpc.xmlclient` plugin.
+ """
+ _plugin = rpc.xmlclient
+
+ def test_forward(self):
+ """
+ Test the `ipalib.rpc.xmlclient.forward` method.
+ """
+ class user_add(Command):
+ pass
+
+ # Test that ValueError is raised when forwarding a command that is not
+ # in api.Command:
+ (o, api, home) = self.instance('Backend', in_server=False)
+ e = raises(ValueError, o.forward, 'user_add')
+ assert str(e) == '%s.forward(): %r not in api.Command' % (
+ 'xmlclient', 'user_add'
+ )
+
+ # Test that StandardError is raised when context.xmlconn does not exist:
+ (o, api, home) = self.instance('Backend', user_add, in_server=False)
+ e = raises(StandardError, o.forward, 'user_add')
+ assert str(e) == '%s.forward(%r): need context.xmlconn in thread %r' % (
+ 'xmlclient', 'user_add', threading.currentThread().getName()
+ )
+
+ args = (binary_bytes, utf8_bytes, unicode_str)
+ kw = dict(one=binary_bytes, two=utf8_bytes, three=unicode_str)
+ params = args + (kw,)
+ result = (unicode_str, binary_bytes, utf8_bytes)
+ context.xmlconn = DummyClass(
+ (
+ 'user_add',
+ (rpc.xml_wrap(params),),
+ {},
+ rpc.xml_wrap(result),
+ ),
+ (
+ 'user_add',
+ (rpc.xml_wrap(params),),
+ {},
+ Fault(3005, u"'four' is required"), # RequirementError
+ ),
+ (
+ 'user_add',
+ (rpc.xml_wrap(params),),
+ {},
+ Fault(700, u'no such error'), # There is no error 700
+ ),
+
+ )
+
+ # Test with a successful return value:
+ assert o.forward('user_add', *args, **kw) == result
+
+ # Test with an errno the client knows:
+ e = raises(errors2.RequirementError, o.forward, 'user_add', *args, **kw)
+ assert_equal(e.message, u"'four' is required")
+
+ # Test with an errno the client doesn't know
+ e = raises(errors2.UnknownError, o.forward, 'user_add', *args, **kw)
+ assert_equal(e.code, 700)
+ assert_equal(e.error, u'no such error')
+
+ assert context.xmlconn._calledall() is True
+
+ del context.xmlconn
diff --git a/tests/util.py b/tests/util.py
index af4c23934..f5899dfab 100644
--- a/tests/util.py
+++ b/tests/util.py
@@ -344,3 +344,48 @@ class dummy_ungettext(object):
if n == 1:
return self.translation_singular
return self.translation_plural
+
+
+class DummyMethod(object):
+ def __init__(self, callback, name):
+ self.__callback = callback
+ self.__name = name
+
+ def __call__(self, *args, **kw):
+ return self.__callback(self.__name, args, kw)
+
+
+class DummyClass(object):
+ def __init__(self, *calls):
+ self.__calls = calls
+ self.__i = 0
+ for (name, args, kw, result) in calls:
+ method = DummyMethod(self.__process, name)
+ setattr(self, name, method)
+
+ def __process(self, name_, args_, kw_):
+ if self.__i >= len(self.__calls):
+ raise AssertionError(
+ 'extra call: %s, %r, %r' % (name, args, kw)
+ )
+ (name, args, kw, result) = self.__calls[self.__i]
+ self.__i += 1
+ i = self.__i
+ if name_ != name:
+ raise AssertionError(
+ 'call %d should be to method %r; got %r' % (i, name, name_)
+ )
+ if args_ != args:
+ raise AssertionError(
+ 'call %d to %r should have args %r; got %r' % (i, name, args, args_)
+ )
+ if kw_ != kw:
+ raise AssertionError(
+ 'call %d to %r should have kw %r, got %r' % (i, name, kw, kw_)
+ )
+ if isinstance(result, Exception):
+ raise result
+ return result
+
+ def _calledall(self):
+ return self.__i == len(self.__calls)