1
0

test_telegram_bot_msg.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 NO_MOCK, NO_MOCK_REASON, 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. @skipIf(NO_MOCK, NO_MOCK_REASON)
  21. class TelegramBotMsgBeaconTestCase(TestCase, LoaderModuleMockMixin):
  22. '''
  23. Test case for salt.beacons.telegram_bot
  24. '''
  25. def setup_loader_modules(self):
  26. return {telegram_bot_msg: {}}
  27. def test_validate_empty_config(self, *args, **kwargs):
  28. ret = telegram_bot_msg.validate(None)
  29. self.assertEqual(ret, (False, ('Configuration for telegram_bot_msg '
  30. 'beacon must be a list.')))
  31. def test_validate_missing_accept_from_config(self, *args, **kwargs):
  32. ret = telegram_bot_msg.validate([{
  33. 'token': 'bcd'
  34. }])
  35. self.assertEqual(ret, (False, ('Not all required configuration for '
  36. 'telegram_bot_msg are set.')))
  37. def test_validate_missing_token_config(self, *args, **kwargs):
  38. ret = telegram_bot_msg.validate([{
  39. 'accept_from': []
  40. }])
  41. self.assertEqual(ret, (False, ('Not all required configuration for '
  42. 'telegram_bot_msg are set.')))
  43. def test_validate_config_not_list_in_accept_from(self, *args, **kwargs):
  44. ret = telegram_bot_msg.validate([{
  45. 'token': 'bcd',
  46. 'accept_from': {'nodict': "1"}
  47. }])
  48. self.assertEqual(ret, (False, ('Configuration for telegram_bot_msg, '
  49. 'accept_from must be a list of '
  50. 'usernames.')))
  51. def test_validate_valid_config(self, *args, **kwargs):
  52. ret = telegram_bot_msg.validate([{
  53. 'token': 'bcd',
  54. 'accept_from': [
  55. 'username'
  56. ]
  57. }])
  58. self.assertEqual(ret, (True, 'Valid beacon configuration.'))
  59. def test_call_no_updates(self):
  60. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  61. token = 'abc'
  62. config = [{
  63. 'token': token,
  64. 'accept_from': ['tester']
  65. }]
  66. inst = MagicMock(name='telegram.Bot()')
  67. telegram_api.Bot = MagicMock(name='telegram', return_value=inst)
  68. inst.get_updates.return_value = []
  69. ret = telegram_bot_msg.beacon(config)
  70. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  71. telegram_api.Bot.assert_called_once_with(token)
  72. self.assertEqual(ret, [])
  73. def test_call_telegram_return_no_updates_for_user(self):
  74. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  75. token = 'abc'
  76. username = 'tester'
  77. config = [{
  78. 'token': token,
  79. 'accept_from': [username]
  80. }]
  81. inst = MagicMock(name='telegram.Bot()')
  82. telegram_api.Bot = MagicMock(name='telegram', return_value=inst)
  83. log.debug('telegram {}'.format(telegram))
  84. username = 'different_user'
  85. user = telegram.user.User(id=1, first_name='', username=username)
  86. chat = telegram.chat.Chat(1, 'private', username=username)
  87. date = datetime.datetime(2016, 12, 18, 0, 0)
  88. message = telegram.message.Message(1, user, date=date, chat=chat)
  89. update = telegram.update.Update(update_id=1, message=message)
  90. inst.get_updates.return_value = [update]
  91. ret = telegram_bot_msg.beacon(config)
  92. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  93. telegram_api.Bot.assert_called_once_with(token)
  94. self.assertEqual(ret, [])
  95. def test_call_telegram_returning_updates(self):
  96. with patch("salt.beacons.telegram_bot_msg.telegram") as telegram_api:
  97. token = 'abc'
  98. username = 'tester'
  99. config = [{
  100. 'token': token,
  101. 'accept_from': [username]
  102. }]
  103. inst = MagicMock(name='telegram.Bot()')
  104. telegram_api.Bot = MagicMock(name='telegram', return_value=inst)
  105. user = telegram.User(id=1, first_name='', username=username)
  106. chat = telegram.Chat(1, 'private', username=username)
  107. date = datetime.datetime(2016, 12, 18, 0, 0)
  108. message = telegram.Message(1, user, date=date, chat=chat)
  109. update = telegram.update.Update(update_id=1, message=message)
  110. inst.get_updates.return_value = [update]
  111. ret = telegram_bot_msg.beacon(config)
  112. self.assertEqual(ret, (True, 'Valid beacon configuration'))
  113. telegram_api.Bot.assert_called_once_with(token)
  114. self.assertTrue(ret)
  115. self.assertEqual(ret[0]['msgs'][0], message.to_dict())