"""Fleet token / ticket unit tests.""" from __future__ import annotations import pytest from fastapi import HTTPException from app.api.fleet import ( clear_fleet_token, consume_login_ticket, create_login_ticket, fleet_token_configured, require_fleet_token, set_fleet_token, ) from app.models.db import Database, set_db @pytest.fixture() def db(tmp_path, monkeypatch): monkeypatch.setenv("MODE", "SIM") path = tmp_path / "t.db" d = Database(path) set_db(d) yield d d.close() set_db(None) def test_fleet_token_hash_and_auth(db): assert not fleet_token_configured(db) set_fleet_token("test-fleet-token-32chars-xxxx", db) assert fleet_token_configured(db) assert require_fleet_token(x_fleet_token="test-fleet-token-32chars-xxxx") with pytest.raises(HTTPException) as ei: require_fleet_token(x_fleet_token="wrong-token-xxxxxxxxxx") assert ei.value.status_code == 401 clear_fleet_token(db) with pytest.raises(HTTPException): require_fleet_token(x_fleet_token="test-fleet-token-32chars-xxxx") def test_login_ticket_once(db): t, ttl = create_login_ticket("admin") assert ttl >= 30 assert consume_login_ticket(t) == "admin" with pytest.raises(HTTPException): consume_login_ticket(t)