### Question:
AsyncChangePassword implements AsynchronousProcess { public void changePasswordAsAdmin(CommandSender sender, final String playerName, String newPassword) { final String lowerCaseName = playerName.toLowerCase(); if (!(playerCache.isAuthenticated(lowerCaseName) || dataSource.isAuthAvailable(lowerCaseName))) { if (sender == null) { ConsoleLogger.warning("Tried to change password for user " + lowerCaseName + " but it doesn't exist!"); } else { commonService.send(sender, MessageKey.UNKNOWN_USER); } return; } HashedPassword hashedPassword = passwordSecurity.computeHash(newPassword, lowerCaseName); if (dataSource.updatePassword(lowerCaseName, hashedPassword)) { bungeeSender.sendAuthMeBungeecordMessage(MessageType.REFRESH_PASSWORD, lowerCaseName); if (sender != null) { commonService.send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS); ConsoleLogger.info(sender.getName() + " changed password of " + lowerCaseName); } else { ConsoleLogger.info("Changed password of " + lowerCaseName); } } else { if (sender != null) { commonService.send(sender, MessageKey.ERROR); } ConsoleLogger.warning("An error occurred while changing password for user " + lowerCaseName + "!"); } } AsyncChangePassword(); void changePassword(final Player player, String oldPassword, String newPassword); void changePasswordAsAdmin(CommandSender sender, final String playerName, String newPassword);  }### Answer:
@Test public void shouldRejectCommandForUnknownUser() { CommandSender sender = mock(CommandSender.class); String player = "player"; String password = "password"; given(playerCache.isAuthenticated(player)).willReturn(false); given(dataSource.isAuthAvailable(player)).willReturn(false); asyncChangePassword.changePasswordAsAdmin(sender, player, password); verify(commonService).send(sender, MessageKey.UNKNOWN_USER); verify(dataSource, only()).isAuthAvailable(player); }
@Test public void shouldUpdatePasswordOfLoggedInUser() { CommandSender sender = mock(CommandSender.class); String player = "my_user12"; String password = "passPass"; given(playerCache.isAuthenticated(player)).willReturn(true); HashedPassword hashedPassword = mock(HashedPassword.class); given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword); given(dataSource.updatePassword(player, hashedPassword)).willReturn(true); asyncChangePassword.changePasswordAsAdmin(sender, player, password); verify(commonService).send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS); verify(passwordSecurity).computeHash(password, player); verify(dataSource).updatePassword(player, hashedPassword); }
@Test public void shouldUpdatePasswordOfOfflineUser() { CommandSender sender = mock(CommandSender.class); String player = "my_user12"; String password = "passPass"; given(playerCache.isAuthenticated(player)).willReturn(false); given(dataSource.isAuthAvailable(player)).willReturn(true); HashedPassword hashedPassword = mock(HashedPassword.class); given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword); given(dataSource.updatePassword(player, hashedPassword)).willReturn(true); asyncChangePassword.changePasswordAsAdmin(sender, player, password); verify(commonService).send(sender, MessageKey.PASSWORD_CHANGED_SUCCESS); verify(passwordSecurity).computeHash(password, player); verify(dataSource).updatePassword(player, hashedPassword); }
@Test public void shouldReportWhenSaveFailed() { CommandSender sender = mock(CommandSender.class); String player = "my_user12"; String password = "passPass"; given(playerCache.isAuthenticated(player)).willReturn(true); HashedPassword hashedPassword = mock(HashedPassword.class); given(passwordSecurity.computeHash(password, player)).willReturn(hashedPassword); given(dataSource.updatePassword(player, hashedPassword)).willReturn(false); asyncChangePassword.changePasswordAsAdmin(sender, player, password); verify(commonService).send(sender, MessageKey.ERROR); verify(passwordSecurity).computeHash(password, player); verify(dataSource).updatePassword(player, hashedPassword); }