[Fix]: Changes SMTP testing to use saved settings in the database (#710)

[Fix]: Changes SMTP testing to use saved settings in the database (#710)
This commit is contained in:
Dakota G
2026-03-15 09:13:47 +01:00
committed by GitHub
parent 79f4a62308
commit 30b34bc255
5 changed files with 11 additions and 44 deletions
+4 -10
View File
@@ -415,21 +415,15 @@ async def test_smtp_connection(
current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
db: AsyncSession = Depends(get_db),
):
"""Test SMTP connection with provided settings (admin only when auth enabled)."""
"""Test SMTP connection using saved settings (admin only when auth enabled)."""
import logging
logger = logging.getLogger(__name__)
try:
smtp_settings = SMTPSettings(
smtp_host=test_request.smtp_host,
smtp_port=test_request.smtp_port,
smtp_username=test_request.smtp_username,
smtp_password=test_request.smtp_password,
smtp_security=test_request.smtp_security,
smtp_auth_enabled=test_request.smtp_auth_enabled,
smtp_from_email=test_request.smtp_from_email,
)
smtp_settings = await get_smtp_settings(db)
if not smtp_settings:
return TestSMTPResponse(success=False, message="SMTP settings not configured. Save SMTP settings first.")
# Send test email
send_email(
-9
View File
@@ -100,16 +100,7 @@ class SMTPSettings(BaseModel):
class TestSMTPRequest(BaseModel):
smtp_host: str
smtp_port: int
smtp_username: str | None = None # Optional when auth is disabled
smtp_password: str | None = None # Optional when auth is disabled
smtp_security: str = "starttls" # 'starttls', 'ssl', 'none'
smtp_auth_enabled: bool = True
smtp_from_email: str
test_recipient: str
# Deprecated field for backward compatibility
smtp_use_tls: bool | None = None
class TestSMTPResponse(BaseModel):
@@ -120,12 +120,17 @@ class TestSMTPConfigAPI:
@pytest.mark.integration
async def test_test_smtp_connection(self, async_client: AsyncClient, admin_token: str):
"""POST /auth/smtp/test with mocked send_email returns success."""
await async_client.post(
"/api/v1/auth/smtp",
headers={"Authorization": f"Bearer {admin_token}"},
json=SMTP_DATA,
)
with patch("backend.app.api.routes.auth.send_email"):
response = await async_client.post(
"/api/v1/auth/smtp/test",
headers={"Authorization": f"Bearer {admin_token}"},
json={
**SMTP_DATA,
"test_recipient": "recipient@test.com",
},
)
-7
View File
@@ -2186,13 +2186,6 @@ export interface SMTPSettings {
}
export interface TestSMTPRequest {
smtp_host: string;
smtp_port: number;
smtp_username?: string;
smtp_password?: string;
smtp_security: 'starttls' | 'ssl' | 'none';
smtp_auth_enabled: boolean;
smtp_from_email: string;
test_recipient: string;
}
+1 -17
View File
@@ -26,7 +26,7 @@ export function EmailSettings() {
const { t } = useTranslation();
const { showToast } = useToast();
const queryClient = useQueryClient();
const [smtpSettings, setSMTPSettings] = useState<SMTPSettings>({
smtp_host: '',
smtp_port: 587,
@@ -144,23 +144,7 @@ export function EmailSettings() {
showToast(t('settings.email.errors.enterTestEmail'), 'error');
return;
}
if (!smtpSettings.smtp_host || !smtpSettings.smtp_from_email) {
showToast(t('settings.email.errors.smtpServerAndEmail'), 'error');
return;
}
// Validate auth fields when authentication is enabled
if (smtpSettings.smtp_auth_enabled && (!smtpSettings.smtp_username || !smtpSettings.smtp_password)) {
showToast(t('settings.email.errors.usernamePasswordRequired'), 'error');
return;
}
testMutation.mutate({
smtp_host: smtpSettings.smtp_host,
smtp_port: smtpSettings.smtp_port,
smtp_username: smtpSettings.smtp_username,
smtp_password: smtpSettings.smtp_password,
smtp_security: smtpSettings.smtp_security,
smtp_auth_enabled: smtpSettings.smtp_auth_enabled,
smtp_from_email: smtpSettings.smtp_from_email,
test_recipient: testEmail,
});
};