2017-08-11 23:58:41 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2023-01-01 21:31:29 +01:00
|
|
|
# Copyright (C) 2015-2023
|
2017-08-11 23:58:41 +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/].
|
2022-04-24 12:38:09 +02:00
|
|
|
import asyncio
|
2019-10-12 15:11:09 +02:00
|
|
|
import datetime
|
2017-08-11 23:58:41 +02:00
|
|
|
import os
|
2019-10-12 15:11:09 +02:00
|
|
|
import re
|
2022-05-05 09:27:54 +02:00
|
|
|
import sys
|
2021-10-13 08:12:48 +02:00
|
|
|
from pathlib import Path
|
2022-12-12 10:51:33 +01:00
|
|
|
from typing import Callable, Optional
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
import pytest
|
2022-04-24 12:38:09 +02:00
|
|
|
from httpx import AsyncClient, Response
|
2017-08-11 23:58:41 +02:00
|
|
|
|
2019-11-15 21:51:22 +01:00
|
|
|
from telegram import (
|
2022-05-05 09:27:54 +02:00
|
|
|
Bot,
|
2019-11-15 21:51:22 +01:00
|
|
|
CallbackQuery,
|
2022-05-05 09:27:54 +02:00
|
|
|
Chat,
|
2019-11-15 21:51:22 +01:00
|
|
|
ChosenInlineResult,
|
2022-05-05 09:27:54 +02:00
|
|
|
InlineQuery,
|
|
|
|
Message,
|
|
|
|
MessageEntity,
|
|
|
|
PreCheckoutQuery,
|
|
|
|
ShippingQuery,
|
|
|
|
Update,
|
|
|
|
User,
|
2019-11-15 21:51:22 +01:00
|
|
|
)
|
2022-12-12 10:51:33 +01:00
|
|
|
from telegram._utils.defaultvalue import DEFAULT_NONE
|
2022-04-24 12:38:09 +02:00
|
|
|
from telegram._utils.types import ODVInput
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram.error import BadRequest, RetryAfter, TimedOut
|
|
|
|
from telegram.ext import Application, ApplicationBuilder, Defaults, ExtBot, Updater
|
|
|
|
from telegram.ext.filters import MessageFilter, UpdateFilter
|
2022-04-24 12:38:09 +02:00
|
|
|
from telegram.request import RequestData
|
|
|
|
from telegram.request._httpxrequest import HTTPXRequest
|
2022-12-12 10:51:33 +01:00
|
|
|
from tests.auxil.object_conversions import env_var_2_bool
|
2017-08-11 23:58:41 +02:00
|
|
|
from tests.bots import get_bot
|
|
|
|
|
2021-02-13 22:07:37 +01:00
|
|
|
|
|
|
|
# This is here instead of in setup.cfg due to https://github.com/pytest-dev/pytest/issues/8343
|
|
|
|
def pytest_runtestloop(session):
|
|
|
|
session.add_marker(
|
2021-09-22 16:49:10 +02:00
|
|
|
pytest.mark.filterwarnings("ignore::telegram.warnings.PTBDeprecationWarning")
|
2021-02-13 22:07:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-10-27 14:28:33 +01:00
|
|
|
GITHUB_ACTION = os.getenv("GITHUB_ACTION", False)
|
|
|
|
|
|
|
|
if GITHUB_ACTION:
|
|
|
|
pytest_plugins = ["tests.plugin_github_group"]
|
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
# THIS KEY IS OBVIOUSLY COMPROMISED
|
|
|
|
# DO NOT USE IN PRODUCTION!
|
|
|
|
PRIVATE_KEY = b"-----BEGIN RSA PRIVATE KEY-----\r\nMIIEowIBAAKCAQEA0AvEbNaOnfIL3GjB8VI4M5IaWe+GcK8eSPHkLkXREIsaddum\r\nwPBm/+w8lFYdnY+O06OEJrsaDtwGdU//8cbGJ/H/9cJH3dh0tNbfszP7nTrQD+88\r\nydlcYHzClaG8G+oTe9uEZSVdDXj5IUqR0y6rDXXb9tC9l+oSz+ShYg6+C4grAb3E\r\nSTv5khZ9Zsi/JEPWStqNdpoNuRh7qEYc3t4B/a5BH7bsQENyJSc8AWrfv+drPAEe\r\njQ8xm1ygzWvJp8yZPwOIYuL+obtANcoVT2G2150Wy6qLC0bD88Bm40GqLbSazueC\r\nRHZRug0B9rMUKvKc4FhG4AlNzBCaKgIcCWEqKwIDAQABAoIBACcIjin9d3Sa3S7V\r\nWM32JyVF3DvTfN3XfU8iUzV7U+ZOswA53eeFM04A/Ly4C4ZsUNfUbg72O8Vd8rg/\r\n8j1ilfsYpHVvphwxaHQlfIMa1bKCPlc/A6C7b2GLBtccKTbzjARJA2YWxIaqk9Nz\r\nMjj1IJK98i80qt29xRnMQ5sqOO3gn2SxTErvNchtBiwOH8NirqERXig8VCY6fr3n\r\nz7ZImPU3G/4qpD0+9ULrt9x/VkjqVvNdK1l7CyAuve3D7ha3jPMfVHFtVH5gqbyp\r\nKotyIHAyD+Ex3FQ1JV+H7DkP0cPctQiss7OiO9Zd9C1G2OrfQz9el7ewAPqOmZtC\r\nKjB3hUECgYEA/4MfKa1cvaCqzd3yUprp1JhvssVkhM1HyucIxB5xmBcVLX2/Kdhn\r\nhiDApZXARK0O9IRpFF6QVeMEX7TzFwB6dfkyIePsGxputA5SPbtBlHOvjZa8omMl\r\nEYfNa8x/mJkvSEpzvkWPascuHJWv1cEypqphu/70DxubWB5UKo/8o6cCgYEA0HFy\r\ncgwPMB//nltHGrmaQZPFT7/Qgl9ErZT3G9S8teWY4o4CXnkdU75tBoKAaJnpSfX3\r\nq8VuRerF45AFhqCKhlG4l51oW7TUH50qE3GM+4ivaH5YZB3biwQ9Wqw+QyNLAh/Q\r\nnS4/Wwb8qC9QuyEgcCju5lsCaPEXZiZqtPVxZd0CgYEAshBG31yZjO0zG1TZUwfy\r\nfN3euc8mRgZpSdXIHiS5NSyg7Zr8ZcUSID8jAkJiQ3n3OiAsuq1MGQ6kNa582kLT\r\nFPQdI9Ea8ahyDbkNR0gAY9xbM2kg/Gnro1PorH9PTKE0ekSodKk1UUyNrg4DBAwn\r\nqE6E3ebHXt/2WmqIbUD653ECgYBQCC8EAQNX3AFegPd1GGxU33Lz4tchJ4kMCNU0\r\nN2NZh9VCr3nTYjdTbxsXU8YP44CCKFG2/zAO4kymyiaFAWEOn5P7irGF/JExrjt4\r\nibGy5lFLEq/HiPtBjhgsl1O0nXlwUFzd7OLghXc+8CPUJaz5w42unqT3PBJa40c3\r\nQcIPdQKBgBnSb7BcDAAQ/Qx9juo/RKpvhyeqlnp0GzPSQjvtWi9dQRIu9Pe7luHc\r\nm1Img1EO1OyE3dis/rLaDsAa2AKu1Yx6h85EmNjavBqP9wqmFa0NIQQH8fvzKY3/\r\nP8IHY6009aoamLqYaexvrkHVq7fFKiI6k8myMJ6qblVNFv14+KXU\r\n-----END RSA PRIVATE KEY-----" # noqa: E501
|
|
|
|
|
2022-10-31 10:12:18 +01:00
|
|
|
TEST_WITH_OPT_DEPS = env_var_2_bool(os.getenv("TEST_WITH_OPT_DEPS", True))
|
|
|
|
if TEST_WITH_OPT_DEPS:
|
|
|
|
import pytz
|
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Redefine the event_loop fixture to have a session scope. Otherwise `bot` fixture can't be
|
|
|
|
# session. See https://github.com/pytest-dev/pytest-asyncio/issues/68 for more details.
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def event_loop(request):
|
2022-05-05 09:27:54 +02:00
|
|
|
# ever since ProactorEventLoop became the default in Win 3.8+, the app crashes after the loop
|
|
|
|
# is closed. Hence, we use SelectorEventLoop on Windows to avoid this. See
|
|
|
|
# https://github.com/python/cpython/issues/83413, https://github.com/encode/httpx/issues/914
|
|
|
|
if sys.version_info[0] == 3 and sys.version_info[1] >= 8 and sys.platform.startswith("win"):
|
|
|
|
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
|
2022-04-24 12:38:09 +02:00
|
|
|
loop = asyncio.get_event_loop_policy().new_event_loop()
|
|
|
|
yield loop
|
|
|
|
# loop.close() # instead of closing here, do that at the every end of the test session
|
|
|
|
|
|
|
|
|
|
|
|
# Related to the above, see https://stackoverflow.com/a/67307042/10606962
|
|
|
|
def pytest_sessionfinish(session, exitstatus):
|
|
|
|
asyncio.get_event_loop().close()
|
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def bot_info():
|
|
|
|
return get_bot()
|
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Below classes are used to monkeypatch attributes since parent classes don't have __dict__
|
|
|
|
|
|
|
|
|
|
|
|
class TestHttpxRequest(HTTPXRequest):
|
|
|
|
async def _request_wrapper(
|
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
url: str,
|
|
|
|
request_data: RequestData = None,
|
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
) -> bytes:
|
|
|
|
try:
|
|
|
|
return await super()._request_wrapper(
|
|
|
|
method=method,
|
|
|
|
url=url,
|
|
|
|
request_data=request_data,
|
|
|
|
read_timeout=read_timeout,
|
|
|
|
write_timeout=write_timeout,
|
|
|
|
connect_timeout=connect_timeout,
|
|
|
|
pool_timeout=pool_timeout,
|
|
|
|
)
|
|
|
|
except RetryAfter as e:
|
|
|
|
pytest.xfail(f"Not waiting for flood control: {e}")
|
|
|
|
except TimedOut as e:
|
|
|
|
pytest.xfail(f"Ignoring TimedOut error: {e}")
|
2021-08-19 22:01:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DictExtBot(ExtBot):
|
2022-12-15 15:00:36 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
# Makes it easier to work with the bot in tests
|
|
|
|
self._unfreeze()
|
2021-08-19 22:01:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DictBot(Bot):
|
2022-12-15 15:00:36 +01:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
# Makes it easier to work with the bot in tests
|
|
|
|
self._unfreeze()
|
2021-08-19 22:01:10 +02:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
class DictApplication(Application):
|
2021-08-30 16:31:19 +02:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope="session")
|
2022-04-24 12:38:09 +02:00
|
|
|
async def bot(bot_info):
|
|
|
|
"""Makes an ExtBot instance with the given bot_info"""
|
|
|
|
async with make_bot(bot_info) as _bot:
|
|
|
|
yield _bot
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2022-10-07 10:18:08 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
async def cdc_bot(bot_info):
|
|
|
|
"""Makes an ExtBot instance with the given bot_info that uses arbitrary callback_data"""
|
|
|
|
async with make_bot(bot_info, arbitrary_callback_data=True) as _bot:
|
|
|
|
yield _bot
|
|
|
|
|
|
|
|
|
2021-10-03 15:10:13 +02:00
|
|
|
@pytest.fixture(scope="session")
|
2022-04-24 12:38:09 +02:00
|
|
|
async def raw_bot(bot_info):
|
|
|
|
"""Makes an regular Bot instance with the given bot_info"""
|
|
|
|
async with DictBot(
|
|
|
|
bot_info["token"],
|
|
|
|
private_key=PRIVATE_KEY,
|
|
|
|
request=TestHttpxRequest(8),
|
|
|
|
get_updates_request=TestHttpxRequest(1),
|
|
|
|
) as _bot:
|
|
|
|
yield _bot
|
2020-10-06 19:28:40 +02:00
|
|
|
|
|
|
|
|
2020-02-06 11:22:56 +01:00
|
|
|
@pytest.fixture(scope="function")
|
2022-04-24 12:38:09 +02:00
|
|
|
async def default_bot(request, bot_info):
|
2020-02-06 11:22:56 +01:00
|
|
|
param = request.param if hasattr(request, "param") else {}
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
default_bot = make_bot(bot_info, defaults=Defaults(**param))
|
|
|
|
async with default_bot:
|
|
|
|
yield default_bot
|
2020-02-06 11:22:56 +01:00
|
|
|
|
|
|
|
|
2020-09-27 12:59:48 +02:00
|
|
|
@pytest.fixture(scope="function")
|
2022-04-24 12:38:09 +02:00
|
|
|
async def tz_bot(timezone, bot_info):
|
|
|
|
default_bot = make_bot(bot_info, defaults=Defaults(tzinfo=timezone))
|
|
|
|
async with default_bot:
|
|
|
|
yield default_bot
|
2020-09-27 12:59:48 +02:00
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def chat_id(bot_info):
|
|
|
|
return bot_info["chat_id"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
2019-08-23 21:20:41 +02:00
|
|
|
def super_group_id(bot_info):
|
|
|
|
return bot_info["super_group_id"]
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
2022-11-22 10:43:50 +01:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def forum_group_id(bot_info):
|
|
|
|
return int(bot_info["forum_group_id"])
|
|
|
|
|
|
|
|
|
2017-08-11 23:58:41 +02:00
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def channel_id(bot_info):
|
|
|
|
return bot_info["channel_id"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def provider_token(bot_info):
|
|
|
|
return bot_info["payment_provider_token"]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
2022-04-24 12:38:09 +02:00
|
|
|
async def app(bot_info):
|
|
|
|
# We build a new bot each time so that we use `app` in a context manager without problems
|
|
|
|
application = (
|
|
|
|
ApplicationBuilder().bot(make_bot(bot_info)).application_class(DictApplication).build()
|
|
|
|
)
|
|
|
|
yield application
|
|
|
|
if application.running:
|
|
|
|
await application.stop()
|
|
|
|
await application.shutdown()
|
2017-08-11 23:58:41 +02:00
|
|
|
|
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
@pytest.fixture(scope="function")
|
2022-04-24 12:38:09 +02:00
|
|
|
async def updater(bot_info):
|
|
|
|
# We build a new bot each time so that we use `updater` in a context manager without problems
|
|
|
|
up = Updater(bot=make_bot(bot_info), update_queue=asyncio.Queue())
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
yield up
|
|
|
|
if up.running:
|
2022-04-24 12:38:09 +02:00
|
|
|
await up.stop()
|
|
|
|
await up.shutdown()
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
|
|
|
|
2021-10-13 08:12:48 +02:00
|
|
|
PROJECT_ROOT_PATH = Path(__file__).parent.parent.resolve()
|
|
|
|
TEST_DATA_PATH = Path(__file__).parent.resolve() / "data"
|
|
|
|
|
|
|
|
|
2022-11-24 12:11:37 +01:00
|
|
|
def data_file(filename: str) -> Path:
|
2021-10-13 08:12:48 +02:00
|
|
|
return TEST_DATA_PATH / filename
|
|
|
|
|
|
|
|
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
@pytest.fixture(scope="function")
|
|
|
|
def thumb_file():
|
2021-10-13 08:12:48 +02:00
|
|
|
f = data_file("thumb.jpg").open("rb")
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
yield f
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def class_thumb_file():
|
2021-10-13 08:12:48 +02:00
|
|
|
f = data_file("thumb.jpg").open("rb")
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
yield f
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
|
2022-06-27 18:46:52 +02:00
|
|
|
def make_bot(bot_info=None, **kwargs):
|
2021-06-06 11:48:48 +02:00
|
|
|
"""
|
|
|
|
Tests are executed on tg.ext.ExtBot, as that class only extends the functionality of tg.bot
|
|
|
|
"""
|
2022-06-27 18:46:52 +02:00
|
|
|
token = kwargs.pop("token", (bot_info or {}).get("token"))
|
|
|
|
private_key = kwargs.pop("private_key", PRIVATE_KEY)
|
|
|
|
kwargs.pop("token", None)
|
2022-04-24 12:38:09 +02:00
|
|
|
_bot = DictExtBot(
|
2022-06-27 18:46:52 +02:00
|
|
|
token=token,
|
2022-10-31 10:12:18 +01:00
|
|
|
private_key=private_key if TEST_WITH_OPT_DEPS else None,
|
2022-04-24 12:38:09 +02:00
|
|
|
request=TestHttpxRequest(8),
|
|
|
|
get_updates_request=TestHttpxRequest(1),
|
|
|
|
**kwargs,
|
|
|
|
)
|
|
|
|
return _bot
|
2019-10-12 15:11:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
CMD_PATTERN = re.compile(r"/[\da-z_]{1,32}(?:@\w{1,32})?")
|
|
|
|
DATE = datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
|
|
def make_message(text, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create a fake ``telegram.Message`` with
|
|
|
|
reasonable defaults for mimicking a real message.
|
|
|
|
:param text: (str) message text
|
|
|
|
:return: a (fake) ``telegram.Message``
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
bot = kwargs.pop("bot", None)
|
|
|
|
if bot is None:
|
|
|
|
bot = make_bot(get_bot())
|
2022-10-07 11:51:53 +02:00
|
|
|
message = Message(
|
2019-10-12 15:11:09 +02:00
|
|
|
message_id=1,
|
|
|
|
from_user=kwargs.pop("user", User(id=1, first_name="", is_bot=False)),
|
|
|
|
date=kwargs.pop("date", DATE),
|
|
|
|
chat=kwargs.pop("chat", Chat(id=1, type="")),
|
|
|
|
text=text,
|
|
|
|
**kwargs,
|
|
|
|
)
|
2022-10-07 11:51:53 +02:00
|
|
|
message.set_bot(bot)
|
|
|
|
return message
|
2019-10-12 15:11:09 +02:00
|
|
|
|
|
|
|
|
|
|
|
def make_command_message(text, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create a message containing a single telegram
|
|
|
|
command.
|
|
|
|
Mimics the Telegram API in that it identifies commands within the message
|
|
|
|
and tags the returned ``Message`` object with the appropriate ``MessageEntity``
|
|
|
|
tag (but it does this only for commands).
|
|
|
|
|
|
|
|
:param text: (str) message text containing (or not) the command
|
|
|
|
:return: a (fake) ``telegram.Message`` containing only the command
|
|
|
|
"""
|
|
|
|
|
|
|
|
match = re.search(CMD_PATTERN, text)
|
|
|
|
entities = (
|
|
|
|
[
|
|
|
|
MessageEntity(
|
|
|
|
type=MessageEntity.BOT_COMMAND, offset=match.start(0), length=len(match.group(0))
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-10-12 15:11:09 +02:00
|
|
|
]
|
|
|
|
if match
|
|
|
|
else []
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-10-12 15:11:09 +02:00
|
|
|
|
|
|
|
return make_message(text, entities=entities, **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def make_message_update(message, message_factory=make_message, edited=False, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create an update from a message, as either a
|
|
|
|
``telegram.Message`` or a string. In the latter case ``message_factory``
|
|
|
|
is used to convert ``message`` to a ``telegram.Message``.
|
|
|
|
:param message: either a ``telegram.Message`` or a string with the message text
|
|
|
|
:param message_factory: function to convert the message text into a ``telegram.Message``
|
|
|
|
:param edited: whether the message should be stored as ``edited_message`` (vs. ``message``)
|
|
|
|
:return: ``telegram.Update`` with the given message
|
|
|
|
"""
|
|
|
|
if not isinstance(message, Message):
|
|
|
|
message = message_factory(message, **kwargs)
|
|
|
|
update_kwargs = {"message" if not edited else "edited_message": message}
|
|
|
|
return Update(0, **update_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
def make_command_update(message, edited=False, **kwargs):
|
|
|
|
"""
|
|
|
|
Testing utility factory to create an update from a message that potentially
|
|
|
|
contains a command. See ``make_command_message`` for more details.
|
|
|
|
:param message: message potentially containing a command
|
|
|
|
:param edited: whether the message should be stored as ``edited_message`` (vs. ``message``)
|
|
|
|
:return: ``telegram.Update`` with the given message
|
|
|
|
"""
|
|
|
|
return make_message_update(message, make_command_message, edited, **kwargs)
|
|
|
|
|
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
@pytest.fixture(
|
|
|
|
scope="class",
|
|
|
|
params=[{"class": MessageFilter}, {"class": UpdateFilter}],
|
|
|
|
ids=["MessageFilter", "UpdateFilter"],
|
|
|
|
)
|
|
|
|
def mock_filter(request):
|
|
|
|
class MockFilter(request.param["class"]):
|
2019-10-12 15:11:09 +02:00
|
|
|
def __init__(self):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__()
|
2019-10-12 15:11:09 +02:00
|
|
|
self.tested = False
|
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
def filter(self, _):
|
2019-10-12 15:11:09 +02:00
|
|
|
self.tested = True
|
|
|
|
|
|
|
|
return MockFilter()
|
|
|
|
|
|
|
|
|
|
|
|
def get_false_update_fixture_decorator_params():
|
2020-10-06 19:28:40 +02:00
|
|
|
message = Message(1, DATE, Chat(1, ""), from_user=User(1, "", False), text="test")
|
2019-10-12 15:11:09 +02:00
|
|
|
params = [
|
|
|
|
{"callback_query": CallbackQuery(1, User(1, "", False), "chat", message=message)},
|
|
|
|
{"channel_post": message},
|
|
|
|
{"edited_channel_post": message},
|
|
|
|
{"inline_query": InlineQuery(1, User(1, "", False), "", "")},
|
|
|
|
{"chosen_inline_result": ChosenInlineResult("id", User(1, "", False), "")},
|
|
|
|
{"shipping_query": ShippingQuery("id", User(1, "", False), "", None)},
|
|
|
|
{"pre_checkout_query": PreCheckoutQuery("id", User(1, "", False), "", 0, "")},
|
|
|
|
{"callback_query": CallbackQuery(1, User(1, "", False), "chat")},
|
|
|
|
]
|
|
|
|
ids = tuple(key for kwargs in params for key in kwargs)
|
|
|
|
return {"params": params, "ids": ids}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(scope="function", **get_false_update_fixture_decorator_params())
|
|
|
|
def false_update(request):
|
|
|
|
return Update(update_id=1, **request.param)
|
2019-11-15 21:51:22 +01:00
|
|
|
|
|
|
|
|
2022-10-31 10:12:18 +01:00
|
|
|
class BasicTimezone(datetime.tzinfo):
|
|
|
|
def __init__(self, offset, name):
|
|
|
|
self.offset = offset
|
|
|
|
self.name = name
|
|
|
|
|
|
|
|
def utcoffset(self, dt):
|
|
|
|
return self.offset
|
|
|
|
|
|
|
|
def dst(self, dt):
|
|
|
|
return datetime.timedelta(0)
|
|
|
|
|
|
|
|
|
2020-07-10 13:11:28 +02:00
|
|
|
@pytest.fixture(params=["Europe/Berlin", "Asia/Singapore", "UTC"])
|
|
|
|
def tzinfo(request):
|
2022-10-31 10:12:18 +01:00
|
|
|
if TEST_WITH_OPT_DEPS:
|
|
|
|
return pytz.timezone(request.param)
|
|
|
|
else:
|
|
|
|
hours_offset = {"Europe/Berlin": 2, "Asia/Singapore": 8, "UTC": 0}[request.param]
|
|
|
|
return BasicTimezone(offset=datetime.timedelta(hours=hours_offset), name=request.param)
|
2019-11-15 21:51:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture()
|
2020-07-10 13:11:28 +02:00
|
|
|
def timezone(tzinfo):
|
|
|
|
return tzinfo
|
2020-05-01 13:27:46 +02:00
|
|
|
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
@pytest.fixture()
|
|
|
|
def mro_slots():
|
2022-04-24 12:38:09 +02:00
|
|
|
def _mro_slots(_class, only_parents: bool = False):
|
|
|
|
if only_parents:
|
|
|
|
classes = _class.__class__.__mro__[1:-1]
|
|
|
|
else:
|
|
|
|
classes = _class.__class__.__mro__[:-1]
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
return [
|
|
|
|
attr
|
2022-04-24 12:38:09 +02:00
|
|
|
for cls in classes
|
2021-08-19 22:01:10 +02:00
|
|
|
if hasattr(cls, "__slots__") # The Exception class doesn't have slots
|
2021-05-29 16:18:16 +02:00
|
|
|
for attr in cls.__slots__
|
2021-08-19 22:01:10 +02:00
|
|
|
if attr != "__dict__" # left here for classes which still has __dict__
|
2021-05-29 16:18:16 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
return _mro_slots
|
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
def call_after(function: Callable, after: Callable):
|
|
|
|
"""Run a callable after another has executed. Useful when trying to make sure that a function
|
|
|
|
did actually run, but just monkeypatching it doesn't work because this would break some other
|
|
|
|
functionality.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
|
|
|
def test_stuff(self, bot, monkeypatch):
|
|
|
|
|
|
|
|
def after(arg):
|
|
|
|
# arg is the return value of `send_message`
|
|
|
|
self.received = arg
|
|
|
|
|
|
|
|
monkeypatch.setattr(bot, 'send_message', call_after(bot.send_message, after)
|
|
|
|
|
|
|
|
"""
|
|
|
|
if asyncio.iscoroutinefunction(function):
|
|
|
|
|
|
|
|
async def wrapped(*args, **kwargs):
|
|
|
|
out = await function(*args, **kwargs)
|
2022-05-05 09:27:54 +02:00
|
|
|
if asyncio.iscoroutinefunction(after):
|
|
|
|
await after(out)
|
|
|
|
else:
|
|
|
|
after(out)
|
2022-04-24 12:38:09 +02:00
|
|
|
return out
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
def wrapped(*args, **kwargs):
|
|
|
|
out = function(*args, **kwargs)
|
|
|
|
after(out)
|
|
|
|
return out
|
|
|
|
|
|
|
|
return wrapped
|
|
|
|
|
|
|
|
|
|
|
|
async def expect_bad_request(func, message, reason):
|
2020-05-01 13:27:46 +02:00
|
|
|
"""
|
|
|
|
Wrapper for testing bot functions expected to result in an :class:`telegram.error.BadRequest`.
|
|
|
|
Makes it XFAIL, if the specified error message is present.
|
|
|
|
|
|
|
|
Args:
|
2022-04-24 12:38:09 +02:00
|
|
|
func: The awaitable to be executed.
|
2020-05-01 13:27:46 +02:00
|
|
|
message: The expected message of the bad request error. If another message is present,
|
|
|
|
the error will be reraised.
|
|
|
|
reason: Explanation for the XFAIL.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
On success, returns the return value of :attr:`func`
|
|
|
|
"""
|
|
|
|
try:
|
2022-04-24 12:38:09 +02:00
|
|
|
return await func()
|
2020-05-01 13:27:46 +02:00
|
|
|
except BadRequest as e:
|
|
|
|
if message in str(e):
|
2020-11-23 22:09:29 +01:00
|
|
|
pytest.xfail(f"{reason}. {e}")
|
2020-05-01 13:27:46 +02:00
|
|
|
else:
|
|
|
|
raise e
|
2020-12-30 13:41:07 +01:00
|
|
|
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def send_webhook_message(
|
|
|
|
ip: str,
|
|
|
|
port: int,
|
|
|
|
payload_str: Optional[str],
|
|
|
|
url_path: str = "",
|
|
|
|
content_len: int = -1,
|
|
|
|
content_type: str = "application/json",
|
|
|
|
get_method: str = None,
|
2022-06-27 18:54:11 +02:00
|
|
|
secret_token: str = None,
|
2022-04-24 12:38:09 +02:00
|
|
|
) -> Response:
|
|
|
|
headers = {
|
|
|
|
"content-type": content_type,
|
|
|
|
}
|
2022-06-27 18:54:11 +02:00
|
|
|
if secret_token:
|
|
|
|
headers["X-Telegram-Bot-Api-Secret-Token"] = secret_token
|
2022-04-24 12:38:09 +02:00
|
|
|
|
|
|
|
if not payload_str:
|
|
|
|
content_len = None
|
|
|
|
payload = None
|
|
|
|
else:
|
|
|
|
payload = bytes(payload_str, encoding="utf-8")
|
|
|
|
|
|
|
|
if content_len == -1:
|
|
|
|
content_len = len(payload)
|
|
|
|
|
|
|
|
if content_len is not None:
|
|
|
|
headers["content-length"] = str(content_len)
|
|
|
|
|
|
|
|
url = f"http://{ip}:{port}/{url_path}"
|
|
|
|
|
|
|
|
async with AsyncClient() as client:
|
|
|
|
return await client.request(
|
|
|
|
url=url, method=get_method or "POST", data=payload, headers=headers
|
|
|
|
)
|