fix: seed admin user on startup and run sync as background task

- Seed/update admin user from ADMIN_USERNAME/ADMIN_PASSWORD settings on
  startup (admin_users table was empty, login was impossible)
- POST /api/admin/sync now returns immediately and runs the equipment
  sync via BackgroundTasks (full sync took minutes and hit nginx 504)
- run_sync accepts optional sync_log_id to reuse a pre-created log entry
- Adapt admin router test to background sync behavior
This commit is contained in:
Agent Zero
2026-09-24 21:41:35 +02:00
parent 709190719f
commit e66bcc7058
4 changed files with 77 additions and 18 deletions
+7 -4
View File
@@ -35,15 +35,18 @@ async def test_sync_with_valid_token(client, seeded_admin, test_db):
})
token = login_resp.json()["access_token"]
# Mock sync service
# Mock sync service (background task)
with patch.object(SyncService, "run_sync", new_callable=AsyncMock) as mock_sync:
mock_sync.return_value = {"sync_id": 42, "items_processed": 10, "items_failed": 0, "status": "completed"}
mock_sync.return_value = {"sync_id": 1, "items_processed": 10, "items_failed": 0, "status": "completed"}
resp = await client.post("/api/admin/sync", cookies={"hms_admin_token": token})
assert resp.status_code == 200
data = resp.json()
assert data["sync_id"] == 42
assert data["status"] == "completed"
# Background sync: request returns immediately with a running log entry
assert data["sync_id"] == 1
assert data["status"] == "running"
# Background task executed by TestClient: run_sync was called once
mock_sync.assert_awaited_once()
@pytest.mark.asyncio