UserService { public Optional<User> requestPasswordReset(String mail) { return this.userRepository.findByEmail(mail) .filter(User::isActivated) .map(user -> { user.setResetKey(RandomStringUtils.randomNumeric(KEY_SIZE)); return user; }); } UserService(UserRepository userRepository, AuthorityRepository authorityRepository, PasswordEncoder passwordEncoder); User register(UserRegister user); void update(UserDTO userDTO); void update(String id, String login, String firstName, String lastName, String email, String langKey); void delete(String login); Optional<User> activateRegistration(String key); Page<UserDTO> search(Pageable pageable, String query); Page<UserDTO> getAll(Pageable pageable); Optional<User> getById(String id); Optional<User> getByLogin(String login); Optional<User> getByEmail(String email); void changePassword(String id, String password); Optional<User> requestPasswordReset(String mail); Optional<User> completePasswordReset(String password, String key); @Scheduled(cron = "0 0 1 * * ?") void removeNotActivatedUsers();  }
@Test public void whenRequestForPasswordResetForActivatedUser() { User expected = this.createUser(); expected.setActivated(true); this.userRepository.save(expected); User result = this.userService.requestPasswordReset(expected.getEmail()).get(); assertNotNull(result); assertEquals(expected.getEmail(), result.getEmail()); assertNotNull(result.getResetKey()); }
@Test public void whenNotActivatedUserSendRequestPasswordReset() { User user = this.createUser(); Optional<User> result = this.userService.requestPasswordReset(user.getEmail()); assertFalse(result.isPresent()); }