use of uk.gov.pay.adminusers.model.InviteServiceRequest in project pay-adminusers by alphagov.
the class ServiceInviteCreator method doInvite.
@Transactional
public Invite doInvite(InviteServiceRequest inviteServiceRequest) {
String requestEmail = inviteServiceRequest.getEmail();
Optional<UserEntity> anExistingUser = userDao.findByEmail(requestEmail);
if (anExistingUser.isPresent()) {
UserEntity user = anExistingUser.get();
if (user.isDisabled()) {
sendUserDisabledNotification(requestEmail, user.getExternalId());
} else {
sendUserExistsNotification(requestEmail, user.getExternalId());
}
throw conflictingEmail(requestEmail);
}
List<InviteEntity> exitingInvites = inviteDao.findByEmail(requestEmail);
List<InviteEntity> existingValidServiceInvitesForSameEmail = exitingInvites.stream().filter(inviteEntity -> !inviteEntity.isDisabled() && !inviteEntity.isExpired()).filter(InviteEntity::isServiceType).collect(toUnmodifiableList());
if (!existingValidServiceInvitesForSameEmail.isEmpty()) {
InviteEntity foundInvite = existingValidServiceInvitesForSameEmail.get(0);
return constructInviteAndSendEmail(inviteServiceRequest, foundInvite, inviteEntity -> {
inviteDao.merge(inviteEntity);
return null;
});
}
return roleDao.findByRoleName(inviteServiceRequest.getRoleName()).map(roleEntity -> {
InviteEntity inviteEntity = new InviteEntity(requestEmail, randomUuid(), inviteServiceRequest.getOtpKey(), roleEntity);
inviteEntity.setType(SERVICE);
return constructInviteAndSendEmail(inviteServiceRequest, inviteEntity, inviteToPersist -> {
inviteDao.persist(inviteToPersist);
return null;
});
}).orElseThrow(() -> internalServerError(format("Role [%s] not a valid role for creating a invite service request", inviteServiceRequest.getRoleName())));
}
use of uk.gov.pay.adminusers.model.InviteServiceRequest in project pay-adminusers by alphagov.
the class ServiceInviteCreatorTest method shouldSuccess_serviceInvite_IfEmailDoesNotConflict.
@Test
public void shouldSuccess_serviceInvite_IfEmailDoesNotConflict() {
String email = "email@example.gov.uk";
InviteServiceRequest request = new InviteServiceRequest("password", email, "01134960000");
RoleEntity roleEntity = new RoleEntity(Role.role(2, "admin", "Adminstrator"));
when(userDao.findByEmail(email)).thenReturn(Optional.empty());
when(inviteDao.findByEmail(email)).thenReturn(emptyList());
when(roleDao.findByRoleName("admin")).thenReturn(Optional.of(roleEntity));
when(notificationService.sendServiceInviteEmail(eq(email), anyString())).thenReturn("done");
when(linksConfig.getSelfserviceInvitesUrl()).thenReturn("http://selfservice/invites");
when(linksConfig.getSelfserviceUrl()).thenReturn("http://selfservice");
when(passwordHasher.hash("password")).thenReturn("encrypted-password");
Invite invite = serviceInviteCreator.doInvite(request);
verify(inviteDao, times(1)).persist(persistedInviteEntity.capture());
assertThat(invite.getEmail(), is(request.getEmail()));
assertThat(invite.getTelephoneNumber(), is("+441134960000"));
assertThat(invite.getType(), is("service"));
assertThat(invite.getLinks().get(0).getHref(), matchesPattern("^http://selfservice/invites/[0-9a-z]{32}$"));
assertThat(persistedInviteEntity.getValue().getPassword(), is("encrypted-password"));
}
use of uk.gov.pay.adminusers.model.InviteServiceRequest in project pay-adminusers by alphagov.
the class ServiceInviteCreatorTest method shouldError_ifUserAlreadyExistsAndDisabledWithGivenEmail.
@Test
public void shouldError_ifUserAlreadyExistsAndDisabledWithGivenEmail() {
String email = "email@example.gov.uk";
InviteServiceRequest request = new InviteServiceRequest("password", email, "01134960000");
UserEntity existingUserEntity = new UserEntity();
existingUserEntity.setDisabled(true);
when(userDao.findByEmail(email)).thenReturn(Optional.of(existingUserEntity));
when(linksConfig.getSupportUrl()).thenReturn("http://frontend");
when(notificationService.sendServiceInviteUserDisabledEmail(eq(email), anyString())).thenReturn("done");
WebApplicationException exception = assertThrows(WebApplicationException.class, () -> serviceInviteCreator.doInvite(request));
assertThat(exception.getMessage(), is("HTTP 409 Conflict"));
}
use of uk.gov.pay.adminusers.model.InviteServiceRequest in project pay-adminusers by alphagov.
the class ServiceInviteCreatorTest method shouldError_ifUserAlreadyExistsWithGivenEmail.
@Test
public void shouldError_ifUserAlreadyExistsWithGivenEmail() {
String email = "email@example.gov.uk";
InviteServiceRequest request = new InviteServiceRequest("password", email, "01134960000");
UserEntity existingUserEntity = new UserEntity();
when(userDao.findByEmail(email)).thenReturn(Optional.of(existingUserEntity));
when(linksConfig.getSupportUrl()).thenReturn("http://frontend");
when(linksConfig.getSelfserviceForgottenPasswordUrl()).thenReturn("http://selfservice/forgotten-password");
when(linksConfig.getSelfserviceInvitesUrl()).thenReturn("http://selfservice/invites");
when(linksConfig.getSelfserviceLoginUrl()).thenReturn("http://selfservice/login");
when(linksConfig.getSelfserviceUrl()).thenReturn("http://selfservice");
when(notificationService.sendServiceInviteUserExistsEmail(eq(email), anyString(), anyString(), anyString())).thenReturn("done");
WebApplicationException exception = assertThrows(WebApplicationException.class, () -> serviceInviteCreator.doInvite(request));
assertThat(exception.getMessage(), is("HTTP 409 Conflict"));
}
use of uk.gov.pay.adminusers.model.InviteServiceRequest in project pay-adminusers by alphagov.
the class ServiceInviteCreatorTest method shouldSuccess_ifUserAlreadyHasAValidServiceInvitationWithGivenEmail.
@Test
public void shouldSuccess_ifUserAlreadyHasAValidServiceInvitationWithGivenEmail() {
String email = "email@example.gov.uk";
InviteServiceRequest request = new InviteServiceRequest("password", email, "01134960000");
UserEntity sender = mock(UserEntity.class);
ServiceEntity service = mock(ServiceEntity.class);
RoleEntity role = mock(RoleEntity.class);
InviteEntity validInvite = new InviteEntity(email, "code", "otpKey", role);
validInvite.setService(service);
validInvite.setSender(sender);
validInvite.setType(InviteType.SERVICE);
when(userDao.findByEmail(email)).thenReturn(Optional.empty());
when(sender.getExternalId()).thenReturn("inviter-id");
when(sender.getEmail()).thenReturn("inviter@example.com");
when(inviteDao.findByEmail(email)).thenReturn(List.of(validInvite));
when(linksConfig.getSelfserviceInvitesUrl()).thenReturn("http://selfservice/invites");
when(notificationService.sendServiceInviteEmail(eq(email), anyString())).thenReturn("done");
Invite invite = serviceInviteCreator.doInvite(request);
verify(inviteDao, times(1)).merge(persistedInviteEntity.capture());
assertThat(invite.getEmail(), is(request.getEmail()));
assertThat(invite.getType(), is("service"));
assertThat(invite.getLinks().get(0).getHref(), is("http://selfservice/invites/code"));
}
Aggregations