Beispiel #1
0
    def test_equality(self):
        ident1 = disco_xso.Identity()
        self.assertEqual("client", ident1.category)
        self.assertEqual("bot", ident1.type_)
        self.assertIsNone(ident1.name)
        self.assertIsNone(ident1.lang)

        ident2 = disco_xso.Identity()
        self.assertEqual("client", ident2.category)
        self.assertEqual("bot", ident2.type_)
        self.assertIsNone(ident2.name)
        self.assertIsNone(ident2.lang)

        self.assertTrue(ident1 == ident2)
        self.assertFalse(ident1 != ident2)

        ident1.category = "foo"

        self.assertFalse(ident1 == ident2)
        self.assertTrue(ident1 != ident2)

        ident2.category = "foo"

        self.assertTrue(ident1 == ident2)
        self.assertFalse(ident1 != ident2)

        ident1.type_ = "bar"

        self.assertFalse(ident1 == ident2)
        self.assertTrue(ident1 != ident2)

        ident2.type_ = "bar"

        self.assertTrue(ident1 == ident2)
        self.assertFalse(ident1 != ident2)

        ident1.name = "baz"

        self.assertFalse(ident1 == ident2)
        self.assertTrue(ident1 != ident2)

        ident2.name = "baz"

        self.assertTrue(ident1 == ident2)
        self.assertFalse(ident1 != ident2)

        ident1.lang = structs.LanguageTag.fromstr("en")

        self.assertFalse(ident1 == ident2)
        self.assertTrue(ident1 != ident2)

        ident2.lang = structs.LanguageTag.fromstr("en")

        self.assertTrue(ident1 == ident2)
        self.assertFalse(ident1 != ident2)
Beispiel #2
0
    def test_init(self):
        ident = disco_xso.Identity()
        self.assertEqual("client", ident.category)
        self.assertEqual("bot", ident.type_)
        self.assertIsNone(ident.name)
        self.assertIsNone(ident.lang)

        ident = disco_xso.Identity(category="account",
                                   type_="anonymous",
                                   name="Foobar",
                                   lang=structs.LanguageTag.fromstr("de"))
        self.assertEqual("account", ident.category)
        self.assertEqual("anonymous", ident.type_)
        self.assertEqual("Foobar", ident.name)
        self.assertEqual(structs.LanguageTag.fromstr("DE"), ident.lang)
Beispiel #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())
Beispiel #4
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())
Beispiel #5
0
    def test_equality_is_robust_against_other_data_types(self):
        ident1 = disco_xso.Identity()
        self.assertEqual("client", ident1.category)
        self.assertEqual("bot", ident1.type_)
        self.assertIsNone(ident1.name)
        self.assertIsNone(ident1.lang)

        self.assertFalse(ident1 == None)  # NOQA
        self.assertFalse(ident1 == 1)
        self.assertFalse(ident1 == "foo")

        self.assertTrue(ident1 != None)  # NOQA
        self.assertTrue(ident1 != 1)
        self.assertTrue(ident1 != "foo")
Beispiel #6
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",
                    ],
                }]
            })
Beispiel #7
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",
                        ]
                    }
                ]
            }
        )