test_slack.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Jayesh Kariya <jayeshk@saltstack.com>
  4. :codeauthor: Gareth J. Greenaway <gareth@saltstack.com>
  5. '''
  6. # Import Python libs
  7. from __future__ import absolute_import, unicode_literals, print_function
  8. # Import Salt Testing Libs
  9. from tests.support.mixins import LoaderModuleMockMixin
  10. from tests.support.unit import TestCase
  11. from tests.support.mock import (
  12. MagicMock,
  13. patch
  14. )
  15. # Import Salt Libs
  16. import salt.states.slack as slack
  17. class SlackTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test cases for salt.states.slack
  20. '''
  21. def setup_loader_modules(self):
  22. return {slack: {}}
  23. # 'post_message' function tests: 1
  24. def test_post_message_apikey(self):
  25. '''
  26. Test to send a message to a Slack channel using an API Key.
  27. '''
  28. name = 'slack-message'
  29. channel = '#general'
  30. from_name = 'SuperAdmin'
  31. message = 'This state was executed successfully.'
  32. api_key = 'xoxp-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXX'
  33. ret = {'name': name,
  34. 'changes': {},
  35. 'result': None,
  36. 'comment': ''}
  37. with patch.dict(slack.__opts__, {'test': True}):
  38. comt = 'The following message is to be sent to Slack: {0}'.format(message)
  39. ret.update({'comment': comt})
  40. self.assertDictEqual(slack.post_message(name,
  41. channel=channel,
  42. from_name=from_name,
  43. message=message,
  44. api_key=api_key), ret)
  45. with patch.dict(slack.__opts__, {'test': False}):
  46. comt = 'Please specify api_key or webhook.'
  47. ret.update({'comment': comt, 'result': False})
  48. self.assertDictEqual(slack.post_message(name,
  49. channel=None,
  50. from_name=from_name,
  51. message=message,
  52. api_key=None), ret)
  53. comt = 'Slack channel is missing.'
  54. ret.update({'comment': comt, 'result': False})
  55. self.assertDictEqual(slack.post_message(name,
  56. channel=None,
  57. from_name=from_name,
  58. message=message,
  59. api_key=api_key), ret)
  60. comt = 'Slack from name is missing.'
  61. ret.update({'comment': comt, 'result': False})
  62. self.assertDictEqual(slack.post_message(name,
  63. channel=channel,
  64. from_name=None,
  65. message=message,
  66. api_key=api_key), ret)
  67. comt = 'Slack message is missing.'
  68. ret.update({'comment': comt, 'result': False})
  69. self.assertDictEqual(slack.post_message(name,
  70. channel=channel,
  71. from_name=from_name,
  72. message=None,
  73. api_key=api_key), ret)
  74. mock = MagicMock(return_value=True)
  75. with patch.dict(slack.__salt__, {'slack.post_message': mock}):
  76. comt = 'Sent message: slack-message'
  77. ret.update({'comment': comt, 'result': True})
  78. self.assertDictEqual(slack.post_message(name,
  79. channel=channel,
  80. from_name=from_name,
  81. message=message,
  82. api_key=api_key),
  83. ret)
  84. def test_post_message_webhook(self):
  85. '''
  86. Test to send a message to a Slack channel using an webhook.
  87. '''
  88. name = 'slack-message'
  89. channel = '#general'
  90. username = 'SuperAdmin'
  91. message = 'This state was executed successfully.'
  92. webhook = 'XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
  93. api_key = 'xoxp-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXX'
  94. ret = {'name': name,
  95. 'changes': {},
  96. 'result': None,
  97. 'comment': ''}
  98. with patch.dict(slack.__opts__, {'test': True}):
  99. comt = 'The following message is to be sent to Slack: {0}'.format(message)
  100. ret.update({'comment': comt})
  101. self.assertDictEqual(slack.post_message(name,
  102. channel=channel,
  103. username=username,
  104. message=message,
  105. webhook=webhook), ret)
  106. with patch.dict(slack.__opts__, {'test': False}):
  107. comt = 'Please specify api_key or webhook.'
  108. ret.update({'comment': comt, 'result': False})
  109. self.assertDictEqual(slack.post_message(name,
  110. channel=channel,
  111. username=username,
  112. message=None,
  113. webhook=None), ret)
  114. comt = 'Please specify only either api_key or webhook.'
  115. ret.update({'comment': comt, 'result': False})
  116. self.assertDictEqual(slack.post_message(name,
  117. channel=channel,
  118. username=username,
  119. message=message,
  120. api_key=api_key,
  121. webhook=webhook), ret)
  122. comt = 'Slack message is missing.'
  123. ret.update({'comment': comt, 'result': False})
  124. self.assertDictEqual(slack.post_message(name,
  125. channel=channel,
  126. username=username,
  127. message=None,
  128. webhook=webhook), ret)
  129. mock = MagicMock(return_value=True)
  130. with patch.dict(slack.__salt__, {'slack.call_hook': mock}):
  131. comt = 'Sent message: slack-message'
  132. ret.update({'comment': comt, 'result': True})
  133. self.assertDictEqual(slack.post_message(name,
  134. channel=channel,
  135. username=username,
  136. message=message,
  137. webhook=webhook),
  138. ret)