package com.documents4j.server.auth;

import org.junit.Before;
import org.junit.Test;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.UriInfo;
import java.util.Base64;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class AuthFilterTest {

    private ContainerRequestContext containerRequestContext;

    @Before
    public void setUp() {
        containerRequestContext = mock(ContainerRequestContext.class);
    }

    @Test
    public void assureStatus403_withBasicAuth_noAuthGivenInRequest() {
        final AuthFilter authFilter = new AuthFilter("user:pass");

        authFilter.filter(containerRequestContext);

        verify(containerRequestContext).abortWith(argThat(response -> response.getStatus() == 403));
    }

    @Test
    public void assureStatus403_withBasicAuth_incorrectAuthGivenInRequest() {
        final AuthFilter authFilter = new AuthFilter("user:pass");
        when(containerRequestContext.getHeaderString("authorization")).thenReturn(
                "Basic " + new String(Base64.getEncoder().encode("user:incorrectpass".getBytes()))
        );

        authFilter.filter(containerRequestContext);

        verify(containerRequestContext).abortWith(argThat(response -> response.getStatus() == 403));
    }

    @Test
    public void assureStatusOk_withBasicAuth_correctAuthGivenInRequest() {
        final AuthFilter authFilter = new AuthFilter("user:pass");
        when(containerRequestContext.getHeaderString("authorization")).thenReturn(
                "Basic " + new String(Base64.getEncoder().encode("user:pass".getBytes()))
        );

        authFilter.filter(containerRequestContext);

        verify(containerRequestContext, never()).abortWith(any());
    }

    @Test
    public void assureStatusOk_withBasicAuth_exludedEndpointCalled_withExcludePatterns_noAuthGiven() {
        final AuthFilter authFilter = new AuthFilter("user:pass", "/hea[lth]{3}.*");
        final UriInfo uriInfo = mock(UriInfo.class);
        when(uriInfo.getPath()).thenReturn("/health");
        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);

        authFilter.filter(containerRequestContext);

        verify(containerRequestContext, never()).abortWith(any());
    }

    @Test
    public void assureStatus403_withBasicAuth_protectedEndpointCalled_withExcludePatterns_noAuthGiven() {
        final AuthFilter authFilter = new AuthFilter("user:pass", "/hea[lth]{3}.*");
        final UriInfo uriInfo = mock(UriInfo.class);
        when(uriInfo.getPath()).thenReturn("/");
        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);

        authFilter.filter(containerRequestContext);

        verify(containerRequestContext).abortWith(argThat(response -> response.getStatus() == 403));
    }
}

@@ -548,16 +548,45 @@
 Context;
+%0A    private UriInfo uriInfo;
 %0A%0A    @B
@@ -684,24 +684,63 @@
 ext.class);%0A
+        uriInfo = mock(UriInfo.class);%0A
     %7D%0A%0A    @
@@ -881,16 +881,137 @@
 :pass%22);
+%0A        when(uriInfo.getPath()).thenReturn(%22/%22);%0A        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
 %0A%0A      
@@ -1504,32 +1504,153 @@
 s()))%0A        );
+%0A        when(uriInfo.getPath()).thenReturn(%22/%22);%0A        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
 %0A%0A        authFi
@@ -2131,16 +2131,137 @@
       );
+%0A        when(uriInfo.getPath()).thenReturn(%22/%22);%0A        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);
 %0A%0A      
@@ -2565,79 +2565,20 @@
 %22, %22
-/
+%5E
 hea
-%5B
 lth
-%5D%7B3%7D.*
+$
 %22);%0A
-        final UriInfo uriInfo = mock(UriInfo.class);%0A
     
@@ -2617,17 +2617,16 @@
 Return(%22
-/
 health%22)
@@ -3014,76 +3014,17 @@
 %22, %22
-/
+%5E
 hea
-%5B
 lth
-%5D%7B3%7D.*%22);%0A        final UriInfo uriInfo = mock(UriInfo.class
+$%22
 );%0A 

Fix tests