diff --git a/tests/test_api.py b/tests/test_api.py index 2664647..0e3f2f0 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -4,8 +4,11 @@ import respx from fastapi.testclient import TestClient from httpx import Response +from app import auth as auth_module from app.conf_store import config_path +from app.database import SessionLocal from app.main import app +from app.models import User def configure_radarr(client: TestClient) -> None: @@ -145,3 +148,41 @@ def test_settings_persist_to_conf_json() -> None: assert raw["qbittorrent"]["port"] == 8080 assert raw["qbittorrent"]["username"] == "admin" assert raw["qbittorrent"]["password"] == "pass" + + +def test_setup_link_only_without_existing_account() -> None: + """Le lien de premier lancement ne doit pas apparaitre si un compte existe. + + Verifie aussi que l'endpoint de setup refuse tout second compte, meme avec + un jeton valide. + """ + with TestClient(app) as client: + client.post("/api/auth/setup", json={"token": "invalide", "username": "x", "password": "y"}) + # Nettoyage: la base de test est partagee entre les tests. + with SessionLocal() as db: + db.query(User).delete() + db.commit() + # Simule un redemarrage: le lifespan relance bootstrap_setup. + with TestClient(app): + assert auth_module.setup_token is not None + with TestClient(app) as client: + response = client.post( + "/api/auth/setup", + json={ + "token": auth_module.setup_token, + "username": "admin", + "password": "pass", + }, + ) + assert response.status_code == 200 + # Le jeton est consomme apres creation du compte. + assert auth_module.setup_token is None + # Nouveau demarrage: un compte existe -> plus aucun jeton genere, + # et la creation d'un second compte est refusee. + with TestClient(app) as client: + assert auth_module.setup_token is None + response = client.post( + "/api/auth/setup", + json={"token": "peu-importe", "username": "autre", "password": "z"}, + ) + assert response.status_code == 403