2026-03-31 17:02:49 -05:00
|
|
|
"""Tests for server public key retrieval from Configuration table."""
|
|
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from wiregui.models.configuration import Configuration
|
|
|
|
|
from wiregui.utils.server_key import get_server_public_key
|
|
|
|
|
|
|
|
|
|
|
2026-03-31 21:27:46 -05:00
|
|
|
async def test_get_server_public_key_returns_key(session, monkeypatch):
|
|
|
|
|
"""Returns the public key when configured."""
|
|
|
|
|
from contextlib import asynccontextmanager
|
2026-03-31 17:02:49 -05:00
|
|
|
|
2026-03-31 21:27:46 -05:00
|
|
|
@asynccontextmanager
|
|
|
|
|
async def mock_session():
|
|
|
|
|
yield session
|
2026-03-31 17:02:49 -05:00
|
|
|
|
2026-03-31 21:27:46 -05:00
|
|
|
monkeypatch.setattr("wiregui.utils.server_key.async_session", mock_session)
|
2026-03-31 17:02:49 -05:00
|
|
|
|
2026-03-31 21:27:46 -05:00
|
|
|
c = Configuration(server_public_key="TestServerPubKey123456789012345678901234w=")
|
|
|
|
|
session.add(c)
|
|
|
|
|
await session.flush()
|
2026-03-31 17:02:49 -05:00
|
|
|
|
|
|
|
|
result = await get_server_public_key()
|
|
|
|
|
assert result == "TestServerPubKey123456789012345678901234w="
|
|
|
|
|
|
|
|
|
|
|
2026-03-31 21:27:46 -05:00
|
|
|
async def test_get_server_public_key_raises_when_missing(session, monkeypatch):
|
2026-03-31 17:02:49 -05:00
|
|
|
"""Raises RuntimeError when server_public_key is None."""
|
2026-03-31 21:27:46 -05:00
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def mock_session():
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("wiregui.utils.server_key.async_session", mock_session)
|
|
|
|
|
|
|
|
|
|
c = Configuration(server_public_key=None)
|
|
|
|
|
session.add(c)
|
|
|
|
|
await session.flush()
|
2026-03-31 17:02:49 -05:00
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError, match="not configured"):
|
|
|
|
|
await get_server_public_key()
|
|
|
|
|
|
|
|
|
|
|
2026-03-31 21:27:46 -05:00
|
|
|
async def test_get_server_public_key_raises_when_empty_string(session, monkeypatch):
|
2026-03-31 17:02:49 -05:00
|
|
|
"""Raises RuntimeError when server_public_key is empty string."""
|
2026-03-31 21:27:46 -05:00
|
|
|
from contextlib import asynccontextmanager
|
|
|
|
|
|
|
|
|
|
@asynccontextmanager
|
|
|
|
|
async def mock_session():
|
|
|
|
|
yield session
|
|
|
|
|
|
|
|
|
|
monkeypatch.setattr("wiregui.utils.server_key.async_session", mock_session)
|
|
|
|
|
|
|
|
|
|
c = Configuration(server_public_key="")
|
|
|
|
|
session.add(c)
|
|
|
|
|
await session.flush()
|
2026-03-31 17:02:49 -05:00
|
|
|
|
|
|
|
|
with pytest.raises(RuntimeError, match="not configured"):
|
2026-03-31 21:27:46 -05:00
|
|
|
await get_server_public_key()
|