global-search-api/test_server_v2.py

52 lines
2.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""Small black-box smoke test for the public V2 routes."""
import json
import os
import subprocess
import sys
import tempfile
import time
import urllib.request
from pathlib import Path
ROOT = Path(__file__).parent
with tempfile.TemporaryDirectory() as temp:
root = Path(temp) / "repos"
repo = root / "fifth-domain"
repo.mkdir(parents=True)
subprocess.run(["git", "init", "-q"], cwd=repo, check=True)
subprocess.run(["git", "config", "user.email", "test@example.invalid"], cwd=repo, check=True)
subprocess.run(["git", "config", "user.name", "test"], cwd=repo, check=True)
(repo / "gls").mkdir()
(repo / "gls" / "GLS-ENTRY.hdlp").write_text("GLSV and HLDP", encoding="utf-8")
subprocess.run(["git", "add", "."], cwd=repo, check=True)
subprocess.run(["git", "commit", "-qm", "fixture"], cwd=repo, check=True)
router = {"schema": "GLW-WORLD-ROUTER-1", "entry_repository": "REPO-001", "repositories": [{"id": "REPO-001", "slug": "fifth-domain", "url": "https://example.invalid/fifth", "state": "ACTIVE_ENTRY", "summary": "entry"}], "personas": [{"id": "ICE-GL-ZY001", "aliases": ["铸渊"], "system": "LL", "repository": "REPO-001", "path": "core/"}]}
router_file = Path(temp) / "world-router.json"
router_file.write_text(json.dumps(router), encoding="utf-8")
env = {**os.environ, "PORT": "43951", "REPO_ROOT": str(root), "WORLD_ROUTER_PATH": str(router_file)}
process = subprocess.Popen([sys.executable, "server_v2.py"], cwd=ROOT, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
for _ in range(30):
try:
with urllib.request.urlopen("http://127.0.0.1:43951/v2/repos", timeout=1) as response:
repos = json.load(response)
break
except OSError:
time.sleep(0.1)
else:
raise AssertionError("server did not start")
assert repos["repositories"][0]["available"] is True
with urllib.request.urlopen("http://127.0.0.1:43951/v2/resolve?q=%E9%93%B8%E6%B8%8A") as response:
assert json.load(response)["matches"][0]["kind"] == "persona"
with urllib.request.urlopen("http://127.0.0.1:43951/v2/search?repo=fifth-domain&q=GLSV") as response:
assert json.load(response)["results"][0]["path"] == "gls/GLS-ENTRY.hdlp"
with urllib.request.urlopen("http://127.0.0.1:43951/v2/file?repo=fifth-domain&path=../secret") as response:
assert json.load(response)["ok"] is False
finally:
process.terminate()
process.wait(timeout=5)
print("V2 smoke tests passed")