### Question:
LoginFilter extends AbstractAuthenticationProcessingFilter { @Override public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws ServletException { if (!request.getMethod().equals(HttpMethod.POST.name())) { throw new AuthMethodNotSupportedException(request.getMethod()); } Optional<LoginRequest> optionalLoginRequest = getCredentialFromAuthorizationHeader(request); LoginRequest loginRequest = optionalLoginRequest.orElseGet(() -> getCredentialsFromBody(request)); if (StringUtils.isBlank(loginRequest.getUsername()) || StringUtils.isBlank(loginRequest.getPassword())) { throw new AuthenticationCredentialsNotFoundException("Username or password not provided."); } UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(loginRequest.getUsername(), loginRequest.getPassword()); Authentication auth = null; try { auth = this.getAuthenticationManager().authenticate(authentication); } catch (RuntimeException ex) { resourceAccessExceptionHandler.handleException(request, response, ex); } return auth; } LoginFilter(
        String authEndpoint,
        AuthenticationSuccessHandler successHandler,
        AuthenticationFailureHandler failureHandler,
        ObjectMapper mapper,
        AuthenticationManager authenticationManager,
        ResourceAccessExceptionHandler resourceAccessExceptionHandler); @Override Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response);  }### Answer:
@Test public void shouldCallAuthenticationManagerAuthenticateWithAuthHeader() throws ServletException { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.POST.name()); httpServletRequest.addHeader(HttpHeaders.AUTHORIZATION, VALID_AUTH_HEADER); httpServletResponse = new MockHttpServletResponse(); loginFilter.attemptAuthentication(httpServletRequest, httpServletResponse); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("user", "pwd"); verify(authenticationManager).authenticate(authentication); }
@Test public void shouldCallAuthenticationManagerAuthenticateWithJson() throws ServletException { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.POST.name()); httpServletRequest.setContent(VALID_JSON.getBytes()); httpServletResponse = new MockHttpServletResponse(); loginFilter.attemptAuthentication(httpServletRequest, httpServletResponse); UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken("user", "pwd"); verify(authenticationManager).authenticate(authentication); }
@Test public void shouldFailWithJsonEmptyCredentials() throws ServletException { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.POST.name()); httpServletRequest.setContent(EMPTY_JSON.getBytes()); httpServletResponse = new MockHttpServletResponse(); exception.expect(AuthenticationCredentialsNotFoundException.class); exception.expectMessage("Username or password not provided."); loginFilter.attemptAuthentication(httpServletRequest, httpServletResponse); }
@Test public void shouldFailWithoutAuth() throws ServletException { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.POST.name()); httpServletResponse = new MockHttpServletResponse(); exception.expect(AuthenticationCredentialsNotFoundException.class); exception.expectMessage("Login object has wrong format."); loginFilter.attemptAuthentication(httpServletRequest, httpServletResponse); }
@Test public void shouldFailWithWrongHttpMethod() throws ServletException { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.GET.name()); httpServletResponse = new MockHttpServletResponse(); exception.expect(AuthMethodNotSupportedException.class); exception.expectMessage("GET"); loginFilter.attemptAuthentication(httpServletRequest, httpServletResponse); }
@Test public void shouldFailWithIncorrectCredentialsFormat() throws ServletException { httpServletRequest = new MockHttpServletRequest(); httpServletRequest.setMethod(HttpMethod.POST.name()); httpServletRequest.addHeader(HttpHeaders.AUTHORIZATION, INVALID_AUTH_HEADER); httpServletResponse = new MockHttpServletResponse(); exception.expect(AuthenticationCredentialsNotFoundException.class); exception.expectMessage("Login object has wrong format."); loginFilter.attemptAuthentication(httpServletRequest, httpServletResponse); }