From c3214fa245a2cafbb807e70db60383309597eaff Mon Sep 17 00:00:00 2001 From: Robin Beer Date: Thu, 24 Oct 2024 11:57:33 +0100 Subject: [PATCH] refactor(Update to send email): Sends accept depentent email to related user on create/update if app --- .../tripmonitor/TrustedCompanion.java | 25 +++++++---- src/main/resources/Message.properties | 2 +- .../api/MonitoredTripControllerTest.java | 1 + .../api/OtpUserControllerTest.java | 1 + .../testutils/PersistenceTestUtils.java | 4 +- .../tripmonitor/TrustedCompanionTest.java | 43 +++++++++++++++++++ 6 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 src/test/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanionTest.java diff --git a/src/main/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanion.java b/src/main/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanion.java index c75f1a3d..4957393d 100644 --- a/src/main/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanion.java +++ b/src/main/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanion.java @@ -79,12 +79,16 @@ public static OtpUser acceptDependent(Request request, Response response) { return relatedUser; } + public static void manageAcceptDependentEmail(OtpUser dependentUser) { + manageAcceptDependentEmail(dependentUser, false); + } + /** * When creating or updating an OTP user, extract a list of newly defined dependents and send an 'accept dependent' * email to each. Then update which dependents have been sent an email so subsequent updates do not trigger * additional emails. */ - public static void manageAcceptDependentEmail(OtpUser dependentUser) { + public static void manageAcceptDependentEmail(OtpUser dependentUser, boolean isTest) { if (dependentUser.relatedUsers.isEmpty()) { // No related users defined by dependent. return; @@ -94,8 +98,8 @@ public static void manageAcceptDependentEmail(OtpUser dependentUser) { .stream() .filter(relatedUser -> !relatedUser.acceptDependentEmailSent) .forEach(relatedUser -> { - OtpUser user = Persistence.otpUsers.getById(relatedUser.userId); - if (user != null && sendAcceptDependentEmail(dependentUser, user)) { + OtpUser userToReceiveEmail = Persistence.otpUsers.getById(relatedUser.userId); + if (userToReceiveEmail != null && (isTest || sendAcceptDependentEmail(dependentUser, userToReceiveEmail))) { relatedUser.acceptDependentEmailSent = true; } }); @@ -114,16 +118,19 @@ private static boolean sendAcceptDependentEmail(OtpUser dependentUser, OtpUser r String acceptDependentUrl = getAcceptDependentUrl(dependentUser); // A HashMap is needed instead of a Map for template data to be serialized to the template renderer. - Map templateData = new HashMap<>(Map.of( + Map templateData = new HashMap<>( + Map.of( "acceptDependentLinkAnchorLabel", acceptDependentLinkLabel, "acceptDependentLinkLabelAndUrl", label(acceptDependentLinkLabel, acceptDependentUrl, locale), "acceptDependentUrl", getAcceptDependentUrl(dependentUser), "emailFooter", Message.ACCEPT_DEPENDENT_EMAIL_FOOTER.get(locale), - "emailGreeting", String.format("%s%s", dependentUser.email, Message.ACCEPT_DEPENDENT_EMAIL_GREETING.get(locale)), - // TODO: This is required in the `OtpUserContainer.ftl` template. Not sure what to link to so providing link back to settings. - "manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH), + // TODO: The user's email address isn't very personal, but that is all I have to work with! Suggetions? + "emailGreeting", String.format("%s%s", dependentUser.email, Message.ACCEPT_DEPENDENT_EMAIL_GREETING.get(locale)), + // TODO: This is required in the `OtpUserContainer.ftl` template. Not sure what to provide. Suggestions? + "manageLinkUrl", String.format("%s%s", OTP_UI_URL, SETTINGS_PATH), "manageLinkText", Message.ACCEPT_DEPENDENT_EMAIL_MANAGE.get(locale) - )); + ) + ); return NotificationUtils.sendEmail( relatedUser, @@ -135,7 +142,7 @@ private static boolean sendAcceptDependentEmail(OtpUser dependentUser, OtpUser r } private static String getAcceptDependentUrl(OtpUser dependentUser) { - // TODO: Is OTP_UI_URL the correct base URL to user here? If not, what?! + // TODO: Is OTP_UI_URL the correct base URL to user here? I'm not sure. return String.format("%s%s?userId=%s", OTP_UI_URL, ACCEPT_DEPENDENT_PATH, dependentUser.id); } } diff --git a/src/main/resources/Message.properties b/src/main/resources/Message.properties index 77a8b273..f7a0e99e 100644 --- a/src/main/resources/Message.properties +++ b/src/main/resources/Message.properties @@ -1,5 +1,5 @@ ACCEPT_DEPENDENT_EMAIL_FOOTER = You are receiving this email because you have been selected to be a trusted companion. -ACCEPT_DEPENDENT_EMAIL_GREETING = %s would like you to be their trusted companion. +ACCEPT_DEPENDENT_EMAIL_GREETING = %s would like you to be a trusted companion. ACCEPT_DEPENDENT_EMAIL_LINK_TEXT = Accept trusted companion ACCEPT_DEPENDENT_EMAIL_SUBJECT = Trusted companion request ACCEPT_DEPENDENT_EMAIL_MANAGE = Manage settings diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/MonitoredTripControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/MonitoredTripControllerTest.java index 6f32e75c..578270c7 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/MonitoredTripControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/MonitoredTripControllerTest.java @@ -104,6 +104,7 @@ public static void tearDown() { multiAdminUser = Persistence.adminUsers.getById(multiAdminUser.id); if (multiAdminUser != null) multiAdminUser.delete(); deleteOtpUser( + IS_END_TO_END, soloOtpUser, multiOtpUser ); diff --git a/src/test/java/org/opentripplanner/middleware/controllers/api/OtpUserControllerTest.java b/src/test/java/org/opentripplanner/middleware/controllers/api/OtpUserControllerTest.java index 70a1b322..0cf3c34d 100644 --- a/src/test/java/org/opentripplanner/middleware/controllers/api/OtpUserControllerTest.java +++ b/src/test/java/org/opentripplanner/middleware/controllers/api/OtpUserControllerTest.java @@ -76,6 +76,7 @@ public static void setUp() throws Exception { @AfterAll public static void tearDown() { deleteOtpUser( + IS_END_TO_END, otpUser, relatedUserOne, relatedUserTwo, diff --git a/src/test/java/org/opentripplanner/middleware/testutils/PersistenceTestUtils.java b/src/test/java/org/opentripplanner/middleware/testutils/PersistenceTestUtils.java index 7ba62494..420c5624 100644 --- a/src/test/java/org/opentripplanner/middleware/testutils/PersistenceTestUtils.java +++ b/src/test/java/org/opentripplanner/middleware/testutils/PersistenceTestUtils.java @@ -268,12 +268,12 @@ static Itinerary createItinerary() { return itinerary; } - public static void deleteOtpUser(OtpUser... optUsers) { + public static void deleteOtpUser(boolean isEndToEnd, OtpUser... optUsers) { for (OtpUser otpUser : optUsers) { if (otpUser != null) { OtpUser user = Persistence.otpUsers.getById(otpUser.id); if (user != null) { - user.delete(user.auth0UserId != null); + user.delete(isEndToEnd); } } } diff --git a/src/test/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanionTest.java b/src/test/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanionTest.java new file mode 100644 index 00000000..2151eeb0 --- /dev/null +++ b/src/test/java/org/opentripplanner/middleware/tripmonitor/TrustedCompanionTest.java @@ -0,0 +1,43 @@ +package org.opentripplanner.middleware.tripmonitor; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.opentripplanner.middleware.models.OtpUser; +import org.opentripplanner.middleware.models.RelatedUser; +import org.opentripplanner.middleware.persistence.Persistence; +import org.opentripplanner.middleware.testutils.ApiTestUtils; +import org.opentripplanner.middleware.testutils.OtpMiddlewareTestEnvironment; +import org.opentripplanner.middleware.testutils.PersistenceTestUtils; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.opentripplanner.middleware.auth.Auth0Connection.setAuthDisabled; +import static org.opentripplanner.middleware.testutils.PersistenceTestUtils.deleteOtpUser; + +public class TrustedCompanionTest extends OtpMiddlewareTestEnvironment { + private static OtpUser relatedUserOne; + private static OtpUser dependentUserOne; + + @BeforeAll + public static void setUp() { + setAuthDisabled(false); + relatedUserOne = PersistenceTestUtils.createUser(ApiTestUtils.generateEmailAddress("related-user-one")); + dependentUserOne = PersistenceTestUtils.createUser(ApiTestUtils.generateEmailAddress("dependent-one")); + } + + @AfterAll + public static void tearDown() { + deleteOtpUser(IS_END_TO_END, + relatedUserOne, + dependentUserOne + ); + } + + @Test + void canManageAcceptDependentEmail() { + dependentUserOne.relatedUsers.add(new RelatedUser(relatedUserOne.id, relatedUserOne.email, RelatedUser.RelatedUserStatus.PENDING)); + Persistence.otpUsers.replace(dependentUserOne.id, dependentUserOne); + TrustedCompanion.manageAcceptDependentEmail(dependentUserOne, true); + assertTrue(dependentUserOne.relatedUsers.get(0).acceptDependentEmailSent); + } +} \ No newline at end of file