Files
Auto_Bangumi/examples/plugins/apprise-notifier/tests/test_apprise_notifier.py
Estrella Pan f2d93993f4 fix(plugins): apprise 的 urls 改为单个字符串秘密字段,回退 secret_field 的 default_factory
宿主只按位置还原列表型秘密的掩码,删除首个 URL 会保留错误的值;
插件注册表 CI 用基线分支的 ab_sdk 校验,插件不能依赖同一 PR 中的 SDK 改动。
2026-10-21 11:28:07 +02:00

71 lines
2.2 KiB
Python

import asyncio
import json
import httpx2
import pytest
from apprise_notifier import AppriseNotifier, Options
from ab_sdk.notify import NotificationMessage, NotifierSettings
from ab_sdk.testing import NotifierContract, create_plugin
MESSAGE = NotificationMessage(kind="event", title="标题", body="正文")
def channel(status: int, options: dict, seen=None):
def handler(request: httpx2.Request) -> httpx2.Response:
if seen is not None:
seen.append(request)
return httpx2.Response(status)
plugin, _ = create_plugin(AppriseNotifier, options)
plugin.client_factory = lambda: httpx2.AsyncClient( # type: ignore[method-assign]
transport=httpx2.MockTransport(handler)
)
return plugin.channel()(NotifierSettings())
class TestChannel(NotifierContract):
def create(self):
return channel(200, {"config_key": "ab"})
def create_failing(self):
return channel(424, {"config_key": "ab"})
@pytest.mark.parametrize(
("options", "path", "payload"),
[
(
{"server": "http://ap:8000/", "urls": "tgram://t/1,\n mailto://u ,"},
"/notify/",
{"title": "标题", "body": "正文", "urls": "tgram://t/1,mailto://u"},
),
(
{"server": "http://ap:8000", "config_key": "ab"},
"/notify/ab",
{"title": "标题", "body": "正文"},
),
],
)
def test_send_posts_stateless_urls_or_config_key(options, path, payload):
seen: list[httpx2.Request] = []
assert asyncio.run(channel(200, options, seen).send(MESSAGE)) is True
[request] = seen
assert request.url.path == path
assert json.loads(request.content) == payload
def test_missing_urls_and_config_key_fails_without_request():
seen: list[httpx2.Request] = []
ch = channel(200, {}, seen)
assert asyncio.run(ch.send(MESSAGE)) is False
assert asyncio.run(ch.test()) == (False, "需填写 Apprise URL 或配置键")
assert seen == []
def test_urls_is_secret_in_config_schema():
# URL 里带着令牌:宿主的读配置接口按 schema 中的 secret 标记掩码
urls = Options.model_json_schema()["properties"]["urls"]
assert urls["secret"] is True
assert urls["type"] == "string"