148 lines
6.4 KiB
Python
148 lines
6.4 KiB
Python
"""The plugin-catalog actions against a local catalog site: the URL setting's
|
|
rules, the trimmed catalog, the copy kept for when the site is down, and the
|
|
art action's path and content checks. Stdlib only::
|
|
|
|
python3 -m unittest discover -s plugins/plugin-catalog/tests
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import http.server
|
|
import json
|
|
import os
|
|
import pathlib
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import threading
|
|
import unittest
|
|
|
|
HERE = pathlib.Path(__file__).resolve().parents[1]
|
|
HANDLERS = HERE / "handlers"
|
|
sys.path.insert(0, str(HANDLERS))
|
|
import _lib # noqa: E402
|
|
|
|
CATALOG = {
|
|
"schema": 1, "generatedAt": "2026-11-28T00:00:00Z", "repo": "https://git.example.com/p.git",
|
|
"plugins": [
|
|
{"id": "charts", "name": "Charts", "version": "1.0.0", "category": "data", "color": "#515c8e",
|
|
"iconSvg": "art/charts/icon.svg", "illustration": "art/charts/illustration.webp",
|
|
"source": "https://git.example.com/p.git#plugins/charts", "requires": ["ask-form", "../x"],
|
|
"readme": "Hi", "capabilities": ["notify.send"], "secretToken": "must not pass"},
|
|
{"id": "../evil", "name": "Evil"},
|
|
{"id": "bad-art", "name": "Bad", "iconSvg": "../../etc/passwd", "illustration": "https://x/y.png"},
|
|
],
|
|
}
|
|
|
|
|
|
class Site:
|
|
def __init__(self, root: pathlib.Path):
|
|
handler = lambda *a, **k: http.server.SimpleHTTPRequestHandler(*a, directory=str(root), **k) # noqa: E731
|
|
http.server.SimpleHTTPRequestHandler.log_message = lambda *a: None
|
|
self.srv = http.server.ThreadingHTTPServer(("127.0.0.1", 0), handler)
|
|
self.url = f"http://127.0.0.1:{self.srv.server_address[1]}/catalog.json"
|
|
threading.Thread(target=self.srv.serve_forever, daemon=True).start()
|
|
|
|
def stop(self):
|
|
self.srv.shutdown()
|
|
self.srv.server_close()
|
|
|
|
|
|
def run(script: str, payload: dict, url: str, data: pathlib.Path) -> dict:
|
|
env = {**os.environ, "AIA_PLUGIN_CONFIG": json.dumps({"catalogUrl": url}), "AIA_PLUGIN_DATA_DIR": str(data)}
|
|
p = subprocess.run([sys.executable, "-I", str(HANDLERS / script)], input=json.dumps(payload), env=env,
|
|
capture_output=True, text=True, timeout=30)
|
|
assert p.returncode == 0, p.stderr
|
|
return json.loads(p.stdout)
|
|
|
|
|
|
class Url(unittest.TestCase):
|
|
def test_rules(self):
|
|
self.assertEqual(_lib.catalog_url("https://ai-agent.plugins.gabvdl.xyz/catalog.json"),
|
|
"https://ai-agent.plugins.gabvdl.xyz/catalog.json")
|
|
self.assertEqual(_lib.catalog_url("http://127.0.0.1:8000/catalog.json?x=1#y"), "http://127.0.0.1:8000/catalog.json")
|
|
for bad in ("http://example.com/catalog.json", "https://u:p@example.com/catalog.json",
|
|
"https://example.com/", "file:///etc/catalog.json", "ftp://h/catalog.json", "catalog.json"):
|
|
with self.assertRaises(_lib.Refused, msg=bad):
|
|
_lib.catalog_url(bad)
|
|
|
|
|
|
class Actions(unittest.TestCase):
|
|
def setUp(self):
|
|
self.td = tempfile.TemporaryDirectory()
|
|
root = pathlib.Path(self.td.name)
|
|
self.site_dir = root / "site"
|
|
(self.site_dir / "art" / "charts").mkdir(parents=True)
|
|
(self.site_dir / "catalog.json").write_text(json.dumps(CATALOG), encoding="utf-8")
|
|
(self.site_dir / "art" / "charts" / "icon.svg").write_text('<svg xmlns="http://www.w3.org/2000/svg"/>', encoding="utf-8")
|
|
(self.site_dir / "art" / "charts" / "bad.svg").write_text('<svg onload="x()"/>', encoding="utf-8")
|
|
(self.site_dir / "art" / "charts" / "illustration.webp").write_bytes(b"RIFF0000WEBP")
|
|
self.data = root / "data"
|
|
self.site = Site(self.site_dir)
|
|
|
|
def tearDown(self):
|
|
self.site.stop()
|
|
self.td.cleanup()
|
|
|
|
def test_catalog_is_trimmed(self):
|
|
d = run("catalog.py", {}, self.site.url, self.data)
|
|
self.assertFalse(d["stale"])
|
|
self.assertEqual(d["siteUrl"], self.site.url.rsplit("/", 1)[0] + "/")
|
|
ids = [p["id"] for p in d["catalog"]["plugins"]]
|
|
self.assertEqual(ids, ["charts", "bad-art"])
|
|
charts = d["catalog"]["plugins"][0]
|
|
self.assertNotIn("secretToken", charts)
|
|
self.assertEqual(charts["requires"], ["ask-form"])
|
|
bad = d["catalog"]["plugins"][1]
|
|
self.assertIsNone(bad["iconSvg"])
|
|
self.assertIsNone(bad["illustration"])
|
|
|
|
def test_the_last_copy_when_the_site_is_down(self):
|
|
run("catalog.py", {}, self.site.url, self.data)
|
|
self.site.stop()
|
|
d = run("catalog.py", {}, self.site.url, self.data)
|
|
self.assertTrue(d["stale"])
|
|
self.assertTrue(d["error"])
|
|
self.assertEqual(d["catalog"]["plugins"][0]["id"], "charts")
|
|
self.site = Site(self.site_dir) # tearDown stops it
|
|
|
|
def test_down_with_no_copy(self):
|
|
self.site.stop()
|
|
d = run("catalog.py", {}, self.site.url, self.data)
|
|
self.assertIsNone(d["catalog"])
|
|
self.assertTrue(d["error"])
|
|
self.site = Site(self.site_dir)
|
|
|
|
def test_not_a_catalog(self):
|
|
(self.site_dir / "catalog.json").write_text('{"hello": 1}', encoding="utf-8")
|
|
d = run("catalog.py", {}, self.site.url, self.data)
|
|
self.assertIsNone(d["catalog"])
|
|
self.assertIn("isn't a plugin catalog", d["error"])
|
|
|
|
def test_a_bad_setting_is_refused(self):
|
|
d = run("catalog.py", {}, "http://example.com/catalog.json", self.data)
|
|
self.assertEqual(d["_error"]["kind"], "bad_url")
|
|
|
|
def test_art(self):
|
|
d = run("art.py", {"path": "art/charts/icon.svg"}, self.site.url, self.data)
|
|
self.assertTrue(d["dataUrl"].startswith("data:image/svg+xml;base64,"))
|
|
d = run("art.py", {"path": "art/charts/illustration.webp"}, self.site.url, self.data)
|
|
self.assertTrue(d["dataUrl"].startswith("data:image/webp;base64,"))
|
|
# kept: served again with the site down
|
|
self.site.stop()
|
|
d = run("art.py", {"path": "art/charts/icon.svg"}, self.site.url, self.data)
|
|
self.assertIn("dataUrl", d)
|
|
self.site = Site(self.site_dir)
|
|
|
|
def test_art_refusals(self):
|
|
for path in ("art/../catalog.json", "../art/charts/icon.svg", "art/charts/x.html", "catalog.json",
|
|
"art/charts/../../x.svg", "art/Charts!/x.svg", None):
|
|
d = run("art.py", {"path": path}, self.site.url, self.data)
|
|
self.assertEqual(d.get("_error", {}).get("kind"), "bad_path", path)
|
|
d = run("art.py", {"path": "art/charts/bad.svg"}, self.site.url, self.data)
|
|
self.assertEqual(d["_error"]["kind"], "bad_art")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|