Exemple #1
0
    def test_get_avatar_metadata_mask_xmpp_errors(self):
        with unittest.mock.patch.object(self.pubsub,
                                        "get_items",
                                        new=CoroutineMock()):
            self.pubsub.get_items.side_effect = errors.XMPPCancelError(
                (namespaces.stanzas, "feature-not-implemented"))

            res = run_coroutine(self.s.get_avatar_metadata(TEST_JID1))

            self.assertSequenceEqual(res, [])
            self.assertSequenceEqual(self.pubsub.get_items.mock_calls, [
                unittest.mock.call(
                    TEST_JID1, namespaces.xep0084_metadata, max_items=1),
            ])

        with unittest.mock.patch.object(self.pubsub,
                                        "get_items",
                                        new=CoroutineMock()):
            self.pubsub.get_items.side_effect = errors.XMPPCancelError(
                (namespaces.stanzas, "item-not-found"))

            res = run_coroutine(self.s.get_avatar_metadata(TEST_JID2))

            self.assertSequenceEqual(res, [])
            self.assertSequenceEqual(self.pubsub.get_items.mock_calls, [
                unittest.mock.call(
                    TEST_JID2, namespaces.xep0084_metadata, max_items=1),
            ])
Exemple #2
0
 def _process_iq(self, payload):
     if payload.seq != self._incoming_seq:
         raise errors.XMPPCancelError(
             condition=errors.ErrorCondition.UNEXPECTED_REQUEST)
     self._incoming_seq += 1
     self._incoming_seq &= 0xffff
     self._data_received(payload.content)
Exemple #3
0
    def _handle_open_request(self, iq):
        peer_jid = iq.from_
        sid = iq.payload.sid
        block_size = iq.payload.block_size
        stanza_type = iq.payload.stanza

        if block_size > MAX_BLOCK_SIZE:
            raise errors.XMPPModifyError(
                condition=errors.ErrorCondition.RESOURCE_CONSTRAINT
            )

        try:
            protocol_factory, expected_future = \
                self._expected_sessions[sid, peer_jid]
        except KeyError:
            if len(self._sessions) >= self.session_limit:
                raise errors.XMPPCancelError(
                    condition=errors.ErrorCondition.NOT_ACCEPTABLE
                )
            expected_future = None
            protocol_factory = self.default_protocol_factory

        if (sid, peer_jid) in self._sessions:
            # disallow opening a session twice
            if expected_future is not None:
                # is this correct?
                expected_future.cancel()
            raise errors.XMPPCancelError(
                condition=errors.ErrorCondition.NOT_ACCEPTABLE
            )

        handle = self._sessions[sid, peer_jid] = IBBTransport(
            self,
            peer_jid,
            sid,
            stanza_type,
            block_size
        )

        protocol = protocol_factory()
        handle.set_protocol(protocol)

        if expected_future is None:
            self.on_session_accepted((handle, protocol))
        else:
            expected_future.set_result((handle, protocol))
Exemple #4
0
 def mock(*args, **kwargs):
     nonlocal ncall
     ncall += 1
     if ncall == 1:
         raise errors.XMPPCancelError(
             condition=(namespaces.stanzas,
                        "feature-not-implemented"), )
     else:
         raise ConnectionError()
Exemple #5
0
    async def _handle_data(self, iq):
        peer_jid = iq.from_
        sid = iq.payload.sid

        try:
            session_handle = self._sessions[sid, peer_jid]
        except KeyError:
            raise errors.XMPPCancelError(
                condition=errors.ErrorCondition.ITEM_NOT_FOUND)

        session_handle._process_iq(iq.payload)
Exemple #6
0
    async def _handle_close_request(self, iq):
        peer_jid = iq.from_
        sid = iq.payload.sid

        try:
            session_handle = self._sessions[sid, peer_jid]
        except KeyError:
            raise errors.XMPPCancelError(
                condition=errors.ErrorCondition.ITEM_NOT_FOUND)

        session_handle._connection_closed()
Exemple #7
0
    def test_query_info_reraises_but_does_not_cache_exception(self):
        to = structs.JID.fromstr("[email protected]/res1")

        self.cc.stream.send_iq_and_wait_for_reply.side_effect = \
            errors.XMPPCancelError(
                condition=(namespaces.stanzas, "feature-not-implemented"),
            )

        with self.assertRaises(errors.XMPPCancelError):
            run_coroutine(self.s.query_items(to, node="foobar"))

        self.cc.stream.send_iq_and_wait_for_reply.side_effect = \
            ConnectionError()

        with self.assertRaises(ConnectionError):
            run_coroutine(self.s.query_items(to, node="foobar"))
Exemple #8
0
    def test_query_info_reraises_but_does_not_cache_exception(self):
        to = structs.JID.fromstr("[email protected]/res1")

        with unittest.mock.patch.object(
                self.s, "send_and_decode_info_query",
                new=CoroutineMock()) as send_and_decode:
            send_and_decode.side_effect = errors.XMPPCancelError(
                condition=(namespaces.stanzas, "feature-not-implemented"), )

            with self.assertRaises(errors.XMPPCancelError):
                run_coroutine(self.s.query_info(to, node="foobar"))

            send_and_decode.side_effect = ConnectionError()

            with self.assertRaises(ConnectionError):
                run_coroutine(self.s.query_info(to, node="foobar"))
Exemple #9
0
    def test_get_avatar_metadata_vcard_fallback(self):
        sha1 = hashlib.sha1(b'')
        empty_sha1 = sha1.hexdigest()

        with contextlib.ExitStack() as e:
            e.enter_context(unittest.mock.patch.object(
                self.pubsub, "get_items",
                new=CoroutineMock()))
            e.enter_context(unittest.mock.patch.object(
                self.vcard, "get_vcard",
                new=CoroutineMock()))
            vcard_mock = unittest.mock.Mock()
            vcard_mock.get_photo_data.return_value = b''
            self.vcard.get_vcard.return_value = vcard_mock

            self.pubsub.get_items.side_effect = errors.XMPPCancelError(
                (namespaces.stanzas, "feature-not-implemented")
            )

            res = run_coroutine(self.s.get_avatar_metadata(TEST_JID1))

            self.assertEqual(len(res), 1)
            self.assertIsInstance(res[0],
                                  avatar_service.VCardAvatarDescriptor)
            self.assertEqual(res[0]._image_bytes,
                             b'')
            self.assertEqual(res[0].id_,
                             empty_sha1)
            self.assertEqual(res[0].nbytes,
                             0)

            self.assertSequenceEqual(
                self.pubsub.get_items.mock_calls,
                [
                    unittest.mock.call(
                        TEST_JID1,
                        namespaces.xep0084_metadata,
                        max_items=1
                    ),
                ]
            )

        with contextlib.ExitStack() as e:
            e.enter_context(unittest.mock.patch.object(
                self.pubsub, "get_items",
                new=CoroutineMock()))
            e.enter_context(unittest.mock.patch.object(
                self.vcard, "get_vcard",
                new=CoroutineMock()))
            vcard_mock = unittest.mock.Mock()
            vcard_mock.get_photo_data.return_value = b''
            self.vcard.get_vcard.return_value = vcard_mock

            self.pubsub.get_items.side_effect = errors.XMPPCancelError(
                (namespaces.stanzas, "item-not-found")
            )

            res = run_coroutine(self.s.get_avatar_metadata(TEST_JID2))

            self.assertEqual(len(res), 1)
            self.assertIsInstance(res[0],
                                  avatar_service.VCardAvatarDescriptor)
            self.assertEqual(res[0]._image_bytes,
                             b'')
            self.assertEqual(res[0].id_,
                             empty_sha1)
            self.assertEqual(res[0].nbytes,
                             0)

            self.assertSequenceEqual(
                self.pubsub.get_items.mock_calls,
                [
                    unittest.mock.call(
                        TEST_JID2,
                        namespaces.xep0084_metadata,
                        max_items=1
                    ),
                ]
            )

        with contextlib.ExitStack() as e:
            e.enter_context(unittest.mock.patch.object(
                self.pubsub, "get_items",
                new=CoroutineMock()))
            e.enter_context(unittest.mock.patch.object(
                self.vcard, "get_vcard",
                new=CoroutineMock()))
            vcard_mock = unittest.mock.Mock()
            vcard_mock.get_photo_data.return_value = None
            self.vcard.get_vcard.return_value = vcard_mock

            self.pubsub.get_items.side_effect = errors.XMPPCancelError(
                (namespaces.stanzas, "item-not-found")
            )

            res = run_coroutine(self.s.get_avatar_metadata(TEST_JID3))
            self.assertEqual(len(res), 0)

            self.assertSequenceEqual(
                self.pubsub.get_items.mock_calls,
                [
                    unittest.mock.call(
                        TEST_JID3,
                        namespaces.xep0084_metadata,
                        max_items=1
                    ),
                ]
            )