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 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
|
#!/usr/bin/env python
#
# A library that provides a Python interface to the Telegram Bot API
# Copyright (C) 2015-2025
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser Public License as published by
# 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
# GNU Lesser Public License for more details.
#
# You should have received a copy of the GNU Lesser Public License
# along with this program. If not, see [http://www.gnu.org/licenses/].
from collections.abc import Sequence
import pytest
from telegram import BotCommand, Gift, GiftInfo, Gifts, MessageEntity, Sticker
from telegram._gifts import AcceptedGiftTypes
from telegram._utils.defaultvalue import DEFAULT_NONE
from telegram.request import RequestData
from tests.auxil.slots import mro_slots
@pytest.fixture
def gift(request):
return Gift(
id=GiftTestBase.id,
sticker=GiftTestBase.sticker,
star_count=GiftTestBase.star_count,
total_count=GiftTestBase.total_count,
remaining_count=GiftTestBase.remaining_count,
upgrade_star_count=GiftTestBase.upgrade_star_count,
)
class GiftTestBase:
id = "some_id"
sticker = Sticker(
file_id="file_id",
file_unique_id="file_unique_id",
width=512,
height=512,
is_animated=False,
is_video=False,
type="regular",
)
star_count = 5
total_count = 10
remaining_count = 5
upgrade_star_count = 10
class TestGiftWithoutRequest(GiftTestBase):
def test_slot_behaviour(self, gift):
for attr in gift.__slots__:
assert getattr(gift, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(gift)) == len(set(mro_slots(gift))), "duplicate slot"
def test_de_json(self, offline_bot, gift):
json_dict = {
"id": self.id,
"sticker": self.sticker.to_dict(),
"star_count": self.star_count,
"total_count": self.total_count,
"remaining_count": self.remaining_count,
"upgrade_star_count": self.upgrade_star_count,
}
gift = Gift.de_json(json_dict, offline_bot)
assert gift.api_kwargs == {}
assert gift.id == self.id
assert gift.sticker == self.sticker
assert gift.star_count == self.star_count
assert gift.total_count == self.total_count
assert gift.remaining_count == self.remaining_count
assert gift.upgrade_star_count == self.upgrade_star_count
def test_to_dict(self, gift):
gift_dict = gift.to_dict()
assert isinstance(gift_dict, dict)
assert gift_dict["id"] == self.id
assert gift_dict["sticker"] == self.sticker.to_dict()
assert gift_dict["star_count"] == self.star_count
assert gift_dict["total_count"] == self.total_count
assert gift_dict["remaining_count"] == self.remaining_count
assert gift_dict["upgrade_star_count"] == self.upgrade_star_count
def test_equality(self, gift):
a = gift
b = Gift(
self.id,
self.sticker,
self.star_count,
self.total_count,
self.remaining_count,
self.upgrade_star_count,
)
c = Gift(
"other_uid",
self.sticker,
self.star_count,
self.total_count,
self.remaining_count,
self.upgrade_star_count,
)
d = BotCommand("start", "description")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.mark.parametrize(
"gift",
[
"gift_id",
Gift(
"gift_id",
Sticker("file_id", "file_unique_id", 512, 512, False, False, "regular"),
5,
10,
5,
10,
),
],
ids=["string", "Gift"],
)
@pytest.mark.parametrize("id_name", ["user_id", "chat_id"])
async def test_send_gift(self, offline_bot, gift, monkeypatch, id_name):
# We can't send actual gifts, so we just check that the correct parameters are passed
text_entities = [
MessageEntity(MessageEntity.TEXT_LINK, 0, 4, "url"),
MessageEntity(MessageEntity.BOLD, 5, 9),
]
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
received_id = request_data.parameters[id_name] == id_name
gift_id = request_data.parameters["gift_id"] == "gift_id"
text = request_data.parameters["text"] == "text"
text_parse_mode = request_data.parameters["text_parse_mode"] == "text_parse_mode"
tes = request_data.parameters["text_entities"] == [
me.to_dict() for me in text_entities
]
pay_for_upgrade = request_data.parameters["pay_for_upgrade"] is True
return received_id and gift_id and text and text_parse_mode and tes and pay_for_upgrade
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.send_gift(
gift,
"text",
text_parse_mode="text_parse_mode",
text_entities=text_entities,
pay_for_upgrade=True,
**{id_name: id_name},
)
@pytest.mark.parametrize("default_bot", [{"parse_mode": "Markdown"}], indirect=True)
@pytest.mark.parametrize(
("passed_value", "expected_value"),
[(DEFAULT_NONE, "Markdown"), ("HTML", "HTML"), (None, None)],
)
async def test_send_gift_default_parse_mode(
self, default_bot, monkeypatch, passed_value, expected_value
):
async def make_assertion(url, request_data: RequestData, *args, **kwargs):
return request_data.parameters.get("text_parse_mode") == expected_value
monkeypatch.setattr(default_bot.request, "post", make_assertion)
kwargs = {
"user_id": "user_id",
"gift_id": "gift_id",
}
if passed_value is not DEFAULT_NONE:
kwargs["text_parse_mode"] = passed_value
assert await default_bot.send_gift(**kwargs)
@pytest.fixture
def gifts(request):
return Gifts(gifts=GiftsTestBase.gifts)
class GiftsTestBase:
gifts: Sequence[Gift] = [
Gift(
id="id1",
sticker=Sticker(
file_id="file_id",
file_unique_id="file_unique_id",
width=512,
height=512,
is_animated=False,
is_video=False,
type="regular",
),
star_count=5,
total_count=5,
remaining_count=5,
upgrade_star_count=5,
),
Gift(
id="id2",
sticker=Sticker(
file_id="file_id",
file_unique_id="file_unique_id",
width=512,
height=512,
is_animated=False,
is_video=False,
type="regular",
),
star_count=6,
total_count=6,
remaining_count=6,
upgrade_star_count=6,
),
Gift(
id="id3",
sticker=Sticker(
file_id="file_id",
file_unique_id="file_unique_id",
width=512,
height=512,
is_animated=False,
is_video=False,
type="regular",
),
star_count=7,
total_count=7,
remaining_count=7,
upgrade_star_count=7,
),
]
class TestGiftsWithoutRequest(GiftsTestBase):
def test_slot_behaviour(self, gifts):
for attr in gifts.__slots__:
assert getattr(gifts, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(gifts)) == len(set(mro_slots(gifts))), "duplicate slot"
def test_de_json(self, offline_bot, gifts):
json_dict = {"gifts": [gift.to_dict() for gift in self.gifts]}
gifts = Gifts.de_json(json_dict, offline_bot)
assert gifts.api_kwargs == {}
assert gifts.gifts == tuple(self.gifts)
for de_json_gift, original_gift in zip(gifts.gifts, self.gifts):
assert de_json_gift.id == original_gift.id
assert de_json_gift.sticker == original_gift.sticker
assert de_json_gift.star_count == original_gift.star_count
assert de_json_gift.total_count == original_gift.total_count
assert de_json_gift.remaining_count == original_gift.remaining_count
assert de_json_gift.upgrade_star_count == original_gift.upgrade_star_count
def test_to_dict(self, gifts):
gifts_dict = gifts.to_dict()
assert isinstance(gifts_dict, dict)
assert gifts_dict["gifts"] == [gift.to_dict() for gift in self.gifts]
def test_equality(self, gifts):
a = gifts
b = Gifts(self.gifts)
c = Gifts(self.gifts[:2])
d = BotCommand("start", "description")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
class TestGiftsWithRequest(GiftTestBase):
async def test_get_available_gifts(self, bot, chat_id):
# We don't control the available gifts, so we can not make any better assertions
assert isinstance(await bot.get_available_gifts(), Gifts)
@pytest.fixture
def gift_info():
return GiftInfo(
gift=GiftInfoTestBase.gift,
owned_gift_id=GiftInfoTestBase.owned_gift_id,
convert_star_count=GiftInfoTestBase.convert_star_count,
prepaid_upgrade_star_count=GiftInfoTestBase.prepaid_upgrade_star_count,
can_be_upgraded=GiftInfoTestBase.can_be_upgraded,
text=GiftInfoTestBase.text,
entities=GiftInfoTestBase.entities,
is_private=GiftInfoTestBase.is_private,
)
class GiftInfoTestBase:
gift = Gift(
id="some_id",
sticker=Sticker("file_id", "file_unique_id", 512, 512, False, False, "regular"),
star_count=5,
total_count=10,
remaining_count=15,
upgrade_star_count=20,
)
owned_gift_id = "some_owned_gift_id"
convert_star_count = 100
prepaid_upgrade_star_count = 200
can_be_upgraded = True
text = "test text"
entities = (
MessageEntity(MessageEntity.BOLD, 0, 4),
MessageEntity(MessageEntity.ITALIC, 5, 8),
)
is_private = True
class TestGiftInfoWithoutRequest(GiftInfoTestBase):
def test_slot_behaviour(self, gift_info):
for attr in gift_info.__slots__:
assert getattr(gift_info, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(gift_info)) == len(set(mro_slots(gift_info))), "duplicate slot"
def test_de_json(self, offline_bot):
json_dict = {
"gift": self.gift.to_dict(),
"owned_gift_id": self.owned_gift_id,
"convert_star_count": self.convert_star_count,
"prepaid_upgrade_star_count": self.prepaid_upgrade_star_count,
"can_be_upgraded": self.can_be_upgraded,
"text": self.text,
"entities": [e.to_dict() for e in self.entities],
"is_private": self.is_private,
}
gift_info = GiftInfo.de_json(json_dict, offline_bot)
assert gift_info.api_kwargs == {}
assert gift_info.gift == self.gift
assert gift_info.owned_gift_id == self.owned_gift_id
assert gift_info.convert_star_count == self.convert_star_count
assert gift_info.prepaid_upgrade_star_count == self.prepaid_upgrade_star_count
assert gift_info.can_be_upgraded == self.can_be_upgraded
assert gift_info.text == self.text
assert gift_info.entities == self.entities
assert gift_info.is_private == self.is_private
def test_to_dict(self, gift_info):
json_dict = gift_info.to_dict()
assert json_dict["gift"] == self.gift.to_dict()
assert json_dict["owned_gift_id"] == self.owned_gift_id
assert json_dict["convert_star_count"] == self.convert_star_count
assert json_dict["prepaid_upgrade_star_count"] == self.prepaid_upgrade_star_count
assert json_dict["can_be_upgraded"] == self.can_be_upgraded
assert json_dict["text"] == self.text
assert json_dict["entities"] == [e.to_dict() for e in self.entities]
assert json_dict["is_private"] == self.is_private
def test_parse_entity(self, gift_info):
entity = MessageEntity(MessageEntity.BOLD, 0, 4)
assert gift_info.parse_entity(entity) == "test"
with pytest.raises(RuntimeError, match="GiftInfo has no"):
GiftInfo(
gift=self.gift,
).parse_entity(entity)
def test_parse_entities(self, gift_info):
entity = MessageEntity(MessageEntity.BOLD, 0, 4)
entity_2 = MessageEntity(MessageEntity.ITALIC, 5, 8)
assert gift_info.parse_entities(MessageEntity.BOLD) == {entity: "test"}
assert gift_info.parse_entities() == {entity: "test", entity_2: "text"}
with pytest.raises(RuntimeError, match="GiftInfo has no"):
GiftInfo(
gift=self.gift,
).parse_entities()
def test_equality(self, gift_info):
a = gift_info
b = GiftInfo(gift=self.gift)
c = GiftInfo(
gift=Gift(
id="some_other_gift_id",
sticker=Sticker("file_id", "file_unique_id", 512, 512, False, False, "regular"),
star_count=5,
),
)
d = BotCommand("start", "description")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
@pytest.fixture
def accepted_gift_types():
return AcceptedGiftTypes(
unlimited_gifts=AcceptedGiftTypesTestBase.unlimited_gifts,
limited_gifts=AcceptedGiftTypesTestBase.limited_gifts,
unique_gifts=AcceptedGiftTypesTestBase.unique_gifts,
premium_subscription=AcceptedGiftTypesTestBase.premium_subscription,
)
class AcceptedGiftTypesTestBase:
unlimited_gifts = False
limited_gifts = True
unique_gifts = True
premium_subscription = True
class TestAcceptedGiftTypesWithoutRequest(AcceptedGiftTypesTestBase):
def test_slot_behaviour(self, accepted_gift_types):
for attr in accepted_gift_types.__slots__:
assert getattr(accepted_gift_types, attr, "err") != "err", f"got extra slot '{attr}'"
assert len(mro_slots(accepted_gift_types)) == len(
set(mro_slots(accepted_gift_types))
), "duplicate slot"
def test_de_json(self, offline_bot):
json_dict = {
"unlimited_gifts": self.unlimited_gifts,
"limited_gifts": self.limited_gifts,
"unique_gifts": self.unique_gifts,
"premium_subscription": self.premium_subscription,
}
accepted_gift_types = AcceptedGiftTypes.de_json(json_dict, offline_bot)
assert accepted_gift_types.api_kwargs == {}
assert accepted_gift_types.unlimited_gifts == self.unlimited_gifts
assert accepted_gift_types.limited_gifts == self.limited_gifts
assert accepted_gift_types.unique_gifts == self.unique_gifts
assert accepted_gift_types.premium_subscription == self.premium_subscription
def test_to_dict(self, accepted_gift_types):
json_dict = accepted_gift_types.to_dict()
assert json_dict["unlimited_gifts"] == self.unlimited_gifts
assert json_dict["limited_gifts"] == self.limited_gifts
assert json_dict["unique_gifts"] == self.unique_gifts
assert json_dict["premium_subscription"] == self.premium_subscription
def test_equality(self, accepted_gift_types):
a = accepted_gift_types
b = AcceptedGiftTypes(
self.unlimited_gifts, self.limited_gifts, self.unique_gifts, self.premium_subscription
)
c = AcceptedGiftTypes(
not self.unlimited_gifts,
self.limited_gifts,
self.unique_gifts,
self.premium_subscription,
)
d = BotCommand("start", "description")
assert a == b
assert hash(a) == hash(b)
assert a != c
assert hash(a) != hash(c)
assert a != d
assert hash(a) != hash(d)
|