2017-05-14 23:29:31 +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-05-14 23:29:31 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2017-08-11 23:58:41 +02:00
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
2017-05-14 23:29:31 +02:00
|
|
|
# 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
|
2017-08-11 23:58:41 +02:00
|
|
|
# GNU Lesser Public License for more details.
|
2017-05-14 23:29:31 +02:00
|
|
|
#
|
2017-08-11 23:58:41 +02:00
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
2017-05-14 23:29:31 +02:00
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2017-08-11 23:58:41 +02:00
|
|
|
import datetime
|
2021-08-29 18:17:06 +02:00
|
|
|
import inspect
|
2021-07-01 17:45:19 +02:00
|
|
|
from copy import deepcopy
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2021-07-01 17:45:19 +02:00
|
|
|
from telegram import (
|
|
|
|
ChatMember,
|
|
|
|
ChatMemberAdministrator,
|
2022-05-05 09:27:54 +02:00
|
|
|
ChatMemberBanned,
|
|
|
|
ChatMemberLeft,
|
2021-07-01 17:45:19 +02:00
|
|
|
ChatMemberMember,
|
2022-05-05 09:27:54 +02:00
|
|
|
ChatMemberOwner,
|
2021-07-01 17:45:19 +02:00
|
|
|
ChatMemberRestricted,
|
|
|
|
Dice,
|
2022-05-05 09:27:54 +02:00
|
|
|
User,
|
2021-07-01 17:45:19 +02:00
|
|
|
)
|
2023-04-18 16:16:23 +02:00
|
|
|
from telegram._utils.datetime import UTC, to_timestamp
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.slots import mro_slots
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2022-10-07 11:51:53 +02:00
|
|
|
ignored = ["self", "api_kwargs"]
|
2021-08-29 18:17:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
class CMDefaults:
|
|
|
|
user = User(1, "First name", False)
|
|
|
|
custom_title: str = "PTB"
|
|
|
|
is_anonymous: bool = True
|
|
|
|
until_date: datetime.datetime = to_timestamp(datetime.datetime.utcnow())
|
|
|
|
can_be_edited: bool = False
|
|
|
|
can_change_info: bool = True
|
|
|
|
can_post_messages: bool = True
|
|
|
|
can_edit_messages: bool = True
|
|
|
|
can_delete_messages: bool = True
|
|
|
|
can_invite_users: bool = True
|
|
|
|
can_restrict_members: bool = True
|
|
|
|
can_pin_messages: bool = True
|
|
|
|
can_promote_members: bool = True
|
|
|
|
can_send_messages: bool = True
|
|
|
|
can_send_media_messages: bool = True
|
|
|
|
can_send_polls: bool = True
|
|
|
|
can_send_other_messages: bool = True
|
|
|
|
can_add_web_page_previews: bool = True
|
|
|
|
is_member: bool = True
|
|
|
|
can_manage_chat: bool = True
|
2022-05-03 18:21:50 +02:00
|
|
|
can_manage_video_chats: bool = True
|
2022-11-22 10:43:50 +01:00
|
|
|
can_manage_topics: bool = True
|
2023-02-08 11:47:21 +01:00
|
|
|
can_send_audios: bool = True
|
|
|
|
can_send_documents: bool = True
|
|
|
|
can_send_photos: bool = True
|
|
|
|
can_send_videos: bool = True
|
|
|
|
can_send_video_notes: bool = True
|
|
|
|
can_send_voice_notes: bool = True
|
2023-10-03 14:32:57 +02:00
|
|
|
can_post_stories: bool = True
|
|
|
|
can_edit_stories: bool = True
|
|
|
|
can_delete_stories: bool = True
|
2021-08-29 18:17:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
def chat_member_owner():
|
|
|
|
return ChatMemberOwner(CMDefaults.user, CMDefaults.is_anonymous, CMDefaults.custom_title)
|
|
|
|
|
|
|
|
|
|
|
|
def chat_member_administrator():
|
|
|
|
return ChatMemberAdministrator(
|
|
|
|
CMDefaults.user,
|
|
|
|
CMDefaults.can_be_edited,
|
|
|
|
CMDefaults.is_anonymous,
|
|
|
|
CMDefaults.can_manage_chat,
|
|
|
|
CMDefaults.can_delete_messages,
|
2022-05-03 18:21:50 +02:00
|
|
|
CMDefaults.can_manage_video_chats,
|
2021-08-29 18:17:06 +02:00
|
|
|
CMDefaults.can_restrict_members,
|
|
|
|
CMDefaults.can_promote_members,
|
|
|
|
CMDefaults.can_change_info,
|
|
|
|
CMDefaults.can_invite_users,
|
2024-04-06 17:13:43 +02:00
|
|
|
CMDefaults.can_post_stories,
|
|
|
|
CMDefaults.can_edit_stories,
|
|
|
|
CMDefaults.can_delete_stories,
|
2021-08-29 18:17:06 +02:00
|
|
|
CMDefaults.can_post_messages,
|
|
|
|
CMDefaults.can_edit_messages,
|
|
|
|
CMDefaults.can_pin_messages,
|
2022-11-22 10:43:50 +01:00
|
|
|
CMDefaults.can_manage_topics,
|
2021-08-29 18:17:06 +02:00
|
|
|
CMDefaults.custom_title,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def chat_member_member():
|
|
|
|
return ChatMemberMember(CMDefaults.user)
|
|
|
|
|
|
|
|
|
|
|
|
def chat_member_restricted():
|
|
|
|
return ChatMemberRestricted(
|
|
|
|
CMDefaults.user,
|
|
|
|
CMDefaults.is_member,
|
|
|
|
CMDefaults.can_change_info,
|
|
|
|
CMDefaults.can_invite_users,
|
|
|
|
CMDefaults.can_pin_messages,
|
|
|
|
CMDefaults.can_send_messages,
|
|
|
|
CMDefaults.can_send_polls,
|
|
|
|
CMDefaults.can_send_other_messages,
|
|
|
|
CMDefaults.can_add_web_page_previews,
|
2022-11-22 10:43:50 +01:00
|
|
|
CMDefaults.can_manage_topics,
|
2021-08-29 18:17:06 +02:00
|
|
|
CMDefaults.until_date,
|
2023-02-08 11:47:21 +01:00
|
|
|
CMDefaults.can_send_audios,
|
|
|
|
CMDefaults.can_send_documents,
|
|
|
|
CMDefaults.can_send_photos,
|
|
|
|
CMDefaults.can_send_videos,
|
|
|
|
CMDefaults.can_send_video_notes,
|
|
|
|
CMDefaults.can_send_voice_notes,
|
2021-08-29 18:17:06 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def chat_member_left():
|
|
|
|
return ChatMemberLeft(CMDefaults.user)
|
|
|
|
|
|
|
|
|
|
|
|
def chat_member_banned():
|
|
|
|
return ChatMemberBanned(CMDefaults.user, CMDefaults.until_date)
|
|
|
|
|
|
|
|
|
|
|
|
def make_json_dict(instance: ChatMember, include_optional_args: bool = False) -> dict:
|
|
|
|
"""Used to make the json dict which we use for testing de_json. Similar to iter_args()"""
|
|
|
|
json_dict = {"status": instance.status}
|
|
|
|
sig = inspect.signature(instance.__class__.__init__)
|
|
|
|
|
|
|
|
for param in sig.parameters.values():
|
|
|
|
if param.name in ignored: # ignore irrelevant params
|
|
|
|
continue
|
|
|
|
|
|
|
|
val = getattr(instance, param.name)
|
|
|
|
# Compulsory args-
|
|
|
|
if param.default is inspect.Parameter.empty:
|
|
|
|
if hasattr(val, "to_dict"): # convert the user object or any future ones to dict.
|
|
|
|
val = val.to_dict()
|
|
|
|
json_dict[param.name] = val
|
|
|
|
|
2024-03-02 10:56:15 +01:00
|
|
|
# If we want to test all args (for de_json)
|
|
|
|
# or if the param is optional but for backwards compatability
|
|
|
|
elif (
|
|
|
|
param.default is not inspect.Parameter.empty
|
|
|
|
and include_optional_args
|
|
|
|
or param.name in ["can_delete_stories", "can_post_stories", "can_edit_stories"]
|
|
|
|
):
|
2021-08-29 18:17:06 +02:00
|
|
|
json_dict[param.name] = val
|
|
|
|
return json_dict
|
|
|
|
|
|
|
|
|
|
|
|
def iter_args(instance: ChatMember, de_json_inst: ChatMember, include_optional: bool = False):
|
|
|
|
"""
|
|
|
|
We accept both the regular instance and de_json created instance and iterate over them for
|
|
|
|
easy one line testing later one.
|
|
|
|
"""
|
|
|
|
yield instance.status, de_json_inst.status # yield this here cause it's not available in sig.
|
|
|
|
|
|
|
|
sig = inspect.signature(instance.__class__.__init__)
|
|
|
|
for param in sig.parameters.values():
|
|
|
|
if param.name in ignored:
|
|
|
|
continue
|
|
|
|
inst_at, json_at = getattr(instance, param.name), getattr(de_json_inst, param.name)
|
|
|
|
if isinstance(json_at, datetime.datetime): # Convert datetime to int
|
|
|
|
json_at = to_timestamp(json_at)
|
2023-03-25 19:18:04 +01:00
|
|
|
if (
|
|
|
|
param.default is not inspect.Parameter.empty and include_optional
|
|
|
|
) or param.default is inspect.Parameter.empty:
|
2021-08-29 18:17:06 +02:00
|
|
|
yield inst_at, json_at
|
|
|
|
|
|
|
|
|
2024-08-07 21:56:46 +02:00
|
|
|
@pytest.fixture
|
2021-08-29 18:17:06 +02:00
|
|
|
def chat_member_type(request):
|
|
|
|
return request.param()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"chat_member_type",
|
|
|
|
[
|
|
|
|
chat_member_owner,
|
|
|
|
chat_member_administrator,
|
|
|
|
chat_member_member,
|
|
|
|
chat_member_restricted,
|
|
|
|
chat_member_left,
|
|
|
|
chat_member_banned,
|
2021-07-01 17:45:19 +02:00
|
|
|
],
|
2021-08-29 18:17:06 +02:00
|
|
|
indirect=True,
|
2021-07-01 17:45:19 +02:00
|
|
|
)
|
2023-02-11 10:45:17 +01:00
|
|
|
class TestChatMemberTypesWithoutRequest:
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self, chat_member_type):
|
2021-08-29 18:17:06 +02:00
|
|
|
inst = chat_member_type
|
|
|
|
for attr in inst.__slots__:
|
|
|
|
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
|
|
|
|
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
|
|
|
|
|
|
|
|
def test_de_json_required_args(self, bot, chat_member_type):
|
|
|
|
cls = chat_member_type.__class__
|
|
|
|
assert cls.de_json({}, bot) is None
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
json_dict = make_json_dict(chat_member_type)
|
|
|
|
const_chat_member = ChatMember.de_json(json_dict, bot)
|
2022-10-07 11:51:53 +02:00
|
|
|
assert const_chat_member.api_kwargs == {}
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
assert isinstance(const_chat_member, ChatMember)
|
|
|
|
assert isinstance(const_chat_member, cls)
|
|
|
|
for chat_mem_type_at, const_chat_mem_at in iter_args(chat_member_type, const_chat_member):
|
|
|
|
assert chat_mem_type_at == const_chat_mem_at
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def test_de_json_all_args(self, bot, chat_member_type):
|
|
|
|
json_dict = make_json_dict(chat_member_type, include_optional_args=True)
|
|
|
|
const_chat_member = ChatMember.de_json(json_dict, bot)
|
2022-10-07 11:51:53 +02:00
|
|
|
assert const_chat_member.api_kwargs == {}
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
assert isinstance(const_chat_member, ChatMember)
|
|
|
|
assert isinstance(const_chat_member, chat_member_type.__class__)
|
|
|
|
for c_mem_type_at, const_c_mem_at in iter_args(chat_member_type, const_chat_member, True):
|
|
|
|
assert c_mem_type_at == const_c_mem_at
|
2021-07-01 17:45:19 +02:00
|
|
|
|
2023-04-18 16:16:23 +02:00
|
|
|
def test_de_json_chatmemberbanned_localization(self, chat_member_type, tz_bot, bot, raw_bot):
|
|
|
|
# We only test two classes because the other three don't have datetimes in them.
|
|
|
|
if isinstance(chat_member_type, (ChatMemberBanned, ChatMemberRestricted)):
|
|
|
|
json_dict = make_json_dict(chat_member_type, include_optional_args=True)
|
|
|
|
chatmember_raw = ChatMember.de_json(json_dict, raw_bot)
|
|
|
|
chatmember_bot = ChatMember.de_json(json_dict, bot)
|
|
|
|
chatmember_tz = ChatMember.de_json(json_dict, tz_bot)
|
|
|
|
|
|
|
|
# comparing utcoffsets because comparing timezones is unpredicatable
|
|
|
|
chatmember_offset = chatmember_tz.until_date.utcoffset()
|
|
|
|
tz_bot_offset = tz_bot.defaults.tzinfo.utcoffset(
|
|
|
|
chatmember_tz.until_date.replace(tzinfo=None)
|
|
|
|
)
|
|
|
|
|
|
|
|
assert chatmember_raw.until_date.tzinfo == UTC
|
|
|
|
assert chatmember_bot.until_date.tzinfo == UTC
|
|
|
|
assert chatmember_offset == tz_bot_offset
|
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def test_de_json_invalid_status(self, chat_member_type, bot):
|
|
|
|
json_dict = {"status": "invalid", "user": CMDefaults.user.to_dict()}
|
2021-07-01 17:45:19 +02:00
|
|
|
chat_member_type = ChatMember.de_json(json_dict, bot)
|
|
|
|
|
|
|
|
assert type(chat_member_type) is ChatMember
|
|
|
|
assert chat_member_type.status == "invalid"
|
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def test_de_json_subclass(self, chat_member_type, bot, chat_id):
|
2021-07-01 17:45:19 +02:00
|
|
|
"""This makes sure that e.g. ChatMemberAdministrator(data, bot) never returns a
|
2021-08-29 18:17:06 +02:00
|
|
|
ChatMemberBanned instance."""
|
|
|
|
cls = chat_member_type.__class__
|
|
|
|
json_dict = make_json_dict(chat_member_type, True)
|
2021-07-01 17:45:19 +02:00
|
|
|
assert type(cls.de_json(json_dict, bot)) is cls
|
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def test_to_dict(self, chat_member_type):
|
|
|
|
chat_member_dict = chat_member_type.to_dict()
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
assert isinstance(chat_member_dict, dict)
|
2021-08-29 18:17:06 +02:00
|
|
|
assert chat_member_dict["status"] == chat_member_type.status
|
|
|
|
assert chat_member_dict["user"] == chat_member_type.user.to_dict()
|
|
|
|
|
2022-11-13 21:28:41 +01:00
|
|
|
for slot in chat_member_type.__slots__: # additional verification for the optional args
|
|
|
|
assert getattr(chat_member_type, slot) == chat_member_dict[slot]
|
|
|
|
|
2023-09-03 14:23:48 +02:00
|
|
|
def test_chat_member_restricted_api_kwargs(self, chat_member_type):
|
|
|
|
json_dict = make_json_dict(chat_member_restricted())
|
|
|
|
json_dict["can_send_media_messages"] = "can_send_media_messages"
|
|
|
|
chat_member_restricted_instance = ChatMember.de_json(json_dict, None)
|
|
|
|
assert chat_member_restricted_instance.api_kwargs == {
|
|
|
|
"can_send_media_messages": "can_send_media_messages",
|
|
|
|
}
|
|
|
|
|
2021-08-29 18:17:06 +02:00
|
|
|
def test_equality(self, chat_member_type):
|
|
|
|
a = ChatMember(status="status", user=CMDefaults.user)
|
|
|
|
b = ChatMember(status="status", user=CMDefaults.user)
|
|
|
|
c = chat_member_type
|
|
|
|
d = deepcopy(chat_member_type)
|
2021-07-01 17:45:19 +02:00
|
|
|
e = Dice(4, "emoji")
|
2017-05-14 23:29:31 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
assert a == b
|
|
|
|
assert hash(a) == hash(b)
|
2021-07-01 17:45:19 +02:00
|
|
|
|
|
|
|
assert a != c
|
|
|
|
assert hash(a) != hash(c)
|
2017-05-14 23:29:31 +02:00
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
assert a != d
|
|
|
|
assert hash(a) != hash(d)
|
2017-05-14 23:29:31 +02:00
|
|
|
|
2021-07-01 17:45:19 +02:00
|
|
|
assert a != e
|
|
|
|
assert hash(a) != hash(e)
|
|
|
|
|
|
|
|
assert c == d
|
|
|
|
assert hash(c) == hash(d)
|
|
|
|
|
|
|
|
assert c != e
|
|
|
|
assert hash(c) != hash(e)
|