Search in sources :

Example 1 with InviteUserRequest

use of uk.gov.pay.adminusers.model.InviteUserRequest in project pay-adminusers by alphagov.

the class UserInviteCreatorTest method create_shouldResendTheSameInviteEmail_ifAValidInviteExistsForTheSameServiceBySameSender_forNewUser.

@Test
public void create_shouldResendTheSameInviteEmail_ifAValidInviteExistsForTheSameServiceBySameSender_forNewUser() {
    // Given
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.empty());
    InviteEntity anInvite = mockInviteSuccessExistingInvite();
    when(mockNotificationService.sendInviteEmail(eq(senderEmail), eq(email), matches("^http://selfservice/invites/[0-9a-z]{32}$"))).thenReturn("random-notify-id");
    // When
    InviteUserRequest inviteUserRequest = inviteRequestFrom(senderExternalId, email, roleName);
    Optional<Invite> invite = userInviteCreator.doInvite(inviteUserRequest);
    // Then
    assertThat(invite.isPresent(), is(true));
    assertThat(invite.get().getCode(), is(anInvite.getCode()));
    assertThat(invite.get().getEmail(), is(anInvite.getEmail()));
}
Also used : InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) InviteUserRequest(uk.gov.pay.adminusers.model.InviteUserRequest) Invite(uk.gov.pay.adminusers.model.Invite) Test(org.junit.jupiter.api.Test)

Example 2 with InviteUserRequest

use of uk.gov.pay.adminusers.model.InviteUserRequest in project pay-adminusers by alphagov.

the class UserInviteCreatorTest method create_shouldFailWithConflict_WhenValidInviteExistsInvitingUserIsDifferent.

@Test
public void create_shouldFailWithConflict_WhenValidInviteExistsInvitingUserIsDifferent() {
    ServiceEntity service = new ServiceEntity();
    service.setId(serviceId);
    service.setExternalId(serviceExternalId);
    String inviteCode = "code";
    UserEntity someOtherSender = new UserEntity();
    String someOtherSenderId = "7834ny0t7cr";
    someOtherSender.setExternalId(someOtherSenderId);
    someOtherSender.setEmail(senderEmail);
    RoleEntity role = new RoleEntity(role(ADMIN.getId(), "admin", "Admin Role"));
    someOtherSender.addServiceRole(new ServiceRoleEntity(service, role));
    when(mockServiceDao.findByExternalId(serviceExternalId)).thenReturn(Optional.of(service));
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.empty());
    InviteEntity anInvite = anInvite(email, inviteCode, "otpKey", someOtherSender, service, role);
    when(mockInviteDao.findByEmail(email)).thenReturn(List.of(anInvite));
    InviteUserRequest inviteUserRequest = inviteRequestFrom(senderExternalId, email, roleName);
    WebApplicationException webApplicationException = assertThrows(WebApplicationException.class, () -> userInviteCreator.doInvite(inviteUserRequest));
    assertThat(webApplicationException.getMessage(), is("HTTP 409 Conflict"));
}
Also used : RoleEntity(uk.gov.pay.adminusers.persistence.entity.RoleEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) WebApplicationException(javax.ws.rs.WebApplicationException) InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) ServiceRoleEntity(uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity) InviteUserRequest(uk.gov.pay.adminusers.model.InviteUserRequest) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 3 with InviteUserRequest

use of uk.gov.pay.adminusers.model.InviteUserRequest in project pay-adminusers by alphagov.

the class UserInviteCreatorTest method create_shouldResendTheSameInviteEmail_ifAValidInviteExistsForTheSameServiceBySameSender_forExistingUser.

@Test
public void create_shouldResendTheSameInviteEmail_ifAValidInviteExistsForTheSameServiceBySameSender_forExistingUser() {
    // Given
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.of(UserEntity.from(aUser(email))));
    InviteEntity anInvite = mockInviteSuccessExistingInvite();
    when(mockNotificationService.sendInviteExistingUserEmail(eq(senderEmail), eq(email), matches("^http://selfservice/invites/[0-9a-z]{32}$"), eq(anInvite.getService().getServiceNames().get(SupportedLanguage.ENGLISH).getName()))).thenReturn("random-notify-id");
    InviteUserRequest inviteUserRequest = inviteRequestFrom(senderExternalId, email, roleName);
    Optional<Invite> invite = userInviteCreator.doInvite(inviteUserRequest);
    assertThat(invite.isPresent(), is(true));
    assertThat(invite.get().getCode(), is(anInvite.getCode()));
    assertThat(invite.get().getEmail(), is(anInvite.getEmail()));
}
Also used : InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) InviteUserRequest(uk.gov.pay.adminusers.model.InviteUserRequest) Invite(uk.gov.pay.adminusers.model.Invite) Test(org.junit.jupiter.api.Test)

Example 4 with InviteUserRequest

use of uk.gov.pay.adminusers.model.InviteUserRequest in project pay-adminusers by alphagov.

the class UserInviteCreatorTest method shouldReturnEmpty_ifServiceNotFound.

@Test
public void shouldReturnEmpty_ifServiceNotFound() {
    when(mockServiceDao.findByExternalId(serviceExternalId)).thenReturn(Optional.empty());
    InviteUserRequest inviteUserRequest = inviteRequestFrom(senderEmail, email, roleName);
    Optional<Invite> invite = userInviteCreator.doInvite(inviteUserRequest);
    assertFalse(invite.isPresent());
}
Also used : InviteUserRequest(uk.gov.pay.adminusers.model.InviteUserRequest) Invite(uk.gov.pay.adminusers.model.Invite) Test(org.junit.jupiter.api.Test)

Example 5 with InviteUserRequest

use of uk.gov.pay.adminusers.model.InviteUserRequest in project pay-adminusers by alphagov.

the class UserInviteCreatorTest method create_shouldOnlyConsider_nonExpiredNonDisabledSameService_whenCheckingForExistingInvite.

@Test
public void create_shouldOnlyConsider_nonExpiredNonDisabledSameService_whenCheckingForExistingInvite() {
    InviteEntity validInvite = mockInviteSuccessExistingInvite();
    InviteEntity expiredInvite = new InviteEntity();
    expiredInvite.setExpiryDate(ZonedDateTime.now().minusDays(2));
    expiredInvite.setService(validInvite.getService());
    InviteEntity disabledInvite = new InviteEntity();
    disabledInvite.setDisabled(true);
    disabledInvite.setExpiryDate(ZonedDateTime.now().plusDays(1));
    disabledInvite.setService(validInvite.getService());
    InviteEntity emptyServiceInvite = new InviteEntity();
    emptyServiceInvite.setExpiryDate(ZonedDateTime.now().plusDays(1));
    InviteEntity nonMatchingServiceInvite = new InviteEntity();
    ServiceEntity serviceEntity = ServiceEntity.from(Service.from(new ServiceName("another-service")));
    nonMatchingServiceInvite.setService(serviceEntity);
    nonMatchingServiceInvite.setExpiryDate(ZonedDateTime.now().plusDays(1));
    when(mockInviteDao.findByEmail(email)).thenReturn(List.of(expiredInvite, disabledInvite, emptyServiceInvite, nonMatchingServiceInvite, validInvite));
    when(mockUserDao.findByEmail(email)).thenReturn(Optional.of(UserEntity.from(aUser(email))));
    when(mockNotificationService.sendInviteExistingUserEmail(eq(senderEmail), eq(email), matches("^http://selfservice/invites/[0-9a-z]{32}$"), eq(validInvite.getService().getServiceNames().get(SupportedLanguage.ENGLISH).getName()))).thenReturn("random-notify-id");
    InviteUserRequest inviteUserRequest = inviteRequestFrom(senderExternalId, email, roleName);
    Optional<Invite> invite = userInviteCreator.doInvite(inviteUserRequest);
    assertThat(invite.isPresent(), is(true));
    assertThat(invite.get().getCode(), is(validInvite.getCode()));
    assertThat(invite.get().getEmail(), is(validInvite.getEmail()));
}
Also used : InviteEntity(uk.gov.pay.adminusers.persistence.entity.InviteEntity) ServiceName(uk.gov.pay.adminusers.model.ServiceName) ServiceEntity(uk.gov.pay.adminusers.persistence.entity.ServiceEntity) InviteUserRequest(uk.gov.pay.adminusers.model.InviteUserRequest) Invite(uk.gov.pay.adminusers.model.Invite) Test(org.junit.jupiter.api.Test)

Aggregations

InviteUserRequest (uk.gov.pay.adminusers.model.InviteUserRequest)9 Test (org.junit.jupiter.api.Test)8 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)7 Invite (uk.gov.pay.adminusers.model.Invite)6 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)4 WebApplicationException (javax.ws.rs.WebApplicationException)3 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)3 RoleEntity (uk.gov.pay.adminusers.persistence.entity.RoleEntity)2 ServiceRoleEntity (uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)2 Inject (com.google.inject.Inject)1 Transactional (com.google.inject.persist.Transactional)1 String.format (java.lang.String.format)1 List (java.util.List)1 Optional (java.util.Optional)1 Collectors.toUnmodifiableList (java.util.stream.Collectors.toUnmodifiableList)1 UriBuilder.fromUri (javax.ws.rs.core.UriBuilder.fromUri)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