117 lines
3.9 KiB
Python
117 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
from unittest.mock import patch
|
|
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
import notify_failure
|
|
|
|
|
|
class FakeResponse:
|
|
status = 200
|
|
|
|
def __init__(self, payload: dict[str, object]) -> None:
|
|
self.payload = payload
|
|
|
|
def __enter__(self) -> "FakeResponse":
|
|
return self
|
|
|
|
def __exit__(self, *_args: object) -> None:
|
|
return None
|
|
|
|
def read(self) -> bytes:
|
|
return json.dumps(self.payload).encode("utf-8")
|
|
|
|
def getcode(self) -> int:
|
|
return self.status
|
|
|
|
|
|
WEBHOOK = "https://oapi.dingtalk.com/robot/send?access_token=test-token"
|
|
|
|
|
|
class NotifyFailureTest(unittest.TestCase):
|
|
def test_message_contains_keyword_and_redacts_sensitive_values(self) -> None:
|
|
_title, text = notify_failure.notification_message(
|
|
"2026-11-27",
|
|
"auth",
|
|
notify_failure.safe_reason(
|
|
"登录失败 finance@example.com https://example.test/private"
|
|
),
|
|
test=False,
|
|
)
|
|
|
|
self.assertIn("招商银行", text)
|
|
self.assertIn("[邮箱已隐藏]", text)
|
|
self.assertIn("[地址已隐藏]", text)
|
|
self.assertNotIn("finance@example.com", text)
|
|
self.assertNotIn("example.test", text)
|
|
|
|
def test_sends_markdown_with_the_custom_keyword(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
webhook_file = Path(temporary) / "webhook"
|
|
webhook_file.write_text(WEBHOOK + "\n", encoding="utf-8")
|
|
webhook = notify_failure.load_webhook(webhook_file)
|
|
|
|
with patch(
|
|
"notify_failure.urllib.request.urlopen",
|
|
return_value=FakeResponse({"errcode": 0, "errmsg": "ok"}),
|
|
) as urlopen:
|
|
notify_failure.send_message(
|
|
webhook,
|
|
"招商银行邮件任务失败:通知通道测试",
|
|
"### 招商银行邮件任务失败:通知通道测试",
|
|
)
|
|
|
|
request = urlopen.call_args.args[0]
|
|
payload = json.loads(request.data.decode("utf-8"))
|
|
self.assertEqual(request.full_url, WEBHOOK)
|
|
self.assertEqual(payload["msgtype"], "markdown")
|
|
self.assertIn("招商银行", payload["markdown"]["text"])
|
|
|
|
def test_channel_test_contains_both_supported_keywords(self) -> None:
|
|
title, text = notify_failure.notification_message(
|
|
"2026-11-27",
|
|
"unknown",
|
|
"",
|
|
test=True,
|
|
)
|
|
|
|
self.assertIn("招商银行", title)
|
|
self.assertIn("招商银行邮件任务失败", title)
|
|
self.assertIn("招商银行邮件任务失败", text)
|
|
|
|
def test_rejects_non_dingtalk_webhook(self) -> None:
|
|
with tempfile.TemporaryDirectory() as temporary:
|
|
webhook_file = Path(temporary) / "webhook"
|
|
webhook_file.write_text(
|
|
"https://example.test/robot/send?access_token=test-token",
|
|
encoding="utf-8",
|
|
)
|
|
with self.assertRaisesRegex(
|
|
notify_failure.NotificationError,
|
|
"钉钉群机器人地址无效",
|
|
):
|
|
notify_failure.load_webhook(webhook_file)
|
|
|
|
def test_reports_dingtalk_error_without_returning_the_webhook(self) -> None:
|
|
with patch(
|
|
"notify_failure.urllib.request.urlopen",
|
|
return_value=FakeResponse({"errcode": 310000, "errmsg": "keywords not in content"}),
|
|
):
|
|
with self.assertRaises(notify_failure.NotificationError) as raised:
|
|
notify_failure.send_message(WEBHOOK, "招商银行邮件任务失败", "test")
|
|
|
|
self.assertIn("errcode 310000", str(raised.exception))
|
|
self.assertNotIn("access_token", str(raised.exception))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|