2026-11-05 14:25:57 +01:00
|
|
|
"""Vérifications de la web app, sans appel à l'API (faux client) : API/.venv/bin/python app/serveur/check.py [nom]"""
|
|
|
|
|
import json
|
|
|
|
|
import shutil
|
|
|
|
|
import socket
|
|
|
|
|
import sys
|
|
|
|
|
import threading
|
|
|
|
|
import time
|
|
|
|
|
import urllib.request
|
|
|
|
|
from pathlib import Path
|
|
|
|
|
from urllib.error import HTTPError
|
|
|
|
|
|
|
|
|
|
ICI = Path(__file__).resolve().parent
|
|
|
|
|
sys.path[:0] = [str(ICI), str(ICI.parents[1] / "API" / "support")]
|
|
|
|
|
import uvicorn # noqa: E402
|
|
|
|
|
import seance # noqa: E402
|
|
|
|
|
import serveur # noqa: E402
|
|
|
|
|
import station # noqa: E402
|
|
|
|
|
from check import Bloc, FauxClient, reponse, seance_temporaire # noqa: E402 (les faux de API/support/check.py)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_serveur():
|
|
|
|
|
# le vrai serveur sur un port libre, une séance au faux client : la garde, l'analyse, le flux, le PDF absent
|
|
|
|
|
with socket.socket() as so:
|
|
|
|
|
so.bind(("127.0.0.1", 0))
|
|
|
|
|
port = so.getsockname()[1]
|
|
|
|
|
cases = seance.CASES
|
|
|
|
|
seance.CASES, serveur.CLIENT = seance_temporaire(), FauxClient(reponse([Bloc(type="text", text="Bonjour Marc")], "end_turn"))
|
|
|
|
|
srv = uvicorn.Server(uvicorn.Config(serveur.app, host="127.0.0.1", port=port, log_level="error"))
|
|
|
|
|
threading.Thread(target=srv.run, daemon=True).start()
|
|
|
|
|
while not srv.started:
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
|
base = f"http://127.0.0.1:{port}"
|
|
|
|
|
|
|
|
|
|
def appel(chemin, corps=None, entetes=None):
|
|
|
|
|
r = urllib.request.Request(base + chemin, json.dumps(corps).encode() if corps is not None else None,
|
|
|
|
|
{"Content-Type": "application/json", **(entetes or {})})
|
|
|
|
|
try:
|
|
|
|
|
with urllib.request.urlopen(r) as rep:
|
|
|
|
|
return rep.status
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
return e.code
|
|
|
|
|
try:
|
|
|
|
|
assert appel("/api/pdf", entetes={"Host": "evil.example:80"}) == 403
|
|
|
|
|
assert appel("/api/analyser", {"mail": "x"}, {"Content-Type": "text/plain"}) == 403
|
|
|
|
|
assert appel("/api/analyser", {"mail": " "}) == 400
|
|
|
|
|
assert appel("/api/analyser", {"mail": "Bonjour", "contexte": "", "images": []}) == 200
|
|
|
|
|
with urllib.request.urlopen(base + "/api/flux", timeout=10) as f:
|
|
|
|
|
premier, textes = None, []
|
|
|
|
|
for ligne in f:
|
|
|
|
|
if ligne.startswith(b"data: "):
|
|
|
|
|
m = json.loads(ligne[6:])
|
|
|
|
|
premier = premier or m
|
|
|
|
|
textes += [e["texte"] for e in m["evenements"]]
|
|
|
|
|
if "Bonjour Marc" in textes and not m["occupe"]["support"]:
|
|
|
|
|
break
|
|
|
|
|
assert premier["depuis"] == 0 and premier["id"], premier
|
|
|
|
|
assert appel("/api/pdf") == 404
|
|
|
|
|
(serveur.etat["seance"].dossier / "v1.pdf").write_bytes(b"%PDF-1.4")
|
|
|
|
|
with urllib.request.urlopen(base + "/api/pdf") as rep:
|
|
|
|
|
assert rep.read() == b"%PDF-1.4" and rep.headers["Content-Disposition"].startswith("inline"), rep.headers
|
|
|
|
|
finally:
|
|
|
|
|
srv.should_exit = True
|
|
|
|
|
shutil.rmtree(seance.CASES)
|
|
|
|
|
seance.CASES = cases
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def leve(f, *a):
|
|
|
|
|
try:
|
|
|
|
|
f(*a)
|
|
|
|
|
except station.Refus as e:
|
|
|
|
|
return str(e)
|
|
|
|
|
raise AssertionError(f"{f.__name__}{a} n'a pas refusé")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_station():
|
2026-11-05 15:01:39 +01:00
|
|
|
# les données des pages Images et Data, sur les vraies données ; les étiquettes dans un dossier temporaire
|
2026-11-05 14:25:57 +01:00
|
|
|
assert "NIEDERHORN" in station.stations()
|
|
|
|
|
im = station.images("NIEDERHORN", "2026-09-25", "2026-09-26")
|
|
|
|
|
assert im["images"] and im["positions"] and set(im["corr"]) == set(im["positions"]), list(im)
|
|
|
|
|
assert all(len(c) == len(im["images"]) for c in im["corr"].values())
|
|
|
|
|
assert station.image("NIEDERHORN", im["images"][0]["nom"]).is_file()
|
|
|
|
|
leve(station.image, "NIEDERHORN", "../../../.env")
|
|
|
|
|
leve(station.images, "../API", "", "")
|
|
|
|
|
assert station.images("LENZSPITZE", "2026-10-26", "2026-10-27")["images"]
|
|
|
|
|
c = station.courbes("LENZSPITZE", "2026-10-26", "2026-10-27")
|
|
|
|
|
titres = [p["titre"] for p in c["plots"]]
|
|
|
|
|
assert "Relative Corrected Vertical Shift" in titres and any(t.startswith("System: ") for t in titres), titres
|
|
|
|
|
assert not any("SIM_CCID" in t or t.startswith("System: ip") for t in titres)
|
|
|
|
|
assert all(len(c["T"][tr["t"]]) == len(tr["y"]) for p in c["plots"] for tr in p["traces"])
|
|
|
|
|
avant, station.ETIQUETTES = station.ETIQUETTES, seance_temporaire()
|
|
|
|
|
try:
|
|
|
|
|
e = station.ajouter({"station": "NIEDERHORN", "dossier": "auto", "debut": "2026-09-25T01:00Z",
|
|
|
|
|
"fin": "2026-09-25T02:00Z", "categorie": "Visibility", "description": "brouillard"})
|
|
|
|
|
assert e["id"] == "NIEDERHORN-1" and next(iter(e)) == "id", e
|
|
|
|
|
e2 = station.modifier({"station": "NIEDERHORN", "dossier": "auto", "avant": e,
|
|
|
|
|
"debut": e["debut"], "fin": e["fin"], "categorie": "", "description": "pluie"})
|
|
|
|
|
assert e2 == {"id": "NIEDERHORN-1", "debut": e["debut"], "fin": e["fin"], "description": "pluie"}, e2
|
|
|
|
|
assert station.lire("NIEDERHORN", "auto") == [e2]
|
|
|
|
|
assert "plus dans le fichier" in leve(station.supprimer, {"station": "NIEDERHORN", "dossier": "auto", "avant": e})
|
|
|
|
|
station.supprimer({"station": "NIEDERHORN", "dossier": "auto", "avant": e2})
|
|
|
|
|
assert station.lire("NIEDERHORN", "auto") == []
|
|
|
|
|
leve(station.ajouter, {"station": "NIEDERHORN", "dossier": "../x", "debut": "a", "fin": "b", "description": "x"})
|
|
|
|
|
leve(station.ajouter, {"station": "NIEDERHORN", "debut": "2026-09-25T02:00Z", "fin": "2026-09-25T01:00Z", "description": "x"})
|
|
|
|
|
finally:
|
|
|
|
|
shutil.rmtree(station.ETIQUETTES)
|
|
|
|
|
station.ETIQUETTES = avant
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CHECKS = [check_serveur, check_station]
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
nom = sys.argv[1] if len(sys.argv) > 1 else None
|
|
|
|
|
for f in CHECKS:
|
|
|
|
|
if nom in (None, f.__name__):
|
|
|
|
|
f()
|
|
|
|
|
print("ok", f.__name__)
|