1
0

test_telegram_bot_msg.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import datetime
  5. import logging
  6. # Salt libs
  7. from salt.beacons import telegram_bot_msg
  8. # Salt testing libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.mock import MagicMock, patch
  11. from tests.support.unit import TestCase, skipIf
  12. # Third-party libs
  13. try:
  14. import telegram
  15. HAS_TELEGRAM = True
  16. except ImportError:
  17. HAS_TELEGRAM = False
  18. log = logging.getLogger(__name__)
  19. @skipIf(not HAS_TELEGRAM, "telegram is not available")
  20. class TelegramBotMsgBeaconTestCase(TestCase, LoaderModuleMockMixin):
  21. """
  22. Test case for salt.beacons.telegram_bot
  23. """
  24. def setup_loader_modules(self):
  25. return {telegram_bot_msg: {}}
  26. def test_validate_empty_config(self, *args, **kwargs):
  27. ret = telegram_bot_msg.validate(None)
  28. self.assertEqual(
  29. ret, (False, ("Configuration for telegram_bot_msg beacon must be a list.")),
  30. )
  31. def test_validate_missing_accept_from_config(self, *args, **kwargs):
  32. ret = telegram_bot_msg.validate([{"token": "bcd"}])
  33. self.assertEqual(
  34. ret,
  35. (False, ("Not all required configuration for telegram_bot_msg are set."),),
  36. )
  37. def test_validate_missing_token_config(self, *args, **kwargs):
  38. ret = telegram_bot_msg.validate([{"accept_from": []}])
  39. self.assertEqual(
  40. ret,
  41. (False, ("Not all required configuration for telegram_bot_msg are set."),),
  42. )
  43. def test_validate_config_not_list_in_accept_from(self, *args, **kwargs):
  44. ret = telegram_bot_msg.validate(
  45. [{"token": "bcd", "accept_from": {"nodict": "1"}}]
  46. )
  47. self.assertEqual(
  48. ret,
  49. (
  50. False,
  51. (
  52. "Configuration for telegram_bot_msg, "
  53. "accept_from must be a list of "
  54. "usernames."
  55. ),
  56. ),
  57. )
  58. def test_validate_valid_config(self, *args, **kwargs):
  59. ret = telegram_bot_msg.validate([{"token": "bcd", "accept_from": ["username"]}])
  60. self.assertEqual(ret, (True, "Valid beacon configuration."))
  61. def test_call_no_updates(self):
  62. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  63. token = "abc"
  64. config = [{"token": token, "accept_from": ["tester"]}]
  65. inst = MagicMock(name="telegram.Bot()")
  66. telegram_api.Bot = MagicMock(name="telegram", return_value=inst)
  67. inst.get_updates.return_value = []
  68. ret = telegram_bot_msg.beacon(config)
  69. self.assertEqual(ret, (True, "Valid beacon configuration"))
  70. telegram_api.Bot.assert_called_once_with(token)
  71. self.assertEqual(ret, [])
  72. def test_call_telegram_return_no_updates_for_user(self):
  73. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  74. token = "abc"
  75. username = "tester"
  76. config = [{"token": token, "accept_from": [username]}]
  77. inst = MagicMock(name="telegram.Bot()")
  78. telegram_api.Bot = MagicMock(name="telegram", return_value=inst)
  79. log.debug("telegram {}".format(telegram))
  80. username = "different_user"
  81. user = telegram.user.User(id=1, first_name="", username=username)
  82. chat = telegram.chat.Chat(1, "private", username=username)
  83. date = datetime.datetime(2016, 12, 18, 0, 0)
  84. message = telegram.message.Message(1, user, date=date, chat=chat)
  85. update = telegram.update.Update(update_id=1, message=message)
  86. inst.get_updates.return_value = [update]
  87. ret = telegram_bot_msg.beacon(config)
  88. self.assertEqual(ret, (True, "Valid beacon configuration"))
  89. telegram_api.Bot.assert_called_once_with(token)
  90. self.assertEqual(ret, [])
  91. def test_call_telegram_returning_updates(self):
  92. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  93. token = "abc"
  94. username = "tester"
  95. config = [{"token": token, "accept_from": [username]}]
  96. inst = MagicMock(name="telegram.Bot()")
  97. telegram_api.Bot = MagicMock(name="telegram", return_value=inst)
  98. user = telegram.User(id=1, first_name="", username=username)
  99. chat = telegram.Chat(1, "private", username=username)
  100. date = datetime.datetime(2016, 12, 18, 0, 0)
  101. message = telegram.Message(1, user, date=date, chat=chat)
  102. update = telegram.update.Update(update_id=1, message=message)
  103. inst.get_updates.return_value = [update]
  104. ret = telegram_bot_msg.beacon(config)
  105. self.assertEqual(ret, (True, "Valid beacon configuration"))
  106. telegram_api.Bot.assert_called_once_with(token)
  107. self.assertTrue(ret)
  108. self.assertEqual(ret[0]["msgs"][0], message.to_dict())