Skip to content

Commit 84cb8c8

Browse files
author
gaojingyu
committed
move test function to new file
1 parent 1dfdda7 commit 84cb8c8

File tree

2 files changed

+50
-45
lines changed

2 files changed

+50
-45
lines changed

tests/destinations/test_webhook.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import json
2+
from unittest import mock
3+
4+
from redash.destinations.webhook import Webhook
5+
from redash.models import Alert
6+
7+
8+
def test_webhook_notify_handles_unicode():
9+
# Create a mock alert with all the properties needed by serialize_alert
10+
alert = mock.Mock()
11+
alert.id = 1
12+
alert.name = "Test Alert"
13+
alert.custom_subject = "Test Subject With Unicode: 晨"
14+
alert.custom_body = "Test Body"
15+
alert.options = {}
16+
alert.state = "ok"
17+
alert.last_triggered_at = None
18+
alert.updated_at = "2025-12-02T08:00:00Z"
19+
alert.created_at = "2025-12-02T08:00:00Z"
20+
alert.rearm = None
21+
alert.query_id = 10
22+
alert.user_id = 20
23+
24+
query = mock.Mock()
25+
user = mock.Mock()
26+
app = mock.Mock()
27+
host = "http://redash.local"
28+
options = {"url": "https://example.com/webhook", "username": "user", "password": "password"}
29+
metadata = {}
30+
new_state = Alert.TRIGGERED_STATE
31+
destination = Webhook(options)
32+
33+
with mock.patch("redash.destinations.webhook.requests.post") as mock_post:
34+
mock_response = mock.Mock()
35+
mock_response.status_code = 200
36+
mock_post.return_value = mock_response
37+
38+
destination.notify(alert, query, user, new_state, app, host, metadata, options)
39+
40+
# Get the data passed to the mock
41+
call_args, call_kwargs = mock_post.call_args
42+
sent_data = call_kwargs["data"]
43+
44+
# 1. Make sure we send bytes
45+
assert isinstance(sent_data, bytes)
46+
47+
# 2. Make sure the bytes are the correct UTF-8 encoded JSON
48+
decoded_data = json.loads(sent_data.decode("utf-8"))
49+
assert decoded_data["alert"]["title"] == alert.custom_subject
50+
assert "Test Subject With Unicode: 晨" in sent_data.decode("utf-8")

tests/handlers/test_destinations.py

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -519,48 +519,3 @@ def test_datadog_notify_calls_requests_post():
519519
)
520520

521521
assert mock_response.status_code == 202
522-
523-
524-
def test_webhook_notify_handles_unicode():
525-
# Create a mock alert with all the properties needed by serialize_alert
526-
alert = mock.Mock()
527-
alert.id = 1
528-
alert.name = "Test Alert"
529-
alert.custom_subject = "Test Subject With Unicode: 晨"
530-
alert.custom_body = "Test Body"
531-
alert.options = {}
532-
alert.state = "ok"
533-
alert.last_triggered_at = None
534-
alert.updated_at = "2025-12-02T08:00:00Z"
535-
alert.created_at = "2025-12-02T08:00:00Z"
536-
alert.rearm = None
537-
alert.query_id = 10
538-
alert.user_id = 20
539-
540-
query = mock.Mock()
541-
user = mock.Mock()
542-
app = mock.Mock()
543-
host = "http://redash.local"
544-
options = {"url": "https://example.com/webhook", "username": "user", "password": "password"}
545-
metadata = {}
546-
new_state = Alert.TRIGGERED_STATE
547-
destination = Webhook(options)
548-
549-
with mock.patch("redash.destinations.webhook.requests.post") as mock_post:
550-
mock_response = mock.Mock()
551-
mock_response.status_code = 200
552-
mock_post.return_value = mock_response
553-
554-
destination.notify(alert, query, user, new_state, app, host, metadata, options)
555-
556-
# Get the data passed to the mock
557-
call_args, call_kwargs = mock_post.call_args
558-
sent_data = call_kwargs["data"]
559-
560-
# 1. Make sure we send bytes
561-
assert isinstance(sent_data, bytes)
562-
563-
# 2. Make sure the bytes are the correct UTF-8 encoded JSON
564-
decoded_data = json.loads(sent_data.decode("utf-8"))
565-
assert decoded_data["alert"]["title"] == alert.custom_subject
566-
assert "Test Subject With Unicode: 晨" in sent_data.decode("utf-8")

0 commit comments

Comments
 (0)