Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion redash/destinations/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ def notify(self, alert, query, user, new_state, app, host, metadata, options):
payload["channel"] = options.get("channel")

try:
resp = requests.post(options.get("url"), data=json_dumps(payload), timeout=5.0)
resp = requests.post(
options.get("url"),
data=json_dumps(payload).encode("utf-8"),
timeout=5.0,
)
logging.warning(resp.text)

if resp.status_code != 200:
Expand Down
39 changes: 39 additions & 0 deletions tests/destinations/test_mattermost.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import json
from unittest import mock

from redash.destinations.mattermost import Mattermost


def test_mattermost_notify_sends_utf8_payload():
alert = mock.Mock()
alert.custom_subject = (
"Test Subject With Unicode: "
"\u041f\u0440\u0438\u0432\u0435\u0442"
)
alert.custom_body = None
alert.name = "Test Alert"

query = mock.Mock()
user = mock.Mock()
app = mock.Mock()
host = "http://redash.local"
metadata = {}
options = {"url": "https://example.com/mattermost"}
destination = Mattermost(options)

with mock.patch("redash.destinations.mattermost.requests.post") as mock_post:
mock_response = mock.Mock()
mock_response.status_code = 200
mock_post.return_value = mock_response

destination.notify(
alert, query, user, "triggered", app, host, metadata, options
)

sent_data = mock_post.call_args.kwargs["data"]

assert isinstance(sent_data, bytes)

decoded_data = json.loads(sent_data.decode("utf-8"))
assert decoded_data["text"] == alert.custom_subject
assert alert.custom_subject in sent_data.decode("utf-8")
Loading