test_slack.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, print_function, unicode_literals
  8. # Import Salt Libs
  9. import salt.states.slack as slack
  10. # Import Salt Testing Libs
  11. from tests.support.mixins import LoaderModuleMockMixin
  12. from tests.support.mock import MagicMock, patch
  13. from tests.support.unit import TestCase
  14. class SlackTestCase(TestCase, LoaderModuleMockMixin):
  15. """
  16. Test cases for salt.states.slack
  17. """
  18. def setup_loader_modules(self):
  19. return {slack: {}}
  20. # 'post_message' function tests: 1
  21. def test_post_message_apikey(self):
  22. """
  23. Test to send a message to a Slack channel using an API Key.
  24. """
  25. name = "slack-message"
  26. channel = "#general"
  27. from_name = "SuperAdmin"
  28. message = "This state was executed successfully."
  29. api_key = "xoxp-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXX"
  30. ret = {"name": name, "changes": {}, "result": None, "comment": ""}
  31. with patch.dict(slack.__opts__, {"test": True}):
  32. comt = "The following message is to be sent to Slack: {0}".format(message)
  33. ret.update({"comment": comt})
  34. self.assertDictEqual(
  35. slack.post_message(
  36. name,
  37. channel=channel,
  38. from_name=from_name,
  39. message=message,
  40. api_key=api_key,
  41. ),
  42. ret,
  43. )
  44. with patch.dict(slack.__opts__, {"test": False}):
  45. comt = "Please specify api_key or webhook."
  46. ret.update({"comment": comt, "result": False})
  47. self.assertDictEqual(
  48. slack.post_message(
  49. name,
  50. channel=None,
  51. from_name=from_name,
  52. message=message,
  53. api_key=None,
  54. ),
  55. ret,
  56. )
  57. comt = "Slack channel is missing."
  58. ret.update({"comment": comt, "result": False})
  59. self.assertDictEqual(
  60. slack.post_message(
  61. name,
  62. channel=None,
  63. from_name=from_name,
  64. message=message,
  65. api_key=api_key,
  66. ),
  67. ret,
  68. )
  69. comt = "Slack from name is missing."
  70. ret.update({"comment": comt, "result": False})
  71. self.assertDictEqual(
  72. slack.post_message(
  73. name,
  74. channel=channel,
  75. from_name=None,
  76. message=message,
  77. api_key=api_key,
  78. ),
  79. ret,
  80. )
  81. comt = "Slack message is missing."
  82. ret.update({"comment": comt, "result": False})
  83. self.assertDictEqual(
  84. slack.post_message(
  85. name,
  86. channel=channel,
  87. from_name=from_name,
  88. message=None,
  89. api_key=api_key,
  90. ),
  91. ret,
  92. )
  93. mock = MagicMock(return_value=True)
  94. with patch.dict(slack.__salt__, {"slack.post_message": mock}):
  95. comt = "Sent message: slack-message"
  96. ret.update({"comment": comt, "result": True})
  97. self.assertDictEqual(
  98. slack.post_message(
  99. name,
  100. channel=channel,
  101. from_name=from_name,
  102. message=message,
  103. api_key=api_key,
  104. ),
  105. ret,
  106. )
  107. def test_post_message_webhook(self):
  108. """
  109. Test to send a message to a Slack channel using an webhook.
  110. """
  111. name = "slack-message"
  112. channel = "#general"
  113. username = "SuperAdmin"
  114. message = "This state was executed successfully."
  115. webhook = "XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
  116. api_key = "xoxp-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXXXX"
  117. ret = {"name": name, "changes": {}, "result": None, "comment": ""}
  118. with patch.dict(slack.__opts__, {"test": True}):
  119. comt = "The following message is to be sent to Slack: {0}".format(message)
  120. ret.update({"comment": comt})
  121. self.assertDictEqual(
  122. slack.post_message(
  123. name,
  124. channel=channel,
  125. username=username,
  126. message=message,
  127. webhook=webhook,
  128. ),
  129. ret,
  130. )
  131. with patch.dict(slack.__opts__, {"test": False}):
  132. comt = "Please specify api_key or webhook."
  133. ret.update({"comment": comt, "result": False})
  134. self.assertDictEqual(
  135. slack.post_message(
  136. name, channel=channel, username=username, message=None, webhook=None
  137. ),
  138. ret,
  139. )
  140. comt = "Please specify only either api_key or webhook."
  141. ret.update({"comment": comt, "result": False})
  142. self.assertDictEqual(
  143. slack.post_message(
  144. name,
  145. channel=channel,
  146. username=username,
  147. message=message,
  148. api_key=api_key,
  149. webhook=webhook,
  150. ),
  151. ret,
  152. )
  153. comt = "Slack message is missing."
  154. ret.update({"comment": comt, "result": False})
  155. self.assertDictEqual(
  156. slack.post_message(
  157. name,
  158. channel=channel,
  159. username=username,
  160. message=None,
  161. webhook=webhook,
  162. ),
  163. ret,
  164. )
  165. mock = MagicMock(return_value=True)
  166. with patch.dict(slack.__salt__, {"slack.call_hook": mock}):
  167. comt = "Sent message: slack-message"
  168. ret.update({"comment": comt, "result": True})
  169. self.assertDictEqual(
  170. slack.post_message(
  171. name,
  172. channel=channel,
  173. username=username,
  174. message=message,
  175. webhook=webhook,
  176. ),
  177. ret,
  178. )