/tests/integration/test_master_message_destination.py

https://github.com/blueset/efb-telegram-master · Python · 147 lines · 103 code · 27 blank · 17 comment · 6 complexity · 52c6cbc764b116e366c466c2a5ccfb22 MD5 · raw file

  1. """
  2. Test message destinations
  3. Only testing with text messages, as everything else shall follow suit.
  4. - Singly linked chat is tested in test_master_messages.py
  5. - Chat head is tested in test_chat_head.py
  6. """
  7. import asyncio
  8. import time
  9. from contextlib import suppress
  10. from typing import List
  11. from unittest.mock import patch, MagicMock
  12. from pytest import mark, raises
  13. from telethon.tl.custom import Message, MessageButton
  14. from .helper.filters import in_chats, has_button, edited, regex, text
  15. pytestmark = mark.asyncio
  16. async def test_master_master_quick_reply_no_cache(helper, client, bot_id, slave, channel):
  17. assert channel.chat_dest_cache.enabled
  18. channel.chat_dest_cache.weak.clear()
  19. channel.chat_dest_cache.strong.clear()
  20. slave.clear_messages()
  21. await client.send_message(bot_id,
  22. "test_master_master_quick_reply_no_cache this shall not be sent due to empty cache")
  23. await helper.wait_for_message()
  24. assert slave.messages.empty()
  25. async def test_master_master_quick_reply(helper, client, bot_id, slave, channel):
  26. """Tests if the quick reply cache exists, and changes afterwards by
  27. incoming message from slave channel.
  28. """
  29. assert channel.chat_dest_cache.enabled
  30. assert channel.flag("send_to_last_chat") == "warn"
  31. chat = slave.chat_with_alias
  32. content = "test_master_master_quick_reply set cache with chat head"
  33. # Send a message to ``chat`` via chat head
  34. await client.send_message(bot_id, f"/chat {chat.uid}")
  35. message = await helper.wait_for_message(in_chats(bot_id) & has_button)
  36. await message.click(0)
  37. message = await helper.wait_for_message(in_chats(bot_id) & edited(message.id) & ~has_button)
  38. await message.reply(content)
  39. message = slave.messages.get(timeout=5)
  40. slave.messages.task_done()
  41. assert message.text == content
  42. assert message.chat == chat
  43. content = "test_master_master_quick_reply send new message with quick reply"
  44. await client.send_message(bot_id, content)
  45. text = await helper.wait_for_message_text(in_chats(bot_id))
  46. assert chat.display_name in text, f"{text!r} is not a warning message for {chat}"
  47. message = slave.messages.get(timeout=5)
  48. slave.messages.task_done()
  49. assert message.text == content
  50. assert message.chat == chat
  51. content = "test_master_master_quick_reply send another new message " \
  52. "with quick reply, should give no warning"
  53. await client.send_message(bot_id, content)
  54. message = slave.messages.get(timeout=5)
  55. slave.messages.task_done()
  56. assert message.text == content
  57. assert message.chat == chat
  58. # Error message shall not appear again
  59. with raises(asyncio.TimeoutError):
  60. await helper.wait_for_message_text(in_chats(bot_id) & regex(chat.display_name), timeout=3)
  61. # Clear destination with new message from slave channel
  62. chat_alt = slave.chat_without_alias
  63. message = slave.send_text_message(chat_alt, author=chat_alt.other)
  64. text = await helper.wait_for_message_text(in_chats(bot_id))
  65. assert message.text in text # there might be message header in ``text``
  66. content = "test_master_master_quick_reply this shall not be sent due to cleared cache"
  67. await client.send_message(bot_id, content)
  68. message = await helper.wait_for_message(in_chats(bot_id)) # Error message
  69. assert slave.messages.empty()
  70. await cancel_destination_suggestion(helper, message)
  71. async def test_master_master_quick_reply_cache_expiry(helper, client, bot_id, slave, channel):
  72. assert channel.chat_dest_cache.enabled
  73. slave.clear_messages()
  74. chat = slave.chat_with_alias
  75. content = "test_master_master_quick_reply_cache_expiry set cache with chat head"
  76. # slave.send_text_message(chat, author=chat)
  77. # Send a message to ``chat`` via chat head
  78. await client.send_message(bot_id, f"/chat {chat.uid}")
  79. message = await helper.wait_for_message(in_chats(bot_id) & has_button)
  80. await message.click(0)
  81. message = await helper.wait_for_message(in_chats(bot_id) & edited(message.id) & ~has_button)
  82. await message.reply(content)
  83. slave.messages.get(timeout=5)
  84. slave.messages.task_done()
  85. time_now = time.time()
  86. with patch("time.time", MagicMock(return_value=time_now + 24 * 60 * 60)): # one day later
  87. content = "test_master_master_quick_reply_cache_expiry this shall not be sent due to expired cache"
  88. await client.send_message(bot_id, content)
  89. message = await helper.wait_for_message(in_chats(bot_id) & text) # Error message
  90. assert slave.messages.empty()
  91. await cancel_destination_suggestion(helper, message)
  92. async def test_master_master_destination_suggestion(helper, client, bot_id, slave, channel):
  93. with patch.dict(channel.flag.config, send_to_last_chat="disabled"), \
  94. patch.multiple(channel.chat_dest_cache, enabled=False):
  95. assert not channel.chat_dest_cache.enabled
  96. slave.clear_messages()
  97. chat = slave.chat_with_alias
  98. slave.send_text_message(chat, author=chat.other)
  99. await helper.wait_for_message_text(in_chats(bot_id) & regex(chat.display_name))
  100. content = "test_master_master_destination_suggestion this shall be replied with a list of candidates"
  101. sent_message: Message = await client.send_message(bot_id, content)
  102. message: Message = await helper.wait_for_message(in_chats(bot_id) & has_button)
  103. buttons: List[List[MessageButton]] = message.buttons
  104. first_button: MessageButton = buttons[0][0]
  105. assert chat.display_name in first_button.text # The message from previous chat should come first in the list
  106. # await buttons[-1][0].click() # Cancel the error message.
  107. await first_button.click() # deliver the message
  108. slave.clear_messages()
  109. content = "test_master_master_destination_suggestion edited message shall be delivered without a prompt"
  110. await sent_message.edit(text=content)
  111. slave_message = slave.messages.get(timeout=5)
  112. assert slave_message.text == content
  113. async def cancel_destination_suggestion(helper, message: Message):
  114. """Cancel chat destination suggestions if available."""
  115. with suppress(asyncio.TimeoutError):
  116. while not message.button_count:
  117. message = await helper.wait_for_message(in_chats(message.chat_id))
  118. if message.button_count:
  119. await message.buttons[-1][-1].click()