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