Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/modules/test_slack.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import json
8 from unittest.mock import Mock, patch
9
10 import pytest
11 from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import (
12 AnsibleExitJson,
13 AnsibleFailJson,
14 ModuleTestCase,
15 set_module_args,
16 )
17
18 from ansible_collections.community.general.plugins.modules import slack
19
20
21 class TestSlackModule(ModuleTestCase):
22 def setUp(self):
23 super().setUp()
24 self.module = slack
25
26 def tearDown(self):
27 super().tearDown()
28
29 @pytest.fixture
30 def fetch_url_mock(self, mocker):
31 return mocker.patch("ansible.module_utils.notification.slack.fetch_url")
32
33 def test_without_required_parameters(self):
34 """Failure must occurs when all parameters are missing"""
35 with self.assertRaises(AnsibleFailJson):
36 with set_module_args({}):
37 self.module.main()
38
39 def test_invalid_old_token(self):
40 """Failure if there is an old style token"""
41 with set_module_args(
42 {
43 "token": "test",
44 }
45 ):
46 with self.assertRaises(AnsibleFailJson):
47 self.module.main()
48
49 def test_successful_message(self):
50 """tests sending a message. This is example 1 from the docs"""
51 with set_module_args({"token": "XXXX/YYYY/ZZZZ", "msg": "test"}):
52 with patch.object(slack, "fetch_url") as fetch_url_mock:
53 fetch_url_mock.return_value = (None, {"status": 200})
54 with self.assertRaises(AnsibleExitJson):
55 self.module.main()
56
57 self.assertTrue(fetch_url_mock.call_count, 1)
58 call_data = json.loads(fetch_url_mock.call_args[1]["data"])
59 assert call_data["username"] == "Ansible"
60 assert call_data["text"] == "test"
61 assert fetch_url_mock.call_args[1]["url"] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
62
63 def test_failed_message(self):
64 """tests failing to send a message"""
65
66 with set_module_args({"token": "XXXX/YYYY/ZZZZ", "msg": "test"}):
67 with patch.object(slack, "fetch_url") as fetch_url_mock:
68 fetch_url_mock.return_value = (None, {"status": 404, "msg": "test"})
69 with self.assertRaises(AnsibleFailJson):
70 self.module.main()
71
72 def test_message_with_thread(self):
73 """tests sending a message with a thread"""
74 with set_module_args({"token": "XXXX/YYYY/ZZZZ", "msg": "test", "thread_id": "100.00"}):
75 with patch.object(slack, "fetch_url") as fetch_url_mock:
76 fetch_url_mock.return_value = (None, {"status": 200})
77 with self.assertRaises(AnsibleExitJson):
78 self.module.main()
79
80 self.assertTrue(fetch_url_mock.call_count, 1)
81 call_data = json.loads(fetch_url_mock.call_args[1]["data"])
82 assert call_data["username"] == "Ansible"
83 assert call_data["text"] == "test"
84 assert call_data["thread_ts"] == "100.00"
85 assert fetch_url_mock.call_args[1]["url"] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
86
87 # https://github.com/ansible-collections/community.general/issues/1097
88 def test_ts_in_message_does_not_cause_edit(self):
89 with set_module_args({"token": "xoxa-123456789abcdef", "msg": "test with ts"}):
90 with patch.object(slack, "fetch_url") as fetch_url_mock:
91 mock_response = Mock()
92 mock_response.read.return_value = '{"fake":"data"}'
93 fetch_url_mock.return_value = (mock_response, {"status": 200})
94 with self.assertRaises(AnsibleExitJson):
95 self.module.main()
96
97 self.assertTrue(fetch_url_mock.call_count, 1)
98 self.assertEqual(fetch_url_mock.call_args[1]["url"], "https://slack.com/api/chat.postMessage")
99
100 def test_govslack_message(self):
101 with set_module_args({"token": "xoxa-123456789abcdef", "domain": "slack-gov.com", "msg": "test with ts"}):
102 with patch.object(slack, "fetch_url") as fetch_url_mock:
103 mock_response = Mock()
104 mock_response.read.return_value = '{"fake":"data"}'
105 fetch_url_mock.return_value = (mock_response, {"status": 200})
106 with self.assertRaises(AnsibleExitJson):
107 self.module.main()
108
109 self.assertTrue(fetch_url_mock.call_count, 1)
110 self.assertEqual(fetch_url_mock.call_args[1]["url"], "https://slack-gov.com/api/chat.postMessage")
111
112 def test_edit_message(self):
113 with set_module_args({"token": "xoxa-123456789abcdef", "msg": "test2", "message_id": "12345"}):
114 with patch.object(slack, "fetch_url") as fetch_url_mock:
115 mock_response = Mock()
116 mock_response.read.return_value = '{"messages":[{"ts":"12345","msg":"test1"}]}'
117 fetch_url_mock.side_effect = [
118 (mock_response, {"status": 200}),
119 (mock_response, {"status": 200}),
120 ]
121 with self.assertRaises(AnsibleExitJson):
122 self.module.main()
123
124 self.assertTrue(fetch_url_mock.call_count, 2)
125 self.assertEqual(fetch_url_mock.call_args[1]["url"], "https://slack.com/api/chat.update")
126 call_data = json.loads(fetch_url_mock.call_args[1]["data"])
127 self.assertEqual(call_data["ts"], "12345")
128
129 def test_message_with_blocks(self):
130 """tests sending a message with blocks"""
131 with set_module_args(
132 {
133 "token": "XXXX/YYYY/ZZZZ",
134 "msg": "test",
135 "blocks": [
136 {
137 "type": "section",
138 "text": {"type": "mrkdwn", "text": "*test*"},
139 "accessory": {
140 "type": "image",
141 "image_url": "https://docs.ansible.com/favicon.ico",
142 "alt_text": "test",
143 },
144 },
145 {"type": "section", "text": {"type": "plain_text", "text": "test", "emoji": True}},
146 ],
147 }
148 ):
149 with patch.object(slack, "fetch_url") as fetch_url_mock:
150 fetch_url_mock.return_value = (None, {"status": 200})
151 with self.assertRaises(AnsibleExitJson):
152 self.module.main()
153
154 self.assertTrue(fetch_url_mock.call_count, 1)
155 call_data = json.loads(fetch_url_mock.call_args[1]["data"])
156 assert call_data["username"] == "Ansible"
157 assert call_data["blocks"][1]["text"]["text"] == "test"
158 assert fetch_url_mock.call_args[1]["url"] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
159
160 def test_message_with_invalid_color(self):
161 """tests sending invalid color value to module"""
162 with set_module_args(
163 {
164 "token": "XXXX/YYYY/ZZZZ",
165 "msg": "test",
166 "color": "aa",
167 }
168 ):
169 with self.assertRaises(AnsibleFailJson) as exec_info:
170 self.module.main()
171
172 msg = (
173 "Color value specified should be either one of"
174 " ['normal', 'good', 'warning', 'danger'] or any valid"
175 " hex value with length 3 or 6."
176 )
177 assert exec_info.exception.args[0]["msg"] == msg
178
179
180 color_test = [
181 ("#111111", True),
182 ("#00aabb", True),
183 ("#abc", True),
184 ("#gghhjj", False),
185 ("#ghj", False),
186 ("#a", False),
187 ("#aaaaaaaa", False),
188 ("", False),
189 ("aaaa", False),
190 ("$00aabb", False),
191 ("$00a", False),
192 ]
193
194
195 @pytest.mark.parametrize("color_value, ret_status", color_test)
196 def test_is_valid_hex_color(color_value, ret_status):
197 generated_value = slack.is_valid_hex_color(color_value)
198 assert generated_value == ret_status