2017-01-20 20:13:58 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2017-01-20 20:13:58 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2017-08-11 23:58:41 +02:00
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
2017-01-20 20:13:58 +01:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-08-11 23:58:41 +02:00
|
|
|
# GNU Lesser Public License for more details.
|
2017-01-20 20:13:58 +01:00
|
|
|
#
|
2017-08-11 23:58:41 +02:00
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
2017-01-20 20:13:58 +01:00
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2021-09-22 16:49:10 +02:00
|
|
|
import re
|
2019-11-15 21:51:22 +01:00
|
|
|
|
2019-09-13 21:09:05 +02:00
|
|
|
import pytest
|
2017-01-20 20:13:58 +01:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import Message, MessageEntity, Update, helpers
|
2021-10-19 18:28:19 +02:00
|
|
|
from telegram.constants import MessageType
|
2021-01-30 14:15:39 +01:00
|
|
|
|
|
|
|
|
2020-06-15 18:20:51 +02:00
|
|
|
class TestHelpers:
|
2022-07-17 13:07:21 +02:00
|
|
|
@pytest.mark.parametrize(
|
2023-03-25 19:18:04 +01:00
|
|
|
("test_str", "expected"),
|
2022-07-17 13:07:21 +02:00
|
|
|
[
|
|
|
|
("*bold*", r"\*bold\*"),
|
|
|
|
("_italic_", r"\_italic\_"),
|
|
|
|
("`code`", r"\`code\`"),
|
|
|
|
("[text_link](https://github.com/)", r"\[text\_link](https://github.com/)"),
|
2023-05-07 13:44:34 +02:00
|
|
|
("![👍](tg://emoji?id=1)", r"!\[👍](tg://emoji?id=1)"),
|
2022-07-17 13:07:21 +02:00
|
|
|
],
|
2023-05-07 13:44:34 +02:00
|
|
|
ids=["bold", "italic", "code", "text_link", "custom_emoji_id"],
|
2022-07-17 13:07:21 +02:00
|
|
|
)
|
|
|
|
def test_escape_markdown(self, test_str, expected):
|
|
|
|
assert expected == helpers.escape_markdown(test_str)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-03-25 19:18:04 +01:00
|
|
|
("test_str", "expected"),
|
2022-07-17 13:07:21 +02:00
|
|
|
[
|
|
|
|
(r"a_b*c[d]e", r"a\_b\*c\[d\]e"),
|
|
|
|
(r"(fg) ", r"\(fg\) "),
|
|
|
|
(r"h~I`>JK#L+MN", r"h\~I\`\>JK\#L\+MN"),
|
|
|
|
(r"-O=|p{qr}s.t!\ ", r"\-O\=\|p\{qr\}s\.t\!\\ "),
|
|
|
|
(r"\u", r"\\u"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_escape_markdown_v2(self, test_str, expected):
|
|
|
|
assert expected == helpers.escape_markdown(test_str, version=2)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-03-25 19:18:04 +01:00
|
|
|
("test_str", "expected"),
|
2022-07-17 13:07:21 +02:00
|
|
|
[
|
|
|
|
(r"mono/pre:", r"mono/pre:"),
|
|
|
|
("`abc`", r"\`abc\`"),
|
|
|
|
(r"\int", r"\\int"),
|
|
|
|
(r"(`\some \` stuff)", r"(\`\\some \\\` stuff)"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_escape_markdown_v2_monospaced(self, test_str, expected):
|
|
|
|
assert expected == helpers.escape_markdown(
|
2020-10-09 17:22:07 +02:00
|
|
|
test_str, version=2, entity_type=MessageEntity.PRE
|
|
|
|
)
|
2022-07-17 13:07:21 +02:00
|
|
|
assert expected == helpers.escape_markdown(
|
2020-10-09 17:22:07 +02:00
|
|
|
test_str, version=2, entity_type=MessageEntity.CODE
|
|
|
|
)
|
2020-03-28 16:37:26 +01:00
|
|
|
|
2023-05-07 13:44:34 +02:00
|
|
|
def test_escape_markdown_v2_links(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
test_str = "https://url.containing/funny)cha)\\ra\\)cter\\s"
|
|
|
|
expected_str = "https://url.containing/funny\\)cha\\)\\\\ra\\\\\\)cter\\\\s"
|
2020-03-28 16:37:26 +01:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
assert expected_str == helpers.escape_markdown(
|
|
|
|
test_str, version=2, entity_type=MessageEntity.TEXT_LINK
|
|
|
|
)
|
2023-05-07 13:44:34 +02:00
|
|
|
assert expected_str == helpers.escape_markdown(
|
|
|
|
test_str, version=2, entity_type=MessageEntity.CUSTOM_EMOJI
|
|
|
|
)
|
2020-03-28 16:37:26 +01:00
|
|
|
|
|
|
|
def test_markdown_invalid_version(self):
|
2022-07-17 13:07:21 +02:00
|
|
|
with pytest.raises(ValueError, match="Markdown version must be either"):
|
2022-05-05 17:40:22 +02:00
|
|
|
helpers.escape_markdown("abc", version=-1)
|
2022-07-17 13:07:21 +02:00
|
|
|
with pytest.raises(ValueError, match="Markdown version must be either"):
|
|
|
|
helpers.mention_markdown(1, "abc", version=-1)
|
2020-03-28 16:37:26 +01:00
|
|
|
|
2019-09-13 21:09:05 +02:00
|
|
|
def test_create_deep_linked_url(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
username = "JamesTheMock"
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
payload = "hello"
|
2020-11-23 22:09:29 +01:00
|
|
|
expected = f"https://t.me/{username}?start={payload}"
|
2019-09-13 21:09:05 +02:00
|
|
|
actual = helpers.create_deep_linked_url(username, payload)
|
|
|
|
assert expected == actual
|
|
|
|
|
2020-11-23 22:09:29 +01:00
|
|
|
expected = f"https://t.me/{username}?startgroup={payload}"
|
2019-09-13 21:09:05 +02:00
|
|
|
actual = helpers.create_deep_linked_url(username, payload, group=True)
|
|
|
|
assert expected == actual
|
|
|
|
|
|
|
|
payload = ""
|
2020-11-23 22:09:29 +01:00
|
|
|
expected = f"https://t.me/{username}"
|
2019-09-13 21:09:05 +02:00
|
|
|
assert expected == helpers.create_deep_linked_url(username)
|
|
|
|
assert expected == helpers.create_deep_linked_url(username, payload)
|
|
|
|
payload = None
|
|
|
|
assert expected == helpers.create_deep_linked_url(username, payload)
|
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
with pytest.raises(ValueError, match="Only the following characters"):
|
2022-05-05 17:40:22 +02:00
|
|
|
helpers.create_deep_linked_url(username, "text with spaces")
|
2019-09-13 21:09:05 +02:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
with pytest.raises(ValueError, match="must not exceed 64"):
|
2022-05-05 17:40:22 +02:00
|
|
|
helpers.create_deep_linked_url(username, "0" * 65)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
with pytest.raises(ValueError, match="valid bot_username"):
|
2019-09-13 21:09:05 +02:00
|
|
|
helpers.create_deep_linked_url(None, None)
|
2023-03-25 19:18:04 +01:00
|
|
|
with pytest.raises(ValueError, match="valid bot_username"): # too short username, 4 is min
|
2019-09-13 21:09:05 +02:00
|
|
|
helpers.create_deep_linked_url("abc", None)
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
@pytest.mark.parametrize("message_type", list(MessageType))
|
|
|
|
@pytest.mark.parametrize("entity_type", [Update, Message])
|
2021-10-19 18:28:19 +02:00
|
|
|
def test_effective_message_type(self, message_type, entity_type):
|
|
|
|
def build_test_message(kwargs):
|
2023-03-25 19:18:04 +01:00
|
|
|
config = {
|
|
|
|
"message_id": 1,
|
|
|
|
"from_user": None,
|
|
|
|
"date": None,
|
|
|
|
"chat": None,
|
|
|
|
}
|
2019-02-08 11:02:54 +01:00
|
|
|
config.update(**kwargs)
|
|
|
|
return Message(**config)
|
|
|
|
|
2022-12-15 15:00:36 +01:00
|
|
|
message = build_test_message({message_type: (True,)}) # tuple for array-type args
|
2021-10-19 18:28:19 +02:00
|
|
|
entity = message if entity_type is Message else Update(1, message=message)
|
|
|
|
assert helpers.effective_message_type(entity) == message_type
|
2018-02-15 10:21:19 +01:00
|
|
|
|
|
|
|
empty_update = Update(2)
|
|
|
|
assert helpers.effective_message_type(empty_update) is None
|
2018-05-28 22:51:39 +02:00
|
|
|
|
2021-09-22 16:49:10 +02:00
|
|
|
def test_effective_message_type_wrong_type(self):
|
|
|
|
with pytest.raises(
|
2023-06-29 18:17:47 +02:00
|
|
|
TypeError, match=re.escape(f"neither Message nor Update (got: {type(entity := {})})")
|
2021-09-22 16:49:10 +02:00
|
|
|
):
|
|
|
|
helpers.effective_message_type(entity)
|
|
|
|
|
2018-05-28 22:51:39 +02:00
|
|
|
def test_mention_html(self):
|
|
|
|
expected = '<a href="tg://user?id=1">the name</a>'
|
|
|
|
|
2022-05-05 17:40:22 +02:00
|
|
|
assert expected == helpers.mention_html(1, "the name")
|
2018-05-28 22:51:39 +02:00
|
|
|
|
2022-07-17 13:07:21 +02:00
|
|
|
@pytest.mark.parametrize(
|
2023-03-25 19:18:04 +01:00
|
|
|
("test_str", "expected"),
|
2022-07-17 13:07:21 +02:00
|
|
|
[
|
|
|
|
("the name", "[the name](tg://user?id=1)"),
|
|
|
|
("under_score", "[under_score](tg://user?id=1)"),
|
|
|
|
("starred*text", "[starred*text](tg://user?id=1)"),
|
|
|
|
("`backtick`", "[`backtick`](tg://user?id=1)"),
|
|
|
|
("[square brackets", "[[square brackets](tg://user?id=1)"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_mention_markdown(self, test_str, expected):
|
|
|
|
assert expected == helpers.mention_markdown(1, test_str)
|
2020-03-28 16:37:26 +01:00
|
|
|
|
|
|
|
def test_mention_markdown_2(self):
|
2022-05-05 17:40:22 +02:00
|
|
|
expected = r"[the\_name](tg://user?id=1)"
|
2020-03-28 16:37:26 +01:00
|
|
|
|
2022-07-17 13:07:21 +02:00
|
|
|
assert expected == helpers.mention_markdown(1, "the_name", 2)
|