fix(accounts): add POST /accounts/{id}/test combined endpoint (BIT-26141)

The admin UI calls POST /accounts/{id}/test when the user clicks "Testar
ligação", but only granular /test/imap, /test/smtp, /test/send endpoints
existed — causing a 404. Adds the missing combined endpoint that runs
IMAP + SMTP tests in parallel and returns the ConnectionTestResult schema
the frontend expects (imap_ok, smtp_ok, bizdocs_ok).

Paperclip-Issue: BIT-26141
This commit is contained in:
Xika committed 2026-07-01 01:13:06 +00:00
1 parent c281b2b56d
commit c834560f81
1 file changed
+48
+48
View File
@@ -1,8 +1,11 @@
from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor
from fastapi import APIRouter, HTTPException, Query, status
from forwarder.api.deps import CurrentUser, DbSession
from forwarder.api.routes.connection import ConnectionTestResult
from forwarder.api.schemas import (
AccountCreate,
AccountResponse,
@@ -11,6 +14,9 @@ from forwarder.api.schemas import (
InlineConnectionConfig,
SyncRunResponse,
)
from forwarder.crypto import decrypt_password
from forwarder.email.imap_client import ImapConfig, test_imap_connection
from forwarder.email.smtp_client import SmtpConfig, test_smtp_connection
from forwarder.services import account_service
from forwarder.services.sync_service import sync_account
@@ -122,6 +128,48 @@ def _inline_dict(inline: InlineConnectionConfig | None) -> dict | None:
return data
@router.post("/{account_id}/test", response_model=ConnectionTestResult)
def test_account(
account_id: int,
db: DbSession,
_user: CurrentUser,
) -> ConnectionTestResult:
"""Run IMAP + SMTP tests for a saved account (combined result for the UI)."""
try:
account = account_service.get_account(db, account_id)
except account_service.AccountNotFoundError as exc:
raise HTTPException(status_code=404, detail=str(exc)) from exc
imap_cfg = ImapConfig(
host=account.imap_host,
port=account.imap_port,
user=account.imap_user,
password=decrypt_password(account.imap_password_encrypted),
)
smtp_cfg = SmtpConfig(
host=account.smtp_host,
port=account.smtp_port,
user=account.smtp_user,
password=decrypt_password(account.smtp_password_encrypted),
use_tls=account.smtp_use_tls,
)
with ThreadPoolExecutor(max_workers=2) as executor:
imap_future = executor.submit(test_imap_connection, imap_cfg)
smtp_future = executor.submit(test_smtp_connection, smtp_cfg)
imap_ok, imap_msg = imap_future.result()
smtp_ok, smtp_msg = smtp_future.result()
return ConnectionTestResult(
imap_ok=imap_ok,
imap_error=None if imap_ok else imap_msg,
smtp_ok=smtp_ok,
smtp_error=None if smtp_ok else smtp_msg,
bizdocs_ok=True,
bizdocs_error=None,
)
@router.post("/{account_id}/test/imap", response_model=ConnectionTestResponse)
def test_imap(
account_id: int,