- SPA unter /omega/ ohne Bibliotheken; HTML, CSS, JS und IBM-Plex-Schriften eingebettet, Content-Security-Policy nur für den Proxy selbst - Browser auf / werden nach /omega/ umgeleitet, Ollama-Clients nicht - Anmeldung mit Passwort (Argon2) nur mit ui.password_login; Sitzung im HttpOnly-Cookie (SameSite=Strict), Änderungen nur mit X-Omega-CSRF - Seiten nach den Mockups: Übersicht, API-Keys mit Aufzeichnung, Konto, Gruppen & Kontingente, Sparks & Modelle, Zeitplan, Benutzer, Audit-Log, öffentliches Dashboard - tests/multispark/demo.sh (admin, dozent, student; Passwort 1234), ui_test.py Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
80 lines
4.5 KiB
Python
80 lines
4.5 KiB
Python
"""Test der Weboberfläche ohne Browser: Auslieferung, keine externen Quellen, Anmeldung mit
|
|
Passwort, Cookie, Schutz gegen CSRF, Abmelden. Erwartet [ui] password_login = true und demo.sh."""
|
|
|
|
import http.client
|
|
import json
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from common import check, failures # noqa: E402
|
|
|
|
|
|
def request(method, path, body=None, headers=None):
|
|
conn = http.client.HTTPConnection("127.0.0.1", 8000, timeout=30)
|
|
data = json.dumps(body).encode() if body is not None else None
|
|
hdrs = {"Content-Type": "application/json", **(headers or {})}
|
|
conn.request(method, path, body=data, headers=hdrs)
|
|
r = conn.getresponse()
|
|
content = r.read()
|
|
result = (r.status, {k.lower(): v for k, v in r.getheaders()}, content)
|
|
conn.close()
|
|
return result
|
|
|
|
|
|
print("1) Browser und Ollama-Clients auf der Wurzel")
|
|
st, hdrs, _ = request("GET", "/", headers={"Accept": "text/html,application/xhtml+xml"})
|
|
check(st in (302, 307) and hdrs.get("location") == "/omega/", f"Browser wird nach /omega/ umgeleitet ({st})")
|
|
st, _, body = request("GET", "/")
|
|
check(st == 200 and b"Ollama is running" in body, "Ollama-Clients bekommen weiter die Ollama-Antwort")
|
|
|
|
print("2) Oberfläche ohne externe Quellen")
|
|
st, hdrs, index = request("GET", "/omega/")
|
|
csp = hdrs.get("content-security-policy", "")
|
|
check(st == 200 and "default-src 'none'" in csp and "font-src 'self'" in csp and "connect-src 'self'" in csp,
|
|
"Content-Security-Policy erlaubt nur den Proxy selbst")
|
|
assets = re.findall(rb'(?:href|src)="(/omega/assets/[^"]+)"', index)
|
|
css = request("GET", "/omega/assets/app.css")[2]
|
|
fonts = re.findall(rb"url\((/omega/assets/[^)]+)\)", css)
|
|
files = {p.decode(): request("GET", p.decode()) for p in assets + fonts}
|
|
print(f" {len(files)} Dateien: {', '.join(os.path.basename(p) for p in files)}")
|
|
check(len(fonts) == 6 and all(r[0] == 200 for r in files.values()), "alle Dateien und sechs Schriften werden ausgeliefert")
|
|
check(all(r[2][:4] == b"wOF2" and r[1]["content-type"] == "font/woff2" for p, r in files.items() if p.endswith(".woff2")),
|
|
"Schriften als WOFF2")
|
|
external = [p for p, r in list(files.items()) + [("/omega/", (st, hdrs, index))]
|
|
if not p.endswith(".woff2") and re.search(rb"https?://(?!www\.w3\.org/2000/svg)", r[2])]
|
|
check(not external, f"keine externen Adressen in HTML, CSS oder JavaScript {external}")
|
|
check(request("GET", "/omega/assets/gibtsnicht.js")[0] == 404, "unbekannte Datei: 404")
|
|
|
|
print("3) Anmeldung mit Passwort")
|
|
st, _, body = request("POST", "/omega/api/login", {"username": "student", "password": "falsch"})
|
|
check(st == 401, f"falsches Passwort: {st} {body[:60]!r}")
|
|
st, _, _ = request("POST", "/omega/api/login", {"username": "niemand", "password": "1234"})
|
|
check(st == 401, "unbekannter Benutzer: 401")
|
|
cookies = {}
|
|
for user in ["admin", "dozent", "student"]:
|
|
st, hdrs, body = request("POST", "/omega/api/login", {"username": user, "password": "1234"})
|
|
cookie = hdrs.get("set-cookie", "")
|
|
cookies[user] = cookie.split(";")[0]
|
|
check(st == 200 and json.loads(body)["user"]["username"] == user, f"{user} meldet sich mit 1234 an")
|
|
check(all(a in cookie for a in ["HttpOnly", "SameSite=Strict", "Path=/omega"]), f"Cookie: {cookie.split(';', 1)[1].strip()}")
|
|
st, _, body = request("GET", "/omega/api/me", headers={"Cookie": cookies["dozent"]})
|
|
me = json.loads(body)
|
|
check(st == 200 and me["user"]["role"] == "lecturer" and me["managed"] is not None, "Cookie genügt zum Lesen")
|
|
st, _, body = request("POST", "/omega/api/groups", {"name": "ohne-header"}, headers={"Cookie": cookies["dozent"]})
|
|
check(st == 403 and b"CSRF" in body, "Ändern nur mit Kopfzeile X-Omega-CSRF")
|
|
st, _, _ = request("POST", "/omega/api/groups", {"name": "mit-header"}, headers={"Cookie": cookies["dozent"], "X-Omega-CSRF": "1"})
|
|
check(st == 201, "mit Kopfzeile: angelegt")
|
|
st, _, body = request("GET", "/omega/api/me", headers={"Cookie": cookies["student"]})
|
|
me = json.loads(body)
|
|
check(me["keys"] and me["quotas"] and me["quotas"][0]["usage"][0]["usage"]["day"]["requests"] >= 3,
|
|
"student sieht Key und Kontingent aus demo.sh")
|
|
|
|
print("4) Abmelden")
|
|
st, hdrs, _ = request("POST", "/omega/api/logout", headers={"Cookie": cookies["student"], "X-Omega-CSRF": "1"})
|
|
check(st == 204 and "Max-Age=0" in hdrs.get("set-cookie", ""), "Abmelden löscht das Cookie")
|
|
check(request("GET", "/omega/api/me", headers={"Cookie": cookies["student"]})[0] == 401, "die Sitzung ist widerrufen")
|
|
|
|
print("ERGEBNIS:", "alles bestanden" if not failures else f"{len(failures)} Fehler: {failures}")
|