示例#1
0
    def test_message_with_python_handler(self):
        """ Test messaging with python handler """

        messager = messaging.Messaging()
        messager.set_handler(messaging.PythonHandler())

        messages = []

        def receive_msg():
            """ Worker thread which add message to messages """

            for msg in messager.listen():
                messages.append(msg)

        thread = threading.Thread(target=receive_msg)
        thread.start()
        time.sleep(0.1)

        msg1 = event.Event(event.EventKind.chat_message, data={"bla": "bla"})
        msg2 = event.Event(event.EventKind.chat_message)
        msg_invalid = "Bla"

        messager.send(msg1)
        messager.send(msg2)
        with self.assertRaises(ValueError):
            messager.send(msg_invalid)

        messager.stop()
        time.sleep(0.1)
        self.assertFalse(thread.is_alive())
        self.assertEqual(messages, [msg1, msg2])
        self.assertEqual(messages[0].data, msg1.data)
        self.assertEqual(messages[0].kind, msg1.kind)
        self.assertEqual(messages[1].data, msg2.data)
        self.assertEqual(messages[1].kind, msg2.kind)
示例#2
0
    def test_run(self, msg_listen, has_room):
        """ Test running the listener """

        msg1 = event.Event(event.EventKind.chat_message, data="bla", room_id=3)
        msg2 = event.Event(event.EventKind.chat_message,
                           data="blabla",
                           token="aa")
        msg_listen.return_value = [msg1, msg2]

        worker = mock.MagicMock()
        self.listener.dispatch = {event.EventKind.chat_message: worker}

        has_room.return_value = True
        worker.need_session = True

        self.listener.run()
        self.assertNotLog("ERROR")
        msg_listen.assert_called_once()
        self.assertEqual(worker.handle.call_count, 2)

        args, kwargs = worker.handle.call_args_list[0]
        self.assertEqual(args[0], msg1.data)
        self.assertEqual(kwargs["token"], msg1.token)
        self.assertIn("session", kwargs)

        args, kwargs = worker.handle.call_args_list[1]
        self.assertEqual(args[0], msg2.data)
        self.assertEqual(kwargs["token"], msg2.token)
        self.assertIn("session", kwargs)
示例#3
0
    def test_run_exceptions(self, msg_listen):
        """ Test running the listener with unexpected error"""

        self.listener.dispatch = {}

        msg1 = event.Event(event.EventKind.chat_message, data="bla")
        msg_listen.return_value = [msg1]

        self.listener.run()
        logs = self.get_logs("ERROR")
        self.assertEqual(len(logs), 1)
        self.assertEqual(
            logs[0].message,
            "Unknown event kind %s" % event.EventKind.chat_message)
        self.reset_log()

        worker = mock.MagicMock()
        worker.handle = mock.Mock(side_effect=Exception())

        self.listener.dispatch = {event.EventKind.chat_message: worker}

        self.listener.run()
        self.assertEqual(worker.handle.call_args[0][0], "bla")
        self.assertIn("session", worker.handle.call_args[1])
        self.assertLog("ERROR")

        self.reset_log()

        worker.need_session = False
        self.listener.run()
        self.assertEqual(worker.handle.call_args[0][0], "bla")
        self.assertNotIn("session", worker.handle.call_args[1])
        self.assertLog("ERROR")
示例#4
0
    def test_send(send):
        """ Test sending a message with the global helper """

        msg = event.Event(event.EventKind.chat_message)

        messaging.send(msg)

        send.assert_called_once_with(msg)
示例#5
0
def send_event(kind, data=None, token=None, room_id=None):
    """ Send an event with the global message class """

    _MESSAGING.send(event.Event(
        kind=kind,
        data=data,
        token=token,
        room_id=room_id
    ))
示例#6
0
    def test_run_without_room(self, msg_listen, has_room):
        """ Test running the listener without room """

        msg1 = event.Event(event.EventKind.chat_message, data="bla", room_id=3)
        msg_listen.return_value = [msg1]

        worker = mock.MagicMock()
        self.listener.dispatch = {event.EventKind.chat_message: worker}

        has_room.return_value = False

        self.listener.run()
        self.assertNotLog("ERROR")
        msg_listen.assert_called_once()
        worker.handle.assert_not_called()
示例#7
0
    def test_encode_decode(self):
        """ Test encoding and decoding a message """

        msg = event.Event(
            event.EventKind.chat_message,
            data={"a": "b"},
            token="bla",
            room_id=3,
        )

        msg_after_decode = event.Event.decode(msg.encode())

        self.assertEqual(msg, msg_after_decode)
        self.assertEqual(msg.kind, msg_after_decode.kind)
        self.assertEqual(msg.data, msg_after_decode.data)
        self.assertEqual(msg.token, msg_after_decode.token)
        self.assertEqual(msg.room_id, msg_after_decode.room_id)
示例#8
0
def send_message_token(token, message, source=None):
    """
        Send a chat message to the given target

        :param str token: target of the message (token of the connection)
        :param str message: Message to send
        :param str source: token which have send the event
    """

    msg = event.Event(
        kind=event.EventKind.chat_message,
        data={
            "source": source,
            "message": message,
            "target": {"type": "token", "value": token},
            "type": "chat"
        },
        token=source,
    )
    messaging.send(msg)
示例#9
0
def send_message_room(room_id, message, source=None):
    """
        Send a chat message to the given target

        :param room: room target of the message
        :type room: smserver.models.room.Room
        :param str message: Message to send
        :param str source: token which have send the event
    """

    msg = event.Event(
        kind=event.EventKind.chat_message,
        data={
            "source": source,
            "message": message,
            "target": {"type": "room", "value": room_id},
            "type": "chat"
        },
        room_id=room_id,
        token=source,
    )
    messaging.send(msg)