@Test public void testCanAuthWithNewPassword() { final String password = "password"; UserEntry user = new UserEntry(); user.setUsername(username); user.setPassword(passwordEncoder.encode(password)); UserQueryRepository userRepository = mock(UserQueryRepository.class); when(userRepository.findByUsername(eq(username))).thenReturn(user, null); UserServiceFacadeImpl service = new UserServiceFacadeImpl(); service.setUserRepository(userRepository); service.setPasswordEncoder(passwordEncoder); boolean auth1 = service.canAuthWithNewPassword("", ""); boolean auth2 = service.canAuthWithNewPassword(null, password); boolean auth3 = service.canAuthWithNewPassword(username, ""); boolean auth4 = service.canAuthWithNewPassword(username, null); boolean auth5 = service.canAuthWithNewPassword(username, password); boolean auth6 = service.canAuthWithNewPassword(username, password + 1); assertThat(auth1, is(false)); assertThat(auth2, is(false)); assertThat(auth3, is(false)); assertThat(auth4, is(false)); assertThat(auth5, is(true)); assertThat(auth6, is(false)); verify(userRepository, times(2)).findByUsername(eq(username)); }
@Override public boolean canAuthWithNewPassword(String username, String newPassword) { if (!Strings.hasText(username) || !Strings.hasText(newPassword)) { logger.warn("username/password empty: username {}, password {}", username, newPassword); return false; } UserEntry user = userRepository.findByUsername(username); if (user == null) { logger.error("user {} cannot be found", username); return false; } return passwordEncoder.matches(newPassword, user.getPassword()); }
UserServiceFacadeImpl implements UserServiceFacade { @Override public boolean canAuthWithNewPassword(String username, String newPassword) { if (!Strings.hasText(username) || !Strings.hasText(newPassword)) { logger.warn("username/password empty: username {}, password {}", username, newPassword); return false; } UserEntry user = userRepository.findByUsername(username); if (user == null) { logger.error("user {} cannot be found", username); return false; } return passwordEncoder.matches(newPassword, user.getPassword()); } }
UserServiceFacadeImpl implements UserServiceFacade { @Override public boolean canAuthWithNewPassword(String username, String newPassword) { if (!Strings.hasText(username) || !Strings.hasText(newPassword)) { logger.warn("username/password empty: username {}, password {}", username, newPassword); return false; } UserEntry user = userRepository.findByUsername(username); if (user == null) { logger.error("user {} cannot be found", username); return false; } return passwordEncoder.matches(newPassword, user.getPassword()); }  }
UserServiceFacadeImpl implements UserServiceFacade { @Override public boolean canAuthWithNewPassword(String username, String newPassword) { if (!Strings.hasText(username) || !Strings.hasText(newPassword)) { logger.warn("username/password empty: username {}, password {}", username, newPassword); return false; } UserEntry user = userRepository.findByUsername(username); if (user == null) { logger.error("user {} cannot be found", username); return false; } return passwordEncoder.matches(newPassword, user.getPassword()); }  @Override UserAccount currentUser(); @Override UserEntry currentDetailUser(); @Override boolean isWithdrawPasswordSet(); @Override boolean isWithdrawPasswordMatched(String withdrawPassword); @Override boolean canAuthWithNewPassword(String username, String newPassword); @Override UserPasswordReset getToken(String token); PortfolioEntry obtainPortfolioForUser(); @Override boolean matchPreviousPassword(String previousPassword); @Override void editDetails(String username, String email, String cellPhoneNumber, String firstName, String lastName); @Override void editAdminDetails(String username,
                                 String email,
                                 Identifier identifier,
                                 String cellPhoneNumber,
                                 String firstName,
                                 String lastName,
                                 List<String> roles); @Override void updateNotificationSettings(boolean logonAlert, boolean withdrawMoneyAlert, boolean withdrawItemAlert, boolean executedAlert); @Override void changePassword(String previousPassword,
                               String newPassword,
                               String confirmedNewPassword,
                               String operatingIp,
                               Date changedTime); @Override boolean createWithdrawPassword(String withdrawPassword, String confirmedWithdrawPassword, String operatingIp, Date changedTime); @Override boolean matchPreviousWithdrawPassword(String previousPassword); boolean generateForgetPasswordToken(String email, String operatingIp, Date currentTime); @Override UserAccount resetPasswordWithToken(String token,
                                              String password,
                                              String confirmedPassword,
                                              String operatingIp,
                                              Date resetTime); @Override void changeWithdrawPassword(String previousPassword,
                                       String withdrawPassword,
                                       String confirmedWithdrawPassword,
                                       String operatingIp,
                                       Date changedTime); @Override UserEntry findByEmail(String email); @Override int findPasswordResetCount(String email, String ip, Date currentDate); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setCommandGateway(CommandGateway commandGateway); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserService(UserService userService); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserRepository(UserQueryRepository userRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPortfolioQueryRepository(PortfolioQueryRepository portfolioQueryRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserPasswordResetRepository(UserPasswordResetRepository userPasswordResetRepository); @Autowired void setEmailSender(ForgetPasswordEmailSender emailSender); }
UserServiceFacadeImpl implements UserServiceFacade { @Override public boolean canAuthWithNewPassword(String username, String newPassword) { if (!Strings.hasText(username) || !Strings.hasText(newPassword)) { logger.warn("username/password empty: username {}, password {}", username, newPassword); return false; } UserEntry user = userRepository.findByUsername(username); if (user == null) { logger.error("user {} cannot be found", username); return false; } return passwordEncoder.matches(newPassword, user.getPassword()); }  @Override UserAccount currentUser(); @Override UserEntry currentDetailUser(); @Override boolean isWithdrawPasswordSet(); @Override boolean isWithdrawPasswordMatched(String withdrawPassword); @Override boolean canAuthWithNewPassword(String username, String newPassword); @Override UserPasswordReset getToken(String token); PortfolioEntry obtainPortfolioForUser(); @Override boolean matchPreviousPassword(String previousPassword); @Override void editDetails(String username, String email, String cellPhoneNumber, String firstName, String lastName); @Override void editAdminDetails(String username,
                                 String email,
                                 Identifier identifier,
                                 String cellPhoneNumber,
                                 String firstName,
                                 String lastName,
                                 List<String> roles); @Override void updateNotificationSettings(boolean logonAlert, boolean withdrawMoneyAlert, boolean withdrawItemAlert, boolean executedAlert); @Override void changePassword(String previousPassword,
                               String newPassword,
                               String confirmedNewPassword,
                               String operatingIp,
                               Date changedTime); @Override boolean createWithdrawPassword(String withdrawPassword, String confirmedWithdrawPassword, String operatingIp, Date changedTime); @Override boolean matchPreviousWithdrawPassword(String previousPassword); boolean generateForgetPasswordToken(String email, String operatingIp, Date currentTime); @Override UserAccount resetPasswordWithToken(String token,
                                              String password,
                                              String confirmedPassword,
                                              String operatingIp,
                                              Date resetTime); @Override void changeWithdrawPassword(String previousPassword,
                                       String withdrawPassword,
                                       String confirmedWithdrawPassword,
                                       String operatingIp,
                                       Date changedTime); @Override UserEntry findByEmail(String email); @Override int findPasswordResetCount(String email, String ip, Date currentDate); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPasswordEncoder(PasswordEncoder passwordEncoder); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setCommandGateway(CommandGateway commandGateway); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserService(UserService userService); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserRepository(UserQueryRepository userRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setPortfolioQueryRepository(PortfolioQueryRepository portfolioQueryRepository); @SuppressWarnings("SpringJavaAutowiringInspection") @Autowired void setUserPasswordResetRepository(UserPasswordResetRepository userPasswordResetRepository); @Autowired void setEmailSender(ForgetPasswordEmailSender emailSender);  }