File: test_commandhandler.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (290 lines) | stat: -rw-r--r-- 12,812 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# 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/].
import asyncio
import re

import pytest

from telegram import Bot, Chat, Message, Update
from telegram.ext import CallbackContext, CommandHandler, JobQueue, filters
from tests.auxil.build_messages import (
    make_command_message,
    make_command_update,
    make_message_update,
)
from tests.auxil.slots import mro_slots


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


class BaseTest:
    """Base class for command and prefix handler test classes. Contains
    utility methods an several callbacks used by both classes."""

    test_flag = False
    SRE_TYPE = type(re.match("", ""))

    @pytest.fixture(autouse=True)
    def _reset(self):
        self.test_flag = False

    async def response(self, application, update):
        """
        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
        async with application:
            await application.process_update(update)
        return self.test_flag

    def callback_basic(self, update, context):
        test_bot = isinstance(context.bot, Bot)
        test_update = isinstance(update, Update)
        self.test_flag = test_bot and test_update

    def make_callback_for(self, pass_keyword):
        def callback(bot, update, **kwargs):
            self.test_flag = kwargs.get(keyword) is not None

        keyword = pass_keyword[5:]
        return callback

    async def callback(self, update, context):
        self.test_flag = (
            isinstance(context, CallbackContext)
            and isinstance(context.bot, Bot)
            and isinstance(update, Update)
            and isinstance(context.update_queue, asyncio.Queue)
            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)
        )

    def callback_args(self, update, context):
        self.test_flag = context.args == ["one", "two"]

    def callback_regex1(self, update, context):
        if context.matches:
            types = all(type(res) is self.SRE_TYPE for res in context.matches)
            num = len(context.matches) == 1
            self.test_flag = types and num

    def callback_regex2(self, update, context):
        if context.matches:
            types = all(type(res) is self.SRE_TYPE for res in context.matches)
            num = len(context.matches) == 2
            self.test_flag = types and num

    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)
        update = make_command_update(text + " one two", bot=app.bot)
        assert await self.response(app, update)

    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)

        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)


# ----------------------------- CommandHandler -----------------------------


class TestCommandHandler(BaseTest):
    CMD = "/test"

    def test_slot_behaviour(self):
        handler = self.make_default_handler()
        for attr in handler.__slots__:
            assert getattr(handler, attr, "err") != "err", f"got extra slot '{attr}'"
        assert len(mro_slots(handler)) == len(set(mro_slots(handler))), "duplicate slot"

    @pytest.fixture(scope="class")
    def command(self):
        return self.CMD

    @pytest.fixture(scope="class")
    def command_message(self, command, bot):
        return make_command_message(command, bot=bot)

    @pytest.fixture(scope="class")
    def command_update(self, command_message):
        return make_command_update(command_message)

    def make_default_handler(self, callback=None, **kwargs):
        callback = callback or self.callback_basic
        return CommandHandler(self.CMD[1:], callback, **kwargs)

    async def test_basic(self, app, command):
        """Test whether a command handler responds to its command
        and not to others, or badly formatted commands"""
        handler = self.make_default_handler()
        app.add_handler(handler)

        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))
        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))
        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"}

    @pytest.mark.parametrize(
        "cmd",
        ["way_too_longcommand1234567yes_way_toooooooLong", "ïñválídletters", "invalid #&* chars"],
        ids=["too long", "invalid letter", "invalid characters"],
    )
    def test_invalid_commands(self, cmd):
        with pytest.raises(
            ValueError, match=f"`{re.escape(cmd.lower())}` is not a valid bot command"
        ):
            CommandHandler(cmd, self.callback_basic)

    def test_command_list(self, bot):
        """A command handler with multiple commands registered should respond to all of them."""
        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))

    def test_edited(self, command_message):
        """Test that a CH responds to an edited message if its filters allow it"""
        handler_edited = self.make_default_handler()
        handler_no_edited = self.make_default_handler(filters=~filters.UpdateType.EDITED_MESSAGE)
        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()
        assert is_match(handler, make_command_update(command + "@" + bot.username, bot=bot))
        assert not is_match(handler, make_command_update(command + "@otherbot", bot=bot))

    def test_with_filter(self, command, bot):
        """Test that a CH with a (generic) filter responds if its filters match"""
        handler = self.make_default_handler(filters=filters.ChatType.GROUP)
        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)
        )

    async def test_newline(self, app, command):
        """Assert that newlines don't interfere with a command handler matching a message"""
        handler = self.make_default_handler()
        app.add_handler(handler)
        update = make_command_update(command + "\nfoobar", bot=app.bot)
        async with app:
            assert is_match(handler, update)
        assert await self.response(app, update)

    def test_other_update_types(self, false_update):
        """Test that a command handler doesn't respond to unrelated updates"""
        handler = self.make_default_handler()
        assert not is_match(handler, false_update)

    def test_filters_for_wrong_command(self, mock_filter, bot):
        """Filters should not be executed if the command does not match the handler"""
        handler = self.make_default_handler(filters=mock_filter)
        assert not is_match(handler, make_command_update("/star", bot=bot))
        assert not mock_filter.tested

    async def test_context(self, app, command_update):
        """Test correct behaviour of CHs with context-based callbacks"""
        handler = self.make_default_handler(self.callback)
        app.add_handler(handler)
        assert await self.response(app, command_update)

    async def test_context_args(self, app, command):
        """Test CHs that pass arguments through ``context``"""
        handler = self.make_default_handler(self.callback_args)
        await self._test_context_args_or_regex(app, handler, command)

    async def test_context_regex(self, app, command):
        """Test CHs with context-based callbacks and a single filter"""
        handler = self.make_default_handler(self.callback_regex1, filters=filters.Regex("one two"))
        await self._test_context_args_or_regex(app, handler, command)

    async def test_context_multiple_regex(self, app, command):
        """Test CHs with context-based callbacks and filters combined"""
        handler = self.make_default_handler(
            self.callback_regex2, filters=filters.Regex("one") & filters.Regex("two")
        )
        await self._test_context_args_or_regex(app, handler, command)

    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))