1
0

test_telegram.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Tests for the Telegram execution module.
  4. :codeauthor: :email:`Roald Nefs (info@roaldnefs.com)`
  5. '''
  6. # Import Python Libs
  7. from __future__ import absolute_import
  8. import logging
  9. # Import Salt Testing Libs
  10. from tests.support.mixins import LoaderModuleMockMixin
  11. from tests.support.unit import TestCase
  12. from tests.support.mock import (
  13. Mock,
  14. MagicMock,
  15. )
  16. # Import Salt Libs
  17. import salt.modules.telegram as telegram
  18. log = logging.getLogger(__name__)
  19. class RequestMock(Mock):
  20. '''
  21. Request Mock
  22. '''
  23. def get(self, *args, **kwargs):
  24. return RequestResponseMock()
  25. def put(self, *args, **kwargs):
  26. self.args = args
  27. self.kwargs = kwargs
  28. return RequestPutResponseMock()
  29. def delete(self, *args, **kwargs):
  30. self.args = args
  31. self.kwargs = kwargs
  32. return RequestResponseMock()
  33. class RequestResponseMock(Mock):
  34. '''
  35. Request Response Mock
  36. '''
  37. def json(self):
  38. return [{'url': 'http://example.org',
  39. '_id': 1234}, ]
  40. class RequestPutResponseMock(Mock):
  41. '''
  42. Request Put Response Mock
  43. '''
  44. ok = True
  45. def json(self):
  46. return {'_id': 4321}
  47. class TelegramModuleTest(TestCase, LoaderModuleMockMixin):
  48. '''
  49. Test cases for salt.modules.telegram.
  50. '''
  51. def setup_loader_modules(self):
  52. module_globals = {
  53. '__salt__': {
  54. 'config.get': MagicMock(return_value={
  55. 'telegram': {
  56. 'chat_id': '123456789',
  57. 'token': '000000000:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
  58. }
  59. }),
  60. 'requests.put': Mock(),
  61. },
  62. 'requests': RequestMock()
  63. }
  64. return {telegram: module_globals}
  65. def test_post_message(self):
  66. '''
  67. Test the post_message function.
  68. '''
  69. message = 'Hello World!'
  70. self.assertTrue(telegram.post_message(message))