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
|
#!/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 pytest
from telegram import ChatAdministratorRights
from tests.auxil.slots import mro_slots
@pytest.fixture(scope="module")
def chat_admin_rights():
return ChatAdministratorRights(
can_change_info=True,
can_delete_messages=True,
can_invite_users=True,
can_pin_messages=True,
can_promote_members=True,
can_restrict_members=True,
can_post_messages=True,
can_edit_messages=True,
can_manage_chat=True,
can_manage_video_chats=True,
can_manage_topics=True,
is_anonymous=True,
can_post_stories=True,
can_edit_stories=True,
can_delete_stories=True,
)
class TestChatAdministratorRightsWithoutRequest:
def test_slot_behaviour(self, chat_admin_rights):
inst = chat_admin_rights
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(self, offline_bot, chat_admin_rights):
json_dict = {
"can_change_info": True,
"can_delete_messages": True,
"can_invite_users": True,
"can_pin_messages": True,
"can_promote_members": True,
"can_restrict_members": True,
"can_post_messages": True,
"can_edit_messages": True,
"can_manage_chat": True,
"can_manage_video_chats": True,
"can_manage_topics": True,
"is_anonymous": True,
"can_post_stories": True,
"can_edit_stories": True,
"can_delete_stories": True,
}
chat_administrator_rights_de = ChatAdministratorRights.de_json(json_dict, offline_bot)
assert chat_administrator_rights_de.api_kwargs == {}
assert chat_admin_rights == chat_administrator_rights_de
def test_to_dict(self, chat_admin_rights):
car = chat_admin_rights
admin_rights_dict = car.to_dict()
assert isinstance(admin_rights_dict, dict)
assert admin_rights_dict["can_change_info"] == car.can_change_info
assert admin_rights_dict["can_delete_messages"] == car.can_delete_messages
assert admin_rights_dict["can_invite_users"] == car.can_invite_users
assert admin_rights_dict["can_pin_messages"] == car.can_pin_messages
assert admin_rights_dict["can_promote_members"] == car.can_promote_members
assert admin_rights_dict["can_restrict_members"] == car.can_restrict_members
assert admin_rights_dict["can_post_messages"] == car.can_post_messages
assert admin_rights_dict["can_edit_messages"] == car.can_edit_messages
assert admin_rights_dict["can_manage_chat"] == car.can_manage_chat
assert admin_rights_dict["is_anonymous"] == car.is_anonymous
assert admin_rights_dict["can_manage_video_chats"] == car.can_manage_video_chats
assert admin_rights_dict["can_manage_topics"] == car.can_manage_topics
assert admin_rights_dict["can_post_stories"] == car.can_post_stories
assert admin_rights_dict["can_edit_stories"] == car.can_edit_stories
assert admin_rights_dict["can_delete_stories"] == car.can_delete_stories
def test_equality(self):
a = ChatAdministratorRights(
True,
*((False,) * 11),
)
b = ChatAdministratorRights(
True,
*((False,) * 11),
)
c = ChatAdministratorRights(
*(False,) * 12,
)
d = ChatAdministratorRights(
True,
True,
*((False,) * 10),
)
e = ChatAdministratorRights(
True,
True,
*((False,) * 10),
)
assert a == b
assert hash(a) == hash(b)
assert a is not b
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
assert d == e
assert hash(d) == hash(e)
def test_all_rights(self):
f = ChatAdministratorRights(
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
True,
)
t = ChatAdministratorRights.all_rights()
# if the dirs are the same, the attributes will all be there
assert dir(f) == dir(t)
# now we just need to check that all attributes are True. __slots__ returns all values,
# if a new one is added without defaulting to True, this will fail
for key in t.__slots__:
assert t[key] is True
# and as a finisher, make sure the default is different.
assert f != t
def test_no_rights(self):
f = ChatAdministratorRights(
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
False,
)
t = ChatAdministratorRights.no_rights()
# if the dirs are the same, the attributes will all be there
assert dir(f) == dir(t)
# now we just need to check that all attributes are True. __slots__ returns all values,
# if a new one is added without defaulting to True, this will fail
for key in t.__slots__:
assert t[key] is False
# and as a finisher, make sure the default is different.
assert f != t
|