Parcourir la source

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

[Fix]: Changes SMTP testing to use saved settings in the database (#710)
Dakota G il y a 2 mois
Parent
commit
30b34bc255

+ 4 - 10
backend/app/api/routes/auth.py

@@ -415,21 +415,15 @@ async def test_smtp_connection(
     current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
     current_user: User | None = RequirePermissionIfAuthEnabled(Permission.SETTINGS_UPDATE),
     db: AsyncSession = Depends(get_db),
     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
     import logging
 
 
     logger = logging.getLogger(__name__)
     logger = logging.getLogger(__name__)
 
 
     try:
     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 test email
         send_email(
         send_email(

+ 0 - 9
backend/app/schemas/auth.py

@@ -100,16 +100,7 @@ class SMTPSettings(BaseModel):
 
 
 
 
 class TestSMTPRequest(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
     test_recipient: str
-    # Deprecated field for backward compatibility
-    smtp_use_tls: bool | None = None
 
 
 
 
 class TestSMTPResponse(BaseModel):
 class TestSMTPResponse(BaseModel):

+ 6 - 1
backend/tests/integration/test_advanced_auth_api.py

@@ -120,12 +120,17 @@ class TestSMTPConfigAPI:
     @pytest.mark.integration
     @pytest.mark.integration
     async def test_test_smtp_connection(self, async_client: AsyncClient, admin_token: str):
     async def test_test_smtp_connection(self, async_client: AsyncClient, admin_token: str):
         """POST /auth/smtp/test with mocked send_email returns success."""
         """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"):
         with patch("backend.app.api.routes.auth.send_email"):
             response = await async_client.post(
             response = await async_client.post(
                 "/api/v1/auth/smtp/test",
                 "/api/v1/auth/smtp/test",
                 headers={"Authorization": f"Bearer {admin_token}"},
                 headers={"Authorization": f"Bearer {admin_token}"},
                 json={
                 json={
-                    **SMTP_DATA,
                     "test_recipient": "recipient@test.com",
                     "test_recipient": "recipient@test.com",
                 },
                 },
             )
             )

+ 0 - 7
frontend/src/api/client.ts

@@ -2186,13 +2186,6 @@ export interface SMTPSettings {
 }
 }
 
 
 export interface TestSMTPRequest {
 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;
   test_recipient: string;
 }
 }
 
 

+ 1 - 17
frontend/src/components/EmailSettings.tsx

@@ -26,7 +26,7 @@ export function EmailSettings() {
   const { t } = useTranslation();
   const { t } = useTranslation();
   const { showToast } = useToast();
   const { showToast } = useToast();
   const queryClient = useQueryClient();
   const queryClient = useQueryClient();
-  
+
   const [smtpSettings, setSMTPSettings] = useState<SMTPSettings>({
   const [smtpSettings, setSMTPSettings] = useState<SMTPSettings>({
     smtp_host: '',
     smtp_host: '',
     smtp_port: 587,
     smtp_port: 587,
@@ -144,23 +144,7 @@ export function EmailSettings() {
       showToast(t('settings.email.errors.enterTestEmail'), 'error');
       showToast(t('settings.email.errors.enterTestEmail'), 'error');
       return;
       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({
     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,
       test_recipient: testEmail,
     });
     });
   };
   };