/
test_deep_linking.py
87 lines (65 loc) · 2.34 KB
/
test_deep_linking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import pytest
from aiogram.utils.deep_linking import (
decode_payload,
encode_payload,
get_start_link,
get_startgroup_link,
)
from tests.types import dataset
PAYLOADS = [
'foo',
'AAbbCCddEEff1122334455',
'aaBBccDDeeFF5544332211',
-12345678901234567890,
12345678901234567890,
]
WRONG_PAYLOADS = [
'@BotFather',
"Some:special$characters#=",
'spaces spaces spaces',
1234567890123456789.0,
]
USERNAME = dataset.USER["username"]
@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. """
from aiogram.utils import deep_linking
async def get_bot_user_mock():
from aiogram.types import User
return User(**dataset.USER)
monkeypatch.setattr(deep_linking, '_get_bot_user', get_bot_user_mock)
class TestDeepLinking:
async def test_get_start_link(self, payload):
link = await get_start_link(payload)
assert link == f'https://t.me/{USERNAME}?start={payload}'
async def test_wrong_symbols(self, wrong_payload):
with pytest.raises(ValueError):
await get_start_link(wrong_payload)
async def test_get_startgroup_link(self, payload):
link = await get_startgroup_link(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, wrong_payload):
# define link
link = await get_start_link(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):
payload = "p" * 64
link = await get_start_link(payload)
assert link
async def test_too_long_payload(self):
payload = "p" * 65
print(payload, len(payload))
with pytest.raises(ValueError):
await get_start_link(payload)