mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-12 10:11:52 +00:00
Backport and improvements (#601)
* Backport RedisStorage, deep-linking * Allow prereleases for aioredis * Bump dependencies * Correctly skip Redis tests on Windows * Reformat tests code and bump Makefile
This commit is contained in:
parent
32bc05130f
commit
83d6ab48c5
43 changed files with 1004 additions and 327 deletions
27
tests/test_utils/test_auth_widget.py
Normal file
27
tests/test_utils/test_auth_widget.py
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import pytest
|
||||
|
||||
from aiogram.utils.auth_widget import check_integrity
|
||||
|
||||
TOKEN = "123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def data():
|
||||
return {
|
||||
"id": "42",
|
||||
"first_name": "John",
|
||||
"last_name": "Smith",
|
||||
"username": "username",
|
||||
"photo_url": "https://t.me/i/userpic/320/picname.jpg",
|
||||
"auth_date": "1565810688",
|
||||
"hash": "c303db2b5a06fe41d23a9b14f7c545cfc11dcc7473c07c9c5034ae60062461ce",
|
||||
}
|
||||
|
||||
|
||||
class TestCheckIntegrity:
|
||||
def test_ok(self, data):
|
||||
assert check_integrity(TOKEN, data) is True
|
||||
|
||||
def test_fail(self, data):
|
||||
data.pop("username")
|
||||
assert check_integrity(TOKEN, data) is False
|
||||
94
tests/test_utils/test_deep_linking.py
Normal file
94
tests/test_utils/test_deep_linking.py
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
import pytest
|
||||
from async_lru import alru_cache
|
||||
|
||||
from aiogram.utils.deep_linking import (
|
||||
create_start_link,
|
||||
create_startgroup_link,
|
||||
decode_payload,
|
||||
encode_payload,
|
||||
)
|
||||
from tests.mocked_bot import MockedBot
|
||||
|
||||
PAYLOADS = [
|
||||
"foo",
|
||||
"AAbbCCddEEff1122334455",
|
||||
"aaBBccDDeeFF5544332211",
|
||||
-12345678901234567890,
|
||||
12345678901234567890,
|
||||
]
|
||||
WRONG_PAYLOADS = [
|
||||
"@BotFather",
|
||||
"Some:special$characters#=",
|
||||
"spaces spaces spaces",
|
||||
1234567890123456789.0,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(params=PAYLOADS, name="payload")
|
||||
def payload_fixture(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(params=WRONG_PAYLOADS, name="wrong_payload")
|
||||
def wrong_payload_fixture(request):
|
||||
return request.param
|
||||
|
||||
|
||||
@pytest.fixture(autouse=True)
|
||||
def get_bot_user_fixture(monkeypatch):
|
||||
"""Monkey patching of bot.me calling."""
|
||||
|
||||
@alru_cache()
|
||||
async def get_bot_user_mock(self):
|
||||
from aiogram.types import User
|
||||
|
||||
return User(
|
||||
id=12345678,
|
||||
is_bot=True,
|
||||
first_name="FirstName",
|
||||
last_name="LastName",
|
||||
username="username",
|
||||
language_code="uk-UA",
|
||||
)
|
||||
|
||||
monkeypatch.setattr(MockedBot, "me", get_bot_user_mock)
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
class TestDeepLinking:
|
||||
async def test_get_start_link(self, bot, payload):
|
||||
link = await create_start_link(bot=bot, payload=payload)
|
||||
assert link == f"https://t.me/username?start={payload}"
|
||||
|
||||
async def test_wrong_symbols(self, bot, wrong_payload):
|
||||
with pytest.raises(ValueError):
|
||||
await create_start_link(bot, wrong_payload)
|
||||
|
||||
async def test_get_startgroup_link(self, bot, payload):
|
||||
link = await create_startgroup_link(bot, payload)
|
||||
assert link == f"https://t.me/username?startgroup={payload}"
|
||||
|
||||
async def test_filter_encode_and_decode(self, payload):
|
||||
encoded = encode_payload(payload)
|
||||
decoded = decode_payload(encoded)
|
||||
assert decoded == str(payload)
|
||||
|
||||
async def test_get_start_link_with_encoding(self, bot, wrong_payload):
|
||||
# define link
|
||||
link = await create_start_link(bot, wrong_payload, encode=True)
|
||||
|
||||
# define reference link
|
||||
encoded_payload = encode_payload(wrong_payload)
|
||||
|
||||
assert link == f"https://t.me/username?start={encoded_payload}"
|
||||
|
||||
async def test_64_len_payload(self, bot):
|
||||
payload = "p" * 64
|
||||
link = await create_start_link(bot, payload)
|
||||
assert link
|
||||
|
||||
async def test_too_long_payload(self, bot):
|
||||
payload = "p" * 65
|
||||
print(payload, len(payload))
|
||||
with pytest.raises(ValueError):
|
||||
await create_start_link(bot, payload)
|
||||
24
tests/test_utils/test_link.py
Normal file
24
tests/test_utils/test_link.py
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
from typing import Any, Dict
|
||||
|
||||
import pytest
|
||||
|
||||
from aiogram.utils.link import create_telegram_link, create_tg_link
|
||||
|
||||
|
||||
class TestLink:
|
||||
@pytest.mark.parametrize(
|
||||
"base,params,result",
|
||||
[["user", dict(id=42), "tg://user?id=42"]],
|
||||
)
|
||||
def test_create_tg_link(self, base: str, params: Dict[str, Any], result: str):
|
||||
assert create_tg_link(base, **params) == result
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"base,params,result",
|
||||
[
|
||||
["username", dict(), "https://t.me/username"],
|
||||
["username", dict(start="test"), "https://t.me/username?start=test"],
|
||||
],
|
||||
)
|
||||
def test_create_telegram_link(self, base: str, params: Dict[str, Any], result: str):
|
||||
assert create_telegram_link(base, **params) == result
|
||||
|
|
@ -35,7 +35,7 @@ class TestMarkdown:
|
|||
[hitalic, ("test", "test"), " ", "<i>test test</i>"],
|
||||
[code, ("test", "test"), " ", "`test test`"],
|
||||
[hcode, ("test", "test"), " ", "<code>test test</code>"],
|
||||
[pre, ("test", "test"), " ", "```test test```"],
|
||||
[pre, ("test", "test"), " ", "```\ntest test\n```"],
|
||||
[hpre, ("test", "test"), " ", "<pre>test test</pre>"],
|
||||
[underline, ("test", "test"), " ", "__\rtest test__\r"],
|
||||
[hunderline, ("test", "test"), " ", "<u>test test</u>"],
|
||||
|
|
|
|||
|
|
@ -55,7 +55,7 @@ class TestTextDecoration:
|
|||
[markdown_decoration, MessageEntity(type="bold", offset=0, length=5), "*test*"],
|
||||
[markdown_decoration, MessageEntity(type="italic", offset=0, length=5), "_\rtest_\r"],
|
||||
[markdown_decoration, MessageEntity(type="code", offset=0, length=5), "`test`"],
|
||||
[markdown_decoration, MessageEntity(type="pre", offset=0, length=5), "```test```"],
|
||||
[markdown_decoration, MessageEntity(type="pre", offset=0, length=5), "```\ntest\n```"],
|
||||
[
|
||||
markdown_decoration,
|
||||
MessageEntity(type="pre", offset=0, length=5, language="python"),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue