"Fossies" - the Fresh Open Source Software Archive 
Member "salt-3002.2/tests/unit/utils/test_slack.py" (18 Nov 2020, 2791 Bytes) of package /linux/misc/salt-3002.2.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
1 """
2 Test case for the slack utils module
3 """
4
5
6 import logging
7
8 import salt.utils.slack as slack
9 from tests.support.mixins import LoaderModuleMockMixin
10 from tests.support.mock import MagicMock, patch
11 from tests.support.unit import TestCase
12
13 log = logging.getLogger(__name__)
14
15
16 class TestSlackUtils(LoaderModuleMockMixin, TestCase):
17 """
18 Test case for the slack utils module
19 """
20
21 def setup_loader_modules(self):
22 return {
23 slack: {
24 "__opts__": {
25 "vault": {
26 "url": "http://127.0.0.1",
27 "auth": {
28 "token": "test",
29 "method": "token",
30 "uses": 15,
31 "ttl": 500,
32 },
33 },
34 },
35 }
36 }
37
38 def test_query(self):
39 """
40 Test case for the query function in the slack utils module
41 """
42
43 function = "message"
44 api_key = "xoxp-xxxxxxxxxx-xxxxxxxxxx-xxxxxxxxxx-xxxxxx"
45 args = None
46 method = "POST"
47 header_dict = {"Content-Type": "application/x-www-form-urlen coded"}
48 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"
49 opts = None
50
51 mock_result = {
52 "body": '{"ok": false, "error": "token_revoked"}',
53 "status": 200,
54 "dict": {"ok": False, "error": "token_revoked"},
55 }
56 mock = MagicMock(return_value=mock_result)
57 with patch("salt.utils.http.query", mock):
58 expected = {"message": "token_revoked", "res": False}
59 ret = slack.query(function, api_key, args, method, header_dict, data, opts)
60 self.assertEqual(ret, expected)
61
62 mock_result = {
63 "status": 0,
64 "error": "[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)",
65 }
66 mock = MagicMock(return_value=mock_result)
67 with patch("salt.utils.http.query", mock):
68 expected = {"message": "invalid_auth", "res": False}
69 ret = slack.query(function, api_key, args, method, header_dict, data, opts)
70 self.assertEqual(ret, expected)
71
72 mock_result = {"status": 0, "dict": {}}
73 mock = MagicMock(return_value=mock_result)
74 with patch("salt.utils.http.query", mock):
75 expected = {"message": "Unknown response", "res": False}
76 ret = slack.query(function, api_key, args, method, header_dict, data, opts)
77 self.assertEqual(ret, expected)