Пример #1
0
def test_cts(hostname, bot_id, secret, right_signature):
    assert (CTS(host=hostname, secret_key=secret).calculate_signature(bot_id)
            == base64.b16encode(
                hmac.new(key=secret.encode(),
                         msg=str(bot_id).encode(),
                         digestmod=hashlib.sha256).digest()).decode() ==
            right_signature)
Пример #2
0
def test_sync_bot_token_obtaining_with_errored_request(hostname, bot_id,
                                                       sync_error_requests):
    bot = Bot()
    cts = CTS(host=hostname, secret_key="secret")
    bot.register_cts(cts)

    bot._obtain_token(hostname, bot_id)
    assert bot._credentials.known_cts[cts.host][1] is None
Пример #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
Пример #4
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
Пример #5
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)
Пример #6
0
def test_init(custom_base_bot_class, hostname):
    bot = custom_base_bot_class()
    assert bot._credentials == BotCredentials()

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

    bot2 = custom_base_bot_class(credentials=credentials)
    assert bot2._credentials == credentials
Пример #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()
Пример #8
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()
Пример #9
0
def test_bot_storage_credentials_retrieving(custom_base_bot_class, hostname):
    bot = custom_base_bot_class()

    assert not bot._get_token_from_credentials(hostname)

    cts = CTS(host=hostname, secret_key="secret_key")
    bot._credentials.known_cts[cts.host] = (
        cts,
        CTSCredentials(bot_id=uuid.uuid4(), result="token_for_bot"),
    )

    assert bot._get_token_from_credentials(hostname) == "token_for_bot"
Пример #10
0
def test_bot_credentials_update(custom_base_bot_class, bot_id, hostname,
                                secret):
    bot = custom_base_bot_class(credentials=BotCredentials(
        known_cts={
            hostname: [
                CTS(host=hostname, secret_key=secret),
                CTSCredentials(bot_id=bot_id,
                               result="result_token_for_operations"),
            ]
        }))

    bot.add_cts_credentials(
        BotCredentials(
            known_cts={
                hostname: [
                    CTS(host=hostname, secret_key=secret),
                    CTSCredentials(
                        bot_id=bot_id,
                        result="result_token_for_operations_replaced"),
                ]
            }))

    assert (bot.get_cts_credentials().known_cts[hostname][1].result ==
            "result_token_for_operations_replaced")

    second_host = hostname + "2"
    bot.add_cts_credentials(
        BotCredentials(
            known_cts={
                second_host: [
                    CTS(host=second_host, secret_key=secret),
                    CTSCredentials(
                        bot_id=bot_id,
                        result="result_token_for_operations_replaced"),
                ]
            }))

    assert len(bot.get_cts_credentials().known_cts) == 2
Пример #11
0
def test_sync_bot_token_obtaining(hostname, bot_id, sync_requests):
    bot = Bot()

    with pytest.raises(BotXException):
        bot._obtain_token(hostname, bot_id)

    cts = CTS(host=hostname, secret_key="secret")

    bot.register_cts(cts)
    bot._obtain_token(hostname, bot_id)
    assert bot._credentials.known_cts[cts.host] == (
        cts,
        CTSCredentials(bot_id=bot_id, result="token_for_operations"),
    )
Пример #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()
Пример #13
0
def test_cts_registration(custom_base_bot_class, hostname):
    bot = custom_base_bot_class()
    cts = CTS(host=hostname, secret_key="secret_key")
    bot.register_cts(cts)

    assert bot.get_cts_credentials().known_cts[hostname] == (cts, None)