|
1 | 1 | from unittest.mock import AsyncMock, MagicMock, patch |
2 | 2 |
|
3 | 3 | import pytest |
4 | | -from telegram import User |
| 4 | +from telegram import Chat, User |
5 | 5 | from telegram.error import BadRequest, Forbidden |
6 | 6 |
|
7 | 7 | from bot.services.telegram_utils import ( |
@@ -97,6 +97,67 @@ def test_get_user_mention_long_full_name(self, mock_mention_markdown): |
97 | 97 | mock_mention_markdown.assert_called_once_with(777888, "A" * 100, version=1) |
98 | 98 | assert result == f"[{'A' * 100}](tg://user?id=777888)" |
99 | 99 |
|
| 100 | + def test_get_user_mention_with_prefixed_username(self): |
| 101 | + """Test that username with @ prefix is normalized.""" |
| 102 | + user = MagicMock(spec=User) |
| 103 | + user.username = "@already_prefixed" |
| 104 | + user.id = 123456 |
| 105 | + user.full_name = "Test User" |
| 106 | + |
| 107 | + result = get_user_mention(user) |
| 108 | + |
| 109 | + assert result == "@already_prefixed" |
| 110 | + |
| 111 | + def test_get_user_mention_chat_with_username(self): |
| 112 | + """Test getting mention for Chat object with username.""" |
| 113 | + chat = MagicMock(spec=Chat) |
| 114 | + chat.username = "john_doe" |
| 115 | + chat.id = 123456 |
| 116 | + chat.full_name = "John Doe" |
| 117 | + |
| 118 | + result = get_user_mention(chat) |
| 119 | + |
| 120 | + assert result == "@john_doe" |
| 121 | + |
| 122 | + def test_get_user_mention_chat_with_prefixed_username(self): |
| 123 | + """Test that Chat with @ prefixed username is normalized.""" |
| 124 | + chat = MagicMock(spec=Chat) |
| 125 | + chat.username = "@prefixed_chat" |
| 126 | + chat.id = 123456 |
| 127 | + chat.full_name = "Prefixed Chat" |
| 128 | + |
| 129 | + result = get_user_mention(chat) |
| 130 | + |
| 131 | + assert result == "@prefixed_chat" |
| 132 | + |
| 133 | + @patch("bot.services.telegram_utils.mention_markdown") |
| 134 | + def test_get_user_mention_chat_without_username(self, mock_mention_markdown): |
| 135 | + """Test getting mention for Chat object without username.""" |
| 136 | + chat = MagicMock(spec=Chat) |
| 137 | + chat.username = None |
| 138 | + chat.id = 123456 |
| 139 | + chat.full_name = "Jane Smith" |
| 140 | + mock_mention_markdown.return_value = "[Jane Smith](tg://user?id=123456)" |
| 141 | + |
| 142 | + result = get_user_mention(chat) |
| 143 | + |
| 144 | + mock_mention_markdown.assert_called_once_with(123456, "Jane Smith", version=1) |
| 145 | + assert result == "[Jane Smith](tg://user?id=123456)" |
| 146 | + |
| 147 | + @patch("bot.services.telegram_utils.mention_markdown") |
| 148 | + def test_get_user_mention_chat_empty_username(self, mock_mention_markdown): |
| 149 | + """Test getting mention for Chat object with empty string username.""" |
| 150 | + chat = MagicMock(spec=Chat) |
| 151 | + chat.username = "" |
| 152 | + chat.id = 987654 |
| 153 | + chat.full_name = "Jane Smith" |
| 154 | + mock_mention_markdown.return_value = "[Jane Smith](tg://user?id=987654)" |
| 155 | + |
| 156 | + result = get_user_mention(chat) |
| 157 | + |
| 158 | + mock_mention_markdown.assert_called_once_with(987654, "Jane Smith", version=1) |
| 159 | + assert result == "[Jane Smith](tg://user?id=987654)" |
| 160 | + |
100 | 161 |
|
101 | 162 | class TestGetUserMentionById: |
102 | 163 | @patch("bot.services.telegram_utils.mention_markdown") |
@@ -160,6 +221,44 @@ def test_get_user_mention_by_id_single_character_name(self, mock_mention_markdow |
160 | 221 | mock_mention_markdown.assert_called_once_with(777888, "A", version=1) |
161 | 222 | assert result == "[A](tg://user?id=777888)" |
162 | 223 |
|
| 224 | + def test_get_user_mention_by_id_with_username(self): |
| 225 | + """Test mention by ID with username provided returns @username.""" |
| 226 | + result = get_user_mention_by_id(123456, "John Doe", username="johndoe") |
| 227 | + |
| 228 | + assert result == "@johndoe" |
| 229 | + |
| 230 | + def test_get_user_mention_by_id_with_username_special_chars(self): |
| 231 | + """Test mention by ID with username containing underscores.""" |
| 232 | + result = get_user_mention_by_id(123456, "John Doe", username="john_doe_123") |
| 233 | + |
| 234 | + assert result == "@john_doe_123" |
| 235 | + |
| 236 | + def test_get_user_mention_by_id_with_prefixed_username(self): |
| 237 | + """Test that username with @ prefix is normalized.""" |
| 238 | + result = get_user_mention_by_id(123456, "Test User", username="@prefixed") |
| 239 | + |
| 240 | + assert result == "@prefixed" |
| 241 | + |
| 242 | + @patch("bot.services.telegram_utils.mention_markdown") |
| 243 | + def test_get_user_mention_by_id_with_none_username(self, mock_mention_markdown): |
| 244 | + """Test mention by ID with explicit None username.""" |
| 245 | + mock_mention_markdown.return_value = "[John Doe](tg://user?id=123456)" |
| 246 | + |
| 247 | + result = get_user_mention_by_id(123456, "John Doe", username=None) |
| 248 | + |
| 249 | + mock_mention_markdown.assert_called_once_with(123456, "John Doe", version=1) |
| 250 | + assert result == "[John Doe](tg://user?id=123456)" |
| 251 | + |
| 252 | + @patch("bot.services.telegram_utils.mention_markdown") |
| 253 | + def test_get_user_mention_by_id_with_empty_username(self, mock_mention_markdown): |
| 254 | + """Test mention by ID with empty string username falls back to markdown.""" |
| 255 | + mock_mention_markdown.return_value = "[John Doe](tg://user?id=123456)" |
| 256 | + |
| 257 | + result = get_user_mention_by_id(123456, "John Doe", username="") |
| 258 | + |
| 259 | + mock_mention_markdown.assert_called_once_with(123456, "John Doe", version=1) |
| 260 | + assert result == "[John Doe](tg://user?id=123456)" |
| 261 | + |
163 | 262 |
|
164 | 263 | class TestUnrestrictUser: |
165 | 264 | async def test_unrestrict_user_basic(self, mock_bot): |
|
0 commit comments