|
|
@@ -0,0 +1,78 @@
|
|
|
+import base64
|
|
|
+import json
|
|
|
+import subprocess
|
|
|
+from pathlib import Path
|
|
|
+
|
|
|
+
|
|
|
+ROOT = Path(__file__).resolve().parents[1]
|
|
|
+FILTER = ROOT / "frontend/src/store/modules/menuFilter.js"
|
|
|
+ROUTES = ROOT / "frontend/src/router/routes.js"
|
|
|
+
|
|
|
+
|
|
|
+def _data_url(source: str) -> str:
|
|
|
+ encoded = base64.b64encode(source.encode("utf-8")).decode("ascii")
|
|
|
+ return f"data:text/javascript;base64,{encoded}"
|
|
|
+
|
|
|
+
|
|
|
+def _system_children(permissions: list[str]) -> list[str] | None:
|
|
|
+ script = f"""
|
|
|
+ const {{ filterRoutes }} = await import({_data_url(FILTER.read_text(encoding='utf-8'))!r})
|
|
|
+ const {{ default: routes }} = await import({_data_url(ROUTES.read_text(encoding='utf-8'))!r})
|
|
|
+ const system = filterRoutes(routes.routes, {json.dumps(permissions)}).find(
|
|
|
+ route => route.name === 'systemManage'
|
|
|
+ )
|
|
|
+ console.log(JSON.stringify(system ? system.children.map(route => route.name) : null))
|
|
|
+ """
|
|
|
+ completed = subprocess.run(
|
|
|
+ ["node", "--input-type=module", "--eval", script],
|
|
|
+ cwd=ROOT,
|
|
|
+ check=True,
|
|
|
+ capture_output=True,
|
|
|
+ text=True,
|
|
|
+ )
|
|
|
+ return json.loads(completed.stdout)
|
|
|
+
|
|
|
+
|
|
|
+def _strict_leaf_is_visible(permissions: list[str]) -> bool:
|
|
|
+ routes = [
|
|
|
+ {
|
|
|
+ "name": "parent",
|
|
|
+ "children": [
|
|
|
+ {
|
|
|
+ "name": "strictLeaf",
|
|
|
+ "children": [],
|
|
|
+ "meta": {"permissions": ["first:read", "second:seal"]},
|
|
|
+ }
|
|
|
+ ],
|
|
|
+ }
|
|
|
+ ]
|
|
|
+ script = f"""
|
|
|
+ const {{ filterRoutes }} = await import({_data_url(FILTER.read_text(encoding='utf-8'))!r})
|
|
|
+ const visible = filterRoutes({json.dumps(routes)}, {json.dumps(permissions)})
|
|
|
+ console.log(JSON.stringify(Boolean(visible[0]?.children?.length)))
|
|
|
+ """
|
|
|
+ completed = subprocess.run(
|
|
|
+ ["node", "--input-type=module", "--eval", script],
|
|
|
+ cwd=ROOT,
|
|
|
+ check=True,
|
|
|
+ capture_output=True,
|
|
|
+ text=True,
|
|
|
+ )
|
|
|
+ return json.loads(completed.stdout)
|
|
|
+
|
|
|
+
|
|
|
+def test_identity_manager_keeps_system_manage_with_only_identity_child():
|
|
|
+ assert _system_children(["identity:manage"]) == ["systemEnterpriseIdentity"]
|
|
|
+
|
|
|
+
|
|
|
+def test_system_manage_is_hidden_without_any_system_child_permission():
|
|
|
+ assert _system_children([]) is None
|
|
|
+
|
|
|
+
|
|
|
+def test_user_manager_keeps_only_user_management_child():
|
|
|
+ assert _system_children(["users:manage"]) == ["systemUserManage"]
|
|
|
+
|
|
|
+
|
|
|
+def test_leaf_requires_all_of_its_permissions():
|
|
|
+ assert _strict_leaf_is_visible(["first:read"]) is False
|
|
|
+ assert _strict_leaf_is_visible(["first:read", "second:seal"]) is True
|