2021-07-01 17:45:19 +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
|
2021-07-01 17:45:19 +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/].
|
|
|
|
from copy import deepcopy
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from telegram import (
|
|
|
|
BotCommandScope,
|
|
|
|
BotCommandScopeAllChatAdministrators,
|
2022-05-05 09:27:54 +02:00
|
|
|
BotCommandScopeAllGroupChats,
|
|
|
|
BotCommandScopeAllPrivateChats,
|
2021-07-01 17:45:19 +02:00
|
|
|
BotCommandScopeChat,
|
|
|
|
BotCommandScopeChatAdministrators,
|
|
|
|
BotCommandScopeChatMember,
|
2022-05-05 09:27:54 +02:00
|
|
|
BotCommandScopeDefault,
|
|
|
|
Dice,
|
2021-07-01 17:45:19 +02:00
|
|
|
)
|
2024-01-17 21:32:37 +01:00
|
|
|
from telegram.constants import BotCommandScopeType
|
2023-02-22 20:19:46 +01:00
|
|
|
from tests.auxil.slots import mro_slots
|
2021-07-01 17:45:19 +02:00
|
|
|
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
@pytest.fixture(scope="module", params=["str", "int"])
|
2021-07-01 17:45:19 +02:00
|
|
|
def chat_id(request):
|
2022-05-05 17:40:22 +02:00
|
|
|
if request.param == "str":
|
|
|
|
return "@supergroupusername"
|
2021-07-01 17:45:19 +02:00
|
|
|
return 43
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(
|
|
|
|
scope="class",
|
|
|
|
params=[
|
|
|
|
BotCommandScope.DEFAULT,
|
|
|
|
BotCommandScope.ALL_PRIVATE_CHATS,
|
|
|
|
BotCommandScope.ALL_GROUP_CHATS,
|
|
|
|
BotCommandScope.ALL_CHAT_ADMINISTRATORS,
|
|
|
|
BotCommandScope.CHAT,
|
|
|
|
BotCommandScope.CHAT_ADMINISTRATORS,
|
|
|
|
BotCommandScope.CHAT_MEMBER,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def scope_type(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(
|
2023-02-11 10:45:17 +01:00
|
|
|
scope="module",
|
2021-07-01 17:45:19 +02:00
|
|
|
params=[
|
|
|
|
BotCommandScopeDefault,
|
|
|
|
BotCommandScopeAllPrivateChats,
|
|
|
|
BotCommandScopeAllGroupChats,
|
|
|
|
BotCommandScopeAllChatAdministrators,
|
|
|
|
BotCommandScopeChat,
|
|
|
|
BotCommandScopeChatAdministrators,
|
|
|
|
BotCommandScopeChatMember,
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
BotCommandScope.DEFAULT,
|
|
|
|
BotCommandScope.ALL_PRIVATE_CHATS,
|
|
|
|
BotCommandScope.ALL_GROUP_CHATS,
|
|
|
|
BotCommandScope.ALL_CHAT_ADMINISTRATORS,
|
|
|
|
BotCommandScope.CHAT,
|
|
|
|
BotCommandScope.CHAT_ADMINISTRATORS,
|
|
|
|
BotCommandScope.CHAT_MEMBER,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def scope_class(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(
|
2023-02-11 10:45:17 +01:00
|
|
|
scope="module",
|
2021-07-01 17:45:19 +02:00
|
|
|
params=[
|
|
|
|
(BotCommandScopeDefault, BotCommandScope.DEFAULT),
|
|
|
|
(BotCommandScopeAllPrivateChats, BotCommandScope.ALL_PRIVATE_CHATS),
|
|
|
|
(BotCommandScopeAllGroupChats, BotCommandScope.ALL_GROUP_CHATS),
|
|
|
|
(BotCommandScopeAllChatAdministrators, BotCommandScope.ALL_CHAT_ADMINISTRATORS),
|
|
|
|
(BotCommandScopeChat, BotCommandScope.CHAT),
|
|
|
|
(BotCommandScopeChatAdministrators, BotCommandScope.CHAT_ADMINISTRATORS),
|
|
|
|
(BotCommandScopeChatMember, BotCommandScope.CHAT_MEMBER),
|
|
|
|
],
|
|
|
|
ids=[
|
|
|
|
BotCommandScope.DEFAULT,
|
|
|
|
BotCommandScope.ALL_PRIVATE_CHATS,
|
|
|
|
BotCommandScope.ALL_GROUP_CHATS,
|
|
|
|
BotCommandScope.ALL_CHAT_ADMINISTRATORS,
|
|
|
|
BotCommandScope.CHAT,
|
|
|
|
BotCommandScope.CHAT_ADMINISTRATORS,
|
|
|
|
BotCommandScope.CHAT_MEMBER,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def scope_class_and_type(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
2023-02-11 10:45:17 +01:00
|
|
|
@pytest.fixture(scope="module")
|
2021-07-01 17:45:19 +02:00
|
|
|
def bot_command_scope(scope_class_and_type, chat_id):
|
2022-10-07 11:51:53 +02:00
|
|
|
# we use de_json here so that we don't have to worry about which class needs which arguments
|
|
|
|
return scope_class_and_type[0].de_json(
|
2023-03-25 19:18:04 +01:00
|
|
|
{"type": scope_class_and_type[1], "chat_id": chat_id, "user_id": 42}, bot=None
|
2022-10-07 11:51:53 +02:00
|
|
|
)
|
2021-07-01 17:45:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
# All the scope types are very similar, so we test everything via parametrization
|
2023-02-11 10:45:17 +01:00
|
|
|
class TestBotCommandScopeWithoutRequest:
|
2023-02-22 20:19:46 +01:00
|
|
|
def test_slot_behaviour(self, bot_command_scope):
|
2021-07-01 17:45:19 +02:00
|
|
|
for attr in bot_command_scope.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(bot_command_scope, attr, "err") != "err", f"got extra slot '{attr}'"
|
2021-07-01 17:45:19 +02:00
|
|
|
assert len(mro_slots(bot_command_scope)) == len(
|
|
|
|
set(mro_slots(bot_command_scope))
|
|
|
|
), "duplicate slot"
|
|
|
|
|
|
|
|
def test_de_json(self, bot, scope_class_and_type, chat_id):
|
|
|
|
cls = scope_class_and_type[0]
|
|
|
|
type_ = scope_class_and_type[1]
|
|
|
|
|
|
|
|
assert cls.de_json({}, bot) is None
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
json_dict = {"type": type_, "chat_id": chat_id, "user_id": 42}
|
2021-07-01 17:45:19 +02:00
|
|
|
bot_command_scope = BotCommandScope.de_json(json_dict, bot)
|
2022-10-07 11:51:53 +02:00
|
|
|
assert set(bot_command_scope.api_kwargs.keys()) == {"chat_id", "user_id"} - set(
|
|
|
|
cls.__slots__
|
|
|
|
)
|
2021-07-01 17:45:19 +02:00
|
|
|
|
|
|
|
assert isinstance(bot_command_scope, BotCommandScope)
|
|
|
|
assert isinstance(bot_command_scope, cls)
|
|
|
|
assert bot_command_scope.type == type_
|
2022-05-05 17:40:22 +02:00
|
|
|
if "chat_id" in cls.__slots__:
|
2021-07-01 17:45:19 +02:00
|
|
|
assert bot_command_scope.chat_id == chat_id
|
2022-05-05 17:40:22 +02:00
|
|
|
if "user_id" in cls.__slots__:
|
2021-07-01 17:45:19 +02:00
|
|
|
assert bot_command_scope.user_id == 42
|
|
|
|
|
|
|
|
def test_de_json_invalid_type(self, bot):
|
2022-05-05 17:40:22 +02:00
|
|
|
json_dict = {"type": "invalid", "chat_id": chat_id, "user_id": 42}
|
2021-07-01 17:45:19 +02:00
|
|
|
bot_command_scope = BotCommandScope.de_json(json_dict, bot)
|
|
|
|
|
|
|
|
assert type(bot_command_scope) is BotCommandScope
|
2022-05-05 17:40:22 +02:00
|
|
|
assert bot_command_scope.type == "invalid"
|
2021-07-01 17:45:19 +02:00
|
|
|
|
|
|
|
def test_de_json_subclass(self, scope_class, bot, chat_id):
|
|
|
|
"""This makes sure that e.g. BotCommandScopeDefault(data) never returns a
|
|
|
|
BotCommandScopeChat instance."""
|
2022-05-05 17:40:22 +02:00
|
|
|
json_dict = {"type": "invalid", "chat_id": chat_id, "user_id": 42}
|
2021-07-01 17:45:19 +02:00
|
|
|
assert type(scope_class.de_json(json_dict, bot)) is scope_class
|
|
|
|
|
|
|
|
def test_to_dict(self, bot_command_scope):
|
|
|
|
bot_command_scope_dict = bot_command_scope.to_dict()
|
|
|
|
|
|
|
|
assert isinstance(bot_command_scope_dict, dict)
|
2022-05-05 17:40:22 +02:00
|
|
|
assert bot_command_scope["type"] == bot_command_scope.type
|
|
|
|
if hasattr(bot_command_scope, "chat_id"):
|
|
|
|
assert bot_command_scope["chat_id"] == bot_command_scope.chat_id
|
|
|
|
if hasattr(bot_command_scope, "user_id"):
|
|
|
|
assert bot_command_scope["user_id"] == bot_command_scope.user_id
|
2021-07-01 17:45:19 +02:00
|
|
|
|
2024-01-17 21:32:37 +01:00
|
|
|
def test_type_enum_conversion(self):
|
|
|
|
assert type(BotCommandScope("default").type) is BotCommandScopeType
|
|
|
|
assert BotCommandScope("unknown").type == "unknown"
|
|
|
|
|
2021-07-01 17:45:19 +02:00
|
|
|
def test_equality(self, bot_command_scope, bot):
|
2022-05-05 17:40:22 +02:00
|
|
|
a = BotCommandScope("base_type")
|
|
|
|
b = BotCommandScope("base_type")
|
2021-07-01 17:45:19 +02:00
|
|
|
c = bot_command_scope
|
|
|
|
d = deepcopy(bot_command_scope)
|
2022-05-05 17:40:22 +02:00
|
|
|
e = Dice(4, "emoji")
|
2021-07-01 17:45:19 +02:00
|
|
|
|
|
|
|
assert a == b
|
|
|
|
assert hash(a) == hash(b)
|
|
|
|
|
|
|
|
assert a != c
|
|
|
|
assert hash(a) != hash(c)
|
|
|
|
|
|
|
|
assert a != d
|
|
|
|
assert hash(a) != hash(d)
|
|
|
|
|
|
|
|
assert a != e
|
|
|
|
assert hash(a) != hash(e)
|
|
|
|
|
|
|
|
assert c == d
|
|
|
|
assert hash(c) == hash(d)
|
|
|
|
|
|
|
|
assert c != e
|
|
|
|
assert hash(c) != hash(e)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if hasattr(c, "chat_id"):
|
2021-07-01 17:45:19 +02:00
|
|
|
json_dict = c.to_dict()
|
2022-05-05 17:40:22 +02:00
|
|
|
json_dict["chat_id"] = 0
|
2021-07-01 17:45:19 +02:00
|
|
|
f = c.__class__.de_json(json_dict, bot)
|
|
|
|
|
|
|
|
assert c != f
|
|
|
|
assert hash(c) != hash(f)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
if hasattr(c, "user_id"):
|
2021-07-01 17:45:19 +02:00
|
|
|
json_dict = c.to_dict()
|
2022-05-05 17:40:22 +02:00
|
|
|
json_dict["user_id"] = 0
|
2021-07-01 17:45:19 +02:00
|
|
|
g = c.__class__.de_json(json_dict, bot)
|
|
|
|
|
|
|
|
assert c != g
|
|
|
|
assert hash(c) != hash(g)
|