Search in sources :

Example 1 with UserEntity

use of uk.gov.pay.adminusers.persistence.entity.UserEntity in project pay-adminusers by alphagov.

the class UserServicesTest method shouldReturnEmptyAndDisable_whenAuthenticate2FA_ifUnsuccessfulMaxRetry.

@Test
void shouldReturnEmptyAndDisable_whenAuthenticate2FA_ifUnsuccessfulMaxRetry() {
    User user = aUser();
    user.setLoginCounter(3);
    UserEntity userEntity = aUserEntityWithTrimmings(user);
    when(userDao.findByExternalId(user.getExternalId())).thenReturn(Optional.of(userEntity));
    when(secondFactorAuthenticator.authorize(user.getOtpKey(), 123456)).thenReturn(false);
    when(userDao.merge(userEntityArgumentCaptor.capture())).thenReturn(mock(UserEntity.class));
    Optional<User> tokenOptional = userServices.authenticateSecondFactor(user.getExternalId(), 123456);
    assertFalse(tokenOptional.isPresent());
    UserEntity savedUser = userEntityArgumentCaptor.getValue();
    assertThat(savedUser.getLoginCounter(), is(4));
    assertThat(savedUser.isDisabled(), is(true));
}
Also used : User(uk.gov.pay.adminusers.model.User) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 2 with UserEntity

use of uk.gov.pay.adminusers.persistence.entity.UserEntity in project pay-adminusers by alphagov.

the class UserServicesTest method shouldReturnUser_whenActivateNewOtpKey_ifUserDisabled.

@Test
void shouldReturnUser_whenActivateNewOtpKey_ifUserDisabled() {
    User user = aUser();
    UserEntity userEntity = UserEntity.from(user);
    userEntity.setSecondFactor(SecondFactorMethod.SMS);
    userEntity.setOtpKey("Original OTP key");
    userEntity.setProvisionalOtpKey("New OTP key");
    userEntity.setProvisionalOtpKeyCreatedAt(ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(89));
    userEntity.setDisabled(true);
    when(userDao.findByExternalId(user.getExternalId())).thenReturn(Optional.of(userEntity));
    Optional<User> result = userServices.activateNewOtpKey(user.getExternalId(), SecondFactorMethod.APP, 123456);
    assertFalse(result.isPresent());
    assertThat(userEntity.getOtpKey(), is("Original OTP key"));
    assertThat(userEntity.getSecondFactor(), is(SecondFactorMethod.SMS));
    verify(userDao, never()).merge(any(UserEntity.class));
}
Also used : User(uk.gov.pay.adminusers.model.User) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 3 with UserEntity

use of uk.gov.pay.adminusers.persistence.entity.UserEntity in project pay-adminusers by alphagov.

the class UserServicesTest method shouldFindAUserByUserName.

@Test
void shouldFindAUserByUserName() {
    User user = aUser();
    UserEntity userEntity = aUserEntityWithTrimmings(user);
    Optional<UserEntity> userEntityOptional = Optional.of(userEntity);
    when(userDao.findByUsername(USER_USERNAME)).thenReturn(userEntityOptional);
    Optional<User> userOptional = userServices.findUserByUsername(USER_USERNAME);
    assertTrue(userOptional.isPresent());
    assertThat(userOptional.get().getUsername(), is(USER_USERNAME));
}
Also used : User(uk.gov.pay.adminusers.model.User) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 4 with UserEntity

use of uk.gov.pay.adminusers.persistence.entity.UserEntity in project pay-adminusers by alphagov.

the class UserServicesTest method shouldFindAUserByExternalId.

@Test
void shouldFindAUserByExternalId() {
    User user = aUser();
    UserEntity userEntity = aUserEntityWithTrimmings(user);
    Optional<UserEntity> userEntityOptional = Optional.of(userEntity);
    when(userDao.findByExternalId(USER_EXTERNAL_ID)).thenReturn(userEntityOptional);
    Optional<User> userOptional = userServices.findUserByExternalId(USER_EXTERNAL_ID);
    assertTrue(userOptional.isPresent());
    assertThat(userOptional.get().getExternalId(), is(USER_EXTERNAL_ID));
}
Also used : User(uk.gov.pay.adminusers.model.User) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 5 with UserEntity

use of uk.gov.pay.adminusers.persistence.entity.UserEntity in project pay-adminusers by alphagov.

the class UserServicesTest method shouldReturnUser_whenActivateNewOtpKey_ifCodeIncorrect.

@Test
void shouldReturnUser_whenActivateNewOtpKey_ifCodeIncorrect() {
    User user = aUser();
    UserEntity userEntity = UserEntity.from(user);
    userEntity.setSecondFactor(SecondFactorMethod.SMS);
    userEntity.setOtpKey("Original OTP key");
    userEntity.setProvisionalOtpKey("New OTP key");
    userEntity.setProvisionalOtpKeyCreatedAt(ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(89));
    when(userDao.findByExternalId(user.getExternalId())).thenReturn(Optional.of(userEntity));
    when(secondFactorAuthenticator.authorize("New OTP key", 123456)).thenReturn(false);
    Optional<User> result = userServices.activateNewOtpKey(user.getExternalId(), SecondFactorMethod.APP, 123456);
    assertFalse(result.isPresent());
    assertThat(userEntity.getOtpKey(), is("Original OTP key"));
    assertThat(userEntity.getSecondFactor(), is(SecondFactorMethod.SMS));
    verify(userDao, never()).merge(any(UserEntity.class));
}
Also used : User(uk.gov.pay.adminusers.model.User) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Aggregations

UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)90 Test (org.junit.jupiter.api.Test)70 User (uk.gov.pay.adminusers.model.User)47 ServiceEntity (uk.gov.pay.adminusers.persistence.entity.ServiceEntity)27 RoleEntity (uk.gov.pay.adminusers.persistence.entity.RoleEntity)21 ServiceRoleEntity (uk.gov.pay.adminusers.persistence.entity.ServiceRoleEntity)20 WebApplicationException (javax.ws.rs.WebApplicationException)14 InviteEntity (uk.gov.pay.adminusers.persistence.entity.InviteEntity)14 Transactional (com.google.inject.persist.Transactional)10 Role (uk.gov.pay.adminusers.model.Role)9 ZonedDateTime (java.time.ZonedDateTime)8 ForgottenPasswordEntity (uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity)8 Matchers.emptyOrNullString (org.hamcrest.Matchers.emptyOrNullString)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 SecondFactorToken (uk.gov.pay.adminusers.model.SecondFactorToken)6 Optional (java.util.Optional)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Invite (uk.gov.pay.adminusers.model.Invite)5 InviteServiceRequest (uk.gov.pay.adminusers.model.InviteServiceRequest)5