- FastAPI-App mit SQLite-Schema (admins, sessions, connections) - Login mit Argon2, serverseitige Sessions (Token nur gehasht gespeichert) - Cookie HttpOnly/Secure/SameSite=Strict, CSRF-Token für schreibende Requests - scripts/create_admin.py zum Anlegen und Zurücksetzen von Admins - Einfaches Frontend mit Login - pytest-Tests, gepinnte requirements.txt, .env.example, .gitignore - SETUP.md Teil 4 ergänzt Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
from app.auth import SESSION_COOKIE
|
|
|
|
|
|
def login(client, admin):
|
|
r = client.post("/api/auth/login", json=admin)
|
|
assert r.status_code == 200, r.text
|
|
return r.json()
|
|
|
|
|
|
def test_frontend_wird_ausgeliefert(client):
|
|
r = client.get("/")
|
|
assert r.status_code == 200
|
|
assert "it-connect" in r.text
|
|
assert r.headers["X-Frame-Options"] == "DENY"
|
|
|
|
|
|
def test_me_ohne_login(client):
|
|
assert client.get("/api/auth/me").status_code == 401
|
|
|
|
|
|
def test_login_falsches_passwort(client, admin):
|
|
r = client.post("/api/auth/login", json={"benutzername": "testadmin", "passwort": "falsch"})
|
|
assert r.status_code == 401
|
|
assert r.json()["detail"] == "Benutzername oder Passwort falsch."
|
|
|
|
|
|
def test_login_unbekannter_benutzer(client, admin):
|
|
r = client.post("/api/auth/login", json={"benutzername": "gibtsnicht", "passwort": "x"})
|
|
assert r.status_code == 401
|
|
|
|
|
|
def test_login_setzt_sicheres_cookie(client, admin):
|
|
r = client.post("/api/auth/login", json=admin)
|
|
assert r.status_code == 200
|
|
cookie = r.headers["set-cookie"].lower()
|
|
assert SESSION_COOKIE in cookie
|
|
assert "httponly" in cookie
|
|
assert "samesite=strict" in cookie
|
|
assert r.json()["csrf_token"]
|
|
|
|
|
|
def test_cookie_secure_standardmaessig(client, admin, monkeypatch):
|
|
monkeypatch.setenv("IT_CONNECT_COOKIE_SECURE", "true")
|
|
r = client.post("/api/auth/login", json=admin)
|
|
assert "secure" in r.headers["set-cookie"].lower()
|
|
|
|
|
|
def test_passwort_wird_gehasht_gespeichert(client, admin):
|
|
from app.db import verbinden
|
|
|
|
conn = verbinden()
|
|
h = conn.execute("SELECT passwort_hash FROM admins").fetchone()[0]
|
|
conn.close()
|
|
assert h.startswith("$argon2")
|
|
assert admin["passwort"] not in h
|
|
|
|
|
|
def test_me_nach_login(client, admin):
|
|
daten = login(client, admin)
|
|
r = client.get("/api/auth/me")
|
|
assert r.status_code == 200
|
|
assert r.json()["benutzername"] == "testadmin"
|
|
assert r.json()["csrf_token"] == daten["csrf_token"]
|
|
|
|
|
|
def test_logout_ohne_csrf_wird_abgelehnt(client, admin):
|
|
login(client, admin)
|
|
r = client.post("/api/auth/logout")
|
|
assert r.status_code == 403
|
|
assert client.get("/api/auth/me").status_code == 200
|
|
|
|
|
|
def test_logout_mit_falschem_csrf(client, admin):
|
|
login(client, admin)
|
|
r = client.post("/api/auth/logout", headers={"X-CSRF-Token": "falsch"})
|
|
assert r.status_code == 403
|
|
|
|
|
|
def test_logout_mit_csrf(client, admin):
|
|
daten = login(client, admin)
|
|
r = client.post("/api/auth/logout", headers={"X-CSRF-Token": daten["csrf_token"]})
|
|
assert r.status_code == 204
|
|
assert client.get("/api/auth/me").status_code == 401
|
|
|
|
|
|
def test_session_token_nicht_im_klartext_gespeichert(client, admin):
|
|
login(client, admin)
|
|
token = client.cookies.get(SESSION_COOKIE)
|
|
from app.db import verbinden
|
|
|
|
conn = verbinden()
|
|
gespeichert = conn.execute("SELECT token_hash FROM sessions").fetchone()[0]
|
|
conn.close()
|
|
assert token != gespeichert
|