Meilenstein 1: Grundgerüst mit Login, Sessions und CSRF
- 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>
This commit is contained in:
1 parent
403606ff66
commit
293367f33e
18 files changed
+837
-6
No files matched your search
@@ -0,0 +1,36 @@
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client(tmp_path, monkeypatch):
|
||||
"""TestClient mit eigener, leerer Datenbank pro Test."""
|
||||
monkeypatch.setenv("IT_CONNECT_DB_PATH", str(tmp_path / "test.db"))
|
||||
# TestClient nutzt http://testserver, daher ohne Secure-Flag
|
||||
monkeypatch.setenv("IT_CONNECT_COOKIE_SECURE", "false")
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import app
|
||||
|
||||
with TestClient(app) as c:
|
||||
yield c
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def admin(client):
|
||||
"""Legt den Test-Admin 'testadmin' an."""
|
||||
from app.auth import passwort_hashen
|
||||
from app.db import jetzt, verbinden
|
||||
|
||||
conn = verbinden()
|
||||
conn.execute(
|
||||
"INSERT INTO admins (benutzername, passwort_hash, erstellt_am) VALUES (?, ?, ?)",
|
||||
("testadmin", passwort_hashen("geheimes-test-passwort"), jetzt()),
|
||||
)
|
||||
conn.close()
|
||||
return {"benutzername": "testadmin", "passwort": "geheimes-test-passwort"}
|
||||
@@ -0,0 +1,94 @@
|
||||
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
|
||||
Reference in new issue
Block a user