import secrets from datetime import date def test_users_are_isolated(admin): from fastapi.testclient import TestClient from app.main import app # Admin legt ein Konto an. acc = admin.post("/api/accounts", json={"name": "AdminKonto", "currency": "EUR"}).json() # Frischen Nutzer anlegen und getrennt einloggen. username = "iso_" + secrets.token_hex(4) password = "pw_" + secrets.token_hex(4) assert admin.post("/api/users", json={"username": username, "password": password, "role": "user"}).status_code == 201 other = TestClient(app) assert other.post("/api/auth/login", data={"username": username, "password": password}).status_code == 200 # Sieht keine fremden Konten/Buchungen. assert other.get("/api/accounts").json() == [] assert other.get("/api/transactions").json() == [] # Zugriff auf fremdes Konto -> 404 (nicht 403: existiert für ihn schlicht nicht). assert other.get(f"/api/accounts/{acc['id']}").status_code == 404 # Buchung auf fremdes Konto -> 404. r = other.post("/api/transactions", json={ "type": "expense", "date": str(date.today()), "account_id": acc["id"], "amount_cents": 100, }) assert r.status_code == 404 # Eigene Startkategorien sind vorhanden. assert len(other.get("/api/categories").json()) >= 6 # Kein Admin: darf keine Nutzer listen. assert other.get("/api/users").status_code == 403