コード例 #1
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_set_presence_with_status_mapping(self):
        # not established
        self.cc.established = False

        m = {
            None: "foo",
            aioxmpp.structs.LanguageTag.fromstr("en-gb"): "bar",
        }

        self.s.set_presence(
            aioxmpp.PresenceState(True),
            status=m,
        )

        self.assertEqual(
            self.s.state,
            aioxmpp.PresenceState(True)
        )

        self.assertDictEqual(
            self.s.status,
            m,
        )

        self.assertIsNot(self.s.status, m)

        del m[None]

        self.assertIn(None, self.s.status)
コード例 #2
0
    def test_set_presence_with_status_string(self):
        # not established
        self.cc.established = False

        self.s.set_presence(aioxmpp.PresenceState(True), status="foo")

        self.assertEqual(self.s.state, aioxmpp.PresenceState(True))

        self.assertDictEqual(self.s.status, {
            None: "foo",
        })
コード例 #3
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_set_presence_rejects_non_integer_priority(self):
        self.cc.established = False

        with self.assertRaisesRegex(
                TypeError,
                r"invalid priority: got <class 'str'>, expected integer"):
            self.s.set_presence(
                aioxmpp.PresenceState(True),
                status="foo",
                priority="10",
            )

        self.assertEqual(self.s.state, aioxmpp.PresenceState(False))
        self.assertDictEqual(self.s.status, {})
コード例 #4
0
ファイル: xmpp.py プロジェクト: pooplyhhhh/fortnitepy
    def set_presence(self, status: Optional[Union[str, dict]] = None) -> None:
        if status is None:
            self.xmpp_client.presence = aioxmpp.PresenceState(available=True)

        elif isinstance(status, dict):
            self.xmpp_client.set_presence(
                aioxmpp.PresenceState(available=True),
                json.dumps(status),
            )

        else:
            self.xmpp_client.set_presence(
                aioxmpp.PresenceState(available=True),
                json.dumps({'Status': status}),
            )
コード例 #5
0
    def __init__(self, bot, jabber_server, logger):
        config = jabber_server['jabber_id'].split(':')
        jabber_id = aioxmpp.JID.fromstr(config[0])
        override_peer = []
        if len(config) > 1:
            port = config[1]
            override_peer = [(jabber_id.domain, port,
                              aioxmpp.connector.STARTTLSConnector())]

        super(JabberRelay, self).__init__(
            aioxmpp.JID.fromstr(jabber_server['jabber_id']),
            aioxmpp.make_security_layer(jabber_server['password'],
                                        no_verify=True),
            override_peer=override_peer,
            logger=logger)

        self.bot = bot
        self.relay_from = jabber_server['relay_from']
        self.jabber_server = jabber_server
        self.embed_colour = get_embed_colour(jabber_server['logo_url'])
        self.languages = [LanguageRange(tag='en'), LanguageRange.WILDCARD]
        self.summon(aioxmpp.DiscoServer)
        self.summon(aioxmpp.RosterClient)

        message_dispatcher = self.summon(
            aioxmpp.dispatcher.SimpleMessageDispatcher)
        message_dispatcher.register_callback(None, None,
                                             self.message_receieved)

        self.presence = aioxmpp.PresenceState(True, aioxmpp.PresenceShow.AWAY)
コード例 #6
0
ファイル: Core.py プロジェクト: ChinSing00/ChatChat
 def start(self, user):
     Log.info('开始登陆', user)
     self.jid = aioxmpp.JID.fromstr(user['JID'])
     self.password = user['PWD']
     try:
         # no_verify=True大坑!,注意关闭认证;参考:https://docs.zombofant.net/aioxmpp/devel/api/public/security_layer.html
         # 可能要自己定义security_layer以达到通信安全
         self.client = aioxmpp.PresenceManagedClient(
             self.jid,
             aioxmpp.make_security_layer(self.password, no_verify=True),
             loop=self._loop)
         self.client.set_presence(
             aioxmpp.PresenceState(available=True,
                                   show=aioxmpp.PresenceShow.FREE_FOR_CHAT),
             {aioxmpp.structs.LanguageTag.fromstr('en'): '在线'})
         self.client.on_failure.connect(self.on_failure)  #启动失败时操作
         self.client.on_stream_established.connect(
             self.on_login_success)  #在链接服务器时操作
         self.client.on_stream_suspended.connect(
             self.on_internet_disconnect)  #服务器链接中断时操作
         self.client.on_stream_destroyed.connect(
             self.on_internet_disconnect)  #服务器链接失败时操作
     except ConnectionRefusedError:
         self._sign_login.emit(3)
         return
     except Exception:
         return
コード例 #7
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_set_presence_state_emits_events(self):
        new_state = aioxmpp.PresenceState(True)
        new_status = {None: "foo"}
        new_priority = -2

        def check_values():
            self.assertEqual(
                self.s.state,
                new_state,
            )
            self.assertDictEqual(
                self.s.status,
                new_status,
            )
            self.assertEqual(
                self.s.priority,
                new_priority,
            )

        overall_cb = unittest.mock.Mock()
        overall_cb.side_effect = check_values
        state_cb = unittest.mock.Mock()
        state_cb.side_effect = check_values

        self.s.on_presence_changed.connect(overall_cb)
        self.s.on_presence_state_changed.connect(state_cb)

        self.s.set_presence(
            new_state,
            status=new_status,
            priority=new_priority,
        )

        overall_cb.assert_called_once_with()
        state_cb.assert_called_once_with()
コード例 #8
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_set_presence_state_does_not_emit_events_if_unchanged(self):
        new_state = aioxmpp.PresenceState(False)
        new_status = {}
        new_priority = 0

        def check_values():
            self.assertEqual(
                self.s.state,
                new_state,
            )
            self.assertDictEqual(
                self.s.status,
                new_status,
            )
            self.assertEqual(
                self.s.priority,
                new_priority,
            )

        overall_cb = unittest.mock.Mock()
        overall_cb.side_effect = check_values
        state_cb = unittest.mock.Mock()
        state_cb.side_effect = check_values

        self.s.on_presence_changed.connect(overall_cb)
        self.s.on_presence_state_changed.connect(state_cb)

        self.s.set_presence(
            new_state,
            status=new_status,
            priority=new_priority,
        )

        overall_cb.assert_not_called()
        state_cb.assert_not_called()
コード例 #9
0
ファイル: test_service.py プロジェクト: yurimataev/aioxmpp
    def test_emit_configured_presence_when_stream_establishes(self):
        self.s.set_presence(aioxmpp.PresenceState(True))

        with unittest.mock.patch.object(self.s, "make_stanza") as make_stanza:
            make_stanza.return_value = unittest.mock.sentinel.presence
            run_coroutine(self.cc.before_stream_established())

        self.cc.send.assert_called_with(unittest.mock.sentinel.presence, )
コード例 #10
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_make_stanza_converts_incorporates_status(self):
        self.s.set_presence(aioxmpp.PresenceState(True), status="foo")

        stanza = self.s.make_stanza()
        self.assertDictEqual(
            stanza.status,
            {None: "foo"}
        )
コード例 #11
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_no_presence_to_emit_by_default(self):
        self.assertEqual(self.s.state, aioxmpp.PresenceState(False))
        self.assertDictEqual(self.s.status, {})
        self.assertEqual(self.s.priority, 0)

        run_coroutine(self.cc.before_stream_established())

        self.cc.send.assert_not_called()
コード例 #12
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_set_presence_priority(self):
        self.cc.established = False

        self.s.set_presence(aioxmpp.PresenceState(False), priority=10)

        self.assertEqual(
            self.s.priority,
            10,
        )
コード例 #13
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_set_presence_state_does_not_emit_stanza_if_unchanged(self):
        new_state = aioxmpp.PresenceState(False)
        new_status = {}
        new_priority = 0

        self.s.set_presence(
            new_state,
            status=new_status,
            priority=new_priority,
        )

        self.cc.enqueue.assert_not_called()
コード例 #14
0
    async def get_connected_client(self,
                                   presence=aioxmpp.PresenceState(True),
                                   *,
                                   services=[],
                                   prepare=None):
        """
        Return a connected client to a unique XMPP account.

        :param presence: initial presence to emit
        :type presence: :class:`aioxmpp.PresenceState`
        :param prepare: a coroutine run after the services
            are summoned but before the client connects.
        :type prepare: coroutine receiving the client
             as argument
        :raise OSError: if the connection failed
        :raise RuntimeError: if a client could not be provisioned due to
                             resource constraints
        :return: Connected presence managed client
        :rtype: :class:`aioxmpp.PresenceManagedClient`

        Each account used by the clients returned from this method is unique;
        all clients are guaranteed to have different bare JIDs.

        The clients and accounts are cleaned up after the tear down of the test
        runs. Some provisioners may have a limit on the number of accounts
        which can be used in the same test.

        Clients obtained from this function are cleaned up automatically on
        tear down of the test. The clients are stopped and the accounts
        deleted or cleared, so that each test starts with a fully fresh state.

        A coroutine may be passed as `prepare` argument. It is called
        with the client as the single argument after all services in
        `services` have been summoned but before the client connects,
        this is for example useful to connect signals that fire early
        in the connection process.
        """
        id_ = self.__counter
        self.__counter += 1
        self._logger.debug("obtaining client%d from %r", id_, self)
        logger = self._logger.getChild("client{}".format(id_))
        client = await self._make_client(logger)
        for service in services:
            client.summon(service)
        if prepare is not None:
            await prepare(client)
        cm = client.connected(presence=presence)
        await cm.__aenter__()
        self._accounts_to_dispose.append(cm)
        return client
コード例 #15
0
ファイル: test_service.py プロジェクト: yurimataev/aioxmpp
    def test_set_presence_broadcasts_if_established(self):
        self.cc.established = True

        def check_state():
            self.assertEqual(self.cc.state, aioxmpp.PresenceState(True))
            self.assertEqual(self.cc.status, {None: "foo"})

        with unittest.mock.patch.object(self.s, "make_stanza") as make_stanza:
            make_stanza.return_value = unittest.mock.sentinel.presence

            result = self.s.set_presence(
                aioxmpp.PresenceState(True),
                status="foo",
            )

        self.cc.enqueue.assert_called_with(unittest.mock.sentinel.presence)

        self.assertEqual(result, self.cc.enqueue())
コード例 #16
0
ファイル: core.py プロジェクト: horazont/hint-lib
 async def __aenter__(self):
     self.__nested_cm = self.client.connected(
         presence=aioxmpp.PresenceState(True), )
     return (await self.__nested_cm.__aenter__())
コード例 #17
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
 def check_state():
     self.assertEqual(self.cc.state, aioxmpp.PresenceState(True))
     self.assertEqual(self.cc.status, {None: "foo"})
コード例 #18
0
 def disconnect(self):
     self.presence = aioxmpp.PresenceState(False)
コード例 #19
0
ファイル: test_service.py プロジェクト: rotoql/aioxmpp
    def test_before_stream_established_handler_returns_true_for_avail(self):
        self.s.set_presence(aioxmpp.PresenceState(True))

        self.assertTrue(
            run_coroutine(self.s._before_stream_established())
        )