2017-08-11 23:58:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2017-08-11 23:58:41 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2022-04-24 12:38:09 +02:00
|
|
|
import asyncio
|
2022-05-05 09:27:54 +02:00
|
|
|
import re
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import Bot, Chat, Message, Update
|
2022-05-26 11:10:00 +02:00
|
|
|
from telegram.ext import CallbackContext, CommandHandler, JobQueue, filters
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.build_messages import (
|
|
|
|
make_command_message,
|
|
|
|
make_command_update,
|
|
|
|
make_message_update,
|
|
|
|
)
|
|
|
|
from tests.auxil.slots import mro_slots
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
def is_match(handler, update):
|
|
|
|
"""
|
|
|
|
Utility function that returns whether an update matched
|
|
|
|
against a specific handler.
|
|
|
|
:param handler: ``CommandHandler`` to check against
|
|
|
|
:param update: update to check
|
|
|
|
:return: (bool) whether ``update`` matched with ``handler``
|
|
|
|
"""
|
|
|
|
check = handler.check_update(update)
|
|
|
|
return check is not None and check is not False
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class BaseTest:
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Base class for command and prefix handler test classes. Contains
|
|
|
|
utility methods an several callbacks used by both classes."""
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
test_flag = False
|
2019-02-13 12:07:25 +01:00
|
|
|
SRE_TYPE = type(re.match("", ""))
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
@pytest.fixture(autouse=True)
|
2023-03-25 19:18:04 +01:00
|
|
|
def _reset(self):
|
2017-08-11 23:58:41 +02:00
|
|
|
self.test_flag = False
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def response(self, application, update):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""
|
|
|
|
Utility to send an update to a dispatcher and assert
|
|
|
|
whether the callback was called appropriately. Its purpose is
|
|
|
|
for repeated usage in the same test function.
|
|
|
|
"""
|
|
|
|
self.test_flag = False
|
2022-04-24 12:38:09 +02:00
|
|
|
async with application:
|
|
|
|
await application.process_update(update)
|
2019-10-12 15:11:09 +02:00
|
|
|
return self.test_flag
|
|
|
|
|
2021-08-29 18:15:04 +02:00
|
|
|
def callback_basic(self, update, context):
|
|
|
|
test_bot = isinstance(context.bot, Bot)
|
2017-08-11 23:58:41 +02:00
|
|
|
test_update = isinstance(update, Update)
|
|
|
|
self.test_flag = test_bot and test_update
|
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
def make_callback_for(self, pass_keyword):
|
|
|
|
def callback(bot, update, **kwargs):
|
2021-04-05 13:25:27 +02:00
|
|
|
self.test_flag = kwargs.get(keyword) is not None
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
keyword = pass_keyword[5:]
|
|
|
|
return callback
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def callback(self, update, context):
|
2020-10-09 17:22:07 +02:00
|
|
|
self.test_flag = (
|
|
|
|
isinstance(context, CallbackContext)
|
|
|
|
and isinstance(context.bot, Bot)
|
|
|
|
and isinstance(update, Update)
|
2022-04-24 12:38:09 +02:00
|
|
|
and isinstance(context.update_queue, asyncio.Queue)
|
2020-10-09 17:22:07 +02:00
|
|
|
and isinstance(context.job_queue, JobQueue)
|
|
|
|
and isinstance(context.user_data, dict)
|
|
|
|
and isinstance(context.chat_data, dict)
|
|
|
|
and isinstance(context.bot_data, dict)
|
|
|
|
and isinstance(update.message, Message)
|
|
|
|
)
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def callback_args(self, update, context):
|
2022-05-05 17:40:22 +02:00
|
|
|
self.test_flag = context.args == ["one", "two"]
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def callback_regex1(self, update, context):
|
2019-02-13 12:07:25 +01:00
|
|
|
if context.matches:
|
2021-05-27 09:38:17 +02:00
|
|
|
types = all(type(res) is self.SRE_TYPE for res in context.matches)
|
2019-02-13 12:07:25 +01:00
|
|
|
num = len(context.matches) == 1
|
|
|
|
self.test_flag = types and num
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def callback_regex2(self, update, context):
|
2019-02-13 12:07:25 +01:00
|
|
|
if context.matches:
|
2021-05-27 09:38:17 +02:00
|
|
|
types = all(type(res) is self.SRE_TYPE for res in context.matches)
|
2019-02-13 12:07:25 +01:00
|
|
|
num = len(context.matches) == 2
|
|
|
|
self.test_flag = types and num
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def _test_context_args_or_regex(self, app, handler, text):
|
|
|
|
app.add_handler(handler)
|
|
|
|
update = make_command_update(text, bot=app.bot)
|
|
|
|
assert not await self.response(app, update)
|
2022-12-15 15:00:36 +01:00
|
|
|
update = make_command_update(text + " one two", bot=app.bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await self.response(app, update)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
def _test_edited(self, message, handler_edited, handler_not_edited):
|
|
|
|
"""
|
|
|
|
Assert whether a handler that should accept edited messages
|
|
|
|
and a handler that shouldn't work correctly.
|
|
|
|
:param message: ``telegram.Message`` to check against the handlers
|
|
|
|
:param handler_edited: handler that should accept edited messages
|
|
|
|
:param handler_not_edited: handler that should not accept edited messages
|
|
|
|
"""
|
|
|
|
update = make_command_update(message)
|
|
|
|
edited_update = make_command_update(message, edited=True)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
assert is_match(handler_edited, update)
|
|
|
|
assert is_match(handler_edited, edited_update)
|
|
|
|
assert is_match(handler_not_edited, update)
|
|
|
|
assert not is_match(handler_not_edited, edited_update)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
# ----------------------------- CommandHandler -----------------------------
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
class TestCommandHandler(BaseTest):
|
2022-05-05 17:40:22 +02:00
|
|
|
CMD = "/test"
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self):
|
2021-05-29 16:18:16 +02:00
|
|
|
handler = self.make_default_handler()
|
|
|
|
for attr in handler.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(handler, attr, "err") != "err", f"got extra slot '{attr}'"
|
2021-05-29 16:18:16 +02:00
|
|
|
assert len(mro_slots(handler)) == len(set(mro_slots(handler))), "duplicate slot"
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2019-10-12 15:11:09 +02:00
|
|
|
def command(self):
|
|
|
|
return self.CMD
|
2017-10-14 20:04:02 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2022-04-24 12:38:09 +02:00
|
|
|
def command_message(self, command, bot):
|
|
|
|
return make_command_message(command, bot=bot)
|
2018-09-21 08:57:43 +02:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2019-10-12 15:11:09 +02:00
|
|
|
def command_update(self, command_message):
|
|
|
|
return make_command_update(command_message)
|
2017-10-14 20:04:02 +02:00
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
def make_default_handler(self, callback=None, **kwargs):
|
|
|
|
callback = callback or self.callback_basic
|
|
|
|
return CommandHandler(self.CMD[1:], callback, **kwargs)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_basic(self, app, command):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Test whether a command handler responds to its command
|
|
|
|
and not to others, or badly formatted commands"""
|
|
|
|
handler = self.make_default_handler()
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await self.response(app, make_command_update(command, bot=app.bot))
|
|
|
|
assert not is_match(handler, make_command_update(command[1:], bot=app.bot))
|
2022-05-05 17:40:22 +02:00
|
|
|
assert not is_match(handler, make_command_update(f"/not{command[1:]}", bot=app.bot))
|
|
|
|
assert not is_match(handler, make_command_update(f"not {command} at start", bot=app.bot))
|
2022-05-26 11:10:00 +02:00
|
|
|
assert not is_match(
|
|
|
|
handler, make_message_update(bot=app.bot, message=None, caption="caption")
|
|
|
|
)
|
|
|
|
|
|
|
|
handler = CommandHandler(["FOO", "bAR"], callback=self.callback)
|
|
|
|
assert isinstance(handler.commands, frozenset)
|
|
|
|
assert handler.commands == {"foo", "bar"}
|
|
|
|
|
|
|
|
handler = CommandHandler(["FOO"], callback=self.callback)
|
|
|
|
assert isinstance(handler.commands, frozenset)
|
|
|
|
assert handler.commands == {"foo"}
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
@pytest.mark.parametrize(
|
2022-05-05 17:40:22 +02:00
|
|
|
"cmd",
|
|
|
|
["way_too_longcommand1234567yes_way_toooooooLong", "ïñválídletters", "invalid #&* chars"],
|
|
|
|
ids=["too long", "invalid letter", "invalid characters"],
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-10-12 15:11:09 +02:00
|
|
|
def test_invalid_commands(self, cmd):
|
2021-12-31 16:04:21 +01:00
|
|
|
with pytest.raises(
|
2022-05-05 17:40:22 +02:00
|
|
|
ValueError, match=f"`{re.escape(cmd.lower())}` is not a valid bot command"
|
2021-12-31 16:04:21 +01:00
|
|
|
):
|
2019-10-12 15:11:09 +02:00
|
|
|
CommandHandler(cmd, self.callback_basic)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_command_list(self, bot):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""A command handler with multiple commands registered should respond to all of them."""
|
2022-05-05 17:40:22 +02:00
|
|
|
handler = CommandHandler(["test", "star"], self.callback_basic)
|
|
|
|
assert is_match(handler, make_command_update("/test", bot=bot))
|
|
|
|
assert is_match(handler, make_command_update("/star", bot=bot))
|
|
|
|
assert not is_match(handler, make_command_update("/stop", bot=bot))
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-10-12 15:11:09 +02:00
|
|
|
def test_edited(self, command_message):
|
2021-08-29 18:15:04 +02:00
|
|
|
"""Test that a CH responds to an edited message if its filters allow it"""
|
2019-10-12 15:11:09 +02:00
|
|
|
handler_edited = self.make_default_handler()
|
2021-11-20 11:36:18 +01:00
|
|
|
handler_no_edited = self.make_default_handler(filters=~filters.UpdateType.EDITED_MESSAGE)
|
2019-10-12 15:11:09 +02:00
|
|
|
self._test_edited(command_message, handler_edited, handler_no_edited)
|
|
|
|
|
|
|
|
def test_directed_commands(self, bot, command):
|
|
|
|
"""Test recognition of commands with a mention to the bot"""
|
|
|
|
handler = self.make_default_handler()
|
2022-05-05 17:40:22 +02:00
|
|
|
assert is_match(handler, make_command_update(command + "@" + bot.username, bot=bot))
|
|
|
|
assert not is_match(handler, make_command_update(command + "@otherbot", bot=bot))
|
2019-10-12 15:11:09 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_with_filter(self, command, bot):
|
2021-08-29 18:15:04 +02:00
|
|
|
"""Test that a CH with a (generic) filter responds if its filters match"""
|
2021-11-20 11:36:18 +01:00
|
|
|
handler = self.make_default_handler(filters=filters.ChatType.GROUP)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert is_match(handler, make_command_update(command, chat=Chat(-23, Chat.GROUP), bot=bot))
|
|
|
|
assert not is_match(
|
|
|
|
handler, make_command_update(command, chat=Chat(23, Chat.PRIVATE), bot=bot)
|
|
|
|
)
|
2019-10-12 15:11:09 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_newline(self, app, command):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Assert that newlines don't interfere with a command handler matching a message"""
|
|
|
|
handler = self.make_default_handler()
|
2022-04-24 12:38:09 +02:00
|
|
|
app.add_handler(handler)
|
2022-05-05 17:40:22 +02:00
|
|
|
update = make_command_update(command + "\nfoobar", bot=app.bot)
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
assert is_match(handler, update)
|
|
|
|
assert await self.response(app, update)
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
def test_other_update_types(self, false_update):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Test that a command handler doesn't respond to unrelated updates"""
|
|
|
|
handler = self.make_default_handler()
|
|
|
|
assert not is_match(handler, false_update)
|
2018-04-17 06:37:29 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_filters_for_wrong_command(self, mock_filter, bot):
|
2018-04-17 06:37:29 +02:00
|
|
|
"""Filters should not be executed if the command does not match the handler"""
|
2019-10-12 15:11:09 +02:00
|
|
|
handler = self.make_default_handler(filters=mock_filter)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert not is_match(handler, make_command_update("/star", bot=bot))
|
2019-10-12 15:11:09 +02:00
|
|
|
assert not mock_filter.tested
|
2018-04-17 06:37:29 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_context(self, app, command_update):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Test correct behaviour of CHs with context-based callbacks"""
|
2022-04-24 12:38:09 +02:00
|
|
|
handler = self.make_default_handler(self.callback)
|
|
|
|
app.add_handler(handler)
|
|
|
|
assert await self.response(app, command_update)
|
2018-09-21 08:57:01 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_context_args(self, app, command):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Test CHs that pass arguments through ``context``"""
|
2022-04-24 12:38:09 +02:00
|
|
|
handler = self.make_default_handler(self.callback_args)
|
|
|
|
await self._test_context_args_or_regex(app, handler, command)
|
2018-09-21 08:57:43 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_context_regex(self, app, command):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Test CHs with context-based callbacks and a single filter"""
|
2022-05-05 17:40:22 +02:00
|
|
|
handler = self.make_default_handler(self.callback_regex1, filters=filters.Regex("one two"))
|
2022-04-24 12:38:09 +02:00
|
|
|
await self._test_context_args_or_regex(app, handler, command)
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_context_multiple_regex(self, app, command):
|
2019-10-12 15:11:09 +02:00
|
|
|
"""Test CHs with context-based callbacks and filters combined"""
|
2020-10-09 17:22:07 +02:00
|
|
|
handler = self.make_default_handler(
|
2022-05-05 17:40:22 +02:00
|
|
|
self.callback_regex2, filters=filters.Regex("one") & filters.Regex("two")
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
await self._test_context_args_or_regex(app, handler, command)
|
2023-08-23 21:28:36 +02:00
|
|
|
|
|
|
|
def test_command_has_args(self, bot):
|
|
|
|
"""Test CHs with optional has_args specified."""
|
|
|
|
handler_true = CommandHandler(["test"], self.callback_basic, has_args=True)
|
|
|
|
handler_false = CommandHandler(["test"], self.callback_basic, has_args=False)
|
|
|
|
handler_int_one = CommandHandler(["test"], self.callback_basic, has_args=1)
|
|
|
|
handler_int_two = CommandHandler(["test"], self.callback_basic, has_args=2)
|
|
|
|
|
|
|
|
assert is_match(handler_true, make_command_update("/test helloworld", bot=bot))
|
|
|
|
assert not is_match(handler_true, make_command_update("/test", bot=bot))
|
|
|
|
|
|
|
|
assert is_match(handler_false, make_command_update("/test", bot=bot))
|
|
|
|
assert not is_match(handler_false, make_command_update("/test helloworld", bot=bot))
|
|
|
|
|
|
|
|
assert is_match(handler_int_one, make_command_update("/test helloworld", bot=bot))
|
|
|
|
assert not is_match(handler_int_one, make_command_update("/test hello world", bot=bot))
|
|
|
|
assert not is_match(handler_int_one, make_command_update("/test", bot=bot))
|
|
|
|
|
|
|
|
assert is_match(handler_int_two, make_command_update("/test hello world", bot=bot))
|
|
|
|
assert not is_match(handler_int_two, make_command_update("/test helloworld", bot=bot))
|
|
|
|
assert not is_match(handler_int_two, make_command_update("/test", bot=bot))
|
|
|
|
|
|
|
|
def test_command_has_negative_args(self, bot):
|
|
|
|
"""Test CHs with optional has_args specified with negative int"""
|
|
|
|
# Assert that CommandHandler cannot be instantiated.
|
|
|
|
with pytest.raises(
|
|
|
|
ValueError, match="CommandHandler argument has_args cannot be a negative integer"
|
|
|
|
):
|
|
|
|
is_match(CommandHandler(["test"], self.callback_basic, has_args=-1))
|