File: test_chatfullinfo.py

package info (click to toggle)
python-telegram-bot 22.3-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 11,060 kB
  • sloc: python: 90,298; makefile: 176; sh: 4
file content (373 lines) | stat: -rw-r--r-- 18,254 bytes parent folder | download
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/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 datetime as dtm

import pytest

from telegram import (
    Birthdate,
    BusinessIntro,
    BusinessLocation,
    BusinessOpeningHours,
    BusinessOpeningHoursInterval,
    Chat,
    ChatFullInfo,
    ChatLocation,
    ChatPermissions,
    Location,
    ReactionTypeCustomEmoji,
    ReactionTypeEmoji,
)
from telegram._gifts import AcceptedGiftTypes
from telegram._utils.datetime import UTC, to_timestamp
from telegram.constants import ReactionEmoji
from telegram.warnings import PTBDeprecationWarning
from tests.auxil.slots import mro_slots


@pytest.fixture(scope="module")
def chat_full_info(bot):
    chat = ChatFullInfo(
        ChatFullInfoTestBase.id_,
        type=ChatFullInfoTestBase.type_,
        accent_color_id=ChatFullInfoTestBase.accent_color_id,
        max_reaction_count=ChatFullInfoTestBase.max_reaction_count,
        accepted_gift_types=ChatFullInfoTestBase.accepted_gift_types,
        title=ChatFullInfoTestBase.title,
        username=ChatFullInfoTestBase.username,
        sticker_set_name=ChatFullInfoTestBase.sticker_set_name,
        can_set_sticker_set=ChatFullInfoTestBase.can_set_sticker_set,
        permissions=ChatFullInfoTestBase.permissions,
        slow_mode_delay=ChatFullInfoTestBase.slow_mode_delay,
        message_auto_delete_time=ChatFullInfoTestBase.message_auto_delete_time,
        bio=ChatFullInfoTestBase.bio,
        linked_chat_id=ChatFullInfoTestBase.linked_chat_id,
        location=ChatFullInfoTestBase.location,
        has_private_forwards=ChatFullInfoTestBase.has_private_forwards,
        has_protected_content=ChatFullInfoTestBase.has_protected_content,
        has_visible_history=ChatFullInfoTestBase.has_visible_history,
        join_to_send_messages=ChatFullInfoTestBase.join_to_send_messages,
        join_by_request=ChatFullInfoTestBase.join_by_request,
        has_restricted_voice_and_video_messages=(
            ChatFullInfoTestBase.has_restricted_voice_and_video_messages
        ),
        is_forum=ChatFullInfoTestBase.is_forum,
        active_usernames=ChatFullInfoTestBase.active_usernames,
        emoji_status_custom_emoji_id=ChatFullInfoTestBase.emoji_status_custom_emoji_id,
        emoji_status_expiration_date=ChatFullInfoTestBase.emoji_status_expiration_date,
        has_aggressive_anti_spam_enabled=ChatFullInfoTestBase.has_aggressive_anti_spam_enabled,
        has_hidden_members=ChatFullInfoTestBase.has_hidden_members,
        available_reactions=ChatFullInfoTestBase.available_reactions,
        background_custom_emoji_id=ChatFullInfoTestBase.background_custom_emoji_id,
        profile_accent_color_id=ChatFullInfoTestBase.profile_accent_color_id,
        profile_background_custom_emoji_id=ChatFullInfoTestBase.profile_background_custom_emoji_id,
        unrestrict_boost_count=ChatFullInfoTestBase.unrestrict_boost_count,
        custom_emoji_sticker_set_name=ChatFullInfoTestBase.custom_emoji_sticker_set_name,
        business_intro=ChatFullInfoTestBase.business_intro,
        business_location=ChatFullInfoTestBase.business_location,
        business_opening_hours=ChatFullInfoTestBase.business_opening_hours,
        birthdate=ChatFullInfoTestBase.birthdate,
        personal_chat=ChatFullInfoTestBase.personal_chat,
        first_name=ChatFullInfoTestBase.first_name,
        last_name=ChatFullInfoTestBase.last_name,
        can_send_paid_media=ChatFullInfoTestBase.can_send_paid_media,
    )
    chat.set_bot(bot)
    chat._unfreeze()
    return chat


# Shortcut methods are tested in test_chat.py.
class ChatFullInfoTestBase:
    id_ = -28767330
    max_reaction_count = 2
    title = "ToledosPalaceBot - Group"
    type_ = "group"
    username = "username"
    sticker_set_name = "stickers"
    can_set_sticker_set = False
    permissions = ChatPermissions(
        can_send_messages=True,
        can_change_info=False,
        can_invite_users=True,
    )
    slow_mode_delay = dtm.timedelta(seconds=30)
    message_auto_delete_time = dtm.timedelta(60)
    bio = "I'm a Barbie Girl in a Barbie World"
    linked_chat_id = 11880
    location = ChatLocation(Location(123, 456), "Barbie World")
    has_protected_content = True
    has_visible_history = True
    has_private_forwards = True
    join_to_send_messages = True
    join_by_request = True
    has_restricted_voice_and_video_messages = True
    is_forum = True
    active_usernames = ["These", "Are", "Usernames!"]
    emoji_status_custom_emoji_id = "VeryUniqueCustomEmojiID"
    emoji_status_expiration_date = dtm.datetime.now(tz=UTC).replace(microsecond=0)
    has_aggressive_anti_spam_enabled = True
    has_hidden_members = True
    available_reactions = [
        ReactionTypeEmoji(ReactionEmoji.THUMBS_DOWN),
        ReactionTypeCustomEmoji("custom_emoji_id"),
    ]
    business_intro = BusinessIntro("Title", "Description", None)
    business_location = BusinessLocation("Address", Location(123, 456))
    business_opening_hours = BusinessOpeningHours(
        "Country/City",
        [BusinessOpeningHoursInterval(opening, opening + 60) for opening in (0, 24 * 60)],
    )
    accent_color_id = 1
    background_custom_emoji_id = "background_custom_emoji_id"
    profile_accent_color_id = 2
    profile_background_custom_emoji_id = "profile_background_custom_emoji_id"
    unrestrict_boost_count = 100
    custom_emoji_sticker_set_name = "custom_emoji_sticker_set_name"
    birthdate = Birthdate(1, 1)
    personal_chat = Chat(3, "private", "private")
    first_name = "first_name"
    last_name = "last_name"
    can_send_paid_media = True
    accepted_gift_types = AcceptedGiftTypes(True, True, True, True)


class TestChatFullInfoWithoutRequest(ChatFullInfoTestBase):
    def test_slot_behaviour(self, chat_full_info):
        cfi = chat_full_info
        for attr in cfi.__slots__:
            assert getattr(cfi, attr, "err") != "err", f"got extra slot '{attr}'"

        assert len(mro_slots(cfi)) == len(set(mro_slots(cfi))), "duplicate slot"

    def test_de_json(self, offline_bot):
        json_dict = {
            "id": self.id_,
            "title": self.title,
            "type": self.type_,
            "accent_color_id": self.accent_color_id,
            "max_reaction_count": self.max_reaction_count,
            "username": self.username,
            "accepted_gift_types": self.accepted_gift_types.to_dict(),
            "sticker_set_name": self.sticker_set_name,
            "can_set_sticker_set": self.can_set_sticker_set,
            "permissions": self.permissions.to_dict(),
            "slow_mode_delay": self.slow_mode_delay.total_seconds(),
            "message_auto_delete_time": self.message_auto_delete_time.total_seconds(),
            "bio": self.bio,
            "business_intro": self.business_intro.to_dict(),
            "business_location": self.business_location.to_dict(),
            "business_opening_hours": self.business_opening_hours.to_dict(),
            "has_protected_content": self.has_protected_content,
            "has_visible_history": self.has_visible_history,
            "has_private_forwards": self.has_private_forwards,
            "linked_chat_id": self.linked_chat_id,
            "location": self.location.to_dict(),
            "join_to_send_messages": self.join_to_send_messages,
            "join_by_request": self.join_by_request,
            "has_restricted_voice_and_video_messages": (
                self.has_restricted_voice_and_video_messages
            ),
            "is_forum": self.is_forum,
            "active_usernames": self.active_usernames,
            "emoji_status_custom_emoji_id": self.emoji_status_custom_emoji_id,
            "emoji_status_expiration_date": to_timestamp(self.emoji_status_expiration_date),
            "has_aggressive_anti_spam_enabled": self.has_aggressive_anti_spam_enabled,
            "has_hidden_members": self.has_hidden_members,
            "available_reactions": [reaction.to_dict() for reaction in self.available_reactions],
            "background_custom_emoji_id": self.background_custom_emoji_id,
            "profile_accent_color_id": self.profile_accent_color_id,
            "profile_background_custom_emoji_id": self.profile_background_custom_emoji_id,
            "unrestrict_boost_count": self.unrestrict_boost_count,
            "custom_emoji_sticker_set_name": self.custom_emoji_sticker_set_name,
            "birthdate": self.birthdate.to_dict(),
            "personal_chat": self.personal_chat.to_dict(),
            "first_name": self.first_name,
            "last_name": self.last_name,
            "can_send_paid_media": self.can_send_paid_media,
        }

        cfi = ChatFullInfo.de_json(json_dict, offline_bot)
        assert cfi.api_kwargs == {}
        assert cfi.id == self.id_
        assert cfi.title == self.title
        assert cfi.type == self.type_
        assert cfi.username == self.username
        assert cfi.accepted_gift_types == self.accepted_gift_types
        assert cfi.sticker_set_name == self.sticker_set_name
        assert cfi.can_set_sticker_set == self.can_set_sticker_set
        assert cfi.permissions == self.permissions
        assert cfi._slow_mode_delay == self.slow_mode_delay
        assert cfi._message_auto_delete_time == self.message_auto_delete_time
        assert cfi.bio == self.bio
        assert cfi.business_intro == self.business_intro
        assert cfi.business_location == self.business_location
        assert cfi.business_opening_hours == self.business_opening_hours
        assert cfi.has_protected_content == self.has_protected_content
        assert cfi.has_visible_history == self.has_visible_history
        assert cfi.has_private_forwards == self.has_private_forwards
        assert cfi.linked_chat_id == self.linked_chat_id
        assert cfi.location.location == self.location.location
        assert cfi.location.address == self.location.address
        assert cfi.join_to_send_messages == self.join_to_send_messages
        assert cfi.join_by_request == self.join_by_request
        assert (
            cfi.has_restricted_voice_and_video_messages
            == self.has_restricted_voice_and_video_messages
        )
        assert cfi.is_forum == self.is_forum
        assert cfi.active_usernames == tuple(self.active_usernames)
        assert cfi.emoji_status_custom_emoji_id == self.emoji_status_custom_emoji_id
        assert cfi.emoji_status_expiration_date == (self.emoji_status_expiration_date)
        assert cfi.has_aggressive_anti_spam_enabled == self.has_aggressive_anti_spam_enabled
        assert cfi.has_hidden_members == self.has_hidden_members
        assert cfi.available_reactions == tuple(self.available_reactions)
        assert cfi.accent_color_id == self.accent_color_id
        assert cfi.background_custom_emoji_id == self.background_custom_emoji_id
        assert cfi.profile_accent_color_id == self.profile_accent_color_id
        assert cfi.profile_background_custom_emoji_id == self.profile_background_custom_emoji_id
        assert cfi.unrestrict_boost_count == self.unrestrict_boost_count
        assert cfi.custom_emoji_sticker_set_name == self.custom_emoji_sticker_set_name
        assert cfi.birthdate == self.birthdate
        assert cfi.personal_chat == self.personal_chat
        assert cfi.first_name == self.first_name
        assert cfi.last_name == self.last_name
        assert cfi.max_reaction_count == self.max_reaction_count
        assert cfi.can_send_paid_media == self.can_send_paid_media

    def test_de_json_localization(self, offline_bot, raw_bot, tz_bot):
        json_dict = {
            "id": self.id_,
            "type": self.type_,
            "accent_color_id": self.accent_color_id,
            "max_reaction_count": self.max_reaction_count,
            "accepted_gift_types": self.accepted_gift_types.to_dict(),
            "emoji_status_expiration_date": to_timestamp(self.emoji_status_expiration_date),
        }
        cfi_bot = ChatFullInfo.de_json(json_dict, offline_bot)
        cfi_bot_raw = ChatFullInfo.de_json(json_dict, raw_bot)
        cfi_bot_tz = ChatFullInfo.de_json(json_dict, tz_bot)

        # comparing utcoffsets because comparing tzinfo objects is not reliable
        emoji_expire_offset = cfi_bot_tz.emoji_status_expiration_date.utcoffset()
        emoji_expire_offset_tz = tz_bot.defaults.tzinfo.utcoffset(
            cfi_bot_tz.emoji_status_expiration_date.replace(tzinfo=None)
        )

        assert cfi_bot.emoji_status_expiration_date.tzinfo == UTC
        assert cfi_bot_raw.emoji_status_expiration_date.tzinfo == UTC
        assert emoji_expire_offset_tz == emoji_expire_offset

    def test_to_dict(self, chat_full_info):
        cfi = chat_full_info
        cfi_dict = cfi.to_dict()

        assert isinstance(cfi_dict, dict)
        assert cfi_dict["id"] == cfi.id
        assert cfi_dict["title"] == cfi.title
        assert cfi_dict["type"] == cfi.type
        assert cfi_dict["username"] == cfi.username
        assert cfi_dict["permissions"] == cfi.permissions.to_dict()
        assert cfi_dict["slow_mode_delay"] == int(self.slow_mode_delay.total_seconds())
        assert cfi_dict["message_auto_delete_time"] == int(
            self.message_auto_delete_time.total_seconds()
        )
        assert cfi_dict["bio"] == cfi.bio
        assert cfi_dict["business_intro"] == cfi.business_intro.to_dict()
        assert cfi_dict["business_location"] == cfi.business_location.to_dict()
        assert cfi_dict["business_opening_hours"] == cfi.business_opening_hours.to_dict()
        assert cfi_dict["has_private_forwards"] == cfi.has_private_forwards
        assert cfi_dict["has_protected_content"] == cfi.has_protected_content
        assert cfi_dict["has_visible_history"] == cfi.has_visible_history
        assert cfi_dict["linked_chat_id"] == cfi.linked_chat_id
        assert cfi_dict["location"] == cfi.location.to_dict()
        assert cfi_dict["join_to_send_messages"] == cfi.join_to_send_messages
        assert cfi_dict["join_by_request"] == cfi.join_by_request
        assert (
            cfi_dict["has_restricted_voice_and_video_messages"]
            == cfi.has_restricted_voice_and_video_messages
        )
        assert cfi_dict["is_forum"] == cfi.is_forum
        assert cfi_dict["active_usernames"] == list(cfi.active_usernames)
        assert cfi_dict["emoji_status_custom_emoji_id"] == cfi.emoji_status_custom_emoji_id
        assert cfi_dict["emoji_status_expiration_date"] == to_timestamp(
            cfi.emoji_status_expiration_date
        )
        assert cfi_dict["has_aggressive_anti_spam_enabled"] == cfi.has_aggressive_anti_spam_enabled
        assert cfi_dict["has_hidden_members"] == cfi.has_hidden_members
        assert cfi_dict["available_reactions"] == [
            reaction.to_dict() for reaction in cfi.available_reactions
        ]
        assert cfi_dict["accent_color_id"] == cfi.accent_color_id
        assert cfi_dict["background_custom_emoji_id"] == cfi.background_custom_emoji_id
        assert cfi_dict["profile_accent_color_id"] == cfi.profile_accent_color_id
        assert (
            cfi_dict["profile_background_custom_emoji_id"]
            == cfi.profile_background_custom_emoji_id
        )
        assert cfi_dict["custom_emoji_sticker_set_name"] == cfi.custom_emoji_sticker_set_name
        assert cfi_dict["unrestrict_boost_count"] == cfi.unrestrict_boost_count
        assert cfi_dict["birthdate"] == cfi.birthdate.to_dict()
        assert cfi_dict["personal_chat"] == cfi.personal_chat.to_dict()
        assert cfi_dict["first_name"] == cfi.first_name
        assert cfi_dict["last_name"] == cfi.last_name
        assert cfi_dict["can_send_paid_media"] == cfi.can_send_paid_media
        assert cfi_dict["accepted_gift_types"] == cfi.accepted_gift_types.to_dict()

        assert cfi_dict["max_reaction_count"] == cfi.max_reaction_count

    def test_time_period_properties(self, PTB_TIMEDELTA, chat_full_info):
        cfi = chat_full_info
        if PTB_TIMEDELTA:
            assert cfi.slow_mode_delay == self.slow_mode_delay
            assert isinstance(cfi.slow_mode_delay, dtm.timedelta)

            assert cfi.message_auto_delete_time == self.message_auto_delete_time
            assert isinstance(cfi.message_auto_delete_time, dtm.timedelta)
        else:
            assert cfi.slow_mode_delay == int(self.slow_mode_delay.total_seconds())
            assert isinstance(cfi.slow_mode_delay, int)

            assert cfi.message_auto_delete_time == int(
                self.message_auto_delete_time.total_seconds()
            )
            assert isinstance(cfi.message_auto_delete_time, int)

    def test_time_period_int_deprecated(self, recwarn, PTB_TIMEDELTA, chat_full_info):
        chat_full_info.slow_mode_delay
        chat_full_info.message_auto_delete_time

        if PTB_TIMEDELTA:
            assert len(recwarn) == 0
        else:
            assert len(recwarn) == 2
            for i, attr in enumerate(["slow_mode_delay", "message_auto_delete_time"]):
                assert f"`{attr}` will be of type `datetime.timedelta`" in str(recwarn[i].message)
                assert recwarn[i].category is PTBDeprecationWarning

    def test_always_tuples_attributes(self):
        cfi = ChatFullInfo(
            id=123,
            type=Chat.PRIVATE,
            accent_color_id=1,
            max_reaction_count=2,
            accepted_gift_types=self.accepted_gift_types,
        )
        assert isinstance(cfi.active_usernames, tuple)
        assert cfi.active_usernames == ()