lundi 13 juillet 2020

Testing access token filter

I have created a Bearer token creator and when i write its test, the test always fails and return nullPointerException what is the problem ?

Note: A bearer token syntax is like that [Bearer {someEncodedCharacter}].

    public class JwtTokenFilterTest {
    
      @MockBean
      private HttpServletRequest httpRequest;
      @MockBean
      private TokenManager tokenManagerTester;
      @MockBean
      private Authentication authentication;
      @MockBean
      private SecurityContext securityContext;
      @MockBean
      private HttpServletResponse httpResponse;
      @MockBean
      private FilterChain filterChain;
      @Test
      public void givenHttpServletRequestandHttpServletResponseandFilterChainDoFilterInternal()
          throws ServletException, IOException {
        String token="eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhYmMxMjMiLCJpc3MiOiJ3d3cuYWJjLmNvbSIsImlhdCI6MTU5NDY0MTUzNywiZXhwIjoxNTk0NjQxODM3fQ.GHzVaQW_tvqo8HlDmoXzZ8WIYGcLHciLOSMFxsZUOsY";
        Mockito.when(httpRequest.getHeader("Authorization")).thenReturn("Bearer "+token);//<--
// Intellij gives the error from above line and say NullPointerException 
        Mockito.when(securityContext.getAuthentication()).thenReturn(authentication);
        SecurityContextHolder.setContext(securityContext);
        Mockito.when(tokenManagerTester.tokenValidate(Mockito.anyString())).thenReturn(true);
        Mockito.when(tokenManagerTester
            .getUsernameToken(token))
            .thenReturn("abc123");
    
    
        JwtTokenFilter filter=new JwtTokenFilter();
    
        filter.doFilterInternal(httpRequest, httpResponse, filterChain);
        Mockito.verify(filterChain,Mockito.times(1));
    
      }
    
    
    }

Here is the JwtfFilter:

@Component
public class JwtTokenFilter extends OncePerRequestFilter {



    @Autowired
    private TokenManager tokenManager;

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest,
                                    @NotNull HttpServletResponse httpServletResponse,
                                    @NotNull FilterChain filterChain) throws ServletException, IOException {

        /**
         * We are parsing our token in two pieces and we process the second
         * "Bearer hvs231asas2355"
         */
        final String authHeader = httpServletRequest.getHeader("Authorization");
        /* */
        String username = null;
        String token = null;


        if (authHeader != null && authHeader.contains("Bearer")) {
            token = authHeader.substring(7);
            try {
                username = tokenManager.getUsernameToken(token);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

        if (username != null && SecurityContextHolder.getContext().getAuthentication() == null) {
            if (tokenManager.tokenValidate(token)) {
                UsernamePasswordAuthenticationToken upassToken =
                        new UsernamePasswordAuthenticationToken(username, null, new ArrayList<>());
                upassToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(httpServletRequest));
                SecurityContextHolder.getContext().setAuthentication(upassToken);
            }
        }

        filterChain.doFilter(httpServletRequest, httpServletResponse); 
    }
}

Note2: I think that if i controll the filterChain.dofilter i can see that the method works correct. For this reason i wrote the Mockito.verify(filterChain,Mockito.times(1));

Aucun commentaire:

Enregistrer un commentaire