def test_repr(self): obj = stanza.Error() self.assertEqual( "<undefined-condition type=<ErrorType.CANCEL: 'cancel'>>", repr(obj)) obj = stanza.Error(type_=structs.ErrorType.MODIFY, condition=errors.ErrorCondition.BAD_REQUEST, text="foobar") self.assertEqual( "<bad-request type=<ErrorType.MODIFY: 'modify'> text='foobar'>", repr(obj))
def test_repr(self): obj = stanza.Error() self.assertEqual( "<undefined-condition type=<ErrorType.CANCEL: 'cancel'>>", repr(obj)) obj = stanza.Error(type_=structs.ErrorType.MODIFY, condition=(namespaces.stanzas, "bad-request"), text="foobar") self.assertEqual( "<bad-request type=<ErrorType.MODIFY: 'modify'> text='foobar'>", repr(obj))
def test_init_works_with_xso(self): condition_obj = errors.ErrorCondition.GONE.to_xso() condition_obj.new_address = "foo" e = stanza.Error(condition_obj) self.assertIs(e.condition_obj, condition_obj)
def test_to_exception(self): types = { structs.ErrorType.MODIFY: errors.XMPPModifyError, structs.ErrorType.CANCEL: errors.XMPPCancelError, structs.ErrorType.AUTH: errors.XMPPAuthError, structs.ErrorType.WAIT: errors.XMPPWaitError, structs.ErrorType.CONTINUE: errors.XMPPContinueError, } conditions = [ errors.ErrorCondition.BAD_REQUEST, errors.ErrorCondition.UNDEFINED_CONDITION, ] texts = [ "foo", "bar", None, ] for (type_name, cls), condition, text in itertools.product( types.items(), conditions, texts): obj = stanza.Error(type_=type_name, condition=condition, text=text) exc = obj.to_exception() self.assertIsInstance(exc, cls) self.assertEqual(condition, exc.condition) self.assertIs( exc.condition_obj, obj.condition_obj, ) self.assertEqual(text, exc.text)
def test_repr(self): s = stanza.IQ(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.IQType.ERROR) s.error = stanza.Error() self.assertEqual( "<iq from='*****@*****.**' to='*****@*****.**'" " id='someid' type=<IQType.ERROR: 'error'>" " error=<undefined-condition type=<ErrorType.CANCEL: 'cancel'>>>", repr(s)) s = stanza.IQ(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.IQType.RESULT) s.payload = TestPayload() self.assertEqual( "<iq from='*****@*****.**' to='*****@*****.**'" " id='someid' type=<IQType.RESULT: 'result'>" " data=foobar>", repr(s)) s = stanza.IQ(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.IQType.RESULT) self.assertEqual( "<iq from='*****@*****.**' to='*****@*****.**'" " id='someid' type=<IQType.RESULT: 'result'>>", repr(s))
def test_to_exception(self): types = { structs.ErrorType.MODIFY: errors.XMPPModifyError, structs.ErrorType.CANCEL: errors.XMPPCancelError, structs.ErrorType.AUTH: errors.XMPPAuthError, structs.ErrorType.WAIT: errors.XMPPWaitError, structs.ErrorType.CONTINUE: errors.XMPPContinueError, } conditions = [ (namespaces.stanzas, "bad-request"), (namespaces.stanzas, "undefined-condition"), ] texts = [ "foo", "bar", None, ] for (type_name, cls), condition, text in itertools.product( types.items(), conditions, texts): obj = stanza.Error(type_=type_name, condition=condition, text=text) exc = obj.to_exception() self.assertIsInstance(exc, cls) self.assertEqual(condition, exc.condition) self.assertEqual(text, exc.text)
def test_repr(self): obj = stanza.Error() self.assertEqual( "<undefined-condition type='cancel'>", repr(obj) ) obj = stanza.Error( type_="modify", condition=(namespaces.stanzas, "bad-request"), text="foobar" ) self.assertEqual( "<bad-request type='modify' text='foobar'>", repr(obj) )
def test_condition_reflects_enum_member_of_object_after_init(self): e = stanza.Error() self.assertEqual( errors.ErrorCondition.UNDEFINED_CONDITION, e.condition, )
def test_condition_reflects_enum_member_of_object_after_change(self): e = stanza.Error() e.condition_obj = errors.ErrorCondition.BAD_REQUEST.xso_class() self.assertEqual( errors.ErrorCondition.BAD_REQUEST, e.condition, )
def test_setting_condition_replaces_object(self): e = stanza.Error() e.condition = errors.ErrorCondition.UNDEFINED_CONDITION self.assertEqual(e.condition, errors.ErrorCondition.UNDEFINED_CONDITION) self.assertIsInstance( e.condition_obj, errors.ErrorCondition.UNDEFINED_CONDITION.xso_class, )
def test_init_works_with_tuple(self): with self.assertWarnsRegex( DeprecationWarning, r"as of aioxmpp 1\.0, error conditions must be members of the " r"aioxmpp\.ErrorCondition enumeration") as ctx: e = stanza.Error( errors.ErrorCondition.REMOTE_SERVER_NOT_FOUND.value) self.assertEqual( e.condition, errors.ErrorCondition.REMOTE_SERVER_NOT_FOUND, ) self.assertTrue(ctx.filename.endswith("test_stanza.py"))
def test_make_error(self): e = stanza.Error(condition=(namespaces.stanzas, "bad-request")) s = stanza.IQ(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.IQType.GET) r = s.make_error(e) self.assertIsInstance(r, stanza.IQ) self.assertEqual(r.type_, structs.IQType.ERROR) self.assertEqual(TEST_FROM, r.to) self.assertEqual(TEST_TO, r.from_) self.assertEqual(s.id_, r.id_)
def test_to_exception_with_application_condition_only_if_cond_supports( self): cond = unittest.mock.Mock([]) obj = stanza.Error(type_=structs.ErrorType.CONTINUE, condition=(namespaces.stanzas, "undefined-condition")) obj.application_condition = cond result = obj.to_exception() self.assertIsInstance(result, errors.XMPPContinueError) self.assertSequenceEqual(cond.mock_calls, [])
def test_to_exception_with_application_condition(self): cond = unittest.mock.Mock(["to_exception"]) obj = stanza.Error(type_=structs.ErrorType.CONTINUE, condition=errors.ErrorCondition.UNDEFINED_CONDITION) obj.application_condition = cond cond.to_exception.return_value = Exception() result = obj.to_exception() self.assertSequenceEqual(cond.mock_calls, [unittest.mock.call.to_exception(obj.type_)]) self.assertEqual(result, cond.to_exception())
def test_make_error(self): e = stanza.Error(condition=errors.ErrorCondition.BAD_REQUEST) s = stanza.IQ(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.IQType.GET) r = s.make_error(e) self.assertIsInstance(r, stanza.IQ) self.assertEqual(r.type_, structs.IQType.ERROR) self.assertEqual(TEST_FROM, r.to) self.assertEqual(TEST_TO, r.from_) self.assertEqual(s.id_, r.id_)
def test_make_error(self): e = stanza.Error(condition=(namespaces.stanzas, "feature-not-implemented")) s = stanza.Message(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.MessageType.GROUPCHAT) r = s.make_error(e) self.assertIsInstance(r, stanza.Message) self.assertEqual(r.type_, structs.MessageType.ERROR) self.assertEqual(TEST_FROM, r.to) self.assertEqual(TEST_TO, r.from_) self.assertEqual(s.id_, r.id_)
def test_to_exception_with_application_condition(self): cond = unittest.mock.Mock(["to_exception"]) obj = stanza.Error(type_=structs.ErrorType.CONTINUE, condition=(namespaces.stanzas, "undefined-condition")) obj.application_condition = cond cond.to_exception.return_value = Exception() result = obj.to_exception() self.assertSequenceEqual(cond.mock_calls, [unittest.mock.call.to_exception(obj.type_)]) self.assertEqual(result, cond.to_exception())
def test_make_error(self): e = stanza.Error( condition=errors.ErrorCondition.FEATURE_NOT_IMPLEMENTED) s = stanza.Message(from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.MessageType.GROUPCHAT) r = s.make_error(e) self.assertIsInstance(r, stanza.Message) self.assertEqual(r.type_, structs.MessageType.ERROR) self.assertEqual(TEST_FROM, r.to) self.assertEqual(TEST_TO, r.from_) self.assertEqual(s.id_, r.id_)
def test_override_with_default_exception_if_result_of_app_cond_is_no_exception( self): cond = unittest.mock.Mock(["to_exception", "TAG"]) cond.TAG = ("foo", "bar") obj = stanza.Error(type_=structs.ErrorType.CONTINUE, condition=errors.ErrorCondition.UNDEFINED_CONDITION) obj.application_condition = cond cond.to_exception.return_value = object() result = obj.to_exception() self.assertIsInstance(result, errors.XMPPContinueError) self.assertSequenceEqual(cond.mock_calls, [unittest.mock.call.to_exception(obj.type_)])
def test_make_error(self): e = stanza.Error(condition=errors.ErrorCondition.GONE) s = stanza.Presence( from_=TEST_FROM, to=TEST_TO, id_="someid", type_=structs.PresenceType.UNAVAILABLE, ) r = s.make_error(e) self.assertIsInstance(r, stanza.Presence) self.assertEqual(r.type_, structs.PresenceType.ERROR) self.assertEqual(TEST_FROM, r.to) self.assertEqual(TEST_TO, r.from_) self.assertEqual(s.id_, r.id_)
def test_override_with_default_exception_if_result_of_app_cond_is_no_exception( self): cond = unittest.mock.Mock(["to_exception"]) obj = stanza.Error(type_=structs.ErrorType.CONTINUE, condition=(namespaces.stanzas, "undefined-condition")) obj.application_condition = cond cond.to_exception.return_value = object() result = obj.to_exception() self.assertIsInstance(result, errors.XMPPContinueError) self.assertSequenceEqual(cond.mock_calls, [unittest.mock.call.to_exception(obj.type_)])
def test_to_exception_with_application_condition_only_if_cond_supports( self): cond = unittest.mock.Mock(["TAG"]) cond.TAG = ("foo", "bar") obj = stanza.Error(type_=structs.ErrorType.CONTINUE, condition=errors.ErrorCondition.UNDEFINED_CONDITION) obj.application_condition = cond result = obj.to_exception() self.assertIsInstance(result, errors.XMPPContinueError) self.assertEqual( result.application_defined_condition, obj.application_condition, ) self.assertSequenceEqual(cond.mock_calls, [])
def test_accepts_tuple_instead_of_enum_for_condition_and_warns(self): e = stanza.Error() with self.assertWarnsRegex( DeprecationWarning, r"as of aioxmpp 1\.0, error conditions must be members of the " r"aioxmpp\.ErrorCondition enumeration") as ctx: e.condition = errors.ErrorCondition.BAD_REQUEST.value self.assertEqual( errors.ErrorCondition.BAD_REQUEST, e.condition, ) self.assertIsInstance( e.condition_obj, errors.ErrorCondition.BAD_REQUEST.xso_class, ) self.assertTrue(ctx.filename.endswith("test_stanza.py"))
def test_to_exception(self): types = { "modify": errors.XMPPModifyError, "cancel": errors.XMPPCancelError, "auth": errors.XMPPAuthError, "wait": errors.XMPPWaitError, "continue": errors.XMPPContinueError, } conditions = [ (namespaces.stanzas, "bad-request"), (namespaces.stanzas, "undefined-condition"), ] texts = [ "foo", "bar", None, ] for (type_name, cls), condition, text in itertools.product( types.items(), conditions, texts): obj = stanza.Error( type_=type_name, condition=condition, text=text) exc = obj.to_exception() self.assertIsInstance( exc, cls ) self.assertEqual( condition, exc.condition ) self.assertEqual( text, exc.text )
def test_repr(self): s = stanza.IQ( from_=TEST_FROM, to=TEST_TO, id_="someid", type_="error") s.error = stanza.Error() self.assertEqual( "<iq from='*****@*****.**' to='*****@*****.**'" " id='someid' type='error'" " error=<undefined-condition type='cancel'>>", repr(s) ) s = stanza.IQ( from_=TEST_FROM, to=TEST_TO, id_="someid", type_="result") s.payload = TestPayload() self.assertEqual( "<iq from='*****@*****.**' to='*****@*****.**'" " id='someid' type='result'" " data=foobar>", repr(s) ) s = stanza.IQ( from_=TEST_FROM, to=TEST_TO, id_="someid", type_="result") self.assertEqual( "<iq from='*****@*****.**' to='*****@*****.**'" " id='someid' type='result'>", repr(s) )
def test_make_error(self): e = stanza.Error( condition=(namespaces.stanzas, "gone") ) s = stanza.Presence(from_=TEST_FROM, to=TEST_TO, id_="someid", type_="unavailable") r = s.make_error(e) self.assertIsInstance(r, stanza.Presence) self.assertEqual( r.type_, "error") self.assertEqual( TEST_FROM, r.to) self.assertEqual( TEST_TO, r.from_) self.assertEqual( s.id_, r.id_)
def test_rejects_xso_for_condition(self): e = stanza.Error() with self.assertRaises(ValueError): e.condition = errors.ErrorCondition.BAD_REQUEST.to_xso()
def test_setting_condition_keeps_object_if_condition_matches(self): e = stanza.Error() old = e.condition_obj e.condition = errors.ErrorCondition.UNDEFINED_CONDITION self.assertIs(e.condition_obj, old)
def test_initialises_with_undefined_condition(self): e = stanza.Error() self.assertIsInstance( e.condition_obj, errors.ErrorCondition.UNDEFINED_CONDITION.xso_class, )