2021-11-08 19:02:20 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2022-01-03 08:15:18 +01:00
|
|
|
# Copyright (C) 2015-2022
|
2021-11-08 19:02:20 +01: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/].
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import pytz
|
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import Bot, Chat, ChatInviteLink, ChatJoinRequest, User
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.datetime import to_timestamp
|
2022-05-05 09:27:54 +02:00
|
|
|
from tests.conftest import check_defaults_handling, check_shortcut_call, check_shortcut_signature
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2021-11-08 19:02:20 +01:00
|
|
|
def time():
|
|
|
|
return datetime.datetime.now(tz=pytz.utc)
|
|
|
|
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.fixture(scope="class")
|
2021-11-08 19:02:20 +01:00
|
|
|
def chat_join_request(bot, time):
|
|
|
|
return ChatJoinRequest(
|
|
|
|
chat=TestChatJoinRequest.chat,
|
|
|
|
from_user=TestChatJoinRequest.from_user,
|
|
|
|
date=time,
|
|
|
|
bio=TestChatJoinRequest.bio,
|
|
|
|
invite_link=TestChatJoinRequest.invite_link,
|
|
|
|
bot=bot,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class TestChatJoinRequest:
|
|
|
|
chat = Chat(1, Chat.SUPERGROUP)
|
2022-05-05 17:40:22 +02:00
|
|
|
from_user = User(2, "first_name", False)
|
|
|
|
bio = "bio"
|
2021-11-08 19:02:20 +01:00
|
|
|
invite_link = ChatInviteLink(
|
2022-05-05 17:40:22 +02:00
|
|
|
"https://invite.link",
|
|
|
|
User(42, "creator", False),
|
2021-08-29 18:17:06 +02:00
|
|
|
creates_join_request=False,
|
2022-05-05 17:40:22 +02:00
|
|
|
name="InviteLink",
|
2021-11-08 19:02:20 +01:00
|
|
|
is_revoked=False,
|
|
|
|
is_primary=False,
|
|
|
|
)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def test_slot_behaviour(self, chat_join_request, mro_slots):
|
2021-11-08 19:02:20 +01:00
|
|
|
inst = chat_join_request
|
|
|
|
for attr in inst.__slots__:
|
2022-05-05 17:40:22 +02:00
|
|
|
assert getattr(inst, attr, "err") != "err", f"got extra slot '{attr}'"
|
2021-11-08 19:02:20 +01:00
|
|
|
assert len(mro_slots(inst)) == len(set(mro_slots(inst))), "duplicate slot"
|
|
|
|
|
|
|
|
def test_de_json(self, bot, time):
|
|
|
|
json_dict = {
|
2022-05-05 17:40:22 +02:00
|
|
|
"chat": self.chat.to_dict(),
|
|
|
|
"from": self.from_user.to_dict(),
|
|
|
|
"date": to_timestamp(time),
|
2021-11-08 19:02:20 +01:00
|
|
|
}
|
|
|
|
chat_join_request = ChatJoinRequest.de_json(json_dict, bot)
|
|
|
|
|
|
|
|
assert chat_join_request.chat == self.chat
|
|
|
|
assert chat_join_request.from_user == self.from_user
|
2022-05-05 09:27:54 +02:00
|
|
|
assert abs(chat_join_request.date - time) < datetime.timedelta(seconds=1)
|
2021-11-08 19:02:20 +01:00
|
|
|
assert to_timestamp(chat_join_request.date) == to_timestamp(time)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
json_dict.update({"bio": self.bio, "invite_link": self.invite_link.to_dict()})
|
2021-11-08 19:02:20 +01:00
|
|
|
chat_join_request = ChatJoinRequest.de_json(json_dict, bot)
|
|
|
|
|
|
|
|
assert chat_join_request.chat == self.chat
|
|
|
|
assert chat_join_request.from_user == self.from_user
|
2022-05-05 09:27:54 +02:00
|
|
|
assert abs(chat_join_request.date - time) < datetime.timedelta(seconds=1)
|
2021-11-08 19:02:20 +01:00
|
|
|
assert to_timestamp(chat_join_request.date) == to_timestamp(time)
|
|
|
|
assert chat_join_request.bio == self.bio
|
|
|
|
assert chat_join_request.invite_link == self.invite_link
|
|
|
|
|
|
|
|
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)
|
2022-05-05 17:40:22 +02:00
|
|
|
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()
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
def test_equality(self, chat_join_request, time):
|
|
|
|
a = chat_join_request
|
|
|
|
b = ChatJoinRequest(self.chat, self.from_user, time)
|
2022-05-05 17:40:22 +02:00
|
|
|
c = ChatJoinRequest(self.chat, self.from_user, time, bio="bio")
|
2021-11-08 19:02:20 +01:00
|
|
|
d = ChatJoinRequest(self.chat, self.from_user, time + datetime.timedelta(1))
|
2022-05-05 17:40:22 +02:00
|
|
|
e = ChatJoinRequest(self.chat, User(-1, "last_name", True), time)
|
|
|
|
f = User(456, "", False)
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_approve(self, monkeypatch, chat_join_request):
|
|
|
|
async def make_assertion(*_, **kwargs):
|
2022-05-05 17:40:22 +02:00
|
|
|
chat_id_test = kwargs["chat_id"] == chat_join_request.chat.id
|
|
|
|
user_id_test = kwargs["user_id"] == chat_join_request.from_user.id
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
return chat_id_test and user_id_test
|
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
2022-05-05 17:40:22 +02:00
|
|
|
ChatJoinRequest.approve, Bot.approve_chat_join_request, ["chat_id", "user_id"], []
|
2021-11-08 19:02:20 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2022-05-05 17:40:22 +02:00
|
|
|
chat_join_request.approve, chat_join_request.get_bot(), "approve_chat_join_request"
|
2021-11-08 19:02:20 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
chat_join_request.approve, chat_join_request.get_bot()
|
|
|
|
)
|
2021-11-08 19:02:20 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(
|
2022-05-05 17:40:22 +02:00
|
|
|
chat_join_request.get_bot(), "approve_chat_join_request", make_assertion
|
2021-10-21 11:17:12 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await chat_join_request.approve()
|
2021-11-08 19:02:20 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def test_decline(self, monkeypatch, chat_join_request):
|
|
|
|
async def make_assertion(*_, **kwargs):
|
2022-05-05 17:40:22 +02:00
|
|
|
chat_id_test = kwargs["chat_id"] == chat_join_request.chat.id
|
|
|
|
user_id_test = kwargs["user_id"] == chat_join_request.from_user.id
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
return chat_id_test and user_id_test
|
|
|
|
|
|
|
|
assert check_shortcut_signature(
|
2022-05-05 17:40:22 +02:00
|
|
|
ChatJoinRequest.decline, Bot.decline_chat_join_request, ["chat_id", "user_id"], []
|
2021-11-08 19:02:20 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_shortcut_call(
|
2022-05-05 17:40:22 +02:00
|
|
|
chat_join_request.decline, chat_join_request.get_bot(), "decline_chat_join_request"
|
2021-11-08 19:02:20 +01:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await check_defaults_handling(
|
|
|
|
chat_join_request.decline, chat_join_request.get_bot()
|
|
|
|
)
|
2021-11-08 19:02:20 +01:00
|
|
|
|
2021-10-21 11:17:12 +02:00
|
|
|
monkeypatch.setattr(
|
2022-05-05 17:40:22 +02:00
|
|
|
chat_join_request.get_bot(), "decline_chat_join_request", make_assertion
|
2021-10-21 11:17:12 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
assert await chat_join_request.decline()
|