Skip to content

Commit

Permalink
improvement(OtpUserController): Preserve OtpUser.smsConsentDate as it…
Browse files Browse the repository at this point in the history
… is not expected from incoming
  • Loading branch information
binh-dam-ibigroup committed Nov 15, 2023
1 parent 144484b commit bf434c1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -129,14 +129,18 @@ U preUpdateHook(U user, U preExistingUser, Request req) {
if (user instanceof OtpUser) {
OtpUser otpUser = (OtpUser) user;
OtpUser existingOtpUser = (OtpUser) preExistingUser;
if(!otpUser.storeTripHistory && existingOtpUser.storeTripHistory) {
if (!otpUser.storeTripHistory && existingOtpUser.storeTripHistory) {
// If an OTP user no longer wants their trip history stored, remove all history from MongoDB.
ConnectedDataManager.removeUsersTripHistory(otpUser.id);
// Kick-off a trip history upload job to recompile and upload trip data to S3 minus the user's trip
// history.
TripHistoryUploadJob tripHistoryUploadJob = new TripHistoryUploadJob();
tripHistoryUploadJob.run();
}

// Include select attributes from existingOtpUser marked @JsonIgnore and
// that are not set in otpUser.
otpUser.smsConsentDate = existingOtpUser.smsConsentDate;
}
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public enum Notification {
* If the user starts the phone verification process, this field is populated
* just before the verification code is sent.
*/
@JsonIgnore
public Date smsConsentDate;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.opentripplanner.middleware.controllers.api;

import org.eclipse.jetty.http.HttpMethod;
import org.eclipse.jetty.http.HttpStatus;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand All @@ -14,10 +17,13 @@
import org.opentripplanner.middleware.utils.JsonUtils;

import java.io.IOException;
import java.util.Date;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opentripplanner.middleware.testutils.ApiTestUtils.getMockHeaders;
import static org.opentripplanner.middleware.testutils.ApiTestUtils.makeRequest;
import static org.opentripplanner.middleware.testutils.ApiTestUtils.mockAuthenticatedGet;
import static org.opentripplanner.middleware.auth.Auth0Connection.restoreDefaultAuthDisabled;
import static org.opentripplanner.middleware.auth.Auth0Connection.setAuthDisabled;
Expand All @@ -36,6 +42,7 @@ public static void setUp() throws IOException {
otpUser.hasConsentedToTerms = true;
otpUser.phoneNumber = INITIAL_PHONE_NUMBER;
otpUser.isPhoneNumberVerified = true;
otpUser.smsConsentDate = new Date();
Persistence.otpUsers.create(otpUser);
}

Expand Down Expand Up @@ -105,4 +112,28 @@ private static Stream<Arguments> createPhoneNumberTestCases() {
Arguments.of("555555", false)
);
}

/**
* smsConsentDate is not passed to/from the UI, so make sure that that field still gets persisted.
*/
@Test
void canPreserveSmsConsentDate() throws Exception {
OtpUser u = new OtpUser();
u.id = otpUser.id;
u.email = otpUser.email;
u.hasConsentedToTerms = true;
u.phoneNumber = INITIAL_PHONE_NUMBER;
u.isPhoneNumberVerified = true;
u.smsConsentDate = null;

makeRequest(
String.format("api/secure/user/%s", otpUser.id),
JsonUtils.toJson(u),
getMockHeaders(otpUser),
HttpMethod.PUT
);

OtpUser updatedUser = Persistence.otpUsers.getById(otpUser.id);
Assertions.assertEquals(u.smsConsentDate, updatedUser.smsConsentDate);
}
}

0 comments on commit bf434c1

Please sign in to comment.