test fixes

This commit is contained in:
2025-10-19 19:57:42 +02:00
parent d698ae50a2
commit d87ec398bb
10 changed files with 943 additions and 153 deletions

View File

@@ -10,22 +10,28 @@ from src.server.services.auth_service import auth_service
def reset_auth_state():
"""Reset auth service state before each test."""
# Clear any rate limiting state and password hash
if hasattr(auth_service, '_failed'):
auth_service._failed.clear()
# Force clear all keys in _failed dict
auth_service._failed.clear()
auth_service._hash = None
yield
# Cleanup after test
if hasattr(auth_service, '_failed'):
auth_service._failed.clear()
auth_service._failed.clear()
auth_service._hash = None
@pytest.mark.anyio
@pytest.mark.asyncio
async def test_auth_flow_setup_login_status_logout():
"""Test complete authentication flow."""
transport = ASGITransport(app=app)
async with AsyncClient(transport=transport, base_url="http://test") as client:
async with AsyncClient(
transport=transport, base_url="http://test"
) as client:
# Setup
r = await client.post("/api/auth/setup", json={"master_password": "Aa!strong1"})
r = await client.post(
"/api/auth/setup", json={"master_password": "Aa!strong1"}
)
assert r.status_code == 201
# Bad login
@@ -33,7 +39,9 @@ async def test_auth_flow_setup_login_status_logout():
assert r.status_code == 401
# Good login
r = await client.post("/api/auth/login", json={"password": "Aa!strong1"})
r = await client.post(
"/api/auth/login", json={"password": "Aa!strong1"}
)
assert r.status_code == 200
data = r.json()
assert "access_token" in data
@@ -46,11 +54,14 @@ async def test_auth_flow_setup_login_status_logout():
assert r.json()["configured"] is True
# Status authenticated with header
r = await client.get("/api/auth/status", headers={"Authorization": f"Bearer {token}"})
auth_header = {"Authorization": f"Bearer {token}"}
r = await client.get("/api/auth/status", headers=auth_header)
assert r.status_code == 200
assert r.json()["authenticated"] is True
# Logout
r = await client.post("/api/auth/logout", headers={"Authorization": f"Bearer {token}"})
r = await client.post(
"/api/auth/logout", headers=auth_header
)
assert r.status_code == 200