mirror of
https://github.com/Balshgit/gpt_chat_bot.git
synced 2025-12-16 21:20:39 +03:00
microservices are able to run (#5)
This commit is contained in:
0
bot_microservice/tests/__init__.py
Normal file
0
bot_microservice/tests/__init__.py
Normal file
0
bot_microservice/tests/integration/__init__.py
Normal file
0
bot_microservice/tests/integration/__init__.py
Normal file
0
bot_microservice/tests/integration/bot/__init__.py
Normal file
0
bot_microservice/tests/integration/bot/__init__.py
Normal file
251
bot_microservice/tests/integration/bot/conftest.py
Normal file
251
bot_microservice/tests/integration/bot/conftest.py
Normal file
@@ -0,0 +1,251 @@
|
||||
"""This module contains subclasses of classes from the python-telegram-bot library that
|
||||
modify behavior of the respective parent classes in order to make them easier to use in the
|
||||
pytest framework. A common change is to allow monkeypatching of the class members by not
|
||||
enforcing slots in the subclasses."""
|
||||
import asyncio
|
||||
from asyncio import AbstractEventLoop
|
||||
from datetime import tzinfo
|
||||
from typing import Any, AsyncGenerator
|
||||
|
||||
import pytest
|
||||
import pytest_asyncio
|
||||
from core.bot import BotApplication
|
||||
from core.handlers import command_handlers
|
||||
from fastapi import FastAPI
|
||||
from httpx import AsyncClient
|
||||
from main import Application as AppApplication
|
||||
from pytest_asyncio.plugin import SubRequest
|
||||
from settings.config import AppSettings, get_settings
|
||||
from telegram import Bot, User
|
||||
from telegram.ext import Application, ApplicationBuilder, Defaults, ExtBot
|
||||
from tests.integration.bot.networking import NonchalantHttpxRequest
|
||||
from tests.integration.factories.bot import BotInfoFactory
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def test_settings() -> AppSettings:
|
||||
return get_settings()
|
||||
|
||||
|
||||
class PytestExtBot(ExtBot): # type: ignore
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
# Makes it easier to work with the bot in tests
|
||||
self._unfreeze()
|
||||
|
||||
# Here we override get_me for caching because we don't want to call the API repeatedly in tests
|
||||
async def get_me(self, *args: Any, **kwargs: Any) -> User:
|
||||
return await _mocked_get_me(self)
|
||||
|
||||
|
||||
class PytestBot(Bot):
|
||||
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
||||
super().__init__(*args, **kwargs)
|
||||
# Makes it easier to work with the bot in tests
|
||||
self._unfreeze()
|
||||
|
||||
# Here we override get_me for caching because we don't want to call the API repeatedly in tests
|
||||
async def get_me(self, *args: Any, **kwargs: Any) -> User:
|
||||
return await _mocked_get_me(self)
|
||||
|
||||
|
||||
class PytestApplication(Application): # type: ignore
|
||||
pass
|
||||
|
||||
|
||||
def make_bot(bot_info: dict[str, Any] | None = None, **kwargs: Any) -> PytestExtBot:
|
||||
"""
|
||||
Tests are executed on tg.ext.ExtBot, as that class only extends the functionality of tg.bot
|
||||
"""
|
||||
token = kwargs.pop("token", (bot_info or {}).get("token"))
|
||||
kwargs.pop("token", None)
|
||||
return PytestExtBot(
|
||||
token=token,
|
||||
private_key=None,
|
||||
request=NonchalantHttpxRequest(connection_pool_size=8),
|
||||
get_updates_request=NonchalantHttpxRequest(connection_pool_size=1),
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
async def _mocked_get_me(bot: Bot) -> User:
|
||||
if bot._bot_user is None:
|
||||
bot._bot_user = _get_bot_user(bot.token)
|
||||
return bot._bot_user
|
||||
|
||||
|
||||
def _get_bot_user(token: str) -> User:
|
||||
"""Used to return a mock user in bot.get_me(). This saves API calls on every init."""
|
||||
bot_info = BotInfoFactory()
|
||||
# We don't take token from bot_info, because we need to make a bot with a specific ID. So we
|
||||
# generate the correct user_id from the token (token from bot_info is random each test run).
|
||||
# This is important in e.g. bot equality tests. The other parameters like first_name don't
|
||||
# matter as much. In the future we may provide a way to get all the correct info from the token
|
||||
user_id = int(token.split(":")[0])
|
||||
first_name = bot_info.get(
|
||||
"name",
|
||||
)
|
||||
username = bot_info.get(
|
||||
"username",
|
||||
).strip("@")
|
||||
return User(
|
||||
user_id,
|
||||
first_name,
|
||||
is_bot=True,
|
||||
username=username,
|
||||
can_join_groups=True,
|
||||
can_read_all_group_messages=False,
|
||||
supports_inline_queries=True,
|
||||
)
|
||||
|
||||
|
||||
# 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: SubRequest) -> AbstractEventLoop:
|
||||
"""
|
||||
Пересоздаем луп для изоляции тестов. В основном нужно для запуска юнит тестов
|
||||
в связке с интеграционными, т.к. без этого pytest зависает.
|
||||
Для интеграционных тестов фикстура определяется дополнительная фикстура на всю сессию.
|
||||
"""
|
||||
loop = asyncio.get_event_loop_policy().new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
return loop
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def bot_info() -> dict[str, Any]:
|
||||
return BotInfoFactory()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def bot_application(bot_info: dict[str, Any]) -> AsyncGenerator[Any, None]:
|
||||
# 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(PytestApplication).build()
|
||||
yield application
|
||||
if application.running:
|
||||
await application.stop()
|
||||
await application.shutdown()
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def bot(bot_info: dict[str, Any], bot_application: Any) -> AsyncGenerator[PytestExtBot, None]:
|
||||
"""Makes an ExtBot instance with the given bot_info"""
|
||||
async with make_bot(bot_info) as _bot:
|
||||
_bot.application = bot_application
|
||||
yield _bot
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def one_time_bot(bot_info: dict[str, Any], bot_application: Any) -> PytestExtBot:
|
||||
"""A function scoped bot since the session bot would shutdown when `async with app` finishes"""
|
||||
bot = make_bot(bot_info)
|
||||
bot.application = bot_application
|
||||
return bot
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def cdc_bot(bot_info: dict[str, Any], bot_application: Any) -> AsyncGenerator[PytestExtBot, None]:
|
||||
"""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:
|
||||
_bot.application = bot_application
|
||||
yield _bot
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def raw_bot(bot_info: dict[str, Any], bot_application: Any) -> AsyncGenerator[PytestBot, None]:
|
||||
"""Makes an regular Bot instance with the given bot_info"""
|
||||
async with PytestBot(
|
||||
bot_info["token"],
|
||||
private_key=None,
|
||||
request=NonchalantHttpxRequest(8),
|
||||
get_updates_request=NonchalantHttpxRequest(1),
|
||||
) as _bot:
|
||||
_bot.application = bot_application
|
||||
yield _bot
|
||||
|
||||
|
||||
# Here we store the default bots so that we don't have to create them again and again.
|
||||
# They are initialized but not shutdown on pytest_sessionfinish because it is causing
|
||||
# problems with the event loop (Event loop is closed).
|
||||
_default_bots: dict[Defaults, PytestExtBot] = {}
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def default_bot(request: SubRequest, bot_info: dict[str, Any]) -> PytestExtBot:
|
||||
param = request.param if hasattr(request, "param") else {}
|
||||
defaults = Defaults(**param)
|
||||
|
||||
# If the bot is already created, return it. Else make a new one.
|
||||
default_bot = _default_bots.get(defaults)
|
||||
if default_bot is None:
|
||||
default_bot = make_bot(bot_info, defaults=defaults)
|
||||
await default_bot.initialize()
|
||||
_default_bots[defaults] = default_bot # Defaults object is hashable
|
||||
return default_bot
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def tz_bot(timezone: tzinfo, bot_info: dict[str, Any]) -> PytestExtBot:
|
||||
defaults = Defaults(tzinfo=timezone)
|
||||
try: # If the bot is already created, return it. Saves time since get_me is not called again.
|
||||
return _default_bots[defaults]
|
||||
except KeyError:
|
||||
default_bot = make_bot(bot_info, defaults=defaults)
|
||||
await default_bot.initialize()
|
||||
_default_bots[defaults] = default_bot
|
||||
return default_bot
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def chat_id(bot_info: dict[str, Any]) -> int:
|
||||
return bot_info["chat_id"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def super_group_id(bot_info: dict[str, Any]) -> int:
|
||||
return bot_info["super_group_id"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def forum_group_id(bot_info: dict[str, Any]) -> int:
|
||||
return int(bot_info["forum_group_id"])
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def channel_id(bot_info: dict[str, Any]) -> int:
|
||||
return bot_info["channel_id"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def provider_token(bot_info: dict[str, Any]) -> str:
|
||||
return bot_info["payment_provider_token"]
|
||||
|
||||
|
||||
@pytest_asyncio.fixture(scope="session")
|
||||
async def main_application(
|
||||
bot_application: PytestApplication, test_settings: AppSettings
|
||||
) -> AsyncGenerator[FastAPI, None]:
|
||||
bot_app = BotApplication(
|
||||
application=bot_application,
|
||||
settings=test_settings,
|
||||
handlers=command_handlers.handlers,
|
||||
)
|
||||
fast_api_app = AppApplication(settings=test_settings, bot_app=bot_app).fastapi_app
|
||||
yield fast_api_app
|
||||
|
||||
|
||||
@pytest_asyncio.fixture()
|
||||
async def rest_client(
|
||||
main_application: FastAPI,
|
||||
) -> AsyncGenerator[AsyncClient, None]:
|
||||
"""
|
||||
Default http client. Use to test unauthorized requests, public endpoints
|
||||
or special authorization methods.
|
||||
"""
|
||||
async with AsyncClient(
|
||||
app=main_application,
|
||||
base_url="http://test",
|
||||
headers={"Content-Type": "application/json"},
|
||||
) as client:
|
||||
yield client
|
||||
109
bot_microservice/tests/integration/bot/networking.py
Normal file
109
bot_microservice/tests/integration/bot/networking.py
Normal file
@@ -0,0 +1,109 @@
|
||||
from typing import Any, Callable, Optional
|
||||
|
||||
import pytest
|
||||
from httpx import AsyncClient, Response
|
||||
from telegram._utils.defaultvalue import DEFAULT_NONE
|
||||
from telegram._utils.types import ODVInput
|
||||
from telegram.error import BadRequest, RetryAfter, TimedOut
|
||||
from telegram.request import HTTPXRequest, RequestData
|
||||
|
||||
|
||||
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,
|
||||
url: str,
|
||||
method: str,
|
||||
request_data: Optional[RequestData] = None,
|
||||
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
||||
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
||||
connect_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}")
|
||||
|
||||
|
||||
async def expect_bad_request(func: Callable[..., Any], message: str, reason: str) -> Callable[..., Any]:
|
||||
"""
|
||||
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: str | None,
|
||||
url_path: str = "",
|
||||
content_len: int | None = -1,
|
||||
content_type: str = "application/json",
|
||||
get_method: str | None = None,
|
||||
secret_token: str | None = None,
|
||||
) -> 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:
|
||||
payload = bytes(payload_str, encoding="utf-8")
|
||||
|
||||
if content_len == -1:
|
||||
content_len = len(payload) if payload else None
|
||||
|
||||
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, # type: ignore
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
class MockedRequest:
|
||||
def __init__(self, data: dict[str, Any]) -> None:
|
||||
self.data = data
|
||||
|
||||
async def json(self) -> dict[str, Any]:
|
||||
return self.data
|
||||
71
bot_microservice/tests/integration/bot/test_bot_updates.py
Normal file
71
bot_microservice/tests/integration/bot/test_bot_updates.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import asyncio
|
||||
import time
|
||||
from asyncio import AbstractEventLoop
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
from assertpy import assert_that
|
||||
from core.bot import BotApplication, BotQueue
|
||||
from faker import Faker
|
||||
from httpx import AsyncClient
|
||||
from main import Application
|
||||
from tests.integration.bot.networking import MockedRequest
|
||||
from tests.integration.factories.bot import (
|
||||
BotChatFactory,
|
||||
BotEntitleFactory,
|
||||
BotUserFactory,
|
||||
)
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.asyncio,
|
||||
]
|
||||
|
||||
|
||||
faker = Faker()
|
||||
|
||||
|
||||
async def test_bot_updates(rest_client: AsyncClient) -> None:
|
||||
response = await rest_client.get("/api/healthcheck")
|
||||
assert response.status_code == 200
|
||||
|
||||
|
||||
async def test_bot_webhook_endpoint(
|
||||
rest_client: AsyncClient,
|
||||
main_application: Application,
|
||||
) -> None:
|
||||
bot_update = create_bot_update()
|
||||
response = await rest_client.post(url="/api/123456789:AABBCCDDEEFFaabbccddeeff-1234567890", json=bot_update)
|
||||
assert response.status_code == 202
|
||||
update = await main_application.state._state["queue"].queue.get() # type: ignore[attr-defined]
|
||||
update = update.to_dict()
|
||||
assert update["update_id"] == bot_update["update_id"]
|
||||
assert_that(update["message"]).is_equal_to(
|
||||
bot_update["message"], include=["from", "entities", "message_id", "text"]
|
||||
)
|
||||
|
||||
|
||||
async def test_bot_queue(
|
||||
bot: BotApplication,
|
||||
event_loop: AbstractEventLoop,
|
||||
) -> None:
|
||||
bot_queue = BotQueue(bot_app=bot)
|
||||
event_loop.create_task(bot_queue.get_updates_from_queue())
|
||||
bot_update = create_bot_update()
|
||||
mocked_request = MockedRequest(bot_update)
|
||||
await bot_queue.put_updates_on_queue(mocked_request) # type: ignore
|
||||
await asyncio.sleep(1)
|
||||
assert bot_queue.queue.empty()
|
||||
|
||||
|
||||
def create_bot_update() -> dict[str, Any]:
|
||||
bot_update: dict[str, Any] = {}
|
||||
bot_update["update_id"] = faker.random_int(min=10**8, max=10**9 - 1)
|
||||
bot_update["message"] = {
|
||||
"message_id": faker.random_int(min=10**8, max=10**9 - 1),
|
||||
"from": BotUserFactory()._asdict(),
|
||||
"chat": BotChatFactory()._asdict(),
|
||||
"date": time.time(),
|
||||
"text": "/chatid",
|
||||
"entities": [BotEntitleFactory()],
|
||||
}
|
||||
return bot_update
|
||||
56
bot_microservice/tests/integration/factories/bot.py
Normal file
56
bot_microservice/tests/integration/factories/bot.py
Normal file
@@ -0,0 +1,56 @@
|
||||
import string
|
||||
|
||||
import factory
|
||||
from faker import Faker
|
||||
from tests.integration.factories.models import Chat, User
|
||||
|
||||
faker = Faker("ru_RU")
|
||||
|
||||
|
||||
class BotUserFactory(factory.Factory):
|
||||
id = factory.Sequence(lambda n: 1000 + n)
|
||||
is_bot = False
|
||||
first_name = factory.Faker("first_name")
|
||||
last_name = factory.Faker("last_name")
|
||||
username = faker.profile(fields=["username"])["username"]
|
||||
language_code = "ru"
|
||||
|
||||
class Meta:
|
||||
model = User
|
||||
|
||||
|
||||
class BotChatFactory(factory.Factory):
|
||||
id = factory.Sequence(lambda n: 1 + n)
|
||||
first_name = factory.Faker("first_name")
|
||||
last_name = factory.Faker("last_name")
|
||||
username = faker.profile(fields=["username"])["username"]
|
||||
type = "private"
|
||||
|
||||
class Meta:
|
||||
model = Chat
|
||||
|
||||
|
||||
class BotInfoFactory(factory.DictFactory):
|
||||
token = factory.Faker(
|
||||
"bothify", text="#########:??????????????????????????-#????????#?", letters=string.ascii_letters
|
||||
) # example: 579694714:AAFpK8w6zkkUrD4xSeYwF3MO8e-4Grmcy7c
|
||||
payment_provider_token = factory.Faker(
|
||||
"bothify", text="#########:TEST:????????????????", letters=string.ascii_letters
|
||||
) # example: 579694714:TEST:K8w6zkkUrD4xSeYw
|
||||
chat_id = factory.Faker("random_int", min=10**8, max=10**9 - 1)
|
||||
super_group_id = factory.Faker("random_int", min=-(10**12) - 10**9, max=-(10**12)) # -1001838004577
|
||||
forum_group_id = factory.Faker("random_int", min=-(10**12) - 10**9, max=-(10**12))
|
||||
channel_name = factory.Faker("name")
|
||||
channel_id = factory.LazyAttribute(lambda obj: f"@{obj.channel_name}")
|
||||
name = factory.Faker("name")
|
||||
fake_username = factory.Faker("name")
|
||||
username = factory.LazyAttribute(lambda obj: "_".join(f"@{obj.fake_username}".split(" "))) # @Peter_Parker
|
||||
|
||||
class Meta:
|
||||
exclude = ("channel_name", "fake_username")
|
||||
|
||||
|
||||
class BotEntitleFactory(factory.DictFactory):
|
||||
type = "bot_command"
|
||||
offset = 0
|
||||
length = 7
|
||||
18
bot_microservice/tests/integration/factories/models.py
Normal file
18
bot_microservice/tests/integration/factories/models.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from typing import NamedTuple
|
||||
|
||||
|
||||
class User(NamedTuple):
|
||||
id: int
|
||||
is_bot: bool
|
||||
first_name: str | None
|
||||
last_name: str | None
|
||||
username: str | None
|
||||
language_code: str
|
||||
|
||||
|
||||
class Chat(NamedTuple):
|
||||
id: int
|
||||
first_name: str | None
|
||||
last_name: str | None
|
||||
username: str
|
||||
type: str
|
||||
Reference in New Issue
Block a user