package com.quizdeck.filters;

import com.quizdeck.QuizDeckApplication;
import com.quizdeck.services.AuthenticationService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockFilterConfig;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;

/**
 * Created by Brandon on 2/13/2016.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = QuizDeckApplication.class)
public class AuthenticationFilterTest {
    @Resource(name = "secretKey")
    private String secretKey;
    @Autowired
    private AuthenticationService authService;

    private AuthenticationFilter filter;
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;
    private MockFilterChain chain;

    @Before
    public void setup() {
        this.filter = new AuthenticationFilter(this.secretKey);
        this.filter.init(new MockFilterConfig());

        this.request = new MockHttpServletRequest();
        this.response = new MockHttpServletResponse();
        this.chain = new MockFilterChain();
    }

    @After
    public void teardown() {
        this.filter.destroy();
    }

    @Test
    public void addsClaims() throws IOException, ServletException {
        String token = authService.buildToken("testUser", "User");

        request.setServletPath("/rest/secure/getClaims/");
        request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);

        filter.doFilter(request, response, chain);

        assertNotNull(request.getAttribute(AuthenticationFilter.CLAIMS_ATTRIBUTE));
    }

    @Test
    public void errorsWithBadHeader() throws IOException, ServletException {
        request.setServletPath("/rest/secure/getClaims/");
        request.addHeader(HttpHeaders.AUTHORIZATION, "NotBearer");

        filter.doFilter(request, response, chain);

        assertThat(response.getStatus(), is(equalTo(HttpStatus.UNAUTHORIZED.value())));
    }

    @Test
    public void errorsWithMalformedHeader() throws IOException, ServletException {
        request.setServletPath("/rest/secure/getClaims/");
        request.addHeader(HttpHeaders.AUTHORIZATION, "Bearer abc.abc.abc");

        filter.doFilter(request, response, chain);

        assertThat(response.getStatus(), is(equalTo(HttpStatus.UNAUTHORIZED.value())));
    }

    @Test
    public void errorsWithNoHeader() throws IOException, ServletException {
        request.setServletPath("/rest/secure/getClaims/");

        filter.doFilter(request, response, chain);

        assertThat(response.getStatus(), is(equalTo(HttpStatus.UNAUTHORIZED.value())));
    }
}

@@ -121,68 +121,8 @@
 ce;%0A
-import io.jsonwebtoken.Claims;%0Aimport io.jsonwebtoken.Jwts;%0A
 impo
@@ -1022,16 +1022,53 @@
 ertThat;
+%0Aimport static org.mockito.Mockito.*;
 %0A%0A/**%0A *
@@ -1830,20 +1830,21 @@
 chain = 
-new 
+mock(
 MockFilt
@@ -1850,17 +1850,22 @@
 terChain
-(
+.class
 );%0A    %7D
@@ -2092,68 +2092,8 @@
 r%22);
-%0A%0A        request.setServletPath(%22/rest/secure/getClaims/%22);
 %0A   
@@ -2295,24 +2295,85 @@
 TTRIBUTE));%0A
+        verify(chain, times(1)).doFilter(request, response);%0A
     %7D%0A%0A    @
@@ -2458,67 +2458,8 @@
 n %7B%0A
-        request.setServletPath(%22/rest/secure/getClaims/%22);%0A
     
@@ -2654,32 +2654,92 @@
 ZED.value())));%0A
+        verify(chain, never()).doFilter(request, response);%0A
     %7D%0A%0A    @Test
@@ -2826,67 +2826,8 @@
 n %7B%0A
-        request.setServletPath(%22/rest/secure/getClaims/%22);%0A
     
@@ -3035,24 +3035,84 @@
 value())));%0A
+        verify(chain, never()).doFilter(request, response);%0A
     %7D%0A%0A    @
@@ -3196,68 +3196,8 @@
 n %7B%0A
-        request.setServletPath(%22/rest/secure/getClaims/%22);%0A%0A
     
@@ -3328,20 +3328,80 @@
 value())));%0A
+        verify(chain, never()).doFilter(request, response);%0A
     %7D%0A%7D%0A

Add mockito verifications