Add page editing and publish
This commit is contained in:
@@ -14,6 +14,7 @@ from helpers import SiteTestCase
|
||||
from test_auth import PASSWORD, Clock, make_cfg
|
||||
|
||||
from site_engine.services.auth import COOKIE_NAME, AuthService
|
||||
from site_engine.web.admin.editor import EditorHandlers
|
||||
from site_engine.web.admin.handlers import AdminHandlers
|
||||
|
||||
ORIGIN = {"host": "admin.example", "origin": "https://admin.example"}
|
||||
@@ -50,11 +51,22 @@ class FakeRouter:
|
||||
def post(self, path):
|
||||
return self._register("POST", path)
|
||||
|
||||
def api_route(self, path, methods):
|
||||
def deco(fn):
|
||||
for method in methods:
|
||||
self.routes[(method, self.prefix + path)] = fn
|
||||
return fn
|
||||
return deco
|
||||
|
||||
|
||||
class FakeRequest:
|
||||
def __init__(self, cookies=None, headers=None, ip="1.2.3.4"):
|
||||
def __init__(self, cookies=None, headers=None, ip="1.2.3.4", method="GET", query=None, form=None):
|
||||
self.cookies, self.headers = cookies or {}, headers or {}
|
||||
self.client = types.SimpleNamespace(host=ip)
|
||||
self.method, self.query_params, self._form = method, query or {}, form or []
|
||||
|
||||
async def form(self):
|
||||
return types.SimpleNamespace(multi_items=lambda: list(self._form))
|
||||
|
||||
|
||||
def fake_fastapi():
|
||||
@@ -64,14 +76,25 @@ def fake_fastapi():
|
||||
return mod
|
||||
|
||||
|
||||
def fake_starlette():
|
||||
pkg, conc = types.ModuleType("starlette"), types.ModuleType("starlette.concurrency")
|
||||
|
||||
async def run_in_threadpool(fn, *args):
|
||||
return fn(*args)
|
||||
|
||||
conc.run_in_threadpool = run_in_threadpool
|
||||
return {"starlette": pkg, "starlette.concurrency": conc}
|
||||
|
||||
|
||||
class RoutesWiringTest(SiteTestCase):
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self.auth = AuthService(make_cfg(), self.tmp / "state", clock=Clock())
|
||||
handlers = AdminHandlers(self.auth, self.site)
|
||||
with mock.patch.dict(sys.modules, {"fastapi": fake_fastapi()}):
|
||||
self.editor = EditorHandlers(self.auth, self.site, self.tmp / "dist")
|
||||
with mock.patch.dict(sys.modules, {"fastapi": fake_fastapi(), **fake_starlette()}):
|
||||
from site_engine.web.admin.routes import create_admin_router
|
||||
self.router = create_admin_router(handlers)
|
||||
self.router = create_admin_router(handlers, self.editor)
|
||||
|
||||
def route(self, method, path):
|
||||
return self.router.routes[(method, path)]
|
||||
@@ -79,8 +102,9 @@ class RoutesWiringTest(SiteTestCase):
|
||||
def test_all_expected_routes_exist(self):
|
||||
self.assertEqual(
|
||||
sorted(self.router.routes),
|
||||
[("GET", "/admin/"), ("GET", "/admin/login"), ("GET", "/admin/static/admin.css"),
|
||||
("POST", "/admin/login"), ("POST", "/admin/logout")],
|
||||
[("GET", "/admin/"), ("GET", "/admin/login"), ("GET", "/admin/static/{name}"),
|
||||
("GET", "/admin/{rest:path}"), ("POST", "/admin/login"), ("POST", "/admin/logout"),
|
||||
("POST", "/admin/{rest:path}")],
|
||||
)
|
||||
|
||||
def test_login_flow_through_routes(self):
|
||||
@@ -111,11 +135,36 @@ class RoutesWiringTest(SiteTestCase):
|
||||
self.assertIn("Max-Age=0", out.headers.appended[0][1])
|
||||
|
||||
def test_static_and_login_page_routes(self):
|
||||
css = self.route("GET", "/admin/static/admin.css")()
|
||||
css = self.route("GET", "/admin/static/{name}")("admin.css")
|
||||
self.assertTrue(css.media_type.startswith("text/css"))
|
||||
self.assertEqual(self.route("GET", "/admin/static/{name}")("secret.txt").status_code, 404)
|
||||
page = self.route("GET", "/admin/login")(FakeRequest())
|
||||
self.assertEqual(page.status_code, 200)
|
||||
|
||||
def test_editor_catch_all_passes_method_path_query_form_and_cookie(self):
|
||||
import asyncio
|
||||
|
||||
good = self.route("POST", "/admin/login")(FakeRequest(headers=ORIGIN), login="admin", password=PASSWORD)
|
||||
value = good.headers.appended[0][1].split(";")[0].split("=", 1)[1]
|
||||
cookies = {COOKIE_NAME: value}
|
||||
get = self.route("GET", "/admin/{rest:path}")
|
||||
response = asyncio.run(get(FakeRequest(cookies=cookies, query={"msg": "saved"}), rest="pages"))
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn("Новая страница", response.content)
|
||||
self.assertIn("Сохранено", response.content) # query дошёл до обработчика
|
||||
|
||||
csrf = self.auth.csrf_token(self.auth.authenticate(value))
|
||||
post = self.route("POST", "/admin/{rest:path}")
|
||||
request = FakeRequest(cookies=cookies, headers=ORIGIN, method="POST",
|
||||
form=[("csrf", csrf), ("page_id", "wired-page"), ("title", "Через адаптер")])
|
||||
response = asyncio.run(post(request, rest="pages/new"))
|
||||
self.assertEqual((response.status_code, response.headers["Location"]),
|
||||
(303, "/admin/pages/wired-page?msg=created"))
|
||||
self.assertTrue((self.site / "pages" / "wired-page.md").is_file())
|
||||
|
||||
nobody = asyncio.run(get(FakeRequest(), rest="pages"))
|
||||
self.assertEqual((nobody.status_code, nobody.headers["Location"]), (303, "/admin/login"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user