mirror of
https://github.com/getredash/redash.git
synced 2025-12-19 17:37:19 -05:00
fix(destinations): Handle unicode characters in webhook notifications (#7586)
* fix(destinations): Handle unicode characters in webhook notifications Previously, webhook notifications would fail if they contained unicode characters in the alert data. This was because the JSON payload was not UTF-8 encoded before being sent. This commit fixes the issue by explicitly encoding the JSON data to UTF-8 and adds a test to verify the fix. * move test function to new file --------- Co-authored-by: gaojingyu <gaojingyu>
This commit is contained in:
@@ -42,7 +42,7 @@ class Webhook(BaseDestination):
|
||||
auth = HTTPBasicAuth(options.get("username"), options.get("password")) if options.get("username") else None
|
||||
resp = requests.post(
|
||||
options.get("url"),
|
||||
data=json_dumps(data),
|
||||
data=json_dumps(data).encode("utf-8"),
|
||||
auth=auth,
|
||||
headers=headers,
|
||||
timeout=5.0,
|
||||
|
||||
50
tests/destinations/test_webhook.py
Normal file
50
tests/destinations/test_webhook.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import json
|
||||
from unittest import mock
|
||||
|
||||
from redash.destinations.webhook import Webhook
|
||||
from redash.models import Alert
|
||||
|
||||
|
||||
def test_webhook_notify_handles_unicode():
|
||||
# Create a mock alert with all the properties needed by serialize_alert
|
||||
alert = mock.Mock()
|
||||
alert.id = 1
|
||||
alert.name = "Test Alert"
|
||||
alert.custom_subject = "Test Subject With Unicode: 晨"
|
||||
alert.custom_body = "Test Body"
|
||||
alert.options = {}
|
||||
alert.state = "ok"
|
||||
alert.last_triggered_at = None
|
||||
alert.updated_at = "2025-12-02T08:00:00Z"
|
||||
alert.created_at = "2025-12-02T08:00:00Z"
|
||||
alert.rearm = None
|
||||
alert.query_id = 10
|
||||
alert.user_id = 20
|
||||
|
||||
query = mock.Mock()
|
||||
user = mock.Mock()
|
||||
app = mock.Mock()
|
||||
host = "http://redash.local"
|
||||
options = {"url": "https://example.com/webhook", "username": "user", "password": "password"}
|
||||
metadata = {}
|
||||
new_state = Alert.TRIGGERED_STATE
|
||||
destination = Webhook(options)
|
||||
|
||||
with mock.patch("redash.destinations.webhook.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, new_state, app, host, metadata, options)
|
||||
|
||||
# Get the data passed to the mock
|
||||
call_args, call_kwargs = mock_post.call_args
|
||||
sent_data = call_kwargs["data"]
|
||||
|
||||
# 1. Make sure we send bytes
|
||||
assert isinstance(sent_data, bytes)
|
||||
|
||||
# 2. Make sure the bytes are the correct UTF-8 encoded JSON
|
||||
decoded_data = json.loads(sent_data.decode("utf-8"))
|
||||
assert decoded_data["alert"]["title"] == alert.custom_subject
|
||||
assert "Test Subject With Unicode: 晨" in sent_data.decode("utf-8")
|
||||
Reference in New Issue
Block a user