mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-08 07:18:47 +00:00
Add test for relay reload logging
This commit is contained in:
@@ -11,6 +11,7 @@ from typing import Any, List, Optional
|
||||
|
||||
from datetime import datetime, timedelta, timezone
|
||||
import jwt
|
||||
import logging
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException, Request, Response
|
||||
import asyncio
|
||||
@@ -36,6 +37,8 @@ _RATE_LIMIT_STR = f"{_RATE_LIMIT}/{_RATE_WINDOW} seconds"
|
||||
limiter = Limiter(key_func=get_remote_address, default_limits=[_RATE_LIMIT_STR])
|
||||
app = FastAPI()
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _get_pm(request: Request) -> PasswordManager:
|
||||
pm = getattr(request.app.state, "pm", None)
|
||||
@@ -64,13 +67,13 @@ def _reload_relays(request: Request, relays: list[str]) -> None:
|
||||
pm = _get_pm(request)
|
||||
try:
|
||||
pm.nostr_client.close_client_pool()
|
||||
except Exception:
|
||||
pass
|
||||
except (OSError, RuntimeError, ValueError) as exc:
|
||||
logger.warning("Failed to close NostrClient pool: %s", exc)
|
||||
try:
|
||||
pm.nostr_client.relays = relays
|
||||
pm.nostr_client.initialize_client_pool()
|
||||
except Exception:
|
||||
pass
|
||||
except (OSError, RuntimeError, ValueError) as exc:
|
||||
logger.error("Failed to initialize NostrClient with relays %s: %s", relays, exc)
|
||||
|
||||
|
||||
def start_server(fingerprint: str | None = None) -> str:
|
||||
|
29
src/tests/test_api_reload_relays.py
Normal file
29
src/tests/test_api_reload_relays.py
Normal file
@@ -0,0 +1,29 @@
|
||||
import logging
|
||||
from types import SimpleNamespace
|
||||
|
||||
from seedpass import api
|
||||
|
||||
|
||||
def test_reload_relays_logs_errors(caplog):
|
||||
def close():
|
||||
raise RuntimeError("close fail")
|
||||
|
||||
def init():
|
||||
raise OSError("init fail")
|
||||
|
||||
pm = SimpleNamespace(
|
||||
nostr_client=SimpleNamespace(
|
||||
close_client_pool=close,
|
||||
initialize_client_pool=init,
|
||||
relays=[],
|
||||
)
|
||||
)
|
||||
request = SimpleNamespace(app=SimpleNamespace(state=SimpleNamespace(pm=pm)))
|
||||
|
||||
with caplog.at_level(logging.WARNING):
|
||||
api._reload_relays(request, ["ws://relay"])
|
||||
|
||||
assert "Failed to close NostrClient pool" in caplog.text
|
||||
assert "close fail" in caplog.text
|
||||
assert "Failed to initialize NostrClient with relays" in caplog.text
|
||||
assert "init fail" in caplog.text
|
Reference in New Issue
Block a user