<commit_before>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));
    }
}<commit_msg>Fix tests<commit_after>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;
    private UriInfo uriInfo;

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

    @Test
    public void assureStatus403_withBasicAuth_noAuthGivenInRequest() {
        final AuthFilter authFilter = new AuthFilter("user:pass");
        when(uriInfo.getPath()).thenReturn("/");
        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);

        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()))
        );
        when(uriInfo.getPath()).thenReturn("/");
        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);

        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()))
        );
        when(uriInfo.getPath()).thenReturn("/");
        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);

        authFilter.filter(containerRequestContext);

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

    @Test
    public void assureStatusOk_withBasicAuth_exludedEndpointCalled_withExcludePatterns_noAuthGiven() {
        final AuthFilter authFilter = new AuthFilter("user:pass", "^health$");
        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", "^health$");
        when(uriInfo.getPath()).thenReturn("/");
        when(containerRequestContext.getUriInfo()).thenReturn(uriInfo);

        authFilter.filter(containerRequestContext);

        verify(containerRequestContext).abortWith(argThat(response -> response.getStatus() == 403));
    }
}
{'fraction_non_alphanumeric': 0.11380069160641308, 'fraction_numerical': 0.00817353033637221, 'mean_word_length': 4.271748135874068, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7594567', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1690, 'n_words': 258}