2021-03-14 16:41:35 +01: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
|
2021-03-14 16:41:35 +01: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 time
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from telegram import (
|
|
|
|
Bot,
|
|
|
|
CallbackQuery,
|
2022-05-05 09:27:54 +02:00
|
|
|
Chat,
|
|
|
|
ChatMember,
|
|
|
|
ChatMemberUpdated,
|
2021-03-14 16:41:35 +01:00
|
|
|
ChosenInlineResult,
|
2022-05-05 09:27:54 +02:00
|
|
|
Message,
|
2021-03-14 16:41:35 +01:00
|
|
|
PreCheckoutQuery,
|
2022-05-05 09:27:54 +02:00
|
|
|
ShippingQuery,
|
|
|
|
Update,
|
|
|
|
User,
|
2021-03-14 16:41:35 +01:00
|
|
|
)
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.datetime import from_timestamp
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.ext import CallbackContext, ChatMemberHandler, JobQueue
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.slots import mro_slots
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
message = Message(1, None, Chat(1, ""), from_user=User(1, "", False), text="Text")
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
params = [
|
2022-05-05 17:40:22 +02:00
|
|
|
{"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")},
|
2021-03-14 16:41:35 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
ids = (
|
2022-05-05 17:40:22 +02:00
|
|
|
"message",
|
|
|
|
"edited_message",
|
|
|
|
"callback_query",
|
|
|
|
"channel_post",
|
|
|
|
"edited_channel_post",
|
|
|
|
"chosen_inline_result",
|
|
|
|
"shipping_query",
|
|
|
|
"pre_checkout_query",
|
|
|
|
"callback_query_without_message",
|
2021-03-14 16:41:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class", params=params, ids=ids)
|
2021-03-14 16:41:35 +01:00
|
|
|
def false_update(request):
|
|
|
|
return Update(update_id=2, **request.param)
|
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2021-03-14 16:41:35 +01:00
|
|
|
def chat_member_updated():
|
|
|
|
return ChatMemberUpdated(
|
2022-05-05 17:40:22 +02:00
|
|
|
Chat(1, "chat"),
|
|
|
|
User(1, "", False),
|
2021-03-14 16:41:35 +01:00
|
|
|
from_timestamp(int(time.time())),
|
2022-05-05 17:40:22 +02:00
|
|
|
ChatMember(User(1, "", False), ChatMember.OWNER),
|
|
|
|
ChatMember(User(1, "", False), ChatMember.OWNER),
|
2021-03-14 16:41:35 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-08-07 21:56:46 +02:00
|
|
|
@pytest.fixture
|
2021-03-14 16:41:35 +01:00
|
|
|
def chat_member(bot, chat_member_updated):
|
2022-12-15 15:00:36 +01:00
|
|
|
update = Update(0, my_chat_member=chat_member_updated)
|
|
|
|
update._unfreeze()
|
|
|
|
return update
|
2021-03-14 16:41:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestChatMemberHandler:
|
|
|
|
test_flag = False
|
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self):
|
2022-04-24 12:38:09 +02:00
|
|
|
action = ChatMemberHandler(self.callback)
|
2021-05-29 16:18:16 +02:00
|
|
|
for attr in action.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(action, attr, "err") != "err", f"got extra slot '{attr}'"
|
2021-05-29 16:18:16 +02:00
|
|
|
assert len(mro_slots(action)) == len(set(mro_slots(action))), "duplicate slot"
|
|
|
|
|
2021-03-14 16:41:35 +01:00
|
|
|
@pytest.fixture(autouse=True)
|
2023-03-25 19:18:04 +01:00
|
|
|
def _reset(self):
|
2021-03-14 16:41:35 +01:00
|
|
|
self.test_flag = False
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def callback(self, update, context):
|
2021-03-14 16:41:35 +01: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)
|
2021-03-14 16:41:35 +01: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.chat_member or update.my_chat_member, ChatMemberUpdated)
|
|
|
|
)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-05-05 17:40:22 +02:00
|
|
|
argnames=["allowed_types", "expected"],
|
2021-03-14 16:41:35 +01:00
|
|
|
argvalues=[
|
|
|
|
(ChatMemberHandler.MY_CHAT_MEMBER, (True, False)),
|
|
|
|
(ChatMemberHandler.CHAT_MEMBER, (False, True)),
|
|
|
|
(ChatMemberHandler.ANY_CHAT_MEMBER, (True, True)),
|
|
|
|
],
|
2022-05-05 17:40:22 +02:00
|
|
|
ids=["MY_CHAT_MEMBER", "CHAT_MEMBER", "ANY_CHAT_MEMBER"],
|
2021-03-14 16:41:35 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_chat_member_types(
|
|
|
|
self, app, chat_member_updated, chat_member, expected, allowed_types
|
2021-03-14 16:41:35 +01:00
|
|
|
):
|
|
|
|
result_1, result_2 = expected
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
handler = ChatMemberHandler(self.callback, chat_member_types=allowed_types)
|
|
|
|
app.add_handler(handler)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
assert handler.check_update(chat_member) == result_1
|
|
|
|
await app.process_update(chat_member)
|
|
|
|
assert self.test_flag == result_1
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
self.test_flag = False
|
|
|
|
chat_member.my_chat_member = None
|
|
|
|
chat_member.chat_member = chat_member_updated
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
assert handler.check_update(chat_member) == result_2
|
|
|
|
await app.process_update(chat_member)
|
|
|
|
assert self.test_flag == result_2
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2024-06-06 17:31:45 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
argnames=["allowed_types", "chat_id", "expected"],
|
|
|
|
argvalues=[
|
|
|
|
(ChatMemberHandler.MY_CHAT_MEMBER, None, (True, False)),
|
|
|
|
(ChatMemberHandler.CHAT_MEMBER, None, (False, True)),
|
|
|
|
(ChatMemberHandler.ANY_CHAT_MEMBER, None, (True, True)),
|
|
|
|
(ChatMemberHandler.MY_CHAT_MEMBER, 1, (True, False)),
|
|
|
|
(ChatMemberHandler.CHAT_MEMBER, 1, (False, True)),
|
|
|
|
(ChatMemberHandler.ANY_CHAT_MEMBER, 1, (True, True)),
|
|
|
|
(ChatMemberHandler.MY_CHAT_MEMBER, [1], (True, False)),
|
|
|
|
(ChatMemberHandler.CHAT_MEMBER, [1], (False, True)),
|
|
|
|
(ChatMemberHandler.ANY_CHAT_MEMBER, [1], (True, True)),
|
|
|
|
(ChatMemberHandler.MY_CHAT_MEMBER, 2, (False, False)),
|
|
|
|
(ChatMemberHandler.CHAT_MEMBER, 2, (False, False)),
|
|
|
|
(ChatMemberHandler.ANY_CHAT_MEMBER, 2, (False, False)),
|
|
|
|
(ChatMemberHandler.MY_CHAT_MEMBER, [2], (False, False)),
|
|
|
|
(ChatMemberHandler.CHAT_MEMBER, [2], (False, False)),
|
|
|
|
(ChatMemberHandler.ANY_CHAT_MEMBER, [2], (False, False)),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
"MY_CHAT_MEMBER",
|
|
|
|
"CHAT_MEMBER",
|
|
|
|
"ANY_CHAT_MEMBER",
|
|
|
|
"MY_CHAT_MEMBER, CHAT=1 ",
|
|
|
|
"CHAT_MEMBER, CHAT=1",
|
|
|
|
"ANY_CHAT_MEMBER, CHAT=1",
|
|
|
|
"MY_CHAT_MEMBER, CHAT=[1] ",
|
|
|
|
"CHAT_MEMBER, CHAT=[1]",
|
|
|
|
"ANY_CHAT_MEMBER, CHAT=[1]",
|
|
|
|
"MY_CHAT_MEMBER, CHAT=2 ",
|
|
|
|
"CHAT_MEMBER, CHAT=2",
|
|
|
|
"ANY_CHAT_MEMBER, CHAT=2",
|
|
|
|
"MY_CHAT_MEMBER, CHAT=[2] ",
|
|
|
|
"CHAT_MEMBER, CHAT=[2]",
|
|
|
|
"ANY_CHAT_MEMBER, CHAT=[2]",
|
|
|
|
],
|
|
|
|
)
|
|
|
|
async def test_chat_member_types_with_chat_id(
|
|
|
|
self, app, chat_member_updated, chat_member, expected, allowed_types, chat_id
|
|
|
|
):
|
|
|
|
result_1, result_2 = expected
|
|
|
|
|
|
|
|
handler = ChatMemberHandler(
|
|
|
|
self.callback, chat_member_types=allowed_types, chat_id=chat_id
|
|
|
|
)
|
|
|
|
app.add_handler(handler)
|
|
|
|
|
|
|
|
async with app:
|
|
|
|
assert handler.check_update(chat_member) == result_1
|
|
|
|
await app.process_update(chat_member)
|
|
|
|
assert self.test_flag == result_1
|
|
|
|
|
|
|
|
self.test_flag = False
|
|
|
|
chat_member.my_chat_member = None
|
|
|
|
chat_member.chat_member = chat_member_updated
|
|
|
|
|
|
|
|
assert handler.check_update(chat_member) == result_2
|
|
|
|
await app.process_update(chat_member)
|
|
|
|
assert self.test_flag == result_2
|
|
|
|
|
2021-03-14 16:41:35 +01:00
|
|
|
def test_other_update_types(self, false_update):
|
2022-04-24 12:38:09 +02:00
|
|
|
handler = ChatMemberHandler(self.callback)
|
2021-03-14 16:41:35 +01:00
|
|
|
assert not handler.check_update(false_update)
|
|
|
|
assert not handler.check_update(True)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_context(self, app, chat_member):
|
|
|
|
handler = ChatMemberHandler(self.callback)
|
|
|
|
app.add_handler(handler)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async with app:
|
|
|
|
await app.process_update(chat_member)
|
|
|
|
assert self.test_flag
|