### Question:
TransactionalAuthenticator extends AbstractTransactionalEntityService<JCUser, UserDao> implements Authenticator { @Override public AuthenticationStatus authenticate(LoginUserDto loginUserDto, HttpServletRequest request, HttpServletResponse response) throws UnexpectedErrorException, NoConnectionException { AuthenticationStatus result; JCUser user; try { user = getByUsername(loginUserDto.getUserName()); result = authenticateDefault(user, loginUserDto.getPassword(), loginUserDto.isRememberMe(), request, response); } catch (NotFoundException e) { LOGGER.info("User was not found during login process, username = {}, IP={}", loginUserDto.getUserName(), loginUserDto.getClientIp()); result = authenticateByPluginAndUpdateUserInfo(loginUserDto, true, request, response); } catch(DisabledException e) { LOGGER.info("DisabledException: username = {}, IP={}, message={}", new String[]{loginUserDto.getUserName(), loginUserDto.getClientIp(), e.getMessage()}); result = AuthenticationStatus.NOT_ENABLED; } catch (AuthenticationException e) { LOGGER.info("AuthenticationException: username = {}, IP={}, message={}", new String[]{loginUserDto.getUserName(), loginUserDto.getClientIp(), e.getMessage()}); result = authenticateByPluginAndUpdateUserInfo(loginUserDto, false, request, response); } return result; } TransactionalAuthenticator(PluginLoader pluginLoader, UserDao dao, GroupDao groupDao,
                                      EncryptionService encryptionService,
                                      MailService mailService,
                                      ImageService avatarService,
                                      PluginService pluginService,
                                      SecurityContextFacade securityFacade,
                                      RememberMeServices rememberMeServices,
                                      SessionAuthenticationStrategy sessionStrategy,
                                      Validator validator,
                                      AuthenticationManager authenticationManager); @Override AuthenticationStatus authenticate(LoginUserDto loginUserDto, HttpServletRequest request,
                                HttpServletResponse response); @Override BindingResult register(RegisterUserDto registerUserDto); @Override void activateAccount(String uuid); void registerByPlugin(UserDto userDto, boolean dryRun, BindingResult bindingResult); JCUser storeRegisteredUser(UserDto userDto); static final boolean DEFAULT_AUTOSUBSCRIBE; static final boolean DEFAULT_SEND_PM_NOTIFICATION; }### Answer:
@Test public void authenticateNotEnabledUserShouldFail() throws Exception { LoginUserDto loginUserDto = createDefaultLoginUserDto(); prepareOldUser(loginUserDto.getUserName()); when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class))) .thenThrow(new DisabledException(null)); AuthenticationStatus result = authenticator.authenticate(loginUserDto, httpRequest, httpResponse); assertEquals(result, AuthenticationStatus.NOT_ENABLED, "Authenticate user with bad credentials should fail."); }
@Test(expectedExceptions = UnexpectedErrorException.class) public void authenticateShouldFailIfPluginThrowsAnUnexpectedException() throws Exception { String passwordHash = "5f4dcc3b5aa765d61d8327deb882cf99"; LoginUserDto loginUserDto = createDefaultLoginUserDto(); when(encryptionService.encryptPassword(loginUserDto.getPassword())).thenReturn(passwordHash); when(authPlugin.getState()).thenReturn(Plugin.State.ENABLED); Class cl = AuthenticationPlugin.class; when(pluginLoader.getPluginByClassName(cl)).thenReturn(authPlugin); when(authPlugin.authenticate(loginUserDto.getUserName(), passwordHash)).thenThrow(new UnexpectedErrorException()); authenticator.authenticate(loginUserDto, httpRequest, httpResponse); }