1
0

test_telegram_bot_msg.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # coding: utf-8
  2. # Python libs
  3. from __future__ import absolute_import
  4. import datetime
  5. # Salt libs
  6. from salt.beacons import telegram_bot_msg
  7. # Salt testing libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import MagicMock, patch
  11. # Third-party libs
  12. try:
  13. import telegram
  14. HAS_TELEGRAM = True
  15. except ImportError:
  16. HAS_TELEGRAM = False
  17. import logging
  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(ret, (False, ('Configuration for telegram_bot_msg '
  29. 'beacon must be a list.')))
  30. def test_validate_missing_accept_from_config(self, *args, **kwargs):
  31. ret = telegram_bot_msg.validate([{
  32. 'token': 'bcd'
  33. }])
  34. self.assertEqual(ret, (False, ('Not all required configuration for '
  35. 'telegram_bot_msg are set.')))
  36. def test_validate_missing_token_config(self, *args, **kwargs):
  37. ret = telegram_bot_msg.validate([{
  38. 'accept_from': []
  39. }])
  40. self.assertEqual(ret, (False, ('Not all required configuration for '
  41. 'telegram_bot_msg are set.')))
  42. def test_validate_config_not_list_in_accept_from(self, *args, **kwargs):
  43. ret = telegram_bot_msg.validate([{
  44. 'token': 'bcd',
  45. 'accept_from': {'nodict': "1"}
  46. }])
  47. self.assertEqual(ret, (False, ('Configuration for telegram_bot_msg, '
  48. 'accept_from must be a list of '
  49. 'usernames.')))
  50. def test_validate_valid_config(self, *args, **kwargs):
  51. ret = telegram_bot_msg.validate([{
  52. 'token': 'bcd',
  53. 'accept_from': [
  54. 'username'
  55. ]
  56. }])
  57. self.assertEqual(ret, (True, 'Valid beacon configuration.'))
  58. def test_call_no_updates(self):
  59. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  60. token = 'abc'
  61. config = [{
  62. 'token': token,
  63. 'accept_from': ['tester']
  64. }]
  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 = [{
  77. 'token': token,
  78. 'accept_from': [username]
  79. }]
  80. inst = MagicMock(name='telegram.Bot()')
  81. telegram_api.Bot = MagicMock(name='telegram', return_value=inst)
  82. log.debug('telegram {}'.format(telegram))
  83. username = 'different_user'
  84. user = telegram.user.User(id=1, first_name='', username=username)
  85. chat = telegram.chat.Chat(1, 'private', username=username)
  86. date = datetime.datetime(2016, 12, 18, 0, 0)
  87. message = telegram.message.Message(1, user, date=date, chat=chat)
  88. update = telegram.update.Update(update_id=1, message=message)
  89. inst.get_updates.return_value = [update]
  90. ret = telegram_bot_msg.beacon(config)
  91. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  92. telegram_api.Bot.assert_called_once_with(token)
  93. self.assertEqual(ret, [])
  94. def test_call_telegram_returning_updates(self):
  95. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  96. token = 'abc'
  97. username = 'tester'
  98. config = [{
  99. 'token': token,
  100. 'accept_from': [username]
  101. }]
  102. inst = MagicMock(name='telegram.Bot()')
  103. telegram_api.Bot = MagicMock(name='telegram', return_value=inst)
  104. user = telegram.User(id=1, first_name='', username=username)
  105. chat = telegram.Chat(1, 'private', username=username)
  106. date = datetime.datetime(2016, 12, 18, 0, 0)
  107. message = telegram.Message(1, user, date=date, chat=chat)
  108. update = telegram.update.Update(update_id=1, message=message)
  109. inst.get_updates.return_value = [update]
  110. ret = telegram_bot_msg.beacon(config)
  111. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  112. telegram_api.Bot.assert_called_once_with(token)
  113. self.assertTrue(ret)
  114. self.assertEqual(ret[0]['msgs'][0], message.to_dict())