Ejemplo n.º 1
0
def test_setting_ui_flag_property_for_common_message() -> None:
    msg = Message.from_dict(
        {
            "sync_id": "a465f0f3-1354-491c-8f11-f400164295cb",
            "source_sync_id": "ff934be3-a03f-45d8-b315-738ba1ddec45",
            "command": {
                "body": "/cmd",
                "command_type": "user",
                "data": {
                    "ui": True
                },
                "metadata": {
                    "account_id": 94
                },
            },
            "file": None,
            "from": {
                "user_huid": None,
                "group_chat_id": "8dada2c8-67a6-4434-9dec-570d244e78ee",
                "ad_login": None,
                "ad_domain": None,
                "username": None,
                "chat_type": "group_chat",
                "host": "cts.ccteam.ru",
                "is_admin": False,
                "is_creator": False,
            },
            "bot_id": "dcfa5a7c-7cc4-4c89-b6c0-80325604f9f4",
        },
        Bot(),
    )

    assert msg.source_sync_id == uuid.UUID(
        "ff934be3-a03f-45d8-b315-738ba1ddec45")
Ejemplo n.º 2
0
def test_sync_bot_file_request(command_with_text_and_file, hostname, bot_id,
                               sync_requests):
    bot = Bot()
    bot.register_cts(CTS(host=hostname, secret_key="secret"))

    m = Message(**command_with_text_and_file)

    assert len(bot.send_file(m.file.file, m.sync_id, m.bot_id, m.host)) == 2
Ejemplo n.º 3
0
def test_sync_bot_error_requests(command_with_text_and_file, hostname, bot_id,
                                 sync_error_requests):
    bot = Bot()
    bot.register_cts(CTS(host=hostname, secret_key="secret"))

    m = Message(**command_with_text_and_file)
    assert bot.send_message(m.body, m.sync_id, m.bot_id, m.host) != 200
    assert bot.send_file(m.file.file, m.sync_id, m.bot_id, m.host)[1] != 200
Ejemplo n.º 4
0
def test_sync_answer_message(command_with_text_and_file, sync_requests):
    bot = Bot(disable_credentials=True)
    bot.start()

    message = Message(**command_with_text_and_file)
    bot.answer_message(message.body, message)

    bot.stop()
Ejemplo n.º 5
0
def test_message_command(command_with_text_and_file):
    m = Message(**command_with_text_and_file)
    cmd = m.command
    assert cmd.body == command_with_text_and_file["command"]["body"]
    assert cmd.cmd == command_with_text_and_file["command"]["body"].split(
        " ", 1)[0]
    assert cmd.cmd_arg == "".join(
        command_with_text_and_file["command"]["body"].split(" ", 1)[1:])
Ejemplo n.º 6
0
def test_sync_bot_notification_request(command_with_text_and_file, hostname,
                                       bot_id, sync_requests):
    bot = Bot()
    bot.register_cts(CTS(host=hostname, secret_key="secret"))

    m = Message(**command_with_text_and_file)
    assert (len(
        bot._send_notification_result(m.body, [m.group_chat_id], m.bot_id,
                                      m.host, m.file, "all", [], [], [])) == 2)
Ejemplo n.º 7
0
async def test_async_bot_message_as_command_sending(
        hostname, bot_id, async_requests, command_with_text_and_file):
    command_array = []
    notification_array = []

    async def custom_command_sending(text, chat_id, bot_id, host, file,
                                     recipients, mentions, bubble, keyboard):
        command_array.append((text, chat_id, bot_id, host, file, recipients,
                              mentions, bubble, keyboard))

    async def custom_notification_sending(text, group_chat_ids, bot_id, host,
                                          file, recipients, mentions, bubble,
                                          keyboard):
        notification_array.append((
            text,
            group_chat_ids,
            bot_id,
            host,
            file,
            recipients,
            mentions,
            bubble,
            keyboard,
        ))

    bot = AsyncBot()
    await bot.start()

    bot.register_cts(CTS(host=hostname, secret_key="secret"))

    bot._send_command_result = custom_command_sending
    bot._send_notification_result = custom_notification_sending

    m = Message(**command_with_text_and_file)

    await bot.send_message(m.body,
                           m.sync_id,
                           m.bot_id,
                           m.host,
                           file=m.file.file)

    assert len(command_array) == 1
    assert command_array[0] == (
        m.body,
        m.sync_id,
        m.bot_id,
        m.host,
        m.file,
        "all",
        [],
        [],
        [],
    )

    await bot.stop()
Ejemplo n.º 8
0
 def test_building_from_message(self,
                                sending_message: SendingMessage) -> None:
     builder = MessageBuilder()
     msg = Message(message=builder.message, bot=Bot())
     sending_msg = SendingMessage.from_message(
         text=sending_message.text,
         message=msg,
     )
     assert sending_msg.host == msg.host
     assert sending_msg.sync_id == msg.sync_id
     assert sending_msg.bot_id == msg.bot_id
Ejemplo n.º 9
0
def test_sync_bot_work_with_disabled_credentials(sync_requests,
                                                 command_with_text_and_file):
    bot = Bot(disable_credentials=True)

    def token_obtaining_mock(**data):
        raise Exception()

    bot._obtain_token = token_obtaining_mock

    m = Message(**command_with_text_and_file)
    bot.send_message(m.body, m.sync_id, m.bot_id, m.host)
Ejemplo n.º 10
0
async def test_async_bot_message_sending_error_requests(
        command_with_text_and_file, hostname, bot_id, async_error_requests):
    bot = AsyncBot()
    await bot.start()

    bot.register_cts(CTS(host=hostname, secret_key="secret"))

    m = Message(**command_with_text_and_file)
    assert (await bot.send_message(m.body, m.sync_id, m.bot_id, m.host)) != 200

    await bot.stop()
Ejemplo n.º 11
0
def test_message_properties(command_with_text_and_file):
    m = Message(**command_with_text_and_file)
    assert m.body == m.command.body == command_with_text_and_file["command"][
        "body"]
    assert m.data == m.command.data == command_with_text_and_file["command"][
        "data"]
    assert (m.user_huid == m.user.user_huid == UUID(
        command_with_text_and_file["from"]["user_huid"]))
    assert (m.ad_login == m.user.ad_login ==
            command_with_text_and_file["from"]["ad_login"])
    assert (m.group_chat_id == m.user.group_chat_id == UUID(
        command_with_text_and_file["from"]["group_chat_id"]))
    assert (m.chat_type == m.user.chat_type ==
            command_with_text_and_file["from"]["chat_type"])
    assert m.host == m.user.host == command_with_text_and_file["from"]["host"]
Ejemplo n.º 12
0
async def test_async_bot_requests(command_with_text_and_file, hostname, bot_id,
                                  async_requests):
    bot = AsyncBot()
    await bot.start()

    bot.register_cts(CTS(host=hostname, secret_key="secret"))

    m = Message(**command_with_text_and_file)
    assert (len(await
                bot._send_command_result(m.body, m.sync_id, m.bot_id, m.host,
                                         m.file, "all", [], [], [])) == 2)
    assert (len(await bot._send_notification_result(m.body, [m.group_chat_id],
                                                    m.bot_id, m.host, m.file,
                                                    "all", [], [], [])) == 2)
    assert len(await bot.send_file(m.file.file, m.sync_id, m.bot_id,
                                   m.host)) == 2

    await bot.stop()
Ejemplo n.º 13
0
def test_message_is_proxy_to_incoming_message(incoming_message) -> None:
    msg = Message.from_dict(incoming_message.dict(), Bot())
    assert msg.sync_id == incoming_message.sync_id
    assert msg.command == incoming_message.command
    assert msg.file == incoming_message.file
    assert msg.user == incoming_message.user
    assert msg.bot_id == incoming_message.bot_id
    assert msg.body == incoming_message.command.body
    assert msg.data == {**msg.metadata, **incoming_message.command.data_dict}
    assert msg.metadata == incoming_message.command.metadata
    assert msg.user_huid == incoming_message.user.user_huid
    assert msg.ad_login == incoming_message.user.ad_login
    assert msg.group_chat_id == incoming_message.user.group_chat_id
    assert msg.chat_type == incoming_message.user.chat_type
    assert msg.host == incoming_message.user.host
    assert msg.credentials.sync_id == incoming_message.sync_id
    assert msg.credentials.bot_id == incoming_message.bot_id
    assert msg.credentials.host == incoming_message.user.host
    assert msg.entities == incoming_message.entities
    assert msg.incoming_message == incoming_message
Ejemplo n.º 14
0
def message(incoming_message, bot):
    return Message.from_dict(incoming_message.dict(), bot)
Ejemplo n.º 15
0
 def test_mention_not_in_message(self, bot) -> None:
     builder = MessageBuilder()
     message = Message.from_dict(builder.message.dict(), bot)
     assert message.entities.mentions == []
Ejemplo n.º 16
0
def test_file_properties(command_with_text_and_file):
    m = Message(**command_with_text_and_file)
    assert m.file.file_name == command_with_text_and_file["file"]["file_name"]
    assert m.file.raw_data == base64.b64decode(
        command_with_text_and_file["file"]["data"].split(",", 1)[1])
    assert m.file.media_type == "application/json"
Ejemplo n.º 17
0
def test_data_from_file(command_with_text_and_file):
    m = Message(**command_with_text_and_file)
    with open(f"tests/files/{command_with_text_and_file['file']['file_name']}",
              "rb") as original_file:
        for message_file_line, line in zip(m.file.file, original_file):
            assert message_file_line == line
Ejemplo n.º 18
0
def test_setting_ui_flag_property_for_system_message(incoming_message) -> None:
    msg = Message.from_dict(incoming_message.dict(), Bot())
    assert not msg.source_sync_id
Ejemplo n.º 19
0
 def test_is_forward_message(self, message, bot) -> None:
     builder = MessageBuilder()
     builder.forward(message=message)
     new_message = Message.from_dict(message=builder.message.dict(),
                                     bot=bot)
     assert new_message.is_forward
Ejemplo n.º 20
0
def test_message_sync_id_is_sync_id_instance(command_with_text_and_file):
    assert isinstance(Message(**command_with_text_and_file).sync_id, SyncID)