CE-1068 - Move validateEmailAddresses to utils class; add validation when creating scheduled report

This commit is contained in:
2024-05-01 16:53:12 -05:00
parent e022284ca7
commit 8ca59e0a5b
4 changed files with 156 additions and 40 deletions

View File

@ -31,6 +31,7 @@ import com.kingsrook.qqq.backend.core.actions.processes.QProcessCallbackFactory;
import com.kingsrook.qqq.backend.core.actions.processes.RunProcessAction;
import com.kingsrook.qqq.backend.core.actions.tables.DeleteAction;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessInput;
import com.kingsrook.qqq.backend.core.model.actions.processes.RunProcessOutput;
@ -46,6 +47,8 @@ import com.kingsrook.qqq.backend.core.model.scheduledjobs.ScheduledJob;
import com.kingsrook.qqq.backend.core.model.statusmessages.BadInputStatusMessage;
import com.kingsrook.qqq.backend.core.processes.implementations.etl.streamedwithfrontend.StreamedETLWithFrontendProcess;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import com.kingsrook.qqq.backend.core.utils.ValidationUtils;
import org.quartz.CronScheduleBuilder;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
@ -99,6 +102,23 @@ public class ScheduledReportTableCustomizer implements TableCustomizerInterface
{
record.addError(new BadInputStatusMessage("Cron Expression [" + cronExpression + "] is not valid: " + e.getMessage()));
}
try
{
String toAddresses = record.getValueString("toAddresses");
if(StringUtils.hasContent(toAddresses))
{
ValidationUtils.parseAndValidateEmailAddresses(toAddresses);
}
}
catch(QUserFacingException ufe)
{
record.addError(new BadInputStatusMessage(ufe.getMessage()));
}
catch(Exception e)
{
record.addError(new BadInputStatusMessage("To Addresses is not valid: " + e.getMessage()));
}
}
}

View File

@ -30,7 +30,6 @@ import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@ -42,7 +41,6 @@ import com.kingsrook.qqq.backend.core.actions.tables.StorageAction;
import com.kingsrook.qqq.backend.core.actions.tables.UpdateAction;
import com.kingsrook.qqq.backend.core.context.QContext;
import com.kingsrook.qqq.backend.core.exceptions.QException;
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
import com.kingsrook.qqq.backend.core.logging.QLogger;
import com.kingsrook.qqq.backend.core.model.actions.messaging.Content;
import com.kingsrook.qqq.backend.core.model.actions.messaging.MultiParty;
@ -68,7 +66,7 @@ import com.kingsrook.qqq.backend.core.model.savedreports.SavedReport;
import com.kingsrook.qqq.backend.core.utils.CollectionUtils;
import com.kingsrook.qqq.backend.core.utils.ExceptionUtils;
import com.kingsrook.qqq.backend.core.utils.StringUtils;
import org.apache.commons.validator.EmailValidator;
import com.kingsrook.qqq.backend.core.utils.ValidationUtils;
import static com.kingsrook.qqq.backend.core.logging.LogUtils.logPair;
@ -111,7 +109,7 @@ public class RenderSavedReportExecuteStep implements BackendStep
List<String> toEmailAddressList = new ArrayList<>();
if(StringUtils.hasContent(sendToEmailAddress))
{
toEmailAddressList = validateEmailAddresses(sendToEmailAddress);
toEmailAddressList = ValidationUtils.parseAndValidateEmailAddresses(sendToEmailAddress);
}
StorageAction storageAction = new StorageAction();
@ -222,42 +220,6 @@ public class RenderSavedReportExecuteStep implements BackendStep
/*******************************************************************************
**
*******************************************************************************/
private List<String> validateEmailAddresses(String sendToEmailAddress) throws QUserFacingException
{
////////////////////////////////////////////////////////////////
// split email address string on spaces, comma, and semicolon //
////////////////////////////////////////////////////////////////
List<String> toEmailAddressList = Arrays.asList(sendToEmailAddress.split("[\\s,;]+"));
//////////////////////////////////////////////////////
// check each address keeping track of any bad ones //
//////////////////////////////////////////////////////
List<String> invalidEmails = new ArrayList<>();
EmailValidator validator = EmailValidator.getInstance();
for(String emailAddress : toEmailAddressList)
{
if(!validator.isValid(emailAddress))
{
invalidEmails.add(emailAddress);
}
}
///////////////////////////////////////
// if bad one found, throw exception //
///////////////////////////////////////
if(!invalidEmails.isEmpty())
{
throw (new QUserFacingException("The following email addresses were invalid: " + StringUtils.join(",", invalidEmails)));
}
return (toEmailAddressList);
}
/*******************************************************************************
**
*******************************************************************************/

View File

@ -0,0 +1,72 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2024. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.core.utils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
import org.apache.commons.validator.EmailValidator;
/*******************************************************************************
**
*******************************************************************************/
public class ValidationUtils
{
/*******************************************************************************
**
*******************************************************************************/
public static List<String> parseAndValidateEmailAddresses(String emailAddresses) throws QUserFacingException
{
////////////////////////////////////////////////////////////////
// split email address string on spaces, comma, and semicolon //
////////////////////////////////////////////////////////////////
List<String> toEmailAddressList = Arrays.asList(emailAddresses.split("[\\s,;]+"));
//////////////////////////////////////////////////////
// check each address keeping track of any bad ones //
//////////////////////////////////////////////////////
List<String> invalidEmails = new ArrayList<>();
EmailValidator validator = EmailValidator.getInstance();
for(String emailAddress : toEmailAddressList)
{
if(!validator.isValid(emailAddress))
{
invalidEmails.add(emailAddress);
}
}
///////////////////////////////////////
// if bad one found, throw exception //
///////////////////////////////////////
if(!invalidEmails.isEmpty())
{
throw (new QUserFacingException("The following email addresses were invalid: " + StringUtils.join(",", invalidEmails)));
}
return (toEmailAddressList);
}
}

View File

@ -0,0 +1,62 @@
/*
* QQQ - Low-code Application Framework for Engineers.
* Copyright (C) 2021-2024. Kingsrook, LLC
* 651 N Broad St Ste 205 # 6917 | Middletown DE 19709 | United States
* contact@kingsrook.com
* https://github.com/Kingsrook/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.kingsrook.qqq.backend.core.utils;
import java.util.List;
import com.kingsrook.qqq.backend.core.BaseTest;
import com.kingsrook.qqq.backend.core.exceptions.QUserFacingException;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
/*******************************************************************************
** Unit test for ValidationUtils
*******************************************************************************/
class ValidationUtilsTest extends BaseTest
{
/*******************************************************************************
**
*******************************************************************************/
@Test
void test() throws QUserFacingException
{
assertThatThrownBy(() -> ValidationUtils.parseAndValidateEmailAddresses("notEmail"))
.isInstanceOf(QUserFacingException.class)
.hasMessageContaining("email addresses were invalid: notEmail");
assertThatThrownBy(() -> ValidationUtils.parseAndValidateEmailAddresses("foo@bar.com, whatever"))
.isInstanceOf(QUserFacingException.class)
.hasMessageContaining("email addresses were invalid: whatever");
assertThatThrownBy(() -> ValidationUtils.parseAndValidateEmailAddresses("foo whatever"))
.isInstanceOf(QUserFacingException.class)
.hasMessageContaining("email addresses were invalid: foo,whatever");
assertEquals(List.of("foo@bar.com"), ValidationUtils.parseAndValidateEmailAddresses("foo@bar.com ")); // space here intentional!
assertEquals(List.of("foo@bar.com"), ValidationUtils.parseAndValidateEmailAddresses("foo@bar.com;"));
assertEquals(List.of("foo@bar.com", "fiz@buz.com"), ValidationUtils.parseAndValidateEmailAddresses("foo@bar.com, fiz@buz.com"));
}
}