40 lines
1.3 KiB
Python
40 lines
1.3 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from unittest.mock import MagicMock, patch
|
||
|
|
|
||
|
|
from relay.ntfy import notify, topic_url
|
||
|
|
|
||
|
|
|
||
|
|
def test_topic_url_builds_correctly() -> None:
|
||
|
|
assert topic_url("abc123") == "https://ntfy.sh/abc123"
|
||
|
|
|
||
|
|
|
||
|
|
def test_notify_returns_false_on_empty_topic() -> None:
|
||
|
|
assert notify("", "title", "msg") is False
|
||
|
|
|
||
|
|
|
||
|
|
def test_notify_posts_with_headers_and_body() -> None:
|
||
|
|
with patch("relay.ntfy.requests.post") as post:
|
||
|
|
post.return_value = MagicMock(status_code=200)
|
||
|
|
ok = notify("topic", "the title", "body text", priority="high", tags=["warning"])
|
||
|
|
assert ok is True
|
||
|
|
args, kwargs = post.call_args
|
||
|
|
assert args[0] == "https://ntfy.sh/topic"
|
||
|
|
assert kwargs["data"] == b"body text"
|
||
|
|
assert kwargs["headers"]["Title"] == "the title"
|
||
|
|
assert kwargs["headers"]["Priority"] == "high"
|
||
|
|
assert kwargs["headers"]["Tags"] == "warning"
|
||
|
|
|
||
|
|
|
||
|
|
def test_notify_returns_false_on_non_200() -> None:
|
||
|
|
with patch("relay.ntfy.requests.post") as post:
|
||
|
|
post.return_value = MagicMock(status_code=500, text="oops")
|
||
|
|
assert notify("topic", "t", "m") is False
|
||
|
|
|
||
|
|
|
||
|
|
def test_notify_returns_false_on_network_error() -> None:
|
||
|
|
import requests
|
||
|
|
|
||
|
|
with patch("relay.ntfy.requests.post", side_effect=requests.ConnectionError("down")):
|
||
|
|
assert notify("topic", "t", "m") is False
|