72 lines
1.8 KiB
Python
72 lines
1.8 KiB
Python
"""Unit tests for the Cortex-OS umbrella graph plugin route."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from types import SimpleNamespace
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parents[2]
|
|
sys.path.insert(0, str(ROOT))
|
|
from routes import umbrella
|
|
|
|
|
|
class _Handler:
|
|
def __init__(self):
|
|
self.status = None
|
|
self.headers = {}
|
|
self.body = b""
|
|
|
|
def send_response(self, status):
|
|
self.status = status
|
|
|
|
def send_header(self, key, value):
|
|
self.headers[key] = value
|
|
|
|
def end_headers(self):
|
|
pass
|
|
|
|
@property
|
|
def wfile(self):
|
|
handler = self
|
|
|
|
class _W:
|
|
def write(self, body):
|
|
handler.body += body
|
|
|
|
return _W()
|
|
|
|
|
|
def test_umbrella_registers_graph_routes():
|
|
calls = []
|
|
|
|
class _Api:
|
|
def logger(self, _name):
|
|
return SimpleNamespace(info=lambda *args, **kwargs: None)
|
|
|
|
def register_route(self, path, method, handler):
|
|
calls.append((method, path, handler))
|
|
|
|
umbrella.register(_Api())
|
|
assert ("GET", "/api/umbrella", umbrella._handle_graph_json) in calls
|
|
assert ("GET", "/api/umbrella/doc", umbrella._handle_doc_body) in calls
|
|
|
|
|
|
def test_umbrella_doc_blocks_path_traversal():
|
|
handler = _Handler()
|
|
parsed = SimpleNamespace(query="path=../README.md")
|
|
assert umbrella._handle_doc_body(handler, parsed) is True
|
|
assert handler.status == 403
|
|
assert json.loads(handler.body)["error"] == "path outside workspace"
|
|
|
|
|
|
def test_umbrella_doc_serves_sot_readme():
|
|
handler = _Handler()
|
|
parsed = SimpleNamespace(query="path=sot/README.md")
|
|
assert umbrella._handle_doc_body(handler, parsed) is True
|
|
assert handler.status == 200
|
|
payload = json.loads(handler.body)
|
|
assert payload["path"] == "sot/README.md"
|
|
assert "Source of Truth" in payload["body"]
|