Files
projekt-plattform/server/tests/test_register.py

137 lines
5.6 KiB
Python

"""Einheiten- und Dienst-Tests des Registers (ohne Netz, mit eingebautem Testclient).
Aufruf (aus server/): .venv/bin/python -m unittest discover -s tests -v
"""
import os
import sqlite3
import sys
import tempfile
import threading
import unittest
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from fastapi.testclient import TestClient # noqa: E402
from register.app import erzeuge_app # noqa: E402
from register.normalize import normalisiere # noqa: E402
import run # noqa: E402
P0 = "ssh://git@git.wlkns.org:2222/wlkns-dev/projekt-plattform.git"
def anfrage(c, name="X", git="ssh://git@git.example.org/a/b.git", rechner="r1"):
return c.post("/api/register", json={"name": name, "git_adresse": git, "rechner": rechner})
class Basis(unittest.TestCase):
def setUp(self):
self.tmp = tempfile.TemporaryDirectory()
self.db = os.path.join(self.tmp.name, "r.db")
self.c = TestClient(erzeuge_app(self.db))
def tearDown(self):
self.tmp.cleanup()
class TestNormalisieren(unittest.TestCase):
def test_schreibweisen(self):
a = normalisiere("ssh://git@git.wlkns.org:2222/wlkns-dev/projekt-plattform.git")
self.assertEqual(a, "git.wlkns.org:2222/wlkns-dev/projekt-plattform")
self.assertEqual(normalisiere("ssh://GIT@Git.WLKNS.org:2222/wlkns-dev/projekt-plattform/"), a)
self.assertEqual(normalisiere("git@host.x:o/r.git"), normalisiere("https://u@HOST.x/o/r"))
def test_ssh_und_https_sind_verschieden_wenn_der_port_abweicht(self):
self.assertNotEqual(normalisiere("ssh://git@h:2222/o/r.git"), normalisiere("https://h/o/r.git"))
def test_unlesbar(self):
for schlecht in ["", " ", "kein url", "https://nurhost"]:
with self.assertRaises(ValueError, msg=schlecht):
normalisiere(schlecht)
class TestRegister(Basis):
def test_seed_p0000(self):
r = anfrage(self.c, "Projekt-Plattform", P0, "pc1").json()
self.assertEqual((r["id"], r["neu"]), ("P-0000", False))
def test_neue_ids_fortlaufend(self):
ids = [anfrage(self.c, f"p{i}", f"ssh://git@h/o/r{i}.git").json()["id"] for i in range(3)]
self.assertEqual(ids, ["P-0001", "P-0002", "P-0003"])
def test_bekannte_adresse_gleiche_id_anderer_rechner(self):
a = anfrage(self.c, "X", "ssh://git@h/o/r.git", "pc1").json()
# scp-Schreibweise und ssh:// ohne Port sind dieselbe Adresse (h/o/r)
b = anfrage(self.c, "X", "git@h:o/r.git", "pc2").json()
self.assertTrue(a["neu"])
self.assertEqual((b["id"], b["neu"]), (a["id"], False))
rechner = [p for p in self.c.get("/api/projekte").json() if p["id"] == a["id"]][0]["rechner"]
self.assertEqual(rechner, ["pc1", "pc2"])
def test_gleicher_host_anderer_port_ist_anderes_projekt(self):
a = anfrage(self.c, "X", "ssh://git@h:2222/o/r.git").json()
b = anfrage(self.c, "X", "https://h/o/r.git").json()
self.assertNotEqual(a["id"], b["id"])
def test_namensabweichung_warnt_aendert_nichts(self):
anfrage(self.c, "Alt", "ssh://git@h/o/r.git")
r = anfrage(self.c, "Neu", "ssh://git@h/o/r.git").json()
self.assertEqual(r["name"], "Alt")
self.assertTrue(r["warnungen"])
def test_nummern_nie_wiederverwenden(self):
anfrage(self.c, "A", "ssh://git@h/o/a.git") # P-0001
con = sqlite3.connect(self.db)
con.execute("DELETE FROM rechner WHERE projekt_id = 'P-0001'")
con.execute("DELETE FROM projekte WHERE id = 'P-0001'")
con.commit()
con.close()
self.assertEqual(anfrage(self.c, "B", "ssh://git@h/o/b.git").json()["id"], "P-0002")
def test_zaehler_ueberlebt_neustart(self):
anfrage(self.c, "A", "ssh://git@h/o/a.git")
c2 = TestClient(erzeuge_app(self.db)) # zweite Anwendung auf derselben Datei
self.assertEqual(anfrage(c2, "B", "ssh://git@h/o/b.git").json()["id"], "P-0002")
self.assertEqual(anfrage(c2, "A", "ssh://git@h/o/a.git").json()["id"], "P-0001")
def test_ungueltige_eingaben(self):
self.assertEqual(anfrage(self.c, "", "ssh://git@h/o/r.git").status_code, 422)
self.assertEqual(anfrage(self.c, "X", "keine adresse").status_code, 422)
self.assertEqual(self.c.post("/api/register", json={"name": "X"}).status_code, 422)
self.assertEqual(len(self.c.get("/api/projekte").json()), 1) # nur der Seed
def test_gleichzeitige_anfragen_ergeben_verschiedene_ids(self):
ergebnis, fehler = [], []
def los(i):
try:
ergebnis.append(anfrage(self.c, f"p{i}", f"ssh://git@h/o/r{i}.git").json()["id"])
except Exception as e: # pragma: no cover
fehler.append(e)
ts = [threading.Thread(target=los, args=(i,)) for i in range(20)]
[t.start() for t in ts]
[t.join() for t in ts]
self.assertEqual(fehler, [])
self.assertEqual(len(set(ergebnis)), 20)
def test_keine_schreibenden_wege_ausser_register(self):
for pfad in ["/api/projekte", "/api/register", "/api/health"]:
self.assertEqual(self.c.delete(pfad).status_code, 405)
self.assertEqual(self.c.get("/docs").status_code, 404)
class TestAdressen(unittest.TestCase):
def test_erlaubt(self):
self.assertEqual(run.adressen("127.0.0.1, 192.168.13.64"), ["127.0.0.1", "192.168.13.64"])
self.assertEqual(run.adressen("10.0.0.2"), ["10.0.0.2"])
def test_abgelehnt(self):
for schlecht in ["0.0.0.0", "::", "100.69.190.27", "100.64.0.1", "8.8.8.8", "kein-host", ""]:
with self.assertRaises(ValueError, msg=schlecht):
run.adressen(schlecht)
if __name__ == "__main__":
unittest.main()