"""Sign-in for the web app: an administrator from the environment, users who see some domains, a session cookie, and an API that answers everyone as before.""" import pytest from fastapi.testclient import TestClient from webapi.main import app @pytest.fixture def client(own_platform, monkeypatch): monkeypatch.setenv("AUTH_ADMIN_EMAIL", "admin@isssteleon.mx") monkeypatch.setenv("AUTH_ADMIN_PASSWORD", "una-clave-larga") with TestClient(app) as c: yield c def login(client, email, password): return client.post("/auth/login", data={"username": email, "password": password}) def test_the_first_administrator_signs_in_and_adds_a_team(client): assert client.get("/users/me").status_code == 401 assert login(client, "admin@isssteleon.mx", "mala").status_code == 400 assert login(client, "admin@isssteleon.mx", "una-clave-larga").status_code == 204 me = client.get("/users/me").json() assert me["is_superuser"] and me["domains"] == ["cacu", "cama", "tea"] team = {"email": "cacu@isssteleon.mx", "password": "equipo-cacu", "name": "Equipo CaCu", "domains": ["cacu"]} # fmt: skip assert client.post("/users/register", json=team).status_code == 201 client.post("/auth/logout") assert login(client, "cacu@isssteleon.mx", "equipo-cacu").status_code == 204 assert client.get("/users/me").json()["domains"] == ["cacu"] other = {"email": "x@isssteleon.mx", "password": "otra-clave"} assert client.post("/users/register", json=other).status_code == 403 # not an admin def test_the_api_answers_without_a_session(client): # routes that read no platform data (the in-process app's database is the default one) for path in ("/health", "/domains", "/ingest/schema", "/ingest/sources/types"): assert client.get(path).status_code == 200, path def test_a_restart_keeps_the_administrator_once_and_the_session_secret(client, own_platform): assert login(client, "admin@isssteleon.mx", "una-clave-larga").status_code == 204 secret = (own_platform / "auth_secret").read_text() with TestClient(app): # the app starts again on the same data pass assert (own_platform / "auth_secret").read_text() == secret assert client.get("/users/me").status_code == 200 # the session still holds