2023-02-22 20:19:46 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2023-02-22 20:19:46 +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/].
|
2023-12-14 21:37:00 +01:00
|
|
|
from pathlib import Path
|
2024-10-24 20:48:49 +02:00
|
|
|
from typing import Optional
|
2023-02-22 20:19:46 +01:00
|
|
|
|
|
|
|
import pytest
|
2023-12-14 21:37:00 +01:00
|
|
|
from httpx import AsyncClient, AsyncHTTPTransport, Response
|
2023-02-22 20:19:46 +01:00
|
|
|
|
|
|
|
from telegram._utils.defaultvalue import DEFAULT_NONE
|
2024-07-21 21:13:30 +02:00
|
|
|
from telegram._utils.strings import TextEncoding
|
2023-02-22 20:19:46 +01:00
|
|
|
from telegram._utils.types import ODVInput
|
|
|
|
from telegram.error import BadRequest, RetryAfter, TimedOut
|
2024-09-09 07:32:32 +02:00
|
|
|
from telegram.request import BaseRequest, HTTPXRequest, RequestData
|
2023-02-22 20:19:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
class NonchalantHttpxRequest(HTTPXRequest):
|
|
|
|
"""This Request class is used in the tests to suppress errors that we don't care about
|
|
|
|
in the test suite.
|
|
|
|
"""
|
|
|
|
|
|
|
|
async def _request_wrapper(
|
|
|
|
self,
|
|
|
|
method: str,
|
|
|
|
url: str,
|
2023-05-18 07:57:59 +02:00
|
|
|
request_data: Optional[RequestData] = None,
|
2023-02-22 20:19:46 +01:00
|
|
|
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}")
|
|
|
|
|
|
|
|
|
2024-09-09 07:32:32 +02:00
|
|
|
class OfflineRequest(BaseRequest):
|
|
|
|
"""This Request class disallows making requests to Telegram's servers.
|
|
|
|
Use this in tests that should not depend on the network.
|
|
|
|
"""
|
|
|
|
|
|
|
|
async def initialize(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def shutdown(self) -> None:
|
|
|
|
pass
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
pass
|
|
|
|
|
|
|
|
async def do_request(
|
|
|
|
self,
|
|
|
|
url: str,
|
|
|
|
method: str,
|
|
|
|
request_data: Optional[RequestData] = None,
|
|
|
|
read_timeout: ODVInput[float] = BaseRequest.DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = BaseRequest.DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = BaseRequest.DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = BaseRequest.DEFAULT_NONE,
|
2024-10-24 20:48:49 +02:00
|
|
|
) -> tuple[int, bytes]:
|
2024-09-09 07:32:32 +02:00
|
|
|
pytest.fail("OfflineRequest: Network access disallowed in this test")
|
|
|
|
|
|
|
|
|
2023-02-22 20:19:46 +01:00
|
|
|
async def expect_bad_request(func, message, reason):
|
|
|
|
"""
|
|
|
|
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:
|
|
|
|
func: The awaitable to be executed.
|
|
|
|
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:
|
|
|
|
return await func()
|
|
|
|
except BadRequest as e:
|
|
|
|
if message in str(e):
|
|
|
|
pytest.xfail(f"{reason}. {e}")
|
|
|
|
else:
|
|
|
|
raise e
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
2023-05-18 07:57:59 +02:00
|
|
|
get_method: Optional[str] = None,
|
|
|
|
secret_token: Optional[str] = None,
|
2023-12-14 21:37:00 +01:00
|
|
|
unix: Optional[Path] = None,
|
2023-02-22 20:19:46 +01:00
|
|
|
) -> Response:
|
|
|
|
headers = {
|
|
|
|
"content-type": content_type,
|
|
|
|
}
|
|
|
|
if secret_token:
|
|
|
|
headers["X-Telegram-Bot-Api-Secret-Token"] = secret_token
|
|
|
|
|
|
|
|
if not payload_str:
|
|
|
|
content_len = None
|
|
|
|
payload = None
|
|
|
|
else:
|
2024-07-21 21:13:30 +02:00
|
|
|
payload = bytes(payload_str, encoding=TextEncoding.UTF_8)
|
2023-02-22 20:19:46 +01:00
|
|
|
|
|
|
|
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}"
|
|
|
|
|
2023-12-14 21:37:00 +01:00
|
|
|
transport = AsyncHTTPTransport(uds=unix) if unix else None
|
|
|
|
|
|
|
|
async with AsyncClient(transport=transport) as client:
|
2023-02-22 20:19:46 +01:00
|
|
|
return await client.request(
|
|
|
|
url=url, method=get_method or "POST", data=payload, headers=headers
|
|
|
|
)
|