Search in sources :

Example 1 with ForgottenPassword

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

the class ForgottenPasswordDaoIT method shouldNotFindForgottenPasswordByCode_ifExpired.

@Test
public void shouldNotFindForgottenPasswordByCode_ifExpired() {
    String forgottenPasswordCode = randomUuid();
    String username = randomUuid();
    String email = username + "@example.com";
    User user = userDbFixture(databaseHelper).withUsername(username).withEmail(email).insertUser();
    String userExternalId = user.getExternalId();
    UserEntity userEntity = userDao.findByExternalId(userExternalId).get();
    ZonedDateTime expired = ZonedDateTime.now().minusMinutes(91);
    ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, expired, userExternalId);
    databaseHelper.add(forgottenPassword, userEntity.getId());
    Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
    assertFalse(forgottenPasswordEntityOptional.isPresent());
}
Also used : User(uk.gov.pay.adminusers.model.User) ZonedDateTime(java.time.ZonedDateTime) ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 2 with ForgottenPassword

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

the class ForgottenPasswordDaoIT method shouldFindForgottenPasswordByCode_ifNotExpired.

@Test
public void shouldFindForgottenPasswordByCode_ifNotExpired() {
    String forgottenPasswordCode = random(10);
    String username = randomUuid();
    String email = username + "@example.com";
    User user = userDbFixture(databaseHelper).withUsername(username).withEmail(email).insertUser();
    String userExternalId = user.getExternalId();
    UserEntity userEntity = userDao.findByExternalId(userExternalId).get();
    ZonedDateTime notExpired = ZonedDateTime.now().minusMinutes(89);
    ForgottenPassword forgottenPassword = forgottenPassword(randomInt(), forgottenPasswordCode, notExpired, userExternalId);
    databaseHelper.add(forgottenPassword, userEntity.getId());
    Optional<ForgottenPasswordEntity> forgottenPasswordEntityOptional = forgottenPasswordDao.findNonExpiredByCode(forgottenPassword.getCode());
    assertTrue(forgottenPasswordEntityOptional.isPresent());
    ForgottenPasswordEntity forgottenPasswordEntity = forgottenPasswordEntityOptional.get();
    assertThat(forgottenPasswordEntity.getCode(), is(forgottenPassword.getCode()));
    assertThat(forgottenPasswordEntity.getDate(), within(1, MINUTES, forgottenPassword.getDate()));
}
Also used : User(uk.gov.pay.adminusers.model.User) ZonedDateTime(java.time.ZonedDateTime) ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Example 3 with ForgottenPassword

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

the class LinksBuilderTest method shouldConstruct_forgottenPasswordSelfLinkCorrectly.

@Test
void shouldConstruct_forgottenPasswordSelfLinkCorrectly() throws Exception {
    ForgottenPassword forgottenPassword = ForgottenPassword.forgottenPassword(1, "a-code", ZonedDateTime.now(), "7d19aff33f8948deb97ed16b2912dcd3");
    ForgottenPassword decorated = linksBuilder.decorate(forgottenPassword);
    String linkJson = objectMapper.writeValueAsString(decorated.getLinks().get(0));
    assertThat(linkJson, is("{\"rel\":\"self\",\"method\":\"GET\",\"href\":\"http://localhost:8080/v1/api/forgotten-passwords/a-code\"}"));
}
Also used : ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) Test(org.junit.jupiter.api.Test)

Example 4 with ForgottenPassword

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

the class ForgottenPasswordServicesTest method shouldFindForgottenPassword_whenFindByCode_ifFound.

@Test
public void shouldFindForgottenPassword_whenFindByCode_ifFound() {
    String existingCode = "existing-code";
    ForgottenPasswordEntity forgottenPasswordEntity = mockForgottenPassword(existingCode);
    when(forgottenPasswordDao.findNonExpiredByCode(existingCode)).thenReturn(Optional.of(forgottenPasswordEntity));
    Optional<ForgottenPassword> forgottenPasswordOptional = forgottenPasswordServices.findNonExpired(existingCode);
    assertTrue(forgottenPasswordOptional.isPresent());
    assertThat(forgottenPasswordOptional.get().getCode(), is(existingCode));
    assertThat(forgottenPasswordOptional.get().getLinks(), hasSize(1));
}
Also used : ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) Test(org.junit.jupiter.api.Test)

Example 5 with ForgottenPassword

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

the class ForgottenPasswordDaoIT method shouldPersistAForgottenPasswordEntity.

@Test
public void shouldPersistAForgottenPasswordEntity() {
    String forgottenPasswordCode = random(10);
    String username = randomUuid();
    String email = username + "@example.com";
    User user = userDbFixture(databaseHelper).withUsername(username).withEmail(email).insertUser();
    String userExternalId = user.getExternalId();
    UserEntity userEntity = userDao.findByExternalId(userExternalId).get();
    ForgottenPassword forgottenPassword = forgottenPassword(forgottenPasswordCode, userExternalId);
    ForgottenPasswordEntity forgottenPasswordEntity = ForgottenPasswordEntity.from(forgottenPassword, userEntity);
    forgottenPasswordDao.persist(forgottenPasswordEntity);
    assertThat(forgottenPasswordEntity.getId(), is(notNullValue()));
    List<Map<String, Object>> forgottenPasswordById = databaseHelper.findForgottenPasswordById(forgottenPasswordEntity.getId());
    assertThat(forgottenPasswordById.size(), is(1));
    assertThat(forgottenPasswordById.get(0).get("code"), is(forgottenPasswordCode));
    Timestamp storedDate = (Timestamp) forgottenPasswordById.get(0).get("date");
    ZonedDateTime storedDateTime = ZonedDateTime.ofInstant(storedDate.toInstant(), ZoneId.of("UTC"));
    assertThat(storedDateTime, within(1, MINUTES, forgottenPassword.getDate()));
}
Also used : User(uk.gov.pay.adminusers.model.User) ZonedDateTime(java.time.ZonedDateTime) ForgottenPasswordEntity(uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity) ForgottenPassword(uk.gov.pay.adminusers.model.ForgottenPassword) Map(java.util.Map) Timestamp(java.sql.Timestamp) UserEntity(uk.gov.pay.adminusers.persistence.entity.UserEntity) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)6 ForgottenPassword (uk.gov.pay.adminusers.model.ForgottenPassword)6 ForgottenPasswordEntity (uk.gov.pay.adminusers.persistence.entity.ForgottenPasswordEntity)5 ZonedDateTime (java.time.ZonedDateTime)4 User (uk.gov.pay.adminusers.model.User)4 UserEntity (uk.gov.pay.adminusers.persistence.entity.UserEntity)4 Timestamp (java.sql.Timestamp)1 Map (java.util.Map)1