Repository navigation
Expand file tree
/
Copy pathtest_inline_query_safety.py
More file actions
174 lines (147 loc) · 6.13 KB
/
Copy pathtest_inline_query_safety.py
File metadata and controls
174 lines (147 loc) · 6.13 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from aiogram import types
from aiogram.exceptions import TelegramBadRequest
from handlers.utils import build_inline_album_result, safe_answer_inline_query
@pytest.mark.asyncio
async def test_safe_answer_inline_query_sanitizes_unsupported_thumbnail_url():
query = SimpleNamespace(answer=AsyncMock())
results = [
types.InlineQueryResultArticle(
id="article-1",
title="Article",
description="desc",
thumbnail_url="https://example.com/thumb.webp",
input_message_content=types.InputTextMessageContent(message_text="hello"),
)
]
await safe_answer_inline_query(query, results, cache_time=10, is_personal=True)
answered_results = query.answer.await_args.args[0]
assert answered_results[0].thumbnail_url is None
@pytest.mark.asyncio
async def test_safe_answer_inline_query_keeps_photo_result_when_only_thumbnail_is_invalid():
query = SimpleNamespace(answer=AsyncMock())
results = [
types.InlineQueryResultPhoto(
id="photo-1",
photo_url="https://example.com/photo.jpg",
thumbnail_url="https://example.com/photo.webp",
title="Photo",
description="desc",
caption="caption",
parse_mode="HTML",
)
]
await safe_answer_inline_query(query, results, cache_time=10, is_personal=True)
answered_results = query.answer.await_args.args[0]
assert isinstance(answered_results[0], types.InlineQueryResultPhoto)
assert answered_results[0].photo_url == "https://example.com/photo.jpg"
assert answered_results[0].thumbnail_url == "https://example.com/photo.jpg"
@pytest.mark.asyncio
async def test_safe_answer_inline_query_retries_without_web_preview_urls():
error = TelegramBadRequest(
method=SimpleNamespace(__api_method__="answerInlineQuery"),
message="Telegram server says - Bad Request: WEBDOCUMENT_URL_INVALID",
)
query = SimpleNamespace(
from_user=SimpleNamespace(id=1),
query="inline query",
answer=AsyncMock(side_effect=[error, None]),
)
results = [
types.InlineQueryResultPhoto(
id="photo-1",
photo_url="https://example.com/photo.jpg",
thumbnail_url="https://example.com/photo.jpg",
title="Photo",
description="desc",
caption="caption",
parse_mode="HTML",
)
]
await safe_answer_inline_query(query, results, cache_time=10, is_personal=True)
assert query.answer.await_count == 2
retried_results = query.answer.await_args_list[1].args[0]
assert isinstance(retried_results[0], types.InlineQueryResultArticle)
assert getattr(retried_results[0], "thumbnail_url", None) is None
@pytest.mark.asyncio
async def test_safe_answer_inline_query_retries_with_article_fallback_for_photo_result_errors():
error = TelegramBadRequest(
method=SimpleNamespace(__api_method__="answerInlineQuery"),
message="Telegram server says - Bad Request: RESULT_TYPE_INVALID",
)
query = SimpleNamespace(
from_user=SimpleNamespace(id=1),
query="inline query",
answer=AsyncMock(side_effect=[error, None]),
)
results = [
types.InlineQueryResultPhoto(
id="photo-1",
photo_url="https://example.com/photo.jpg",
thumbnail_url="https://example.com/photo.jpg",
title="Photo",
description="desc",
caption="caption",
parse_mode="HTML",
)
]
await safe_answer_inline_query(query, results, cache_time=10, is_personal=True)
assert query.answer.await_count == 2
retried_results = query.answer.await_args_list[1].args[0]
assert isinstance(retried_results[0], types.InlineQueryResultArticle)
assert getattr(retried_results[0], "thumbnail_url", None) is None
@pytest.mark.asyncio
async def test_safe_answer_inline_query_uses_raw_request_without_aiogram_default_fields():
session = SimpleNamespace(make_request=AsyncMock(return_value=True))
bot = SimpleNamespace(session=session)
query = SimpleNamespace(
id="inline-query-id",
bot=bot,
from_user=SimpleNamespace(id=1),
query="inline query",
)
results = [
types.InlineQueryResultPhoto(
id="photo-1",
photo_url="https://example.com/photo.jpg",
thumbnail_url="https://example.com/photo.jpg",
title="Photo",
description="desc",
caption="caption",
),
types.InlineQueryResultArticle(
id="article-1",
title="Article",
description="desc",
input_message_content=types.InputTextMessageContent(message_text="hello"),
),
]
await safe_answer_inline_query(query, results, cache_time=10, is_personal=True)
session.make_request.assert_awaited_once()
method = session.make_request.await_args.args[1]
assert method.inline_query_id == "inline-query-id"
photo_payload = method.results[0]
assert photo_payload["type"] == "photo"
assert "parse_mode" not in photo_payload
assert "show_caption_above_media" not in photo_payload
article_payload = method.results[1]
assert article_payload["type"] == "article"
input_message_content = article_payload["input_message_content"]
assert input_message_content["message_text"] == "hello"
assert "parse_mode" not in input_message_content
assert "link_preview_options" not in input_message_content
def test_build_inline_album_result_prefers_cached_photo_file_id():
result = build_inline_album_result(
result_id="album-1",
service_name="TikTok",
deep_link="https://t.me/testbot?start=album_abc",
message_text="Open full album in bot",
preview_file_id="cached-photo-file-id",
preview_url="https://example.com/photo.jpg",
thumbnail_url="https://example.com/photo.jpg",
)
assert isinstance(result, types.InlineQueryResultCachedPhoto)
assert result.photo_file_id == "cached-photo-file-id"
assert result.caption == "Open full album in bot"