2022-02-19 01:45:59 +02:00
|
|
|
import asyncio
|
2021-11-08 23:20:12 +02:00
|
|
|
import time
|
2022-02-19 01:45:59 +02:00
|
|
|
from asyncio import Event
|
2021-11-08 23:20:12 +02:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
from typing import Any, Dict
|
2022-10-26 22:21:04 +03:00
|
|
|
from unittest.mock import AsyncMock, patch
|
2021-11-08 23:20:12 +02:00
|
|
|
|
|
|
|
|
import pytest
|
2023-02-12 02:00:42 +02:00
|
|
|
from aiohttp import MultipartReader, web
|
2021-11-08 23:20:12 +02:00
|
|
|
from aiohttp.test_utils import TestClient
|
|
|
|
|
from aiohttp.web_app import Application
|
|
|
|
|
|
|
|
|
|
from aiogram import Dispatcher, F
|
2022-08-14 01:07:52 +03:00
|
|
|
from aiogram.methods import GetMe, Request
|
2023-02-12 02:00:42 +02:00
|
|
|
from aiogram.types import BufferedInputFile, Message, User
|
2022-08-14 01:07:52 +03:00
|
|
|
from aiogram.webhook.aiohttp_server import (
|
2021-11-08 23:20:12 +02:00
|
|
|
SimpleRequestHandler,
|
|
|
|
|
TokenBasedRequestHandler,
|
|
|
|
|
ip_filter_middleware,
|
|
|
|
|
setup_application,
|
|
|
|
|
)
|
2022-08-14 01:07:52 +03:00
|
|
|
from aiogram.webhook.security import IPFilter
|
2021-11-08 23:20:12 +02:00
|
|
|
from tests.mocked_bot import MockedBot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestAiohttpServer:
|
|
|
|
|
def test_setup_application(self):
|
|
|
|
|
app = Application()
|
|
|
|
|
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
setup_application(app, dp)
|
|
|
|
|
|
|
|
|
|
assert len(app.router.routes()) == 0
|
|
|
|
|
assert len(app.on_startup) == 2
|
|
|
|
|
assert len(app.on_shutdown) == 1
|
|
|
|
|
|
|
|
|
|
async def test_middleware(self, aiohttp_client):
|
|
|
|
|
app = Application()
|
|
|
|
|
ip_filter = IPFilter.default()
|
|
|
|
|
app.middlewares.append(ip_filter_middleware(ip_filter))
|
|
|
|
|
|
|
|
|
|
async def handler(request: Request):
|
|
|
|
|
return web.json_response({"ok": True})
|
|
|
|
|
|
|
|
|
|
app.router.add_route("POST", "/webhook", handler)
|
|
|
|
|
client: TestClient = await aiohttp_client(app)
|
|
|
|
|
|
|
|
|
|
resp = await client.post("/webhook")
|
|
|
|
|
assert resp.status == 401
|
|
|
|
|
|
|
|
|
|
resp = await client.post("/webhook", headers={"X-Forwarded-For": "149.154.167.220"})
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
|
|
resp = await client.post(
|
|
|
|
|
"/webhook", headers={"X-Forwarded-For": "149.154.167.220,10.111.0.2"}
|
|
|
|
|
)
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestSimpleRequestHandler:
|
|
|
|
|
async def make_reqest(self, client: TestClient, text: str = "test"):
|
|
|
|
|
return await client.post(
|
|
|
|
|
"/webhook",
|
|
|
|
|
json={
|
|
|
|
|
"update_id": 0,
|
|
|
|
|
"message": {
|
|
|
|
|
"message_id": 0,
|
|
|
|
|
"from": {"id": 42, "first_name": "Test", "is_bot": False},
|
|
|
|
|
"chat": {"id": 42, "is_bot": False, "type": "private"},
|
|
|
|
|
"date": int(time.time()),
|
|
|
|
|
"text": text,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
|
2023-02-12 02:00:42 +02:00
|
|
|
async def test_reply_into_webhook_file(self, bot: MockedBot, aiohttp_client):
|
2021-11-08 23:20:12 +02:00
|
|
|
app = Application()
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
|
2023-02-12 02:00:42 +02:00
|
|
|
@dp.message(F.text == "test")
|
|
|
|
|
def handle_message(msg: Message):
|
|
|
|
|
return msg.answer_document(
|
|
|
|
|
caption="PASS",
|
|
|
|
|
document=BufferedInputFile(b"test", filename="test.txt"),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
handler = SimpleRequestHandler(
|
|
|
|
|
dispatcher=dp,
|
|
|
|
|
bot=bot,
|
|
|
|
|
handle_in_background=False,
|
|
|
|
|
)
|
|
|
|
|
handler.register(app, path="/webhook")
|
|
|
|
|
client: TestClient = await aiohttp_client(app)
|
|
|
|
|
|
|
|
|
|
resp = await self.make_reqest(client=client)
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
assert resp.content_type == "multipart/form-data"
|
|
|
|
|
result = {}
|
|
|
|
|
reader = MultipartReader.from_response(resp)
|
|
|
|
|
while part := await reader.next():
|
|
|
|
|
value = await part.read()
|
|
|
|
|
result[part.name] = value.decode()
|
|
|
|
|
assert result["method"] == "sendDocument"
|
|
|
|
|
assert result["caption"] == "PASS"
|
2023-03-11 20:46:36 +02:00
|
|
|
assert result["document"].startswith("attach://")
|
|
|
|
|
assert result[result["document"][9:]]
|
2023-02-12 02:00:42 +02:00
|
|
|
|
|
|
|
|
async def test_reply_into_webhook_text(self, bot: MockedBot, aiohttp_client):
|
|
|
|
|
app = Application()
|
|
|
|
|
dp = Dispatcher()
|
2022-02-19 01:45:59 +02:00
|
|
|
|
2021-11-08 23:20:12 +02:00
|
|
|
@dp.message(F.text == "test")
|
|
|
|
|
def handle_message(msg: Message):
|
2023-02-12 02:00:42 +02:00
|
|
|
return msg.answer(text="PASS")
|
2021-11-08 23:20:12 +02:00
|
|
|
|
|
|
|
|
handler = SimpleRequestHandler(
|
|
|
|
|
dispatcher=dp,
|
|
|
|
|
bot=bot,
|
|
|
|
|
handle_in_background=False,
|
|
|
|
|
)
|
|
|
|
|
handler.register(app, path="/webhook")
|
|
|
|
|
client: TestClient = await aiohttp_client(app)
|
|
|
|
|
|
|
|
|
|
resp = await self.make_reqest(client=client)
|
|
|
|
|
assert resp.status == 200
|
2023-02-12 02:00:42 +02:00
|
|
|
assert resp.content_type == "multipart/form-data"
|
|
|
|
|
result = {}
|
|
|
|
|
reader = MultipartReader.from_response(resp)
|
|
|
|
|
while part := await reader.next():
|
|
|
|
|
value = await part.read()
|
|
|
|
|
result[part.name] = value.decode()
|
2021-11-08 23:20:12 +02:00
|
|
|
assert result["method"] == "sendMessage"
|
2023-02-12 02:00:42 +02:00
|
|
|
assert result["text"] == "PASS"
|
|
|
|
|
|
|
|
|
|
async def test_reply_into_webhook_unhandled(self, bot: MockedBot, aiohttp_client):
|
|
|
|
|
app = Application()
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
|
|
|
|
|
@dp.message(F.text == "test")
|
|
|
|
|
def handle_message(msg: Message):
|
|
|
|
|
return msg.answer(text="PASS")
|
|
|
|
|
|
|
|
|
|
handler = SimpleRequestHandler(
|
|
|
|
|
dispatcher=dp,
|
|
|
|
|
bot=bot,
|
|
|
|
|
handle_in_background=False,
|
|
|
|
|
)
|
|
|
|
|
handler.register(app, path="/webhook")
|
|
|
|
|
client: TestClient = await aiohttp_client(app)
|
2021-11-08 23:20:12 +02:00
|
|
|
|
|
|
|
|
resp = await self.make_reqest(client=client, text="spam")
|
|
|
|
|
assert resp.status == 200
|
2023-02-12 02:00:42 +02:00
|
|
|
assert resp.content_type == "multipart/form-data"
|
|
|
|
|
result = {}
|
|
|
|
|
reader = MultipartReader.from_response(resp)
|
|
|
|
|
while part := await reader.next():
|
|
|
|
|
value = await part.read()
|
|
|
|
|
result[part.name] = value.decode()
|
2021-11-08 23:20:12 +02:00
|
|
|
assert not result
|
|
|
|
|
|
2023-02-12 02:00:42 +02:00
|
|
|
async def test_reply_into_webhook_background(self, bot: MockedBot, aiohttp_client):
|
|
|
|
|
app = Application()
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
|
|
|
|
|
handler_event = Event()
|
|
|
|
|
|
|
|
|
|
@dp.message(F.text == "test")
|
|
|
|
|
def handle_message(msg: Message):
|
|
|
|
|
handler_event.set()
|
|
|
|
|
return msg.answer(text="PASS")
|
|
|
|
|
|
|
|
|
|
handler = SimpleRequestHandler(
|
|
|
|
|
dispatcher=dp,
|
|
|
|
|
bot=bot,
|
|
|
|
|
handle_in_background=True,
|
|
|
|
|
)
|
|
|
|
|
handler.register(app, path="/webhook")
|
|
|
|
|
client: TestClient = await aiohttp_client(app)
|
|
|
|
|
|
2022-02-19 01:45:59 +02:00
|
|
|
with patch(
|
|
|
|
|
"aiogram.dispatcher.dispatcher.Dispatcher.silent_call_request",
|
2022-10-26 22:21:04 +03:00
|
|
|
new_callable=AsyncMock,
|
2022-02-19 01:45:59 +02:00
|
|
|
) as mocked_silent_call_request:
|
|
|
|
|
handler_event.clear()
|
|
|
|
|
resp = await self.make_reqest(client=client)
|
|
|
|
|
assert resp.status == 200
|
|
|
|
|
await asyncio.wait_for(handler_event.wait(), timeout=1)
|
|
|
|
|
mocked_silent_call_request.assert_awaited()
|
2021-11-08 23:20:12 +02:00
|
|
|
result = await resp.json()
|
|
|
|
|
assert not result
|
|
|
|
|
|
2023-06-25 00:40:04 +03:00
|
|
|
async def test_verify_secret(self, bot: MockedBot, aiohttp_client):
|
|
|
|
|
app = Application()
|
|
|
|
|
dp = Dispatcher()
|
|
|
|
|
handler = SimpleRequestHandler(
|
|
|
|
|
dispatcher=dp, bot=bot, handle_in_background=False, secret_token="vasya228"
|
|
|
|
|
)
|
|
|
|
|
handler.register(app, path="/webhook")
|
|
|
|
|
client: TestClient = await aiohttp_client(app)
|
|
|
|
|
resp = await self.make_reqest(client=client)
|
|
|
|
|
assert resp.status == 401
|
|
|
|
|
|
2021-11-08 23:20:12 +02:00
|
|
|
|
|
|
|
|
class TestTokenBasedRequestHandler:
|
2023-06-25 00:40:04 +03:00
|
|
|
async def test_verify_secret(self, bot: MockedBot):
|
|
|
|
|
dispatcher = Dispatcher()
|
|
|
|
|
handler = TokenBasedRequestHandler(dispatcher=dispatcher)
|
|
|
|
|
assert handler.verify_secret("petro328", bot)
|
|
|
|
|
|
2021-11-08 23:20:12 +02:00
|
|
|
async def test_register(self):
|
|
|
|
|
dispatcher = Dispatcher()
|
|
|
|
|
app = Application()
|
|
|
|
|
|
|
|
|
|
handler = TokenBasedRequestHandler(dispatcher=dispatcher)
|
|
|
|
|
|
|
|
|
|
assert len(app.router.routes()) == 0
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
handler.register(app, path="/webhook")
|
|
|
|
|
|
|
|
|
|
assert len(app.router.routes()) == 0
|
|
|
|
|
handler.register(app, path="/webhook/{bot_token}")
|
|
|
|
|
assert len(app.router.routes()) == 1
|
|
|
|
|
|
|
|
|
|
async def test_close(self):
|
|
|
|
|
dispatcher = Dispatcher()
|
|
|
|
|
|
|
|
|
|
handler = TokenBasedRequestHandler(dispatcher=dispatcher)
|
|
|
|
|
|
|
|
|
|
bot1 = handler.bots["42:TEST"] = MockedBot(token="42:TEST")
|
|
|
|
|
bot1.add_result_for(GetMe, ok=True, result=User(id=42, is_bot=True, first_name="Test"))
|
|
|
|
|
assert await bot1.get_me()
|
|
|
|
|
assert not bot1.session.closed
|
|
|
|
|
bot2 = handler.bots["1337:TEST"] = MockedBot(token="1337:TEST")
|
|
|
|
|
bot2.add_result_for(GetMe, ok=True, result=User(id=1337, is_bot=True, first_name="Test"))
|
|
|
|
|
assert await bot2.get_me()
|
|
|
|
|
assert not bot2.session.closed
|
|
|
|
|
|
|
|
|
|
await handler.close()
|
|
|
|
|
assert bot1.session.closed
|
|
|
|
|
assert bot2.session.closed
|
|
|
|
|
|
|
|
|
|
async def test_resolve_bot(self):
|
|
|
|
|
dispatcher = Dispatcher()
|
|
|
|
|
handler = TokenBasedRequestHandler(dispatcher=dispatcher)
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
|
class FakeRequest:
|
|
|
|
|
match_info: Dict[str, Any]
|
|
|
|
|
|
|
|
|
|
bot1 = await handler.resolve_bot(request=FakeRequest(match_info={"bot_token": "42:TEST"}))
|
|
|
|
|
assert bot1.id == 42
|
|
|
|
|
|
|
|
|
|
bot2 = await handler.resolve_bot(
|
|
|
|
|
request=FakeRequest(match_info={"bot_token": "1337:TEST"})
|
|
|
|
|
)
|
|
|
|
|
assert bot2.id == 1337
|
|
|
|
|
|
|
|
|
|
bot3 = await handler.resolve_bot(
|
|
|
|
|
request=FakeRequest(match_info={"bot_token": "1337:TEST"})
|
|
|
|
|
)
|
|
|
|
|
assert bot3.id == 1337
|
|
|
|
|
|
|
|
|
|
assert bot2 == bot3
|
|
|
|
|
assert len(handler.bots) == 2
|