Files
romm/backend/tests/handler/notification_channels/test_webhook.py
zurdi 7ac95be747 feat: forward notifications to webhooks and email
Each user can add channels that forward their notifications: a webhook
posting RomM's JSON (optionally HMAC-signed), a Discord embed or an ntfy
message, or an email address confirmed with a code. A channel filters by
minimum level and by topic, and every stored notification that passes goes
out in its own RQ job, retried three times; ten failures in a row turn the
channel off and tell its owner.

Channel configs are sealed with a key derived from ROMM_AUTH_SECRET_KEY,
since a webhook URL carries its own token. Only an admin's webhooks may
reach private addresses; everyone else's go through the SSRF guard. Email
goes out over the SMTP_* settings, which the heartbeat reports as
NOTIFICATIONS.EMAIL_ENABLED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 16:30:33 +02:00

204 lines
7.0 KiB
Python

import hashlib
import hmac
import json
import httpx
import pytest
from handler.notification_channels import webhook
from handler.notification_channels.config import WebhookConfig
from handler.notification_channels.messages import OutboundMessage
from handler.notification_channels.webhook import (
WebhookError,
build_request,
check_url,
send,
split_ntfy_url,
)
from models.notification import NotificationLevel
from models.notification_channel import WebhookFormat
from .fixtures import make_actor, make_notification
def _message(**overrides) -> OutboundMessage:
return OutboundMessage(
notification=make_notification(**overrides),
title="Maintenance tonight",
body="RomM restarts at 23:00",
url="https://romm.example.com/platforms",
)
class TestJsonFormat:
def test_carries_the_notification_and_its_text(self):
request = build_request(
WebhookConfig(url="https://hooks.example.com/romm", format="json"),
_message(actor=make_actor()),
)
payload = json.loads(request.content)
assert request.url == "https://hooks.example.com/romm"
assert payload["event"] == "notification"
assert payload["title"] == "Maintenance tonight"
assert payload["url"] == "https://romm.example.com/platforms"
assert payload["actor"] == {"id": 1, "username": "admin"}
assert webhook.SIGNATURE_HEADER not in request.headers
def test_is_signed_with_the_secret(self):
request = build_request(
WebhookConfig(url="https://hooks.example.com", format="json", secret="k"),
_message(),
)
expected = hmac.new(b"k", request.content, hashlib.sha256).hexdigest()
assert request.headers[webhook.SIGNATURE_HEADER] == f"sha256={expected}"
class TestDiscordFormat:
def test_sends_an_embed_that_pings_nobody(self):
request = build_request(
WebhookConfig(url="https://discord.com/api/webhooks/1/t", format="discord"),
_message(level=NotificationLevel.ERROR, actor=make_actor()),
)
payload = json.loads(request.content)
[embed] = payload["embeds"]
assert embed["title"] == "Maintenance tonight"
assert embed["description"] == "RomM restarts at 23:00"
assert embed["url"] == "https://romm.example.com/platforms"
assert embed["color"] == webhook.DISCORD_COLORS[NotificationLevel.ERROR]
assert embed["footer"] == {"text": "From admin"}
assert payload["allowed_mentions"] == {"parse": []}
def test_keeps_within_discords_limits(self):
message = OutboundMessage(
notification=make_notification(),
title="t" * 300,
body="b" * 5000,
url=None,
)
[embed] = json.loads(
build_request(
WebhookConfig(
url="https://discord.com/api/webhooks/1/t", format="discord"
),
message,
).content
)["embeds"]
assert (len(embed["title"]), len(embed["description"])) == (256, 4096)
assert "url" not in embed
class TestNtfyFormat:
def test_publishes_json_to_the_server_with_the_topic(self):
request = build_request(
WebhookConfig(
url="https://ntfy.example.com/sub/romm", format="ntfy", secret="tk_1"
),
_message(level=NotificationLevel.ERROR),
)
payload = json.loads(request.content)
assert request.url == "https://ntfy.example.com/sub"
assert payload["topic"] == "romm"
assert payload["message"] == "RomM restarts at 23:00"
assert payload["priority"] == webhook.NTFY_ERROR_PRIORITY
assert payload["click"] == "https://romm.example.com/platforms"
assert request.headers["Authorization"] == "Bearer tk_1"
assert webhook.SIGNATURE_HEADER not in request.headers
@pytest.mark.parametrize(
"url,expected",
[
("https://ntfy.sh/romm", ("https://ntfy.sh/", "romm")),
("https://host/ntfy/romm/", ("https://host/ntfy", "romm")),
],
)
def test_splits_a_topic_url(self, url, expected):
assert split_ntfy_url(url) == expected
def test_a_url_without_a_topic_is_refused(self):
with pytest.raises(ValueError, match="topic"):
split_ntfy_url("https://ntfy.sh/")
class TestCheckUrl:
@pytest.mark.parametrize("url", ["ftp://example.com/x", "not a url", "https://"])
def test_refuses_anything_but_http(self, url):
with pytest.raises(ValueError):
check_url(url, WebhookFormat.JSON, allow_private=True)
def test_keeps_a_user_off_the_local_network(self):
with pytest.raises(ValueError):
check_url(
"http://192.168.1.10/hook", WebhookFormat.JSON, allow_private=False
)
def test_lets_an_admin_reach_the_local_network(self):
check_url("http://192.168.1.10/hook", WebhookFormat.JSON, allow_private=True)
class TestSend:
@pytest.fixture
def respond(self, mocker):
def install(status_code: int, text: str = ""):
calls: list[httpx.Request] = []
def handler(request: httpx.Request) -> httpx.Response:
calls.append(request)
return httpx.Response(status_code, text=text)
mocker.patch.object(
webhook,
"_client",
return_value=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
)
return calls
return install
async def test_posts_the_built_request(self, respond):
calls = respond(204)
await send(
WebhookConfig(url="https://hooks.example.com/romm", format="json"),
_message(),
allow_private=False,
)
[request] = calls
assert str(request.url) == "https://hooks.example.com/romm"
assert json.loads(request.content)["title"] == "Maintenance tonight"
async def test_a_refusal_says_who_refused_and_why(self, respond):
respond(404, '{"message": "Unknown Webhook"}')
with pytest.raises(WebhookError, match="discord.com answered 404: .*Unknown"):
await send(
WebhookConfig(
url="https://discord.com/api/webhooks/1/t", format="discord"
),
_message(),
allow_private=False,
)
async def test_an_unreachable_host_is_a_webhook_error(self, mocker):
def handler(request: httpx.Request) -> httpx.Response:
raise httpx.ConnectError("Connection refused")
mocker.patch.object(
webhook,
"_client",
return_value=httpx.AsyncClient(transport=httpx.MockTransport(handler)),
)
with pytest.raises(WebhookError, match="Could not reach hooks.example.com"):
await send(
WebhookConfig(url="https://hooks.example.com/romm", format="json"),
_message(),
allow_private=False,
)