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 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
#!/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/].
import datetime as dtm
import pytest
from telegram import (
BusinessBotRights,
BusinessConnection,
Chat,
InputProfilePhotoStatic,
InputStoryContentPhoto,
MessageEntity,
StarAmount,
Story,
StoryAreaTypeLink,
StoryAreaTypeUniqueGift,
User,
)
from telegram._files._inputstorycontent import InputStoryContentVideo
from telegram._files.sticker import Sticker
from telegram._gifts import AcceptedGiftTypes, Gift
from telegram._inline.inlinekeyboardbutton import InlineKeyboardButton
from telegram._inline.inlinekeyboardmarkup import InlineKeyboardMarkup
from telegram._inputchecklist import InputChecklist, InputChecklistTask
from telegram._message import Message
from telegram._ownedgift import OwnedGiftRegular, OwnedGifts
from telegram._reply import ReplyParameters
from telegram._utils.datetime import UTC
from telegram._utils.defaultvalue import DEFAULT_NONE
from telegram.constants import InputProfilePhotoType, InputStoryContentType
from tests.auxil.files import data_file
class BusinessMethodsTestBase:
bci = "42"
class TestBusinessMethodsWithoutRequest(BusinessMethodsTestBase):
async def test_get_business_connection(self, offline_bot, monkeypatch):
user = User(1, "first", False)
user_chat_id = 1
date = dtm.datetime.utcnow()
rights = BusinessBotRights(can_reply=True)
is_enabled = True
bc = BusinessConnection(
self.bci,
user,
user_chat_id,
date,
is_enabled,
rights=rights,
).to_json()
async def do_request(*args, **kwargs):
data = kwargs.get("request_data")
obj = data.parameters.get("business_connection_id")
if obj == self.bci:
return 200, f'{{"ok": true, "result": {bc}}}'.encode()
return 400, b'{"ok": false, "result": []}'
monkeypatch.setattr(offline_bot.request, "do_request", do_request)
obj = await offline_bot.get_business_connection(business_connection_id=self.bci)
assert isinstance(obj, BusinessConnection)
@pytest.mark.parametrize("bool_param", [True, False, None])
async def test_get_business_account_gifts(self, offline_bot, monkeypatch, bool_param):
offset = 50
limit = 50
owned_gifts = OwnedGifts(
total_count=1,
gifts=[
OwnedGiftRegular(
gift=Gift(
id="id1",
sticker=Sticker(
"file_id", "file_unique_id", 512, 512, False, False, "regular"
),
star_count=5,
),
send_date=dtm.datetime.now(tz=UTC).replace(microsecond=0),
owned_gift_id="some_id_1",
)
],
).to_json()
async def do_request_and_make_assertions(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("exclude_unsaved") is bool_param
assert data.get("exclude_saved") is bool_param
assert data.get("exclude_unlimited") is bool_param
assert data.get("exclude_limited") is bool_param
assert data.get("exclude_unique") is bool_param
assert data.get("sort_by_price") is bool_param
assert data.get("offset") == offset
assert data.get("limit") == limit
return 200, f'{{"ok": true, "result": {owned_gifts}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", do_request_and_make_assertions)
obj = await offline_bot.get_business_account_gifts(
business_connection_id=self.bci,
exclude_unsaved=bool_param,
exclude_saved=bool_param,
exclude_unlimited=bool_param,
exclude_limited=bool_param,
exclude_unique=bool_param,
sort_by_price=bool_param,
offset=offset,
limit=limit,
)
assert isinstance(obj, OwnedGifts)
async def test_get_business_account_star_balance(self, offline_bot, monkeypatch):
star_amount_json = StarAmount(amount=100, nanostar_amount=356).to_json()
async def do_request(*args, **kwargs):
data = kwargs.get("request_data")
obj = data.parameters.get("business_connection_id")
if obj == self.bci:
return 200, f'{{"ok": true, "result": {star_amount_json}}}'.encode()
return 400, b'{"ok": false, "result": []}'
monkeypatch.setattr(offline_bot.request, "do_request", do_request)
obj = await offline_bot.get_business_account_star_balance(business_connection_id=self.bci)
assert isinstance(obj, StarAmount)
async def test_read_business_message(self, offline_bot, monkeypatch):
chat_id = 43
message_id = 44
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("chat_id") == chat_id
assert data.get("message_id") == message_id
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.read_business_message(
business_connection_id=self.bci, chat_id=chat_id, message_id=message_id
)
async def test_delete_business_messages(self, offline_bot, monkeypatch):
message_ids = [1, 2, 3]
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("message_ids") == message_ids
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.delete_business_messages(
business_connection_id=self.bci, message_ids=message_ids
)
@pytest.mark.parametrize("last_name", [None, "last_name"])
async def test_set_business_account_name(self, offline_bot, monkeypatch, last_name):
first_name = "Test Business Account"
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("first_name") == first_name
assert data.get("last_name") == last_name
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.set_business_account_name(
business_connection_id=self.bci, first_name=first_name, last_name=last_name
)
@pytest.mark.parametrize("username", ["username", None])
async def test_set_business_account_username(self, offline_bot, monkeypatch, username):
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("username") == username
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.set_business_account_username(
business_connection_id=self.bci, username=username
)
@pytest.mark.parametrize("bio", ["bio", None])
async def test_set_business_account_bio(self, offline_bot, monkeypatch, bio):
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("bio") == bio
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.set_business_account_bio(business_connection_id=self.bci, bio=bio)
async def test_set_business_account_gift_settings(self, offline_bot, monkeypatch):
show_gift_button = True
accepted_gift_types = AcceptedGiftTypes(True, True, True, True)
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").json_parameters
assert data.get("business_connection_id") == self.bci
assert data.get("show_gift_button") == "true"
assert data.get("accepted_gift_types") == accepted_gift_types.to_json()
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.set_business_account_gift_settings(
business_connection_id=self.bci,
show_gift_button=show_gift_button,
accepted_gift_types=accepted_gift_types,
)
async def test_convert_gift_to_stars(self, offline_bot, monkeypatch):
owned_gift_id = "some_id"
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("owned_gift_id") == owned_gift_id
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.convert_gift_to_stars(
business_connection_id=self.bci,
owned_gift_id=owned_gift_id,
)
@pytest.mark.parametrize("keep_original_details", [True, None])
@pytest.mark.parametrize("star_count", [100, None])
async def test_upgrade_gift(self, offline_bot, monkeypatch, keep_original_details, star_count):
owned_gift_id = "some_id"
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("owned_gift_id") == owned_gift_id
assert data.get("keep_original_details") is keep_original_details
assert data.get("star_count") == star_count
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.upgrade_gift(
business_connection_id=self.bci,
owned_gift_id=owned_gift_id,
keep_original_details=keep_original_details,
star_count=star_count,
)
@pytest.mark.parametrize("star_count", [100, None])
async def test_transfer_gift(self, offline_bot, monkeypatch, star_count):
owned_gift_id = "some_id"
new_owner_chat_id = 123
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("owned_gift_id") == owned_gift_id
assert data.get("new_owner_chat_id") == new_owner_chat_id
assert data.get("star_count") == star_count
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.transfer_gift(
business_connection_id=self.bci,
owned_gift_id=owned_gift_id,
new_owner_chat_id=new_owner_chat_id,
star_count=star_count,
)
async def test_transfer_business_account_stars(self, offline_bot, monkeypatch):
star_count = 100
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("star_count") == star_count
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.transfer_business_account_stars(
business_connection_id=self.bci,
star_count=star_count,
)
@pytest.mark.parametrize("is_public", [True, False, None, DEFAULT_NONE])
async def test_set_business_account_profile_photo(self, offline_bot, monkeypatch, is_public):
async def make_assertion(*args, **kwargs):
request_data = kwargs.get("request_data")
params = request_data.parameters
assert params.get("business_connection_id") == self.bci
if is_public is DEFAULT_NONE:
assert "is_public" not in params
else:
assert params.get("is_public") == is_public
assert (photo_dict := params.get("photo")).get("type") == InputProfilePhotoType.STATIC
assert (photo_attach := photo_dict["photo"]).startswith("attach://")
assert isinstance(
request_data.multipart_data.get(photo_attach.removeprefix("attach://")), tuple
)
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"photo": InputProfilePhotoStatic(
photo=data_file("telegram.jpg").read_bytes(),
),
}
if is_public is not DEFAULT_NONE:
kwargs["is_public"] = is_public
assert await offline_bot.set_business_account_profile_photo(**kwargs)
async def test_set_business_account_profile_photo_local_file(self, offline_bot, monkeypatch):
async def make_assertion(*args, **kwargs):
request_data = kwargs.get("request_data")
params = request_data.parameters
assert params.get("business_connection_id") == self.bci
assert (photo_dict := params.get("photo")).get("type") == InputProfilePhotoType.STATIC
assert photo_dict["photo"] == data_file("telegram.jpg").as_uri()
assert not request_data.multipart_data
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"photo": InputProfilePhotoStatic(
photo=data_file("telegram.jpg"),
),
}
assert await offline_bot.set_business_account_profile_photo(**kwargs)
@pytest.mark.parametrize("is_public", [True, False, None, DEFAULT_NONE])
async def test_remove_business_account_profile_photo(
self, offline_bot, monkeypatch, is_public
):
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
if is_public is DEFAULT_NONE:
assert "is_public" not in data
else:
assert data.get("is_public") == is_public
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
kwargs = {"business_connection_id": self.bci}
if is_public is not DEFAULT_NONE:
kwargs["is_public"] = is_public
assert await offline_bot.remove_business_account_profile_photo(**kwargs)
@pytest.mark.parametrize("active_period", [dtm.timedelta(seconds=30), 30])
async def test_post_story_all_args(self, offline_bot, monkeypatch, active_period):
content = InputStoryContentPhoto(photo=data_file("telegram.jpg").read_bytes())
caption = "test caption"
caption_entities = [
MessageEntity(MessageEntity.BOLD, 0, 3),
MessageEntity(MessageEntity.ITALIC, 5, 11),
]
parse_mode = "Markdown"
areas = [StoryAreaTypeLink("http_url"), StoryAreaTypeUniqueGift("unique_gift_name")]
post_to_chat_page = True
protect_content = True
json_story = Story(chat=Chat(123, "private"), id=123).to_json()
async def do_request_and_make_assertions(*args, **kwargs):
request_data = kwargs.get("request_data")
params = kwargs.get("request_data").parameters
assert params.get("business_connection_id") == self.bci
assert params.get("active_period") == 30
assert params.get("caption") == caption
assert params.get("caption_entities") == [e.to_dict() for e in caption_entities]
assert params.get("parse_mode") == parse_mode
assert params.get("areas") == [area.to_dict() for area in areas]
assert params.get("post_to_chat_page") is post_to_chat_page
assert params.get("protect_content") is protect_content
assert (content_dict := params.get("content")).get(
"type"
) == InputStoryContentType.PHOTO
assert (photo_attach := content_dict["photo"]).startswith("attach://")
assert isinstance(
request_data.multipart_data.get(photo_attach.removeprefix("attach://")), tuple
)
return 200, f'{{"ok": true, "result": {json_story}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", do_request_and_make_assertions)
obj = await offline_bot.post_story(
business_connection_id=self.bci,
content=content,
active_period=active_period,
caption=caption,
caption_entities=caption_entities,
parse_mode=parse_mode,
areas=areas,
post_to_chat_page=post_to_chat_page,
protect_content=protect_content,
)
assert isinstance(obj, Story)
@pytest.mark.parametrize("active_period", [dtm.timedelta(seconds=30), 30])
async def test_post_story_local_file(self, offline_bot, monkeypatch, active_period):
json_story = Story(chat=Chat(123, "private"), id=123).to_json()
async def make_assertion(*args, **kwargs):
request_data = kwargs.get("request_data")
params = request_data.parameters
assert params.get("business_connection_id") == self.bci
assert (content_dict := params.get("content")).get(
"type"
) == InputStoryContentType.PHOTO
assert content_dict["photo"] == data_file("telegram.jpg").as_uri()
assert not request_data.multipart_data
return 200, f'{{"ok": true, "result": {json_story}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"content": InputStoryContentPhoto(
photo=data_file("telegram.jpg"),
),
"active_period": active_period,
}
assert await offline_bot.post_story(**kwargs)
@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_post_story_default_parse_mode(
self, default_bot, monkeypatch, passed_value, expected_value
):
async def make_assertion(url, request_data, *args, **kwargs):
assert request_data.parameters.get("parse_mode") == expected_value
return Story(chat=Chat(123, "private"), id=123).to_dict()
monkeypatch.setattr(default_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"content": InputStoryContentPhoto(photo=data_file("telegram.jpg").read_bytes()),
"active_period": dtm.timedelta(seconds=20),
"caption": "caption",
}
if passed_value is not DEFAULT_NONE:
kwargs["parse_mode"] = passed_value
await default_bot.post_story(**kwargs)
@pytest.mark.parametrize("default_bot", [{"protect_content": True}], indirect=True)
@pytest.mark.parametrize(
("passed_value", "expected_value"),
[(DEFAULT_NONE, True), (False, False), (None, None)],
)
async def test_post_story_default_protect_content(
self, default_bot, monkeypatch, passed_value, expected_value
):
async def make_assertion(url, request_data, *args, **kwargs):
assert request_data.parameters.get("protect_content") == expected_value
return Story(chat=Chat(123, "private"), id=123).to_dict()
monkeypatch.setattr(default_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"content": InputStoryContentPhoto(bytes("photo", encoding="utf-8")),
"active_period": dtm.timedelta(seconds=20),
}
if passed_value is not DEFAULT_NONE:
kwargs["protect_content"] = passed_value
await default_bot.post_story(**kwargs)
@pytest.mark.parametrize(
("argument", "expected"),
[(4, 4), (4.0, 4), (dtm.timedelta(seconds=4), 4), (4.5, 4.5)],
)
async def test_post_story_float_time_period(
self, offline_bot, monkeypatch, argument, expected
):
# We test that whole number conversion works properly. Only tested here but
# relevant for some other methods too (e.g bot.set_business_account_profile_photo)
async def make_assertion(url, request_data, *args, **kwargs):
data = request_data.parameters
content = data["content"]
assert content["duration"] == expected
assert type(content["duration"]) is type(expected)
assert content["cover_frame_timestamp"] == expected
assert type(content["cover_frame_timestamp"]) is type(expected)
return Story(chat=Chat(123, "private"), id=123).to_dict()
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"content": InputStoryContentVideo(
video=data_file("telegram.mp4"),
duration=argument,
cover_frame_timestamp=argument,
),
"active_period": dtm.timedelta(seconds=20),
}
assert await offline_bot.post_story(**kwargs)
async def test_edit_story_all_args(self, offline_bot, monkeypatch):
story_id = 1234
content = InputStoryContentPhoto(photo=data_file("telegram.jpg").read_bytes())
caption = "test caption"
caption_entities = [
MessageEntity(MessageEntity.BOLD, 0, 3),
MessageEntity(MessageEntity.ITALIC, 5, 11),
]
parse_mode = "Markdown"
areas = [StoryAreaTypeLink("http_url"), StoryAreaTypeUniqueGift("unique_gift_name")]
json_story = Story(chat=Chat(123, "private"), id=123).to_json()
async def do_request_and_make_assertions(*args, **kwargs):
request_data = kwargs.get("request_data")
params = kwargs.get("request_data").parameters
assert params.get("business_connection_id") == self.bci
assert params.get("story_id") == story_id
assert params.get("caption") == caption
assert params.get("caption_entities") == [e.to_dict() for e in caption_entities]
assert params.get("parse_mode") == parse_mode
assert params.get("areas") == [area.to_dict() for area in areas]
assert (content_dict := params.get("content")).get(
"type"
) == InputStoryContentType.PHOTO
assert (photo_attach := content_dict["photo"]).startswith("attach://")
assert isinstance(
request_data.multipart_data.get(photo_attach.removeprefix("attach://")), tuple
)
return 200, f'{{"ok": true, "result": {json_story}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", do_request_and_make_assertions)
obj = await offline_bot.edit_story(
business_connection_id=self.bci,
story_id=story_id,
content=content,
caption=caption,
caption_entities=caption_entities,
parse_mode=parse_mode,
areas=areas,
)
assert isinstance(obj, Story)
async def test_edit_story_local_file(self, offline_bot, monkeypatch):
json_story = Story(chat=Chat(123, "private"), id=123).to_json()
async def make_assertion(*args, **kwargs):
request_data = kwargs.get("request_data")
params = request_data.parameters
assert params.get("business_connection_id") == self.bci
assert (content_dict := params.get("content")).get(
"type"
) == InputStoryContentType.PHOTO
assert content_dict["photo"] == data_file("telegram.jpg").as_uri()
assert not request_data.multipart_data
return 200, f'{{"ok": true, "result": {json_story}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"story_id": 1234,
"content": InputStoryContentPhoto(
photo=data_file("telegram.jpg"),
),
}
assert await offline_bot.edit_story(**kwargs)
@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_edit_story_default_parse_mode(
self, default_bot, monkeypatch, passed_value, expected_value
):
async def make_assertion(url, request_data, *args, **kwargs):
assert request_data.parameters.get("parse_mode") == expected_value
return Story(chat=Chat(123, "private"), id=123).to_dict()
monkeypatch.setattr(default_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"story_id": 1234,
"content": InputStoryContentPhoto(photo=data_file("telegram.jpg").read_bytes()),
"caption": "caption",
}
if passed_value is not DEFAULT_NONE:
kwargs["parse_mode"] = passed_value
await default_bot.edit_story(**kwargs)
async def test_delete_story(self, offline_bot, monkeypatch):
story_id = 123
async def make_assertion(*args, **kwargs):
data = kwargs.get("request_data").parameters
assert data.get("business_connection_id") == self.bci
assert data.get("story_id") == story_id
return True
monkeypatch.setattr(offline_bot.request, "post", make_assertion)
assert await offline_bot.delete_story(business_connection_id=self.bci, story_id=story_id)
async def test_send_checklist_all_args(self, offline_bot, monkeypatch):
chat_id = 123
checklist = InputChecklist(
title="My Checklist",
tasks=[InputChecklistTask(1, "Task 1"), InputChecklistTask(2, "Task 2")],
)
disable_notification = True
protect_content = False
message_effect_id = 42
reply_parameters = ReplyParameters(23, chat_id, allow_sending_without_reply=True)
reply_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton(text="test", callback_data="test2")]]
)
json_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="test").to_json()
async def make_assertions(*args, **kwargs):
params = kwargs.get("request_data").parameters
assert params.get("business_connection_id") == self.bci
assert params.get("chat_id") == chat_id
assert params.get("checklist") == checklist.to_dict()
assert params.get("disable_notification") is disable_notification
assert params.get("protect_content") is protect_content
assert params.get("message_effect_id") == message_effect_id
assert params.get("reply_parameters") == reply_parameters.to_dict()
assert params.get("reply_markup") == reply_markup.to_dict()
return 200, f'{{"ok": true, "result": {json_message}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", make_assertions)
obj = await offline_bot.send_checklist(
business_connection_id=self.bci,
chat_id=chat_id,
checklist=checklist,
disable_notification=disable_notification,
protect_content=protect_content,
message_effect_id=message_effect_id,
reply_parameters=reply_parameters,
reply_markup=reply_markup,
)
assert isinstance(obj, Message)
@pytest.mark.parametrize("default_bot", [{"disable_notification": True}], indirect=True)
@pytest.mark.parametrize(
("passed_value", "expected_value"),
[(DEFAULT_NONE, True), (False, False), (None, None)],
)
async def test_send_checklist_default_disable_notification(
self, default_bot, monkeypatch, passed_value, expected_value
):
async def make_assertion(url, request_data, *args, **kwargs):
assert request_data.parameters.get("disable_notification") is expected_value
return Message(1, dtm.datetime.now(), Chat(1, ""), text="test").to_dict()
monkeypatch.setattr(default_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"chat_id": 123,
"checklist": InputChecklist(
title="My Checklist",
tasks=[InputChecklistTask(1, "Task 1")],
),
}
if passed_value is not DEFAULT_NONE:
kwargs["disable_notification"] = passed_value
await default_bot.send_checklist(**kwargs)
@pytest.mark.parametrize("default_bot", [{"protect_content": True}], indirect=True)
@pytest.mark.parametrize(
("passed_value", "expected_value"),
[(DEFAULT_NONE, True), (False, False), (None, None)],
)
async def test_send_checklist_default_protect_content(
self, default_bot, monkeypatch, passed_value, expected_value
):
async def make_assertion(url, request_data, *args, **kwargs):
assert request_data.parameters.get("protect_content") is expected_value
return Message(1, dtm.datetime.now(), Chat(1, ""), text="test").to_dict()
monkeypatch.setattr(default_bot.request, "post", make_assertion)
kwargs = {
"business_connection_id": self.bci,
"chat_id": 123,
"checklist": InputChecklist(
title="My Checklist",
tasks=[InputChecklistTask(1, "Task 1")],
),
}
if passed_value is not DEFAULT_NONE:
kwargs["protect_content"] = passed_value
await default_bot.send_checklist(**kwargs)
async def test_send_checklist_mutually_exclusive_reply_parameters(self, offline_bot):
"""Test that reply_to_message_id and allow_sending_without_reply are mutually exclusive
with reply_parameters."""
with pytest.raises(ValueError, match="`reply_to_message_id` and"):
await offline_bot.send_checklist(
self.bci,
123,
InputChecklist(title="My Checklist", tasks=[InputChecklistTask(1, "Task 1")]),
reply_to_message_id=1,
reply_parameters=True,
)
with pytest.raises(ValueError, match="`allow_sending_without_reply` and"):
await offline_bot.send_checklist(
self.bci,
123,
InputChecklist(title="My Checklist", tasks=[InputChecklistTask(1, "Task 1")]),
allow_sending_without_reply=True,
reply_parameters=True,
)
async def test_edit_message_checklist_all_args(self, offline_bot, monkeypatch):
chat_id = 123
message_id = 45
checklist = InputChecklist(
title="My Checklist",
tasks=[InputChecklistTask(1, "Task 1"), InputChecklistTask(2, "Task 2")],
)
reply_markup = InlineKeyboardMarkup(
[[InlineKeyboardButton(text="test", callback_data="test2")]]
)
json_message = Message(1, dtm.datetime.now(), Chat(1, ""), text="test").to_json()
async def make_assertions(*args, **kwargs):
params = kwargs.get("request_data").parameters
assert params.get("business_connection_id") == self.bci
assert params.get("chat_id") == chat_id
assert params.get("message_id") == message_id
assert params.get("checklist") == checklist.to_dict()
assert params.get("reply_markup") == reply_markup.to_dict()
return 200, f'{{"ok": true, "result": {json_message}}}'.encode()
monkeypatch.setattr(offline_bot.request, "do_request", make_assertions)
obj = await offline_bot.edit_message_checklist(
business_connection_id=self.bci,
chat_id=chat_id,
message_id=message_id,
checklist=checklist,
reply_markup=reply_markup,
)
assert isinstance(obj, Message)
|