Пример #1
0
def test_send_methods():
    bot = MockBot()
    chat_id = 42
    chat = Chat(bot, chat_id)

    chat.send_audio(b"foo")
    assert "sendAudio" in bot.calls

    chat.send_voice(b"foo")
    assert "sendVoice" in bot.calls

    chat.send_photo(b"foo")
    assert "sendPhoto" in bot.calls

    chat.send_sticker(b"foo")
    assert "sendSticker" in bot.calls

    chat.send_video(b"foo")
    assert "sendVideo" in bot.calls

    chat.send_document(b"foo")
    assert "sendDocument" in bot.calls

    chat.send_location(13.0, 37.0)
    assert "sendLocation" in bot.calls

    chat.send_venue(13.0, 37.0, b"foo", b"foo")
    assert "sendVenue" in bot.calls

    chat.send_contact("+79260000000", b"foo")
    assert "sendContact" in bot.calls

    chat.send_chat_action("typing")
    assert "sendChatAction" in bot.calls
Пример #2
0
def test_chat_reply():
    bot = MockBot()
    msg = text_msg("Reply!")
    chat = Chat.from_message(bot, msg)

    chat.reply("Hi " + repr(chat.sender))
    assert "sendMessage" in bot.calls
    assert bot.calls["sendMessage"]["text"] == "Hi John"
Пример #3
0
def test_chat_methods():
    bot = MockBot()
    chat_id = 42
    chat = Chat(bot, chat_id)

    chat.send_text("hello")
    assert "sendMessage" in bot.calls
    assert bot.calls["sendMessage"]["text"] == "hello"
Пример #4
0
def test_edit_message():
    bot = MockBot()
    chat_id = 42
    message_id = 1337
    chat = Chat(bot, chat_id)

    chat.edit_text(message_id, "bye")
    assert "editMessageText" in bot.calls
    assert bot.calls["editMessageText"]["text"] == "bye"
    assert bot.calls["editMessageText"]["message_id"] == message_id
Пример #5
0
def test_edit_reply_markup():
    bot = MockBot()
    chat_id = 42
    message_id = 1337
    chat = Chat(bot, chat_id)

    chat.edit_reply_markup(message_id, {"inline_keyboard": [["ok", "cancel"]]})
    assert "editMessageReplyMarkup" in bot.calls
    call = bot.calls["editMessageReplyMarkup"]
    assert call["reply_markup"] == '{"inline_keyboard": [["ok", "cancel"]]}'
    assert call["message_id"] == message_id
Пример #6
0
def test_inline_answer():
    bot = MockBot()
    src = inline_query("Answer!")
    iq = InlineQuery(bot, src)

    results = [{
        "type": "article",
        "id": "000",
        "title": "test",
        "message_text": "Foo bar"
    }]
    iq.answer(results)
    assert "answerInlineQuery" in bot.calls
    assert isinstance(bot.calls["answerInlineQuery"]["results"], str)
Пример #7
0
def test_webhooks_integration():
    bot = MockBot()
    called_with = None

    @bot.command(r"/echo (.+)")
    def echo(chat, match):
        nonlocal called_with
        called_with = match.group(1)
        # Let's check sender repr as well
        assert repr(chat.sender) == "John"

    bot.set_webhook(webhook_url)
    assert "setWebhook" in bot.calls

    thread = Thread(target=bot_loop, args=[bot], daemon=True)
    thread.start()
    server_started.wait()

    update = {
        "update_id": 0,
        "message": {
            "message_id": 0,
            "from": {
                "first_name": "John"
            },
            "chat": {
                "id": 0,
                "type": "private"
            },
            "text": "/echo foo",
        },
    }

    import requests

    requests.post(webhook_url, json=update)
    assert called_with == "foo"
Пример #8
0
def test_delete_webhook():
    bot = MockBot()
    bot.delete_webhook()
    assert "deleteWebhook" in bot.calls
Пример #9
0
def test_set_webhook():
    bot = MockBot()
    bot.set_webhook(webhook_url)
    assert "setWebhook" in bot.calls
Пример #10
0
def bot():
    return MockBot()