Search in sources :

Example 1 with InviteServiceRequest

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())));
}
Also used : Logger(org.slf4j.Logger) RandomIdGenerator.randomUuid(uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid) LoggerFactory(org.slf4j.LoggerFactory) TelephoneNumberUtility(uk.gov.pay.adminusers.utils.telephonenumber.TelephoneNumberUtility) AdminUsersExceptions.internalServerError(uk.gov.pay.adminusers.service.AdminUsersExceptions.internalServerError) AdminUsersExceptions.conflictingEmail(uk.gov.pay.adminusers.service.AdminUsersExceptions.conflictingEmail) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) Function(java.util.function.Function) Invite(uk.gov.pay.adminusers.model.Invite) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) String.format(java.lang.String.format) Transactional(com.google.inject.persist.Transactional) UserDao(uk.gov.pay.adminusers.persistence.dao.UserDao) Collectors.toUnmodifiableList(java.util.stream.Collectors.toUnmodifiableList) LinksConfig(uk.gov.pay.adminusers.app.config.LinksConfig) InviteServiceRequest(uk.gov.pay.adminusers.model.InviteServiceRequest) Inject(javax.inject.Inject) SERVICE(uk.gov.pay.adminusers.model.InviteType.SERVICE) List(java.util.List) InviteDao(uk.gov.pay.adminusers.persistence.dao.InviteDao) Optional(java.util.Optional) RoleDao(uk.gov.pay.adminusers.persistence.dao.RoleDao) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Transactional(com.google.inject.persist.Transactional)

Example 2 with InviteServiceRequest

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"));
}
Also used : RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) InviteServiceRequest(uk.gov.pay.adminusers.model.InviteServiceRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Invite(uk.gov.pay.adminusers.model.Invite) Test(org.junit.jupiter.api.Test)

Example 3 with InviteServiceRequest

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"));
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) InviteServiceRequest(uk.gov.pay.adminusers.model.InviteServiceRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 4 with InviteServiceRequest

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"));
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) InviteServiceRequest(uk.gov.pay.adminusers.model.InviteServiceRequest) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 5 with InviteServiceRequest

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"));
}
Also used : RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) InviteServiceRequest(uk.gov.pay.adminusers.model.InviteServiceRequest) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Invite(uk.gov.pay.adminusers.model.Invite) Test(org.junit.jupiter.api.Test)

Aggregations

InviteServiceRequest (uk.gov.pay.adminusers.model.InviteServiceRequest)9 Test (org.junit.jupiter.api.Test)8 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)8 Invite (uk.gov.pay.adminusers.model.Invite)6 RoleEntity (uk.gov.pay.adminusers.persistence.entity.RoleEntity)5 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)5 WebApplicationException (javax.ws.rs.WebApplicationException)3 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)3 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)2 Transactional (com.google.inject.persist.Transactional)1 String.format (java.lang.String.format)1 List (java.util.List)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors.toUnmodifiableList (java.util.stream.Collectors.toUnmodifiableList)1 Inject (javax.inject.Inject)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1 LinksConfig (uk.gov.pay.adminusers.app.config.LinksConfig)1 RandomIdGenerator.randomUuid (uk.gov.pay.adminusers.app.util.RandomIdGenerator.randomUuid)1