Implement modular publishing core with post and article extensions

This commit is contained in:
2026-12-03 03:43:38 +08:00
parent 01dc71788c
commit 483a523510
11 changed files with 364 additions and 0 deletions
+77
View File
@@ -0,0 +1,77 @@
from importlib import import_module
from io import BytesIO
import json
import sys
from pathlib import Path
from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import patch
from mica.api import create_app
from mica.core import discover
class ContentAPI(TestCase):
def setUp(self):
directory = TemporaryDirectory()
self.addCleanup(directory.cleanup)
self.database = Path(directory.name) / "content.sqlite3"
self.app = create_app(self.database, "writer")
def call(self, method, path, payload=None, token=None):
path, _, query = path.partition("?")
raw = json.dumps(payload).encode() if payload is not None else b""
env = {
"REQUEST_METHOD": method, "PATH_INFO": path, "QUERY_STRING": query,
"CONTENT_TYPE": "application/json", "CONTENT_LENGTH": str(len(raw)),
"wsgi.input": BytesIO(raw),
}
if token:
env["HTTP_AUTHORIZATION"] = "Bearer " + token
status = []
result = self.app(env, lambda line, headers: status.append(int(line.split()[0])))
return status[0], json.loads(b"".join(result))
def test_both_extensions_use_the_same_publication_flow(self):
for kind, fields in [("articles", {"title": "标题", "body": "文章"}),
("posts", {"body": "动态"})]:
status, draft = self.call("POST", "/content", {"type": kind, "fields": fields}, "writer")
self.assertEqual(status, 201)
path = "/content/" + draft["id"]
self.assertEqual(self.call("GET", path)[0], 404)
self.assertEqual(self.call("GET", "/content?type=" + kind), (200, []))
self.assertEqual(self.call("POST", path + "/publish")[0], 401)
status, published = self.call("POST", path + "/publish", token="writer")
self.assertEqual(status, 200)
self.assertTrue(published["published_at"])
self.assertEqual(published["fields"], fields)
self.app = create_app(self.database, "writer")
self.assertEqual(self.call("GET", path), (200, published))
self.assertEqual(self.call("POST", path + "/publish", token="writer"), (200, published))
self.assertEqual(self.call("GET", "/content?type=" + kind), (200, [published]))
self.assertEqual(len(self.call("GET", "/content?limit=1&offset=1")[1]), 1)
def test_invalid_requests(self):
self.assertEqual(self.call("POST", "/content", {}, "wrong")[0], 401)
for kind, fields in [("posts", {"body": " "}), ("posts", {"body": "a", "title": "b"}),
("articles", {"body": "a"}), ("unknown", {})]:
status, _ = self.call("POST", "/content", {"type": kind, "fields": fields}, "writer")
self.assertEqual(status, 400)
self.assertEqual(self.call("GET", "/content?limit=-1")[0], 400)
self.assertEqual(self.call("DELETE", "/content")[0], 405)
self.assertEqual(self.call("GET", "/content"), (200, []))
def test_extension_discovery(self):
with TemporaryDirectory() as path:
package_dir = Path(path) / "sample_extensions"
package_dir.mkdir()
(package_dir / "__init__.py").touch()
(package_dir / "_helper.py").write_text("")
definition = 'from mica.core import ContentType\ncontent_type = ContentType("notes", ("body",))'
(package_dir / "notes.py").write_text(definition)
with patch("sys.path", [path, *sys.path]):
package = import_module("sample_extensions")
self.assertEqual(set(discover(package)), {"notes"})
(package_dir / "other.py").write_text(definition)
with self.assertRaisesRegex(ValueError, "duplicate content type"):
discover(package)