@Test public void testHandlePasswordReset() throws Exception { final UserId aggregateIdentifier = new UserId(); final Date time = currentTime(); final String operatingIp = "223.124.18.123"; final String email = "buyer1@163.com"; final String username = "buyer1"; final String generatedToken = "sdfoasop932723l3i47895df"; final Identifier identifier = new Identifier(Identifier.Type.IDENTITY_CARD, "110101201101019252"); final Date changedTime = currentTime(); UserEntry userEntry = new UserEntry(); userEntry.setUsername("buyer1"); userEntry.setPrimaryKey(aggregateIdentifier.toString()); userEntry.setLastName("Buyer One"); userEntry.setFirstName("Mr"); userEntry.setIdentifier(identifier); when(userQueryRepository.findByUsername("buyer1")).thenReturn(userEntry); UserPasswordReset userPasswordReset = new UserPasswordReset(); userPasswordReset.setUsername(username); userPasswordReset.setUserId(aggregateIdentifier.toString()); userPasswordReset.setExpirationDate(futureMinute(currentTime(), 10)); when(passwordResetRepository.findByToken(eq(generatedToken))).thenReturn(userPasswordReset); List<UserPasswordReset> listToDelete = ImmutableList.of(new UserPasswordReset(), new UserPasswordReset()); when(passwordResetRepository.findByUsername(eq(username))).thenReturn(listToDelete); String newPassword = passwordEncoder.encode("newPassword"); ResetPasswordCommand command = new ResetPasswordCommand(generatedToken, newPassword, newPassword, operatingIp, changedTime); fixture.given(new UserCreatedEvent(aggregateIdentifier, username, "Mr", "Buyer One", identifier, email, passwordEncoder.encode("buyer1"), UserCommandHandler.DEFAULT_ROLES, time)) .when(command) .expectEventsMatching( Matchers.payloadsMatching( sequenceOf(new PasswordChangedEventMatcher<PasswordChangedEvent>( new PasswordChangedEvent(aggregateIdentifier, username, "buyer1@163.com", passwordEncoder.encode("buyer1"), passwordEncoder.encode("buyer1"), operatingIp, changedTime), passwordEncoder, "buyer1")))); verify(passwordResetRepository).findByToken(eq(generatedToken)); verify(passwordResetRepository).findByUsername(eq(username)); verify(passwordResetRepository).delete(eq(listToDelete)); }
@CommandHandler public String handlePasswordReset(ResetPasswordCommand command) { hasLength(command.getToken()); hasLength(command.getPassword()); hasLength(command.getConfirmedPassword()); isTrue(command.isValid(), "The password and confirmed password should be the same!"); UserPasswordReset token = userPasswordResetRepository.findByToken(command.getToken()); if (token == null || !token.isValid(command.getChangedTime())) { logger.warn("cannot find password token {} or token {} is invalid", command.getToken(), token); return ""; } token.setUsed(true); userPasswordResetRepository.save(token); User user = repository.load(new UserId(token.getUserId())); final Date changedTime = nullSafe(command.getChangedTime(), currentTime()); user.changePassword(passwordEncoder.encode(command.getPassword()), passwordEncoder.encode(command.getConfirmedPassword()), command.getOperatingIp(), changedTime); clearPasswordResetTokens(token.getUserId(), token.getUsername()); return token.getUsername(); }
UserCommandHandler { @CommandHandler public String handlePasswordReset(ResetPasswordCommand command) { hasLength(command.getToken()); hasLength(command.getPassword()); hasLength(command.getConfirmedPassword()); isTrue(command.isValid(), "The password and confirmed password should be the same!"); UserPasswordReset token = userPasswordResetRepository.findByToken(command.getToken()); if (token == null || !token.isValid(command.getChangedTime())) { logger.warn("cannot find password token {} or token {} is invalid", command.getToken(), token); return ""; } token.setUsed(true); userPasswordResetRepository.save(token); User user = repository.load(new UserId(token.getUserId())); final Date changedTime = nullSafe(command.getChangedTime(), currentTime()); user.changePassword(passwordEncoder.encode(command.getPassword()), passwordEncoder.encode(command.getConfirmedPassword()), command.getOperatingIp(), changedTime); clearPasswordResetTokens(token.getUserId(), token.getUsername()); return token.getUsername(); } }
UserCommandHandler { @CommandHandler public String handlePasswordReset(ResetPasswordCommand command) { hasLength(command.getToken()); hasLength(command.getPassword()); hasLength(command.getConfirmedPassword()); isTrue(command.isValid(), "The password and confirmed password should be the same!"); UserPasswordReset token = userPasswordResetRepository.findByToken(command.getToken()); if (token == null || !token.isValid(command.getChangedTime())) { logger.warn("cannot find password token {} or token {} is invalid", command.getToken(), token); return ""; } token.setUsed(true); userPasswordResetRepository.save(token); User user = repository.load(new UserId(token.getUserId())); final Date changedTime = nullSafe(command.getChangedTime(), currentTime()); user.changePassword(passwordEncoder.encode(command.getPassword()), passwordEncoder.encode(command.getConfirmedPassword()), command.getOperatingIp(), changedTime); clearPasswordResetTokens(token.getUserId(), token.getUsername()); return token.getUsername(); }  }
UserCommandHandler { @CommandHandler public String handlePasswordReset(ResetPasswordCommand command) { hasLength(command.getToken()); hasLength(command.getPassword()); hasLength(command.getConfirmedPassword()); isTrue(command.isValid(), "The password and confirmed password should be the same!"); UserPasswordReset token = userPasswordResetRepository.findByToken(command.getToken()); if (token == null || !token.isValid(command.getChangedTime())) { logger.warn("cannot find password token {} or token {} is invalid", command.getToken(), token); return ""; } token.setUsed(true); userPasswordResetRepository.save(token); User user = repository.load(new UserId(token.getUserId())); final Date changedTime = nullSafe(command.getChangedTime(), currentTime()); user.changePassword(passwordEncoder.encode(command.getPassword()), passwordEncoder.encode(command.getConfirmedPassword()), command.getOperatingIp(), changedTime); clearPasswordResetTokens(token.getUserId(), token.getUsername()); return token.getUsername(); }  @CommandHandler UserId handleCreateUser(CreateUserCommand command); @CommandHandler UserAccount handleAuthenticateUser(AuthenticateUserCommand command); @CommandHandler String handleForgetPassword(ForgetPasswordCommand command); @CommandHandler String handlePasswordReset(ResetPasswordCommand command); @CommandHandler void handleChangePassword(ChangePasswordCommand command); @CommandHandler void handleChangeWithdrawPasswordCommand(ChangeWithdrawPasswordCommand command); @CommandHandler void handleCreateWithdrawPasswordCommand(CreateWithdrawPasswordCommand command); @CommandHandler void handleUpdateNotificationCommand(UpdateNotificationSettingsCommand command); @CommandHandler void handleChangeInfoCommand(ChangeInfoCommand command); @CommandHandler void handleChangeAdminInfoCommand(ChangeAdminInfoCommand command); @Autowired @Qualifier("userRepository") void setRepository(Repository<User> userRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserPasswordResetRepository(UserPasswordResetRepository userPasswordResetRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserRepository(UserQueryRepository userRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPasswordResetTokenGenerator(PasswordResetTokenGenerator passwordResetTokenGenerator); }
UserCommandHandler { @CommandHandler public String handlePasswordReset(ResetPasswordCommand command) { hasLength(command.getToken()); hasLength(command.getPassword()); hasLength(command.getConfirmedPassword()); isTrue(command.isValid(), "The password and confirmed password should be the same!"); UserPasswordReset token = userPasswordResetRepository.findByToken(command.getToken()); if (token == null || !token.isValid(command.getChangedTime())) { logger.warn("cannot find password token {} or token {} is invalid", command.getToken(), token); return ""; } token.setUsed(true); userPasswordResetRepository.save(token); User user = repository.load(new UserId(token.getUserId())); final Date changedTime = nullSafe(command.getChangedTime(), currentTime()); user.changePassword(passwordEncoder.encode(command.getPassword()), passwordEncoder.encode(command.getConfirmedPassword()), command.getOperatingIp(), changedTime); clearPasswordResetTokens(token.getUserId(), token.getUsername()); return token.getUsername(); }  @CommandHandler UserId handleCreateUser(CreateUserCommand command); @CommandHandler UserAccount handleAuthenticateUser(AuthenticateUserCommand command); @CommandHandler String handleForgetPassword(ForgetPasswordCommand command); @CommandHandler String handlePasswordReset(ResetPasswordCommand command); @CommandHandler void handleChangePassword(ChangePasswordCommand command); @CommandHandler void handleChangeWithdrawPasswordCommand(ChangeWithdrawPasswordCommand command); @CommandHandler void handleCreateWithdrawPasswordCommand(CreateWithdrawPasswordCommand command); @CommandHandler void handleUpdateNotificationCommand(UpdateNotificationSettingsCommand command); @CommandHandler void handleChangeInfoCommand(ChangeInfoCommand command); @CommandHandler void handleChangeAdminInfoCommand(ChangeAdminInfoCommand command); @Autowired @Qualifier("userRepository") void setRepository(Repository<User> userRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserPasswordResetRepository(UserPasswordResetRepository userPasswordResetRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserRepository(UserQueryRepository userRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPasswordResetTokenGenerator(PasswordResetTokenGenerator passwordResetTokenGenerator); static List<String> DEFAULT_ROLES; }