Exemplo n.º 1
0
    def test_init(self):
        iq = disco_xso.InfoQuery()
        self.assertIsNone(iq.captured_events)
        self.assertFalse(iq.features)
        self.assertFalse(iq.identities)
        self.assertIsNone(iq.node)

        iq = disco_xso.InfoQuery(node="foobar",
                                 features=(1, 2),
                                 identities=(3, ))
        self.assertIsNone(iq.captured_events)
        self.assertSetEqual({1, 2}, iq.features)
        self.assertIsInstance(iq.identities, xso_model.XSOList)
        self.assertSequenceEqual([3], iq.identities)
        self.assertEqual("foobar", iq.node)
Exemplo n.º 2
0
    def test_query_info_transparent_deduplication_when_cancelled(self):
        to = structs.JID.fromstr("[email protected]/res1")
        response = disco_xso.InfoQuery()

        with unittest.mock.patch.object(
                self.s, "send_and_decode_info_query",
                new=CoroutineMock()) as send_and_decode:
            response = {}

            send_and_decode.return_value = response
            send_and_decode.delay = 0.1

            q1 = asyncio. async (self.s.query_info(to))
            q2 = asyncio. async (self.s.query_info(to))

            run_coroutine(asyncio.sleep(0.05))

            q1.cancel()

            result = run_coroutine(q2)

        self.assertIs(result, response)

        self.assertSequenceEqual([
            unittest.mock.call(to, None),
            unittest.mock.call(to, None),
        ], send_and_decode.mock_calls)
Exemplo n.º 3
0
    def test_check_for_pep(self):
        disco_info = disco_xso.InfoQuery()
        disco_info.identities.append(
            disco_xso.Identity(type_="pep", category="pubsub"))

        with unittest.mock.patch.object(self.disco_client,
                                        "query_info",
                                        new=CoroutineMock()):
            self.disco_client.query_info.return_value = disco_info

            # run twice, the second call must not query again but use the
            # cache
            run_coroutine(self.s._check_for_pep())
            run_coroutine(self.s._check_for_pep())

            self.assertEqual(1, len(self.disco_client.query_info.mock_calls))
            self.disco_client.query_info.assert_called_with(TEST_FROM.bare())

        # this should wipe the cache and recheck with disco
        mock_reason = unittest.mock.Mock()
        self.s.handle_stream_destroyed(mock_reason)

        with unittest.mock.patch.object(self.disco_client,
                                        "query_info",
                                        new=CoroutineMock()):
            self.disco_client.query_info.return_value = disco_info

            # run twice, the second call must not query again but use the
            # cache
            run_coroutine(self.s._check_for_pep())
            run_coroutine(self.s._check_for_pep())

            self.assertEqual(1, len(self.disco_client.query_info.mock_calls))
            self.disco_client.query_info.assert_called_with(TEST_FROM.bare())
Exemplo n.º 4
0
    def test_check_for_pep_failure(self):
        with unittest.mock.patch.object(
                self.disco_client, "query_info",
                new=CoroutineMock()) as query_info_mock:
            self.disco_client.query_info.return_value = disco_xso.InfoQuery()

            with self.assertRaises(RuntimeError):
                run_coroutine(self.s._check_for_pep())

        query_info_mock.assert_called_once_with(TEST_FROM.bare())
Exemplo n.º 5
0
    def test_check_for_pep(self):
        disco_info = disco_xso.InfoQuery()
        disco_info.identities.append(
            disco_xso.Identity(type_="pep", category="pubsub"))

        with unittest.mock.patch.object(
                self.disco_client, "query_info",
                new=CoroutineMock()) as query_info_mock:
            query_info_mock.return_value = disco_info
            run_coroutine(self.s._check_for_pep())
        query_info_mock.assert_called_once_with(TEST_FROM.bare())
Exemplo n.º 6
0
    def test_check_for_blocking_failure(self):
        disco_info = disco_xso.InfoQuery()
        with unittest.mock.patch.object(self.disco,
                                        "query_info",
                                        new=CoroutineMock()):
            self.disco.query_info.return_value = disco_info

            with self.assertRaises(RuntimeError):
                run_coroutine(self.s._check_for_blocking())

            self.disco.query_info.assert_called_with(
                TEST_FROM.replace(localpart=None, resource=None))
Exemplo n.º 7
0
    def test_check_for_blocking(self):
        disco_info = disco_xso.InfoQuery()
        disco_info.features.add(namespaces.xep0191)

        with unittest.mock.patch.object(self.disco,
                                        "query_info",
                                        new=CoroutineMock()):
            self.disco.query_info.return_value = disco_info

            run_coroutine(self.s._check_for_blocking())

            self.disco.query_info.assert_called_with(
                TEST_FROM.replace(localpart=None, resource=None))
Exemplo n.º 8
0
    def test_set_info_cache(self):
        to = structs.JID.fromstr("[email protected]/res1")
        response = disco_xso.ItemsQuery()

        self.s.set_info_cache(to, None, response)

        other_response = disco_xso.InfoQuery()
        self.cc.stream.send_iq_and_wait_for_reply.return_value = \
            other_response

        result = run_coroutine(self.s.query_info(to, node=None))

        self.assertIs(result, response)
        self.assertFalse(self.cc.stream.mock_calls)
Exemplo n.º 9
0
    def test_select_common_hashes_not_supported(self):
        with unittest.mock.patch.object(self.disco_client,
                                        "query_info",
                                        new=CoroutineMock()) as query_info:
            query_info.return_value = disco_xso.InfoQuery(
                features=('urn:xmpp:hash-function-text-names:md5', ))

            with self.assertRaisesRegex(
                    RuntimeError,
                    "Remote does not support the urn:xmpp:hashes:2 feature."):
                res = run_coroutine(
                    self.s.select_common_hashes(
                        unittest.mock.sentinel.other_jid))

        self.assertSequenceEqual(query_info.mock_calls, [
            unittest.mock.call(unittest.mock.sentinel.other_jid),
        ])
Exemplo n.º 10
0
    def test_check_for_pep_failure(self):

        with unittest.mock.patch.object(self.disco_client,
                                        "query_info",
                                        new=CoroutineMock()):
            self.disco_client.query_info.return_value = disco_xso.InfoQuery()

            # run twice, the second call must not query again but use the
            # cache
            with self.assertRaises(NotImplementedError):
                run_coroutine(self.s._check_for_pep())

            with self.assertRaises(NotImplementedError):
                run_coroutine(self.s._check_for_pep())

            self.assertEqual(1, len(self.disco_client.query_info.mock_calls))

            self.disco_client.query_info.assert_called_with(TEST_FROM.bare())
Exemplo n.º 11
0
    def setUp(self):
        self.cc = make_connected_client()
        self.s = disco_service.Service(self.cc)
        self.cc.reset_mock()

        self.request_iq = stanza.IQ(
            "get",
            from_=structs.JID.fromstr("[email protected]/res1"),
            to=structs.JID.fromstr("[email protected]/res2"))
        self.request_iq.autoset_id()
        self.request_iq.payload = disco_xso.InfoQuery()

        self.request_items_iq = stanza.IQ(
            "get",
            from_=structs.JID.fromstr("[email protected]/res1"),
            to=structs.JID.fromstr("[email protected]/res2"))
        self.request_items_iq.autoset_id()
        self.request_items_iq.payload = disco_xso.ItemsQuery()
Exemplo n.º 12
0
    def test_select_common_hashes_empty_intersection(self):
        with unittest.mock.patch.object(self.disco_client,
                                        "query_info",
                                        new=CoroutineMock()) as query_info:
            query_info.return_value = disco_xso.InfoQuery(features=(
                'urn:xmpp:hashes:2',
                'urn:xmpp:hash-function-text-names:md5',
            ))
            res = run_coroutine(
                self.s.select_common_hashes(unittest.mock.sentinel.other_jid))

        self.assertSequenceEqual(query_info.mock_calls, [
            unittest.mock.call(unittest.mock.sentinel.other_jid),
        ])
        self.assertEqual(
            res,
            set(),
        )
Exemplo n.º 13
0
    def test_query_info_deduplicate_requests(self):
        to = structs.JID.fromstr("[email protected]/res1")
        response = disco_xso.InfoQuery()

        with unittest.mock.patch.object(
                self.s, "send_and_decode_info_query",
                new=CoroutineMock()) as send_and_decode:
            response = {}

            send_and_decode.return_value = response

            result = run_coroutine(
                asyncio.gather(
                    self.s.query_info(to, timeout=10),
                    self.s.query_info(to, timeout=10),
                ))

            self.assertIs(result[0], response)
            self.assertIs(result[1], response)

        self.assertSequenceEqual([
            unittest.mock.call(to, None),
        ], send_and_decode.mock_calls)
Exemplo n.º 14
0
    def test_send_and_decode_info_query(self):
        to = structs.JID.fromstr("[email protected]/res1")
        node = "foobar"
        response = disco_xso.InfoQuery()

        self.cc.stream.send_iq_and_wait_for_reply.return_value = response

        result = run_coroutine(self.s.send_and_decode_info_query(to, node))

        self.assertEqual(result, response)

        self.assertEqual(
            1, len(self.cc.stream.send_iq_and_wait_for_reply.mock_calls))

        call, = self.cc.stream.send_iq_and_wait_for_reply.mock_calls
        # call[1] are args
        request_iq, = call[1]

        self.assertEqual(to, request_iq.to)
        self.assertEqual("get", request_iq.type_)
        self.assertIsInstance(request_iq.payload, disco_xso.InfoQuery)
        self.assertFalse(request_iq.payload.features)
        self.assertFalse(request_iq.payload.identities)
        self.assertIs(request_iq.payload.node, node)
Exemplo n.º 15
0
    def test__set_captured_events(self):
        data = object()

        iq = disco_xso.InfoQuery()
        iq._set_captured_events(data)
        self.assertIs(iq.captured_events, data)
Exemplo n.º 16
0
    def test_to_dict_emits_forms_with_identical_type(self):
        q = disco_xso.InfoQuery()
        q.identities.extend([
            disco_xso.Identity(category="client", type_="pc", name="foobar"),
            disco_xso.Identity(category="client",
                               type_="pc",
                               name="baz",
                               lang=structs.LanguageTag.fromstr("en-GB")),
        ])

        q.features.update([
            "foo",
            "bar",
            "baz",
        ])

        f = forms_xso.Data(type_=forms_xso.DataType.FORM)
        f.fields.extend([
            forms_xso.Field(type_=forms_xso.FieldType.HIDDEN,
                            var="FORM_TYPE",
                            values=[
                                "fnord",
                            ]),
            forms_xso.Field(type_=forms_xso.FieldType.TEXT_SINGLE,
                            var="uiae",
                            values=[
                                "nrtd",
                                "asdf",
                            ]),
            forms_xso.Field(type_=forms_xso.FieldType.FIXED),
        ])
        q.exts.append(f)

        f = forms_xso.Data(type_=forms_xso.DataType.FORM)
        f.fields.extend([
            forms_xso.Field(type_=forms_xso.FieldType.HIDDEN,
                            var="FORM_TYPE",
                            values=[
                                "fnord",
                            ]),
        ])
        q.exts.append(f)

        self.assertDictEqual(
            q.to_dict(), {
                "features": [
                    "bar",
                    "baz",
                    "foo",
                ],
                "identities": [
                    {
                        "category": "client",
                        "type": "pc",
                        "name": "foobar",
                    },
                    {
                        "category": "client",
                        "type": "pc",
                        "name": "baz",
                        "lang": "en-gb",
                    },
                ],
                "forms": [{
                    "FORM_TYPE": [
                        "fnord",
                    ],
                    "uiae": [
                        "nrtd",
                        "asdf",
                    ]
                }, {
                    "FORM_TYPE": [
                        "fnord",
                    ],
                }]
            })
Exemplo n.º 17
0
    def test_to_dict(self):
        q = disco_xso.InfoQuery()
        q.identities.extend([
            disco_xso.Identity(
                category="client",
                type_="pc",
                name="foobar"
            ),
            disco_xso.Identity(
                category="client",
                type_="pc",
                name="baz",
                lang=structs.LanguageTag.fromstr("en-GB")
            ),
        ])

        q.features.update(
            [
                "foo",
                "bar",
                "baz",
            ]
        )

        f = forms_xso.Data()
        f.fields.extend([
            forms_xso.Field(type_="hidden",
                            var="FORM_TYPE",
                            values=[
                                "fnord",
                            ]),
            forms_xso.Field(type_="text-single",
                            var="uiae",
                            values=[
                                "nrtd",
                                "asdf",
                            ]),
            forms_xso.Field(type_="fixed"),
        ])
        q.exts.append(f)

        self.assertDictEqual(
            q.to_dict(),
            {
                "features": [
                    "bar",
                    "baz",
                    "foo",
                ],
                "identities": [
                    {
                        "category": "client",
                        "type": "pc",
                        "name": "foobar",
                    },
                    {
                        "category": "client",
                        "type": "pc",
                        "name": "baz",
                        "lang": "en-gb",
                    },
                ],
                "forms": [
                    {
                        "FORM_TYPE": [
                            "fnord",
                        ],
                        "uiae": [
                            "nrtd",
                            "asdf",
                        ]
                    }
                ]
            }
        )