fix: make keypair generation async to avoid blocking the event loop
All checks were successful
CI / test (push) Successful in 1m58s
CI / release (push) Successful in 35s
CI / docker (push) Successful in 35s

generate_keypair() used synchronous subprocess.run() which blocked
the NiceGUI event loop during wg genkey/pubkey calls. This caused
WebSocket disconnects, page reloads, and the config dialog never
appearing after device creation.

Switched to asyncio.create_subprocess_exec so the event loop stays
responsive while waiting for the wg CLI.
This commit is contained in:
Stefano Bertelli 2026-03-30 22:57:00 -05:00
parent e51c53f247
commit 92554d4089
7 changed files with 31 additions and 22 deletions

View file

@ -1,6 +1,6 @@
"""End-to-end tests for device management UI using NiceGUI's User fixture."""
from unittest.mock import patch
from unittest.mock import AsyncMock, patch
import pytest
from nicegui.testing import User
@ -25,7 +25,7 @@ async def _login(user: User):
@pytest.mark.parametrize("user", [{"storage": {}}], indirect=True)
async def test_add_device_via_ui(user: User, test_user: UserModel):
"""Test the full flow: login → devices → add device → see it in table."""
with patch("wiregui.pages.devices.generate_keypair", return_value=(FAKE_PRIVATE_KEY, FAKE_PUBLIC_KEY)), \
with patch("wiregui.pages.devices.generate_keypair", new_callable=AsyncMock, return_value=(FAKE_PRIVATE_KEY, FAKE_PUBLIC_KEY)), \
patch("wiregui.pages.devices.generate_preshared_key", return_value="cHJlc2hhcmVkMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDA="):
await _login(user)

View file

@ -103,7 +103,7 @@ def test_build_client_config_no_psk():
# --- Crypto (only if wg is installed) ---
def test_generate_keypair():
async def test_generate_keypair():
"""Test keypair generation — requires `wg` CLI to be installed."""
try:
subprocess.run(["wg", "--version"], capture_output=True, check=True)
@ -112,7 +112,7 @@ def test_generate_keypair():
from wiregui.utils.crypto import generate_keypair, generate_preshared_key
priv, pub = generate_keypair()
priv, pub = await generate_keypair()
assert len(priv) == 44 # base64-encoded 32 bytes
assert len(pub) == 44