def test_forward(self): """ Test the `ipalib.rpc.xmlclient.forward` method. """ class user_add(Command): pass o, _api, _home = self.instance('Backend', user_add, in_server=False) 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) conn = DummyClass( ( 'user_add', rpc.xml_wrap(params, API_VERSION), {}, rpc.xml_wrap(result, API_VERSION), ), ( 'user_add', rpc.xml_wrap(params, API_VERSION), {}, Fault(3007, u"'four' is required"), # RequirementError ), ( 'user_add', rpc.xml_wrap(params, API_VERSION), {}, Fault(700, u'no such error'), # There is no error 700 ), ) # Create connection for the current thread setattr(context, o.id, Connection(conn, lambda: None)) context.xmlclient = Connection(conn, lambda: None) # Test with a successful return value: assert o.forward('user_add', *args, **kw) == result # Test with an errno the client knows: e = raises(errors.RequirementError, o.forward, 'user_add', *args, **kw) assert_equal(e.args[0], u"'four' is required") # Test with an errno the client doesn't know e = raises(errors.UnknownError, o.forward, 'user_add', *args, **kw) assert_equal(e.code, 700) assert_equal(e.error, u'no such error') assert context.xmlclient.conn._calledall() is True
def test_xml_loads(): """ Test the `ipalib.rpc.xml_loads` function. """ f = rpc.xml_loads params = (binary_bytes, utf8_bytes, unicode_str, None) wrapped = rpc.xml_wrap(params, API_VERSION) # Test un-serializing an RPC request: data = dumps(wrapped, 'the_method', allow_none=True) (p, m) = f(data) assert_equal(m, u'the_method') assert_equal(p, params) # Test un-serializing an RPC response: data = dumps((wrapped,), methodresponse=True, allow_none=True) (tup, m) = f(data) assert m is None assert len(tup) == 1 assert type(tup) is tuple assert_equal(tup[0], params) # Test un-serializing an RPC response containing a Fault: for error in (unicode_str, u'hello'): fault = Fault(69, error) data = dumps(fault, methodresponse=True, allow_none=True, encoding='UTF-8') e = raises(Fault, f, data) assert e.faultCode == 69 assert_equal(e.faultString, error) assert type(e.faultString) is unicode
def test_xml_dumps(): """ Test the `ipalib.rpc.xml_dumps` function. """ f = rpc.xml_dumps params = (binary_bytes, utf8_bytes, unicode_str, None) # Test serializing an RPC request: data = f(params, API_VERSION, 'the_method') (p, m) = loads(data) assert_equal(m, u'the_method') assert type(p) is tuple assert rpc.xml_unwrap(p) == params # Test serializing an RPC response: data = f((params,), API_VERSION, methodresponse=True) (tup, m) = loads(data) assert m is None assert len(tup) == 1 assert type(tup) is tuple assert rpc.xml_unwrap(tup[0]) == params # Test serializing an RPC response containing a Fault: fault = Fault(69, unicode_str) data = f(fault, API_VERSION, methodresponse=True) e = raises(Fault, loads, data) assert e.faultCode == 69 assert_equal(e.faultString, unicode_str)
def marshal(self, result, error, _id=None, version=VERSION_WITHOUT_CAPABILITIES): if error: self.debug('response: %s: %s', error.__class__.__name__, str(error)) response = Fault(error.errno, error.strerror) else: if isinstance(result, dict): self.debug('response: entries returned %d', result.get('count', 1)) response = (result,) return xml_dumps(response, version, methodresponse=True)
def _decorator(*args, **kwargs): try: return func(*args, **kwargs) except django.core.exceptions.PermissionDenied as e: # 403 Forbidden fault_code = http_client.FORBIDDEN fault_string = str(e) except django.db.models.ObjectDoesNotExist as e: # 404 Not Found fault_code = http_client.NOT_FOUND fault_string = str(e) except (django.db.models.FieldDoesNotExist, django.core.exceptions.FieldError, django.core.exceptions.ValidationError, django.core.exceptions.MultipleObjectsReturned, ValueError, TypeError) as e: # 400 Bad Request fault_code = http_client.BAD_REQUEST fault_string = str(e) except django.db.utils.IntegrityError as e: # 409 Duplicate fault_code = http_client.CONFLICT fault_string = str(e) except NotImplementedError as e: fault_code = http_client.NOT_IMPLEMENTED fault_string = str(e) except Exception as e: # 500 Server Error fault_code = http_client.INTERNAL_SERVER_ERROR fault_string = str(e) if settings.DEBUG: stack_trace = ''.join(traceback.format_exception(*sys.exc_info())) fault_string = '%s\n%s' % (fault_string, stack_trace) raise Fault(faultCode=fault_code, faultString=_format_message(fault_string))
def _get_data(self, advisory_id, key): out = self._ctrl._data[advisory_id] if out is None: raise Fault(100, "No such advisory: %s" % advisory_id) return out.get(key, {})
def test_returning_Fault(self): return Fault(1, 'custom Fault response')