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()));
}
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"));
}
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()));
}
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());
}
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()));
}
Aggregations