use of uk.gov.di.accountmanagement.entity.UpdateEmailRequest in project di-authentication-api by alphagov.
the class UpdateEmailIntegrationTest method shouldReturn400WhenOtpIsInvalid.
@Test
void shouldReturn400WhenOtpIsInvalid() throws Exception {
String publicSubjectID = userStore.signUp(EXISTING_EMAIL_ADDRESS, "password-1", SUBJECT);
String realOtp = redis.generateAndSaveEmailCode(NEW_EMAIL_ADDRESS, 300);
String badOtp = "This is not the correct OTP";
var response = makeRequest(Optional.of(new UpdateEmailRequest(EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS, badOtp)), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Map.of("principalId", publicSubjectID));
assertThat(response, hasStatus(HttpStatus.SC_BAD_REQUEST));
assertThat(response, hasBody(new ObjectMapper().writeValueAsString(ErrorResponse.ERROR_1020)));
assertNoNotificationsReceived(notificationsQueue);
assertNoAuditEventsReceived(auditTopic);
}
use of uk.gov.di.accountmanagement.entity.UpdateEmailRequest in project di-authentication-api by alphagov.
the class UpdateEmailHandler method handleRequest.
@Override
public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) {
return isWarming(input).orElseGet(() -> {
String sessionId = RequestHeaderHelper.getHeaderValueOrElse(input.getHeaders(), SESSION_ID_HEADER, "");
attachSessionIdToLogs(sessionId);
LOG.info("UpdateEmailHandler received request");
try {
UpdateEmailRequest updateInfoRequest = objectMapper.readValue(input.getBody(), UpdateEmailRequest.class);
boolean isValidOtpCode = codeStorageService.isValidOtpCode(updateInfoRequest.getReplacementEmailAddress(), updateInfoRequest.getOtp(), NotificationType.VERIFY_EMAIL);
if (!isValidOtpCode) {
return generateApiGatewayProxyErrorResponse(400, ErrorResponse.ERROR_1020);
}
Optional<ErrorResponse> emailValidationErrors = validationService.validateEmailAddressUpdate(updateInfoRequest.getExistingEmailAddress(), updateInfoRequest.getReplacementEmailAddress());
if (emailValidationErrors.isPresent()) {
return generateApiGatewayProxyErrorResponse(400, emailValidationErrors.get());
}
if (dynamoService.userExists(updateInfoRequest.getReplacementEmailAddress())) {
return generateApiGatewayProxyErrorResponse(400, ErrorResponse.ERROR_1009);
}
UserProfile userProfile = dynamoService.getUserProfileByEmail(updateInfoRequest.getExistingEmailAddress());
Map<String, Object> authorizerParams = input.getRequestContext().getAuthorizer();
RequestBodyHelper.validatePrincipal(new Subject(userProfile.getPublicSubjectID()), authorizerParams);
dynamoService.updateEmail(updateInfoRequest.getExistingEmailAddress(), updateInfoRequest.getReplacementEmailAddress());
LOG.info("Email has successfully been updated. Adding message to SQS queue");
NotifyRequest notifyRequest = new NotifyRequest(updateInfoRequest.getReplacementEmailAddress(), NotificationType.EMAIL_UPDATED);
sqsClient.send(objectMapper.writeValueAsString((notifyRequest)));
auditService.submitAuditEvent(AccountManagementAuditableEvent.UPDATE_EMAIL, context.getAwsRequestId(), sessionId, AuditService.UNKNOWN, userProfile.getSubjectID(), updateInfoRequest.getReplacementEmailAddress(), IpAddressHelper.extractIpAddress(input), userProfile.getPhoneNumber(), PersistentIdHelper.extractPersistentIdFromHeaders(input.getHeaders()));
LOG.info("Message successfully added to queue. Generating successful gateway response");
return generateEmptySuccessApiGatewayResponse();
} catch (JsonProcessingException | IllegalArgumentException e) {
return generateApiGatewayProxyErrorResponse(400, ErrorResponse.ERROR_1001);
}
});
}
use of uk.gov.di.accountmanagement.entity.UpdateEmailRequest in project di-authentication-api by alphagov.
the class UpdateEmailHandlerTest method shouldFormatAllEmailsToLowerCase.
@Test
void shouldFormatAllEmailsToLowerCase() {
final UpdateEmailRequest updateEmailRequest = new UpdateEmailRequest("Joe.Bloggs@digital.cabinet-office.gov.uk", "Bloggs.Joe@digital.cabinet-office.gov.uk", OTP);
assertEquals(updateEmailRequest.getExistingEmailAddress(), EXISTING_EMAIL_ADDRESS);
assertEquals(updateEmailRequest.getReplacementEmailAddress(), NEW_EMAIL_ADDRESS);
}
use of uk.gov.di.accountmanagement.entity.UpdateEmailRequest in project di-authentication-api by alphagov.
the class UpdateEmailIntegrationTest method shouldThrowExceptionWhenSubjectIdMissing.
@Test
void shouldThrowExceptionWhenSubjectIdMissing() {
userStore.signUp(EXISTING_EMAIL_ADDRESS, "password-1", SUBJECT);
String otp = redis.generateAndSaveEmailCode(NEW_EMAIL_ADDRESS, 300);
Exception ex = assertThrows(RuntimeException.class, () -> makeRequest(Optional.of(new UpdateEmailRequest(EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS, otp)), Collections.emptyMap(), Collections.emptyMap()));
assertThat(ex.getMessage(), is("principalId is missing"));
}
use of uk.gov.di.accountmanagement.entity.UpdateEmailRequest in project di-authentication-api by alphagov.
the class UpdateEmailIntegrationTest method shouldThrowExceptionWhenUserAttemptsToUpdateDifferentAccount.
@Test
void shouldThrowExceptionWhenUserAttemptsToUpdateDifferentAccount() {
String correctSubjectID = userStore.signUp(EXISTING_EMAIL_ADDRESS, "password-1", SUBJECT);
String otherSubjectID = userStore.signUp("other.user@digital.cabinet-office.gov.uk", "password-2", new Subject());
String otp = redis.generateAndSaveEmailCode(NEW_EMAIL_ADDRESS, 300);
Exception ex = assertThrows(RuntimeException.class, () -> makeRequest(Optional.of(new UpdateEmailRequest(EXISTING_EMAIL_ADDRESS, NEW_EMAIL_ADDRESS, otp)), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Map.of("principalId", otherSubjectID)));
assertThat(ex.getMessage(), is("Subject ID does not match principalId"));
}
Aggregations