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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
#!/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 datetime as dtm
import pytest
from telegram import (
Bot,
CallbackQuery,
Chat,
ChosenInlineResult,
Message,
MessageReactionCountUpdated,
MessageReactionUpdated,
PreCheckoutQuery,
ReactionCount,
ReactionTypeEmoji,
ShippingQuery,
Update,
User,
)
from telegram._utils.datetime import UTC
from telegram.ext import CallbackContext, JobQueue, MessageReactionHandler
from tests.auxil.slots import mro_slots
message = Message(1, None, Chat(1, ""), from_user=User(1, "", False), text="Text")
params = [
{"message": message},
{"edited_message": message},
{"callback_query": CallbackQuery(1, User(1, "", False), "chat", message=message)},
{"channel_post": message},
{"edited_channel_post": message},
{"chosen_inline_result": ChosenInlineResult("id", User(1, "", False), "")},
{"shipping_query": ShippingQuery("id", User(1, "", False), "", None)},
{"pre_checkout_query": PreCheckoutQuery("id", User(1, "", False), "", 0, "")},
{"callback_query": CallbackQuery(1, User(1, "", False), "chat")},
]
ids = (
"message",
"edited_message",
"callback_query",
"channel_post",
"edited_channel_post",
"chosen_inline_result",
"shipping_query",
"pre_checkout_query",
"callback_query_without_message",
)
@pytest.fixture(scope="class", params=params, ids=ids)
def false_update(request):
return Update(update_id=2, **request.param)
@pytest.fixture(scope="class")
def time():
return dtm.datetime.now(tz=UTC)
@pytest.fixture(scope="class")
def message_reaction_updated(time, bot):
mr = MessageReactionUpdated(
chat=Chat(1, Chat.SUPERGROUP),
message_id=1,
date=time,
old_reaction=[ReactionTypeEmoji("👍")],
new_reaction=[ReactionTypeEmoji("👎")],
user=User(1, "user_a", False),
actor_chat=Chat(2, Chat.SUPERGROUP),
)
mr.set_bot(bot)
mr._unfreeze()
mr.chat._unfreeze()
mr.user._unfreeze()
return mr
@pytest.fixture(scope="class")
def message_reaction_count_updated(time, bot):
mr = MessageReactionCountUpdated(
chat=Chat(1, Chat.SUPERGROUP),
message_id=1,
date=time,
reactions=[
ReactionCount(ReactionTypeEmoji("👍"), 1),
ReactionCount(ReactionTypeEmoji("👎"), 1),
],
)
mr.set_bot(bot)
mr._unfreeze()
mr.chat._unfreeze()
return mr
@pytest.fixture
def message_reaction_update(bot, message_reaction_updated):
return Update(0, message_reaction=message_reaction_updated)
@pytest.fixture
def message_reaction_count_update(bot, message_reaction_count_updated):
return Update(0, message_reaction_count=message_reaction_count_updated)
class TestMessageReactionHandler:
test_flag = False
def test_slot_behaviour(self):
action = MessageReactionHandler(self.callback)
for attr in action.__slots__:
assert getattr(action, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(action)) == len(set(mro_slots(action))), "duplicate slot"
@pytest.fixture(autouse=True)
def _reset(self):
self.test_flag = False
async def callback(self, update: 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 if update.effective_user else type(None))
and isinstance(context.chat_data, dict)
and isinstance(context.bot_data, dict)
and (
isinstance(
update.message_reaction,
MessageReactionUpdated,
)
or isinstance(update.message_reaction_count, MessageReactionCountUpdated)
)
)
def test_other_update_types(self, false_update):
handler = MessageReactionHandler(self.callback)
assert not handler.check_update(false_update)
assert not handler.check_update(True)
async def test_context(self, app, message_reaction_update, message_reaction_count_update):
handler = MessageReactionHandler(callback=self.callback)
app.add_handler(handler)
async with app:
assert handler.check_update(message_reaction_update)
await app.process_update(message_reaction_update)
assert self.test_flag
self.test_flag = False
await app.process_update(message_reaction_count_update)
assert self.test_flag
@pytest.mark.parametrize(
argnames=("allowed_types", "expected"),
argvalues=[
(MessageReactionHandler.MESSAGE_REACTION_UPDATED, (True, False)),
(MessageReactionHandler.MESSAGE_REACTION_COUNT_UPDATED, (False, True)),
(MessageReactionHandler.MESSAGE_REACTION, (True, True)),
],
ids=["MESSAGE_REACTION_UPDATED", "MESSAGE_REACTION_COUNT_UPDATED", "MESSAGE_REACTION"],
)
async def test_message_reaction_types(
self, app, message_reaction_update, message_reaction_count_update, expected, allowed_types
):
result_1, result_2 = expected
handler = MessageReactionHandler(self.callback, message_reaction_types=allowed_types)
app.add_handler(handler)
async with app:
assert handler.check_update(message_reaction_update) == result_1
await app.process_update(message_reaction_update)
assert self.test_flag == result_1
self.test_flag = False
assert handler.check_update(message_reaction_count_update) == result_2
await app.process_update(message_reaction_count_update)
assert self.test_flag == result_2
@pytest.mark.parametrize(
argnames=("allowed_types", "kwargs"),
argvalues=[
(MessageReactionHandler.MESSAGE_REACTION_COUNT_UPDATED, {"user_username": "user"}),
(MessageReactionHandler.MESSAGE_REACTION, {"user_id": 123}),
],
ids=["MESSAGE_REACTION_COUNT_UPDATED", "MESSAGE_REACTION"],
)
async def test_username_with_anonymous_reaction(self, app, allowed_types, kwargs):
with pytest.raises(
ValueError, match="You can not filter for users and include anonymous reactions\\."
):
MessageReactionHandler(self.callback, message_reaction_types=allowed_types, **kwargs)
@pytest.mark.parametrize(
argnames=("chat_id", "expected"),
argvalues=[(1, True), ([1], True), (2, False), ([2], False)],
)
async def test_with_chat_ids(
self, chat_id, expected, message_reaction_update, message_reaction_count_update
):
handler = MessageReactionHandler(self.callback, chat_id=chat_id)
assert handler.check_update(message_reaction_update) == expected
assert handler.check_update(message_reaction_count_update) == expected
@pytest.mark.parametrize(
argnames="chat_username",
argvalues=["group_a", "@group_a", ["group_a"], ["@group_a"]],
ids=["group_a", "@group_a", "['group_a']", "['@group_a']"],
)
async def test_with_chat_usernames(
self, chat_username, message_reaction_update, message_reaction_count_update
):
handler = MessageReactionHandler(self.callback, chat_username=chat_username)
assert not handler.check_update(message_reaction_update)
assert not handler.check_update(message_reaction_count_update)
message_reaction_update.message_reaction.chat.username = "group_a"
message_reaction_count_update.message_reaction_count.chat.username = "group_a"
assert handler.check_update(message_reaction_update)
assert handler.check_update(message_reaction_count_update)
message_reaction_update.message_reaction.chat.username = None
message_reaction_count_update.message_reaction_count.chat.username = None
@pytest.mark.parametrize(
argnames=("user_id", "expected"),
argvalues=[(1, True), ([1], True), (2, False), ([2], False)],
)
async def test_with_user_ids(
self, user_id, expected, message_reaction_update, message_reaction_count_update
):
handler = MessageReactionHandler(
self.callback,
user_id=user_id,
message_reaction_types=MessageReactionHandler.MESSAGE_REACTION_UPDATED,
)
assert handler.check_update(message_reaction_update) == expected
assert not handler.check_update(message_reaction_count_update)
@pytest.mark.parametrize(
argnames="user_username",
argvalues=["user_a", "@user_a", ["user_a"], ["@user_a"]],
ids=["user_a", "@user_a", "['user_a']", "['@user_a']"],
)
async def test_with_user_usernames(
self, user_username, message_reaction_update, message_reaction_count_update
):
handler = MessageReactionHandler(
self.callback,
user_username=user_username,
message_reaction_types=MessageReactionHandler.MESSAGE_REACTION_UPDATED,
)
assert not handler.check_update(message_reaction_update)
assert not handler.check_update(message_reaction_count_update)
message_reaction_update.message_reaction.user.username = "user_a"
assert handler.check_update(message_reaction_update)
assert not handler.check_update(message_reaction_count_update)
message_reaction_update.message_reaction.user.username = None
async def test_message_reaction_count_with_combination(
self, message_reaction_count_update, message_reaction_update
):
handler = MessageReactionHandler(
self.callback,
chat_id=2,
chat_username="group_a",
message_reaction_types=MessageReactionHandler.MESSAGE_REACTION,
)
assert not handler.check_update(message_reaction_count_update)
message_reaction_count_update.message_reaction_count.chat.id = 2
message_reaction_update.message_reaction.chat.id = 2
assert handler.check_update(message_reaction_count_update)
assert handler.check_update(message_reaction_update)
message_reaction_count_update.message_reaction_count.chat.id = 1
message_reaction_update.message_reaction.chat.id = 1
message_reaction_count_update.message_reaction_count.chat.username = "group_a"
message_reaction_update.message_reaction.chat.username = "group_a"
assert handler.check_update(message_reaction_count_update)
assert handler.check_update(message_reaction_update)
message_reaction_count_update.message_reaction_count.chat.username = None
message_reaction_update.message_reaction.chat.username = None
async def test_message_reaction_with_combination(self, message_reaction_update):
handler = MessageReactionHandler(
self.callback,
chat_id=2,
chat_username="group_a",
user_id=2,
user_username="user_a",
message_reaction_types=MessageReactionHandler.MESSAGE_REACTION_UPDATED,
)
assert not handler.check_update(message_reaction_update)
message_reaction_update.message_reaction.chat.id = 2
assert handler.check_update(message_reaction_update)
message_reaction_update.message_reaction.chat.id = 1
message_reaction_update.message_reaction.chat.username = "group_a"
assert handler.check_update(message_reaction_update)
message_reaction_update.message_reaction.chat.username = None
message_reaction_update.message_reaction.user.id = 2
assert handler.check_update(message_reaction_update)
message_reaction_update.message_reaction.user.id = 1
message_reaction_update.message_reaction.user.username = "user_a"
assert handler.check_update(message_reaction_update)
message_reaction_update.message_reaction.user.username = None
|