@Test(expected = BadVerifyEmailTokenException.class) public void updatePasswordUsingTokenWithNonExistentTokenTest() { String token = "foo"; String newPassword = "bar"; when(restorePasswordEmailRepo.findByToken(token)).thenReturn(Optional.empty()); passwordRecoveryService.updatePasswordUsingToken(token, newPassword); }
@Transactional @Override public void updatePasswordUsingToken(String token, String newPassword) { RestorePasswordEmail restorePasswordEmail = restorePasswordEmailRepo .findByToken(token) .orElseThrow(() -> new BadVerifyEmailTokenException(NO_ANY_EMAIL_TO_VERIFY_BY_THIS_TOKEN)); if (isNotExpired(restorePasswordEmail.getExpiryDate())) { applicationEventPublisher.publishEvent( new UpdatePasswordEvent(this, newPassword, restorePasswordEmail.getUser().getId()) ); restorePasswordEmailRepo.delete(restorePasswordEmail); log.info("User with email " + restorePasswordEmail.getUser().getEmail() + " has successfully restored the password using token " + token); } else { log.info("Password restoration token of user with email " + restorePasswordEmail.getUser().getEmail() + " has been expired. Token: " + token); throw new UserActivationEmailTokenExpiredException(EMAIL_TOKEN_EXPIRED); } }
PasswordRecoveryServiceImpl implements PasswordRecoveryService { @Transactional @Override public void updatePasswordUsingToken(String token, String newPassword) { RestorePasswordEmail restorePasswordEmail = restorePasswordEmailRepo .findByToken(token) .orElseThrow(() -> new BadVerifyEmailTokenException(NO_ANY_EMAIL_TO_VERIFY_BY_THIS_TOKEN)); if (isNotExpired(restorePasswordEmail.getExpiryDate())) { applicationEventPublisher.publishEvent( new UpdatePasswordEvent(this, newPassword, restorePasswordEmail.getUser().getId()) ); restorePasswordEmailRepo.delete(restorePasswordEmail); log.info("User with email " + restorePasswordEmail.getUser().getEmail() + " has successfully restored the password using token " + token); } else { log.info("Password restoration token of user with email " + restorePasswordEmail.getUser().getEmail() + " has been expired. Token: " + token); throw new UserActivationEmailTokenExpiredException(EMAIL_TOKEN_EXPIRED); } } }
PasswordRecoveryServiceImpl implements PasswordRecoveryService { @Transactional @Override public void updatePasswordUsingToken(String token, String newPassword) { RestorePasswordEmail restorePasswordEmail = restorePasswordEmailRepo .findByToken(token) .orElseThrow(() -> new BadVerifyEmailTokenException(NO_ANY_EMAIL_TO_VERIFY_BY_THIS_TOKEN)); if (isNotExpired(restorePasswordEmail.getExpiryDate())) { applicationEventPublisher.publishEvent( new UpdatePasswordEvent(this, newPassword, restorePasswordEmail.getUser().getId()) ); restorePasswordEmailRepo.delete(restorePasswordEmail); log.info("User with email " + restorePasswordEmail.getUser().getEmail() + " has successfully restored the password using token " + token); } else { log.info("Password restoration token of user with email " + restorePasswordEmail.getUser().getEmail() + " has been expired. Token: " + token); throw new UserActivationEmailTokenExpiredException(EMAIL_TOKEN_EXPIRED); } } PasswordRecoveryServiceImpl(
        RestorePasswordEmailRepo restorePasswordEmailRepo,
        UserRepo userRepo,
        ApplicationEventPublisher applicationEventPublisher,
        RabbitTemplate rabbitTemplate,
        JwtTool jwtTool
    ); }
PasswordRecoveryServiceImpl implements PasswordRecoveryService { @Transactional @Override public void updatePasswordUsingToken(String token, String newPassword) { RestorePasswordEmail restorePasswordEmail = restorePasswordEmailRepo .findByToken(token) .orElseThrow(() -> new BadVerifyEmailTokenException(NO_ANY_EMAIL_TO_VERIFY_BY_THIS_TOKEN)); if (isNotExpired(restorePasswordEmail.getExpiryDate())) { applicationEventPublisher.publishEvent( new UpdatePasswordEvent(this, newPassword, restorePasswordEmail.getUser().getId()) ); restorePasswordEmailRepo.delete(restorePasswordEmail); log.info("User with email " + restorePasswordEmail.getUser().getEmail() + " has successfully restored the password using token " + token); } else { log.info("Password restoration token of user with email " + restorePasswordEmail.getUser().getEmail() + " has been expired. Token: " + token); throw new UserActivationEmailTokenExpiredException(EMAIL_TOKEN_EXPIRED); } } PasswordRecoveryServiceImpl(
        RestorePasswordEmailRepo restorePasswordEmailRepo,
        UserRepo userRepo,
        ApplicationEventPublisher applicationEventPublisher,
        RabbitTemplate rabbitTemplate,
        JwtTool jwtTool
    ); @Transactional @Override void sendPasswordRecoveryEmailTo(String email); @Transactional @Override void updatePasswordUsingToken(String token, String newPassword); }
PasswordRecoveryServiceImpl implements PasswordRecoveryService { @Transactional @Override public void updatePasswordUsingToken(String token, String newPassword) { RestorePasswordEmail restorePasswordEmail = restorePasswordEmailRepo .findByToken(token) .orElseThrow(() -> new BadVerifyEmailTokenException(NO_ANY_EMAIL_TO_VERIFY_BY_THIS_TOKEN)); if (isNotExpired(restorePasswordEmail.getExpiryDate())) { applicationEventPublisher.publishEvent( new UpdatePasswordEvent(this, newPassword, restorePasswordEmail.getUser().getId()) ); restorePasswordEmailRepo.delete(restorePasswordEmail); log.info("User with email " + restorePasswordEmail.getUser().getEmail() + " has successfully restored the password using token " + token); } else { log.info("Password restoration token of user with email " + restorePasswordEmail.getUser().getEmail() + " has been expired. Token: " + token); throw new UserActivationEmailTokenExpiredException(EMAIL_TOKEN_EXPIRED); } } PasswordRecoveryServiceImpl(
        RestorePasswordEmailRepo restorePasswordEmailRepo,
        UserRepo userRepo,
        ApplicationEventPublisher applicationEventPublisher,
        RabbitTemplate rabbitTemplate,
        JwtTool jwtTool
    ); @Transactional @Override void sendPasswordRecoveryEmailTo(String email); @Transactional @Override void updatePasswordUsingToken(String token, String newPassword);  }