diff --git a/redash/destinations/slack.py b/redash/destinations/slack.py index e4722fd446..a7e44b6a7c 100644 --- a/redash/destinations/slack.py +++ b/redash/destinations/slack.py @@ -26,13 +26,13 @@ def notify(self, alert, query, user, new_state, app, host, metadata, options): fields = [ { "title": "Query", + "type": "mrkdwn", "value": "{host}/queries/{query_id}".format(host=host, query_id=query.id), - "short": True, }, { "title": "Alert", + "type": "mrkdwn", "value": "{host}/alerts/{alert_id}".format(host=host, alert_id=alert.id), - "short": True, }, ] if alert.custom_body: diff --git a/tests/handlers/test_destinations.py b/tests/handlers/test_destinations.py index 9a7778829f..95fcffad09 100644 --- a/tests/handlers/test_destinations.py +++ b/tests/handlers/test_destinations.py @@ -5,6 +5,7 @@ from redash.destinations.asana import Asana from redash.destinations.datadog import Datadog from redash.destinations.discord import Discord +from redash.destinations.slack import Slack from redash.destinations.webex import Webex from redash.models import Alert, NotificationDestination from tests import BaseTestCase @@ -201,6 +202,59 @@ def test_asana_notify_calls_requests_post(): assert mock_response.status_code == 204 +def test_slack_notify_calls_requests_post(): + alert = mock.Mock(spec_set=["id", "name", "custom_subject", "custom_body", "render_template"]) + alert.id = 1 + alert.name = "Test Alert" + alert.custom_subject = "Test custom subject" + alert.custom_body = "Test custom body" + + alert.render_template = mock.Mock(return_value={"Rendered": "template"}) + query = mock.Mock() + query.id = 1 + + user = mock.Mock() + app = mock.Mock() + host = "https://localhost:5000" + options = {"url": "https://slack.com/api/api.test"} + metadata = {"Scheduled": False} + + new_state = Alert.TRIGGERED_STATE + destination = Slack(options) + + with mock.patch("redash.destinations.slack.requests.post") as mock_post: + mock_response = mock.Mock() + mock_response.status_code = 204 + mock_post.return_value = mock_response + + destination.notify(alert, query, user, new_state, app, host, metadata, options) + + query_link = f"{host}/queries/{query.id}" + alert_link = f"{host}/alerts/{alert.id}" + + expected_payload = { + "attachments": [ + { + "text": "Test custom subject", + "color": "#c0392b", + "fields": [ + {"title": "Query", "type": "mrkdwn", "value": query_link}, + {"title": "Alert", "type": "mrkdwn", "value": alert_link}, + {"title": "Description", "value": "Test custom body"}, + ], + } + ] + } + + mock_post.assert_called_once_with( + "https://slack.com/api/api.test", + data=json.dumps(expected_payload).encode(), + timeout=5.0, + ) + + assert mock_response.status_code == 204 + + def test_webex_notify_calls_requests_post(): alert = mock.Mock(spec_set=["id", "name", "custom_subject", "custom_body", "render_template"]) alert.id = 1