Files
OpenVPN-Monitoring-Simple/APP_PROFILER/test_async_pki_init.py
T
ayurishchevandClaude Sonnet 5.5 d251ec73d0 Certificates: profile modal with notes, note tooltip; async PKI init
- Profiler: UserProfile.note (nullable, startup migration), PUT
  /profiles/{id}/note (max 1000 chars); add missing `requests`
  dependency (client creation failed without Public IP set).
- UI: clickable rows on Certificates open a compact profile details
  modal with an editable note; tooltip with the note on the client
  name (v-note-tooltip) and a note icon.
- PKI init runs in a background thread: POST /system/init returns 202,
  GET /system/init/status, 409 for a second start, Clear PKI and client
  creation are blocked while it runs. A marker file identifies an
  interrupted run (only such a PKI is wiped on the next Init).
- PKI Configuration page: state from the server, progress spinner and
  step text, polling, Init/Clear/Save locked while running, Init locked
  once initialized.
- Tests: one pytest each for notes and async init (requirements-dev.txt).
- Docs: plans and summaries in DOCS/Changes, API reference, README, Index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-04 20:56:36 +03:00

60 lines
2.0 KiB
Python

import os
import tempfile
import threading
import time
# The DB path must be set before database.py is imported (via main)
_tmp = tempfile.TemporaryDirectory()
os.environ["OVPMON_PROFILER_DB_PATH"] = os.path.join(_tmp.name, "test.db")
from fastapi.testclient import TestClient
from main import app
from services import pki
from utils.auth import verify_token
app.dependency_overrides[verify_token] = lambda: {"sub": "test"}
client = TestClient(app)
def test_async_init_runs_once(monkeypatch):
pki_dir = os.path.join(_tmp.name, "pki")
release = threading.Event()
def touch(*parts):
path = os.path.join(pki_dir, *parts)
os.makedirs(os.path.dirname(path), exist_ok=True)
open(path, "w").close()
def fake_easyrsa(command, env):
if command[0] == "build-server-full":
touch("issued", command[1] + ".crt")
elif command[0] == "gen-dh":
release.wait(5) # keeps the job running until the test lets go
touch("dh.pem")
elif command[0] == "gen-crl":
touch("crl.pem")
elif "build-ca" in command:
touch("ca.crt")
monkeypatch.setattr(pki, "PKI_DIR", pki_dir)
monkeypatch.setattr(pki, "_run_easyrsa", fake_easyrsa)
monkeypatch.setattr(pki, "_gen_ta_key", lambda: touch("ta.key"))
monkeypatch.setattr(pki, "_publish_crl", lambda: True)
res = client.post("/api/system/init")
assert res.status_code == 202 and res.json() == {"state": "running"}
assert client.post("/api/system/init").status_code == 409
assert client.get("/api/system/init/status").json()["state"] == "running"
assert client.delete("/api/system/pki").status_code == 409
release.set()
deadline = time.time() + 5
while time.time() < deadline:
status = client.get("/api/system/init/status").json()
if status["state"] != "running":
break
time.sleep(0.05)
assert status["state"] == "done" and status["initialized"] is True
assert client.post("/api/system/init").status_code == 409