@Test(expected = InvalidPasswordRequestTokenException.class) public void cant_change_password_if_token_is_expired() { Date now = LocalDate.now().minusDays(2).toDate(); DomainUser domainUser = userService.create(new DomainUser(randomString(), randomEmail(), randomString())); LostPasswordData token = userService.requestPasswordChangeToken(domainUser.getEmail(), now); userService.changePassword(token.getEmail(), token.getToken(), "newPassword", "newPassword"); }
@Test(expected = InvalidPasswordException.class) public void cant_change_password_if_they_not_match() { DomainUser domainUser = userService.create(new DomainUser(randomString(), randomEmail(), randomString())); LostPasswordData token = userService.requestPasswordChangeToken(domainUser.getEmail(), new Date()); userService.changePassword(token.getEmail(), token.getToken(), "Password", "newPassword"); }
@Test public void can_retrieve_lost_password() { String newPassword = "******"; DomainUser domainUser = userService.create(new DomainUser(randomString(), randomEmail(), randomString())); LostPasswordData token = userService.requestPasswordChangeToken(domainUser.getEmail(), new Date()); userService.changePassword(token.getEmail(), token.getToken(), newPassword, newPassword); String expected = passwordEncoder.encodePassword(newPassword, domainUser.getPasswordSalt()); assertThat(userService.findByID(domainUser.getId()).getPassword()).isEqualTo(expected); }
@Test public void should_receive_email_if_user_has_lost_his_password() throws Exception { String email = randomEmail(); DomainUser domainUser = userService.create(new DomainUser(randomString(), email, randomString())); LostPasswordData token = userService.sendLostPasswordMail(domainUser.getEmail(), Locale.ENGLISH); Message message = getMessage(token.getEmail()); assertThat(message.getSubject()).isEqualTo("[Junit] Please reset your password"); assertThat(message.getContent().toString()) .isEqualTo( "Ho, you lost your Junit password. no problemo!\n" + "\n" + "Use the following link within the next 24 hours to reset your password:\n" + "\n" + "/#retrievePage?token=" + token.getToken() + "\n" + "\n" + "Thanks,\n" + "the team"); }