Ejemplo n.º 1
0
 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))
Ejemplo n.º 2
0
 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))
Ejemplo n.º 3
0
    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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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))
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
 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)
     )
Ejemplo n.º 8
0
    def test_condition_reflects_enum_member_of_object_after_init(self):
        e = stanza.Error()

        self.assertEqual(
            errors.ErrorCondition.UNDEFINED_CONDITION,
            e.condition,
        )
Ejemplo n.º 9
0
    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,
        )
Ejemplo n.º 10
0
    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,
        )
Ejemplo n.º 11
0
    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"))
Ejemplo n.º 12
0
    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_)
Ejemplo n.º 13
0
    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, [])
Ejemplo n.º 14
0
    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())
Ejemplo n.º 15
0
    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_)
Ejemplo n.º 16
0
    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_)
Ejemplo n.º 17
0
    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())
Ejemplo n.º 18
0
    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_)
Ejemplo n.º 19
0
    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_)])
Ejemplo n.º 20
0
    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_)
Ejemplo n.º 21
0
    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_)])
Ejemplo n.º 22
0
    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, [])
Ejemplo n.º 23
0
    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"))
Ejemplo n.º 24
0
    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
            )
Ejemplo n.º 25
0
    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)
        )
Ejemplo n.º 26
0
    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_)
Ejemplo n.º 27
0
    def test_rejects_xso_for_condition(self):
        e = stanza.Error()

        with self.assertRaises(ValueError):
            e.condition = errors.ErrorCondition.BAD_REQUEST.to_xso()
Ejemplo n.º 28
0
 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)
Ejemplo n.º 29
0
 def test_initialises_with_undefined_condition(self):
     e = stanza.Error()
     self.assertIsInstance(
         e.condition_obj,
         errors.ErrorCondition.UNDEFINED_CONDITION.xso_class,
     )