@Test public void testHandleForgetPassword() throws Exception { final Date currentTime = new Date(); final String operatingIp = "123.12.3.1"; final String username = "ewrwe"; final Date startDate = new Date(currentTime.getTime() - 24L * 60 * 60 * 1000L); final String email = "buyer1@167.hk"; UserId aggregateIdentifier = new UserId(); final Identifier identifier = new Identifier(Identifier.Type.IDENTITY_CARD, "110101201101019252"); String generatedToken = "avsdfsad"; UserEntry userEntry = new UserEntry(); userEntry.setUsername(username); userEntry.setPrimaryKey(aggregateIdentifier.toString()); userEntry.setLastName("Buyer One"); userEntry.setFirstName("Mr"); userEntry.setIdentifier(identifier); userEntry.setEmail(email); when(userQueryRepository.findByEmail(eq(email))).thenReturn(userEntry); when(passwordResetRepository.findNotExpiredByEmail(eq(email), eq(operatingIp), eq(startDate), eq(currentTime))) .thenReturn(ImmutableList.of(new UserPasswordReset(), new UserPasswordReset())); when(passwordResetRepository.findNotExpiredByEmail(eq(email), eq(startDate), eq(currentTime))) .thenReturn(ImmutableList.of(new UserPasswordReset(), new UserPasswordReset(), new UserPasswordReset(), new UserPasswordReset())); when(passwordResetRepository.findByToken(eq(generatedToken))).thenReturn(new UserPasswordReset(), null); PasswordResetTokenGenerator tokenGenerator = mock(PasswordResetTokenGenerator.class); when(tokenGenerator.generate(eq(username), eq(operatingIp), eq(currentTime))) .thenReturn(generatedToken); ForgetPasswordCommand command = new ForgetPasswordCommand(email, operatingIp, currentTime); handler.setPasswordResetTokenGenerator(tokenGenerator); String token = handler.handleForgetPassword(command); assertThat(token, equalTo(generatedToken)); ArgumentCaptor<UserPasswordReset> captor = ArgumentCaptor.forClass(UserPasswordReset.class); verify(passwordResetRepository).save(captor.capture()); UserPasswordReset reset = captor.getValue(); assertThat(reset, notNullValue()); assertThat(reset.getToken(), equalTo(generatedToken)); assertThat(reset.getUsername(), equalTo(username)); assertThat(reset.getUserId(), equalTo(aggregateIdentifier.toString())); assertThat(reset.getEmail(), equalTo(email)); assertThat(reset.getIp(), equalTo(operatingIp)); assertThat(reset.getExpirationDate(), equalTo(futureMinute(currentTime, 30))); verify(passwordResetRepository).findNotExpiredByEmail(eq(command.getEmail()), eq(operatingIp), eq(startDate), eq(currentTime)); verify(passwordResetRepository).findNotExpiredByEmail(eq(command.getEmail()), eq(startDate), eq(currentTime)); verify(tokenGenerator, times(2)).generate(eq(username), eq(operatingIp), eq(currentTime)); }
@CommandHandler public String handleForgetPassword(ForgetPasswordCommand command) { hasLength(command.getEmail()); notNull(command.getCurrentTime()); UserEntry user = userQueryRepository.findByEmail(command.getEmail()); if (user == null) { logger.warn("can not find user by email!", command.getEmail()); return ""; } Date date = command.getCurrentTime(); Date startDate = DateUtils.addDays(date, -1); List<UserPasswordReset> resets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), command.getOperatingIp(), startDate, date); if (!isEmpty(resets) && resets.size() >= 3) { throw new TooManyResetsException(command.getOperatingIp()); } List<UserPasswordReset> totalResets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), startDate, date); if (!isEmpty(totalResets) && totalResets.size() >= 5) { throw new TooManyResetsException(command.getOperatingIp()); } UserPasswordReset userPasswordReset = createPasswordReset(command, user); userPasswordResetRepository.save(userPasswordReset); return userPasswordReset.getToken(); }
UserCommandHandler { @CommandHandler public String handleForgetPassword(ForgetPasswordCommand command) { hasLength(command.getEmail()); notNull(command.getCurrentTime()); UserEntry user = userQueryRepository.findByEmail(command.getEmail()); if (user == null) { logger.warn("can not find user by email!", command.getEmail()); return ""; } Date date = command.getCurrentTime(); Date startDate = DateUtils.addDays(date, -1); List<UserPasswordReset> resets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), command.getOperatingIp(), startDate, date); if (!isEmpty(resets) && resets.size() >= 3) { throw new TooManyResetsException(command.getOperatingIp()); } List<UserPasswordReset> totalResets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), startDate, date); if (!isEmpty(totalResets) && totalResets.size() >= 5) { throw new TooManyResetsException(command.getOperatingIp()); } UserPasswordReset userPasswordReset = createPasswordReset(command, user); userPasswordResetRepository.save(userPasswordReset); return userPasswordReset.getToken(); } }
UserCommandHandler { @CommandHandler public String handleForgetPassword(ForgetPasswordCommand command) { hasLength(command.getEmail()); notNull(command.getCurrentTime()); UserEntry user = userQueryRepository.findByEmail(command.getEmail()); if (user == null) { logger.warn("can not find user by email!", command.getEmail()); return ""; } Date date = command.getCurrentTime(); Date startDate = DateUtils.addDays(date, -1); List<UserPasswordReset> resets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), command.getOperatingIp(), startDate, date); if (!isEmpty(resets) && resets.size() >= 3) { throw new TooManyResetsException(command.getOperatingIp()); } List<UserPasswordReset> totalResets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), startDate, date); if (!isEmpty(totalResets) && totalResets.size() >= 5) { throw new TooManyResetsException(command.getOperatingIp()); } UserPasswordReset userPasswordReset = createPasswordReset(command, user); userPasswordResetRepository.save(userPasswordReset); return userPasswordReset.getToken(); }  }
UserCommandHandler { @CommandHandler public String handleForgetPassword(ForgetPasswordCommand command) { hasLength(command.getEmail()); notNull(command.getCurrentTime()); UserEntry user = userQueryRepository.findByEmail(command.getEmail()); if (user == null) { logger.warn("can not find user by email!", command.getEmail()); return ""; } Date date = command.getCurrentTime(); Date startDate = DateUtils.addDays(date, -1); List<UserPasswordReset> resets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), command.getOperatingIp(), startDate, date); if (!isEmpty(resets) && resets.size() >= 3) { throw new TooManyResetsException(command.getOperatingIp()); } List<UserPasswordReset> totalResets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), startDate, date); if (!isEmpty(totalResets) && totalResets.size() >= 5) { throw new TooManyResetsException(command.getOperatingIp()); } UserPasswordReset userPasswordReset = createPasswordReset(command, user); userPasswordResetRepository.save(userPasswordReset); return userPasswordReset.getToken(); }  @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 handleForgetPassword(ForgetPasswordCommand command) { hasLength(command.getEmail()); notNull(command.getCurrentTime()); UserEntry user = userQueryRepository.findByEmail(command.getEmail()); if (user == null) { logger.warn("can not find user by email!", command.getEmail()); return ""; } Date date = command.getCurrentTime(); Date startDate = DateUtils.addDays(date, -1); List<UserPasswordReset> resets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), command.getOperatingIp(), startDate, date); if (!isEmpty(resets) && resets.size() >= 3) { throw new TooManyResetsException(command.getOperatingIp()); } List<UserPasswordReset> totalResets = userPasswordResetRepository.findNotExpiredByEmail(command.getEmail(), startDate, date); if (!isEmpty(totalResets) && totalResets.size() >= 5) { throw new TooManyResetsException(command.getOperatingIp()); } UserPasswordReset userPasswordReset = createPasswordReset(command, user); userPasswordResetRepository.save(userPasswordReset); return userPasswordReset.getToken(); }  @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; }