Skip to content

Commit e09e37d

Browse files
author
JHjava
authored
Merge pull request #1540 from alphagov/auth-1480-email-validation-refactor
AUTH-1480 - Validation refactor
2 parents b4349e3 + f136e60 commit e09e37d

14 files changed

Lines changed: 206 additions & 329 deletions

File tree

account-management-api/src/main/java/uk/gov/di/accountmanagement/lambda/SendOtpNotificationHandler.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import uk.gov.di.authentication.shared.services.ConfigurationService;
2525
import uk.gov.di.authentication.shared.services.DynamoService;
2626
import uk.gov.di.authentication.shared.services.RedisConnectionService;
27-
import uk.gov.di.authentication.shared.services.ValidationService;
2827

2928
import java.util.Optional;
3029

@@ -44,7 +43,6 @@ public class SendOtpNotificationHandler
4443
private static final Logger LOG = LogManager.getLogger(SendOtpNotificationHandler.class);
4544

4645
private final ConfigurationService configurationService;
47-
private final ValidationService validationService;
4846
private final AwsSqsClient sqsClient;
4947
private final CodeGeneratorService codeGeneratorService;
5048
private final CodeStorageService codeStorageService;
@@ -54,14 +52,12 @@ public class SendOtpNotificationHandler
5452

5553
public SendOtpNotificationHandler(
5654
ConfigurationService configurationService,
57-
ValidationService validationService,
5855
AwsSqsClient sqsClient,
5956
CodeGeneratorService codeGeneratorService,
6057
CodeStorageService codeStorageService,
6158
DynamoService dynamoService,
6259
AuditService auditService) {
6360
this.configurationService = configurationService;
64-
this.validationService = validationService;
6561
this.sqsClient = sqsClient;
6662
this.codeGeneratorService = codeGeneratorService;
6763
this.codeStorageService = codeStorageService;
@@ -76,7 +72,6 @@ public SendOtpNotificationHandler(ConfigurationService configurationService) {
7672
configurationService.getAwsRegion(),
7773
configurationService.getEmailQueueUri(),
7874
configurationService.getSqsEndpointUri());
79-
this.validationService = new ValidationService();
8075
this.codeGeneratorService = new CodeGeneratorService();
8176
this.codeStorageService =
8277
new CodeStorageService(new RedisConnectionService(configurationService));
@@ -107,7 +102,7 @@ public APIGatewayProxyResponseEvent handleRequest(
107102
case VERIFY_EMAIL:
108103
LOG.info("NotificationType is VERIFY_EMAIL");
109104
Optional<ErrorResponse> emailErrorResponse =
110-
validationService.validateEmailAddress(
105+
ValidationHelper.validateEmailAddress(
111106
sendNotificationRequest.getEmail());
112107
if (emailErrorResponse.isPresent()) {
113108
return generateApiGatewayProxyErrorResponse(

account-management-api/src/main/java/uk/gov/di/accountmanagement/lambda/UpdateEmailHandler.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
import uk.gov.di.authentication.shared.helpers.PersistentIdHelper;
2222
import uk.gov.di.authentication.shared.helpers.RequestBodyHelper;
2323
import uk.gov.di.authentication.shared.helpers.RequestHeaderHelper;
24+
import uk.gov.di.authentication.shared.helpers.ValidationHelper;
2425
import uk.gov.di.authentication.shared.services.AuditService;
2526
import uk.gov.di.authentication.shared.services.ConfigurationService;
2627
import uk.gov.di.authentication.shared.services.DynamoService;
2728
import uk.gov.di.authentication.shared.services.RedisConnectionService;
28-
import uk.gov.di.authentication.shared.services.ValidationService;
2929

3030
import java.util.Map;
3131
import java.util.Optional;
@@ -42,7 +42,6 @@ public class UpdateEmailHandler
4242
private final ObjectMapper objectMapper = new ObjectMapper();
4343
private final DynamoService dynamoService;
4444
private final AwsSqsClient sqsClient;
45-
private final ValidationService validationService;
4645
private final CodeStorageService codeStorageService;
4746
private static final Logger LOG = LogManager.getLogger(UpdateEmailHandler.class);
4847
private final AuditService auditService;
@@ -54,12 +53,10 @@ public UpdateEmailHandler() {
5453
public UpdateEmailHandler(
5554
DynamoService dynamoService,
5655
AwsSqsClient sqsClient,
57-
ValidationService validationService,
5856
CodeStorageService codeStorageService,
5957
AuditService auditService) {
6058
this.dynamoService = dynamoService;
6159
this.sqsClient = sqsClient;
62-
this.validationService = validationService;
6360
this.codeStorageService = codeStorageService;
6461
this.auditService = auditService;
6562
}
@@ -71,7 +68,6 @@ public UpdateEmailHandler(ConfigurationService configurationService) {
7168
configurationService.getAwsRegion(),
7269
configurationService.getEmailQueueUri(),
7370
configurationService.getSqsEndpointUri());
74-
this.validationService = new ValidationService();
7571
this.codeStorageService =
7672
new CodeStorageService(new RedisConnectionService(configurationService));
7773
this.auditService = new AuditService(configurationService);
@@ -102,7 +98,7 @@ public APIGatewayProxyResponseEvent handleRequest(
10298
400, ErrorResponse.ERROR_1020);
10399
}
104100
Optional<ErrorResponse> emailValidationErrors =
105-
validationService.validateEmailAddressUpdate(
101+
ValidationHelper.validateEmailAddressUpdate(
106102
updateInfoRequest.getExistingEmailAddress(),
107103
updateInfoRequest.getReplacementEmailAddress());
108104
if (emailValidationErrors.isPresent()) {

account-management-api/src/main/java/uk/gov/di/accountmanagement/lambda/UpdatePhoneNumberHandler.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,12 @@
2121
import uk.gov.di.authentication.shared.helpers.PersistentIdHelper;
2222
import uk.gov.di.authentication.shared.helpers.RequestBodyHelper;
2323
import uk.gov.di.authentication.shared.helpers.RequestHeaderHelper;
24-
import uk.gov.di.authentication.shared.helpers.ValidationHelper;
2524
import uk.gov.di.authentication.shared.services.AuditService;
2625
import uk.gov.di.authentication.shared.services.ConfigurationService;
2726
import uk.gov.di.authentication.shared.services.DynamoService;
2827
import uk.gov.di.authentication.shared.services.RedisConnectionService;
2928

3029
import java.util.Map;
31-
import java.util.Optional;
3230

3331
import static uk.gov.di.authentication.shared.domain.RequestHeaders.SESSION_ID_HEADER;
3432
import static uk.gov.di.authentication.shared.helpers.ApiGatewayResponseHelper.generateApiGatewayProxyErrorResponse;
@@ -97,13 +95,6 @@ public APIGatewayProxyResponseEvent handleRequest(
9795
return generateApiGatewayProxyErrorResponse(
9896
400, ErrorResponse.ERROR_1020);
9997
}
100-
Optional<ErrorResponse> phoneValidationErrors =
101-
ValidationHelper.validatePhoneNumber(
102-
updatePhoneNumberRequest.getPhoneNumber());
103-
if (phoneValidationErrors.isPresent()) {
104-
return generateApiGatewayProxyErrorResponse(
105-
400, phoneValidationErrors.get());
106-
}
10798
UserProfile userProfile =
10899
dynamoService.getUserProfileByEmail(
109100
updatePhoneNumberRequest.getEmail());

account-management-api/src/test/java/uk/gov/di/accountmanagement/lambda/SendOtpNotificationHandlerTest.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import uk.gov.di.authentication.shared.services.CodeGeneratorService;
2121
import uk.gov.di.authentication.shared.services.ConfigurationService;
2222
import uk.gov.di.authentication.shared.services.DynamoService;
23-
import uk.gov.di.authentication.shared.services.ValidationService;
2423

2524
import java.util.Map;
26-
import java.util.Optional;
2725

2826
import static java.lang.String.format;
2927
import static org.hamcrest.MatcherAssert.assertThat;
@@ -50,7 +48,6 @@ class SendOtpNotificationHandlerTest {
5048
private static final String TEST_SIX_DIGIT_CODE = "123456";
5149
private static final String TEST_PHONE_NUMBER = "07755551084";
5250
private static final long CODE_EXPIRY_TIME = 900;
53-
private final ValidationService validationService = mock(ValidationService.class);
5451
private final ConfigurationService configurationService = mock(ConfigurationService.class);
5552
private final AwsSqsClient awsSqsClient = mock(AwsSqsClient.class);
5653
private final CodeGeneratorService codeGeneratorService = mock(CodeGeneratorService.class);
@@ -62,7 +59,6 @@ class SendOtpNotificationHandlerTest {
6259
private final SendOtpNotificationHandler handler =
6360
new SendOtpNotificationHandler(
6461
configurationService,
65-
validationService,
6662
awsSqsClient,
6763
codeGeneratorService,
6864
codeStorageService,
@@ -78,8 +74,6 @@ void setup() {
7874
@Test
7975
void shouldReturn204AndPutMessageOnQueueForAValidEmailRequest() throws JsonProcessingException {
8076
String persistentIdValue = "some-persistent-session-id";
81-
when(validationService.validateEmailAddress(eq(TEST_EMAIL_ADDRESS)))
82-
.thenReturn(Optional.empty());
8377
NotifyRequest notifyRequest =
8478
new NotifyRequest(TEST_EMAIL_ADDRESS, VERIFY_EMAIL, TEST_SIX_DIGIT_CODE);
8579
ObjectMapper objectMapper = new ObjectMapper();
@@ -156,7 +150,7 @@ void shouldReturn204AndPutMessageOnQueueForAValidPhoneRequest() throws JsonProce
156150
}
157151

158152
@Test
159-
public void shouldReturn400IfRequestIsMissingEmail() {
153+
void shouldReturn400IfRequestIsMissingEmail() {
160154
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
161155
event.setHeaders(Map.of());
162156
event.setBody("{ }");
@@ -169,11 +163,7 @@ public void shouldReturn400IfRequestIsMissingEmail() {
169163
}
170164

171165
@Test
172-
public void shouldReturn400IfEmailAddressIsInvalid() {
173-
174-
when(validationService.validateEmailAddress(eq("joe.bloggs")))
175-
.thenReturn(Optional.of(ErrorResponse.ERROR_1004));
176-
166+
void shouldReturn400IfEmailAddressIsInvalid() {
177167
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
178168
event.setHeaders(Map.of());
179169
event.setBody(
@@ -190,7 +180,7 @@ public void shouldReturn400IfEmailAddressIsInvalid() {
190180
}
191181

192182
@Test
193-
public void shouldReturn400IfPhoneNumberIsInvalid() {
183+
void shouldReturn400IfPhoneNumberIsInvalid() {
194184
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
195185
event.setHeaders(Map.of());
196186
event.setBody(
@@ -207,9 +197,7 @@ public void shouldReturn400IfPhoneNumberIsInvalid() {
207197
}
208198

209199
@Test
210-
public void shouldReturn500IfMessageCannotBeSentToQueue() throws JsonProcessingException {
211-
when(validationService.validateEmailAddress(eq(TEST_EMAIL_ADDRESS)))
212-
.thenReturn(Optional.empty());
200+
void shouldReturn500IfMessageCannotBeSentToQueue() throws JsonProcessingException {
213201
NotifyRequest notifyRequest =
214202
new NotifyRequest(TEST_EMAIL_ADDRESS, VERIFY_EMAIL, TEST_SIX_DIGIT_CODE);
215203
ObjectMapper objectMapper = new ObjectMapper();
@@ -231,10 +219,7 @@ public void shouldReturn500IfMessageCannotBeSentToQueue() throws JsonProcessingE
231219
}
232220

233221
@Test
234-
public void shouldReturn400WhenInvalidNotificationType() {
235-
when(validationService.validateEmailAddress(eq(TEST_EMAIL_ADDRESS)))
236-
.thenReturn(Optional.empty());
237-
222+
void shouldReturn400WhenInvalidNotificationType() {
238223
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
239224
event.setHeaders(Map.of());
240225
event.setBody(
@@ -254,9 +239,7 @@ public void shouldReturn400WhenInvalidNotificationType() {
254239
}
255240

256241
@Test
257-
public void shouldReturn400WhenAccountAlreadyExistsWithGivenEmail() {
258-
when(validationService.validateEmailAddress(eq(TEST_EMAIL_ADDRESS)))
259-
.thenReturn(Optional.empty());
242+
void shouldReturn400WhenAccountAlreadyExistsWithGivenEmail() {
260243
when(dynamoService.userExists(eq(TEST_EMAIL_ADDRESS))).thenReturn(true);
261244

262245
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();

account-management-api/src/test/java/uk/gov/di/accountmanagement/lambda/UpdateEmailHandlerTest.java

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
import uk.gov.di.authentication.shared.helpers.PersistentIdHelper;
1919
import uk.gov.di.authentication.shared.services.AuditService;
2020
import uk.gov.di.authentication.shared.services.DynamoService;
21-
import uk.gov.di.authentication.shared.services.ValidationService;
2221

2322
import java.util.HashMap;
2423
import java.util.Map;
25-
import java.util.Optional;
2624

2725
import static java.lang.String.format;
2826
import static org.hamcrest.MatcherAssert.assertThat;
@@ -43,7 +41,6 @@ class UpdateEmailHandlerTest {
4341
private final Context context = mock(Context.class);
4442
private final DynamoService dynamoService = mock(DynamoService.class);
4543
private final AwsSqsClient sqsClient = mock(AwsSqsClient.class);
46-
private final ValidationService validationService = mock(ValidationService.class);
4744
private final CodeStorageService codeStorageService = mock(CodeStorageService.class);
4845
private UpdateEmailHandler handler;
4946
private static final String EXISTING_EMAIL_ADDRESS = "joe.bloggs@digital.cabinet-office.gov.uk";
@@ -54,18 +51,13 @@ class UpdateEmailHandlerTest {
5451
private final AuditService auditService = mock(AuditService.class);
5552

5653
@BeforeEach
57-
public void setUp() {
54+
void setUp() {
5855
handler =
59-
new UpdateEmailHandler(
60-
dynamoService,
61-
sqsClient,
62-
validationService,
63-
codeStorageService,
64-
auditService);
56+
new UpdateEmailHandler(dynamoService, sqsClient, codeStorageService, auditService);
6557
}
6658

6759
@Test
68-
public void shouldReturn204ForValidUpdateEmailRequest() throws JsonProcessingException {
60+
void shouldReturn204ForValidUpdateEmailRequest() throws JsonProcessingException {
6961
String persistentIdValue = "some-persistent-session-id";
7062
UserProfile userProfile = new UserProfile().setPublicSubjectID(SUBJECT.getValue());
7163
when(dynamoService.getUserProfileByEmail(EXISTING_EMAIL_ADDRESS)).thenReturn(userProfile);
@@ -84,9 +76,6 @@ public void shouldReturn204ForValidUpdateEmailRequest() throws JsonProcessingExc
8476
event.setRequestContext(proxyRequestContext);
8577
when(codeStorageService.isValidOtpCode(NEW_EMAIL_ADDRESS, OTP, VERIFY_EMAIL))
8678
.thenReturn(true);
87-
when(validationService.validateEmailAddressUpdate(
88-
EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS))
89-
.thenReturn(Optional.empty());
9079

9180
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
9281

@@ -109,7 +98,7 @@ public void shouldReturn204ForValidUpdateEmailRequest() throws JsonProcessingExc
10998
}
11099

111100
@Test
112-
public void shouldReturn400WhenReplacementEmailAlreadyExists() throws JsonProcessingException {
101+
void shouldReturn400WhenReplacementEmailAlreadyExists() throws JsonProcessingException {
113102
when(dynamoService.getSubjectFromEmail(EXISTING_EMAIL_ADDRESS)).thenReturn(SUBJECT);
114103
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
115104
event.setBody(
@@ -124,9 +113,6 @@ public void shouldReturn400WhenReplacementEmailAlreadyExists() throws JsonProces
124113
event.setRequestContext(proxyRequestContext);
125114
when(codeStorageService.isValidOtpCode(NEW_EMAIL_ADDRESS, OTP, VERIFY_EMAIL))
126115
.thenReturn(true);
127-
when(validationService.validateEmailAddressUpdate(
128-
EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS))
129-
.thenReturn(Optional.empty());
130116
when(dynamoService.userExists(NEW_EMAIL_ADDRESS)).thenReturn(true);
131117
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
132118

@@ -139,7 +125,7 @@ public void shouldReturn400WhenReplacementEmailAlreadyExists() throws JsonProces
139125
}
140126

141127
@Test
142-
public void shouldReturn400WhenRequestIsMissingParameters() {
128+
void shouldReturn400WhenRequestIsMissingParameters() {
143129
APIGatewayProxyRequestEvent.ProxyRequestContext proxyRequestContext =
144130
new APIGatewayProxyRequestEvent.ProxyRequestContext();
145131
Map<String, Object> authorizerParams = new HashMap<>();
@@ -181,8 +167,7 @@ public void shouldReturnErrorWhenOtpCodeIsNotValid() throws JsonProcessingExcept
181167
}
182168

183169
@Test
184-
public void shouldReturn400AndNotUpdateEmailWhenEmailIsInvalid()
185-
throws JsonProcessingException {
170+
void shouldReturn400AndNotUpdateEmailWhenEmailIsInvalid() throws JsonProcessingException {
186171
when(dynamoService.getSubjectFromEmail(EXISTING_EMAIL_ADDRESS)).thenReturn(SUBJECT);
187172
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
188173
event.setBody(
@@ -197,9 +182,6 @@ public void shouldReturn400AndNotUpdateEmailWhenEmailIsInvalid()
197182
event.setRequestContext(proxyRequestContext);
198183
when(codeStorageService.isValidOtpCode(INVALID_EMAIL_ADDRESS, OTP, VERIFY_EMAIL))
199184
.thenReturn(true);
200-
when(validationService.validateEmailAddressUpdate(
201-
EXISTING_EMAIL_ADDRESS, INVALID_EMAIL_ADDRESS))
202-
.thenReturn(Optional.of(ErrorResponse.ERROR_1004));
203185
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
204186

205187
assertThat(result, hasStatus(400));
@@ -211,7 +193,7 @@ public void shouldReturn400AndNotUpdateEmailWhenEmailIsInvalid()
211193
}
212194

213195
@Test
214-
public void shouldFormatAllEmailsToLowerCase() {
196+
void shouldFormatAllEmailsToLowerCase() {
215197
final UpdateEmailRequest updateEmailRequest =
216198
new UpdateEmailRequest(
217199
"Joe.Bloggs@digital.cabinet-office.gov.uk",

account-management-api/src/test/java/uk/gov/di/accountmanagement/lambda/UpdatePhoneNumberHandlerTest.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,4 @@ public void shouldReturnErrorWhenOtpCodeIsNotValid() throws JsonProcessingExcept
145145
assertThat(result, hasBody(expectedResponse));
146146
verifyNoInteractions(auditService);
147147
}
148-
149-
@Test
150-
public void shouldReturn400AndNotUpdatePhoneNumberWhenPhoneNumberIsInvalid()
151-
throws JsonProcessingException {
152-
when(dynamoService.getSubjectFromEmail(EMAIL_ADDRESS)).thenReturn(SUBJECT);
153-
APIGatewayProxyRequestEvent event = new APIGatewayProxyRequestEvent();
154-
event.setBody(
155-
format(
156-
"{\"email\": \"%s\", \"phoneNumber\": \"%s\", \"otp\": \"%s\" }",
157-
EMAIL_ADDRESS, INVALID_PHONE_NUMBER, OTP));
158-
APIGatewayProxyRequestEvent.ProxyRequestContext proxyRequestContext =
159-
new APIGatewayProxyRequestEvent.ProxyRequestContext();
160-
Map<String, Object> authorizerParams = new HashMap<>();
161-
authorizerParams.put("principalId", SUBJECT.getValue());
162-
proxyRequestContext.setAuthorizer(authorizerParams);
163-
event.setRequestContext(proxyRequestContext);
164-
when(codeStorageService.isValidOtpCode(EMAIL_ADDRESS, OTP, VERIFY_PHONE_NUMBER))
165-
.thenReturn(true);
166-
APIGatewayProxyResponseEvent result = handler.handleRequest(event, context);
167-
168-
assertThat(result, hasStatus(400));
169-
verify(dynamoService, times(0)).updatePhoneNumber(EMAIL_ADDRESS, INVALID_PHONE_NUMBER);
170-
NotifyRequest notifyRequest = new NotifyRequest(INVALID_PHONE_NUMBER, PHONE_NUMBER_UPDATED);
171-
verify(sqsClient, times(0)).send(new ObjectMapper().writeValueAsString(notifyRequest));
172-
String expectedResponse = new ObjectMapper().writeValueAsString(ErrorResponse.ERROR_1012);
173-
assertThat(result, hasBody(expectedResponse));
174-
verifyNoInteractions(auditService);
175-
}
176148
}

0 commit comments

Comments
 (0)