From f273ae70dbb5260a4f2059d005ac3bbee7585898 Mon Sep 17 00:00:00 2001 From: Jon Koops Date: Wed, 6 Oct 2021 22:54:33 +0200 Subject: [PATCH] Fix connection testing for realm e-mail (#1303) --- src/realm-settings/EmailTab.tsx | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/realm-settings/EmailTab.tsx b/src/realm-settings/EmailTab.tsx index ea46e53034..272c44f8d3 100644 --- a/src/realm-settings/EmailTab.tsx +++ b/src/realm-settings/EmailTab.tsx @@ -60,7 +60,7 @@ export const RealmSettingsEmailTab = ({ const authenticationEnabled = useWatch({ control, name: "smtpServer.authentication", - defaultValue: {}, + defaultValue: "", }); const handleModalToggle = () => { @@ -104,10 +104,31 @@ export const RealmSettingsEmailTab = ({ }; const testConnection = async () => { + const serverSettings = { ...getValues()["smtpServer"] }; + + // Code below uses defensive coding as the server configuration uses an ambiguous record type. + if (typeof serverSettings.port === "string") { + serverSettings.port = Number(serverSettings.port); + } + + if (typeof serverSettings.ssl === "string") { + serverSettings.ssl = serverSettings.ssl === true.toString(); + } + + if (typeof serverSettings.starttls === "string") { + serverSettings.starttls = serverSettings.starttls === true.toString(); + } + + // For some reason the API wants a duplicate field for the authentication status. + // Somebody thought this was a good idea, so here we are. + if (serverSettings.authentication === true.toString()) { + serverSettings.auth = true; + } + try { await adminClient.realms.testSMTPConnection( { realm: realm.realm! }, - getValues()["smtpServer"] || {} + serverSettings ); addAlert(t("testConnectionSuccess"), AlertVariant.success); } catch (error) { @@ -311,7 +332,7 @@ export const RealmSettingsEmailTab = ({ (