move conversion of data to json in slack API handling (#1101) (#1120)

* move conversion of data to json in slack API handling

at one point in do_notify_slack, we do operations on the payload
variable assuming it's a dict, but it's not: it's a json encoded string.

it's useful to operate on the payload as a dict rather than a string, so
solve this problem by moving the jsonify call to right before sending
the payload to the slack API.

fixes #1097

* add changelog fragment

* Update changelogs/fragments/1101-slack-ts-fix.yaml

Co-authored-by: Felix Fontein <felix@fontein.de>

* return payload as a json encoded string for backwards compatibility

Co-authored-by: Felix Fontein <felix@fontein.de>
(cherry picked from commit b5b5410575)

Co-authored-by: Andreas Lutro <anlutro@gmail.com>
This commit is contained in:
patchback[bot] 2020-10-19 11:33:03 +03:00 committed by GitHub
parent 3c6e84b21c
commit e3e3682eb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 5 deletions

View file

@ -5,7 +5,7 @@ __metaclass__ = type
import json
import pytest
from ansible_collections.community.general.tests.unit.compat.mock import patch
from ansible_collections.community.general.tests.unit.compat.mock import Mock, patch
from ansible_collections.community.general.plugins.modules.notification import slack
from ansible_collections.community.general.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase, set_module_args
@ -88,6 +88,45 @@ class TestSlackModule(ModuleTestCase):
assert call_data['thread_ts'] == '100.00'
assert fetch_url_mock.call_args[1]['url'] == "https://hooks.slack.com/services/XXXX/YYYY/ZZZZ"
# https://github.com/ansible-collections/community.general/issues/1097
def test_ts_in_message_does_not_cause_edit(self):
set_module_args({
'token': 'xoxa-123456789abcdef',
'msg': 'test with ts'
})
with patch.object(slack, "fetch_url") as fetch_url_mock:
mock_response = Mock()
mock_response.read.return_value = '{"fake":"data"}'
fetch_url_mock.return_value = (mock_response, {"status": 200})
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 1)
self.assertEquals(fetch_url_mock.call_args[1]['url'], "https://slack.com/api/chat.postMessage")
def test_edit_message(self):
set_module_args({
'token': 'xoxa-123456789abcdef',
'msg': 'test2',
'message_id': '12345'
})
with patch.object(slack, "fetch_url") as fetch_url_mock:
mock_response = Mock()
mock_response.read.return_value = '{"messages":[{"ts":"12345","msg":"test1"}]}'
fetch_url_mock.side_effect = [
(mock_response, {"status": 200}),
(mock_response, {"status": 200}),
]
with self.assertRaises(AnsibleExitJson):
self.module.main()
self.assertTrue(fetch_url_mock.call_count, 2)
self.assertEquals(fetch_url_mock.call_args[1]['url'], "https://slack.com/api/chat.update")
call_data = json.loads(fetch_url_mock.call_args[1]['data'])
self.assertEquals(call_data['ts'], "12345")
def test_message_with_blocks(self):
"""tests sending a message with blocks"""
set_module_args({