test_slack.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """
  2. Test case for the slack utils module
  3. """
  4. import logging
  5. import salt.utils.slack as slack
  6. from tests.support.mixins import LoaderModuleMockMixin
  7. from tests.support.mock import MagicMock, patch
  8. from tests.support.unit import TestCase
  9. log = logging.getLogger(__name__)
  10. class TestSlackUtils(LoaderModuleMockMixin, TestCase):
  11. """
  12. Test case for the slack utils module
  13. """
  14. def setup_loader_modules(self):
  15. return {
  16. slack: {
  17. "__opts__": {
  18. "vault": {
  19. "url": "http://127.0.0.1",
  20. "auth": {
  21. "token": "test",
  22. "method": "token",
  23. "uses": 15,
  24. "ttl": 500,
  25. },
  26. },
  27. },
  28. }
  29. }
  30. def test_query(self):
  31. """
  32. Test case for the query function in the slack utils module
  33. """
  34. function = "message"
  35. api_key = "xoxp-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxx"
  36. args = None
  37. method = "POST"
  38. header_dict = {"Content-Type": "application/x-www-form-urlen coded"}
  39. data = "channel=%23general&username=Slack+User&as_user=Slack+User&text=%60%60%60id%3A+minion%0D%0Afunction%3A+test.ping%0D%0Afunction+args%3A+%5B%5D%0D%0Ajid%3A+20201017004822956482%0D%0Areturn%3A+true%0A%0D%0A%60%60%60"
  40. opts = None
  41. mock_result = {
  42. "body": '{"ok": false, "error": "token_revoked"}',
  43. "status": 200,
  44. "dict": {"ok": False, "error": "token_revoked"},
  45. }
  46. mock = MagicMock(return_value=mock_result)
  47. with patch("salt.utils.http.query", mock):
  48. expected = {"message": "token_revoked", "res": False}
  49. ret = slack.query(function, api_key, args, method, header_dict, data, opts)
  50. self.assertEqual(ret, expected)
  51. mock_result = {
  52. "status": 0,
  53. "error": "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)",
  54. }
  55. mock = MagicMock(return_value=mock_result)
  56. with patch("salt.utils.http.query", mock):
  57. expected = {"message": "invalid_auth", "res": False}
  58. ret = slack.query(function, api_key, args, method, header_dict, data, opts)
  59. self.assertEqual(ret, expected)
  60. mock_result = {"status": 0, "dict": {}}
  61. mock = MagicMock(return_value=mock_result)
  62. with patch("salt.utils.http.query", mock):
  63. expected = {"message": "Unknown response", "res": False}
  64. ret = slack.query(function, api_key, args, method, header_dict, data, opts)
  65. self.assertEqual(ret, expected)