File: test_chatjoinrequest.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 (198 lines) | stat: -rw-r--r-- 7,874 bytes parent folder | download | duplicates (2)
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
#!/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 Bot, Chat, ChatInviteLink, ChatJoinRequest, User
from telegram._utils.datetime import UTC, to_timestamp
from tests.auxil.bot_method_checks import (
    check_defaults_handling,
    check_shortcut_call,
    check_shortcut_signature,
)
from tests.auxil.slots import mro_slots


@pytest.fixture(scope="module")
def time():
    return dtm.datetime.now(tz=UTC)


@pytest.fixture(scope="module")
def chat_join_request(bot, time):
    cjr = ChatJoinRequest(
        chat=ChatJoinRequestTestBase.chat,
        from_user=ChatJoinRequestTestBase.from_user,
        date=time,
        bio=ChatJoinRequestTestBase.bio,
        invite_link=ChatJoinRequestTestBase.invite_link,
        user_chat_id=ChatJoinRequestTestBase.from_user.id,
    )
    cjr.set_bot(bot)
    return cjr


class ChatJoinRequestTestBase:
    chat = Chat(1, Chat.SUPERGROUP)
    from_user = User(2, "first_name", False)
    bio = "bio"
    invite_link = ChatInviteLink(
        "https://invite.link",
        User(42, "creator", False),
        creates_join_request=False,
        name="InviteLink",
        is_revoked=False,
        is_primary=False,
    )


class TestChatJoinRequestWithoutRequest(ChatJoinRequestTestBase):
    def test_slot_behaviour(self, chat_join_request):
        inst = chat_join_request
        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, time):
        json_dict = {
            "chat": self.chat.to_dict(),
            "from": self.from_user.to_dict(),
            "date": to_timestamp(time),
            "user_chat_id": self.from_user.id,
        }
        chat_join_request = ChatJoinRequest.de_json(json_dict, offline_bot)
        assert chat_join_request.api_kwargs == {}

        assert chat_join_request.chat == self.chat
        assert chat_join_request.from_user == self.from_user
        assert abs(chat_join_request.date - time) < dtm.timedelta(seconds=1)
        assert to_timestamp(chat_join_request.date) == to_timestamp(time)
        assert chat_join_request.user_chat_id == self.from_user.id

        json_dict.update({"bio": self.bio, "invite_link": self.invite_link.to_dict()})
        chat_join_request = ChatJoinRequest.de_json(json_dict, offline_bot)
        assert chat_join_request.api_kwargs == {}

        assert chat_join_request.chat == self.chat
        assert chat_join_request.from_user == self.from_user
        assert abs(chat_join_request.date - time) < dtm.timedelta(seconds=1)
        assert to_timestamp(chat_join_request.date) == to_timestamp(time)
        assert chat_join_request.user_chat_id == self.from_user.id
        assert chat_join_request.bio == self.bio
        assert chat_join_request.invite_link == self.invite_link

    def test_de_json_localization(self, tz_bot, offline_bot, raw_bot, time):
        json_dict = {
            "chat": self.chat.to_dict(),
            "from": self.from_user.to_dict(),
            "date": to_timestamp(time),
            "user_chat_id": self.from_user.id,
        }

        chatjoin_req_raw = ChatJoinRequest.de_json(json_dict, raw_bot)
        chatjoin_req_bot = ChatJoinRequest.de_json(json_dict, offline_bot)
        chatjoin_req_tz = ChatJoinRequest.de_json(json_dict, tz_bot)

        # comparing utcoffsets because comparing timezones is unpredicatable
        chatjoin_req_offset = chatjoin_req_tz.date.utcoffset()
        tz_bot_offset = tz_bot.defaults.tzinfo.utcoffset(chatjoin_req_tz.date.replace(tzinfo=None))

        assert chatjoin_req_raw.date.tzinfo == UTC
        assert chatjoin_req_bot.date.tzinfo == UTC
        assert chatjoin_req_offset == tz_bot_offset

    def test_to_dict(self, chat_join_request, time):
        chat_join_request_dict = chat_join_request.to_dict()

        assert isinstance(chat_join_request_dict, dict)
        assert chat_join_request_dict["chat"] == chat_join_request.chat.to_dict()
        assert chat_join_request_dict["from"] == chat_join_request.from_user.to_dict()
        assert chat_join_request_dict["date"] == to_timestamp(chat_join_request.date)
        assert chat_join_request_dict["bio"] == chat_join_request.bio
        assert chat_join_request_dict["invite_link"] == chat_join_request.invite_link.to_dict()
        assert chat_join_request_dict["user_chat_id"] == self.from_user.id

    def test_equality(self, chat_join_request, time):
        a = chat_join_request
        b = ChatJoinRequest(self.chat, self.from_user, time, self.from_user.id)
        c = ChatJoinRequest(self.chat, self.from_user, time, self.from_user.id, bio="bio")
        d = ChatJoinRequest(self.chat, self.from_user, time + dtm.timedelta(1), self.from_user.id)
        e = ChatJoinRequest(self.chat, User(-1, "last_name", True), time, -1)
        f = User(456, "", False)

        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 a != e
        assert hash(a) != hash(e)

        assert a != f
        assert hash(a) != hash(f)

    async def test_approve(self, monkeypatch, chat_join_request):
        async def make_assertion(*_, **kwargs):
            chat_id_test = kwargs["chat_id"] == chat_join_request.chat.id
            user_id_test = kwargs["user_id"] == chat_join_request.from_user.id

            return chat_id_test and user_id_test

        assert check_shortcut_signature(
            ChatJoinRequest.approve, Bot.approve_chat_join_request, ["chat_id", "user_id"], []
        )
        assert await check_shortcut_call(
            chat_join_request.approve, chat_join_request.get_bot(), "approve_chat_join_request"
        )
        assert await check_defaults_handling(
            chat_join_request.approve, chat_join_request.get_bot()
        )

        monkeypatch.setattr(
            chat_join_request.get_bot(), "approve_chat_join_request", make_assertion
        )
        assert await chat_join_request.approve()

    async def test_decline(self, monkeypatch, chat_join_request):
        async def make_assertion(*_, **kwargs):
            chat_id_test = kwargs["chat_id"] == chat_join_request.chat.id
            user_id_test = kwargs["user_id"] == chat_join_request.from_user.id

            return chat_id_test and user_id_test

        assert check_shortcut_signature(
            ChatJoinRequest.decline, Bot.decline_chat_join_request, ["chat_id", "user_id"], []
        )
        assert await check_shortcut_call(
            chat_join_request.decline, chat_join_request.get_bot(), "decline_chat_join_request"
        )
        assert await check_defaults_handling(
            chat_join_request.decline, chat_join_request.get_bot()
        )

        monkeypatch.setattr(
            chat_join_request.get_bot(), "decline_chat_join_request", make_assertion
        )
        assert await chat_join_request.decline()