45 lines
1.5 KiB
Python
45 lines
1.5 KiB
Python
import importlib.util
|
||
import sys
|
||
import types
|
||
from pathlib import Path
|
||
|
||
|
||
def load_article_renderer(monkeypatch):
|
||
"""加载纯 HTML 构建逻辑,避免测试中启动 Chromium。"""
|
||
pyppeteer = types.ModuleType("pyppeteer")
|
||
pyppeteer.launch = None
|
||
monkeypatch.setitem(sys.modules, "pyppeteer", pyppeteer)
|
||
|
||
path = Path(__file__).resolve().parents[1] / "danding_bot" / "plugins" / "danding_qqpush" / "article_render.py"
|
||
spec = importlib.util.spec_from_file_location("danding_qqpush_article_render", path)
|
||
module = importlib.util.module_from_spec(spec)
|
||
spec.loader.exec_module(module)
|
||
return module
|
||
|
||
|
||
def test_article_document_preserves_layout_and_removes_script(monkeypatch):
|
||
module = load_article_renderer(monkeypatch)
|
||
|
||
document = module.build_article_document(
|
||
"<p>第一段</p><script>alert('xss')</script><p><strong>第二段</strong></p>",
|
||
"公告 <标题>",
|
||
"2026-07-30 20:27",
|
||
)
|
||
|
||
assert "<p>第一段</p>" in document
|
||
assert "<strong>第二段</strong>" in document
|
||
assert "<script>" not in document
|
||
assert "公告 <标题>" in document
|
||
assert "发布时间:2026-07-30 20:27" in document
|
||
|
||
|
||
def test_article_document_uses_safe_text_fallback_without_bleach(monkeypatch):
|
||
module = load_article_renderer(monkeypatch)
|
||
monkeypatch.setattr(module, "bleach", None)
|
||
|
||
document = module.build_article_document("<p>第一段</p><script>alert('xss')</script><p>第二段</p>", "标题", None)
|
||
|
||
assert "<script>" not in document
|
||
assert "alert" not in document
|
||
assert "第一段<br>第二段" in document
|