Search in sources :

Example 1 with MockFilterChain

use of com.thoughtworks.go.http.mocks.MockFilterChain in project gocd by gocd.

the class ThreadLocalUserFilterTest method shouldSetUserToThreadLocalWhenFilterIsCalledAndRemoveUserFromThreadLocalOnceRequestIsCompleted.

@Test
void shouldSetUserToThreadLocalWhenFilterIsCalledAndRemoveUserFromThreadLocalOnceRequestIsCompleted() throws ServletException, IOException {
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final AuthenticationToken<UsernamePassword> authenticationToken = SessionUtilsHelper.createUsernamePasswordAuthentication("bob", "p@ssw0rd", 0L);
    SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
    final GoUserPrinciple[] currentUserInFilter = { null };
    final FilterChain filterChain = new MockFilterChain(mock(Servlet.class), spy(new OncePerRequestFilter() {

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
            currentUserInFilter[0] = SessionUtils.getCurrentUser();
        }
    }));
    new ThreadLocalUserFilter().doFilter(request, response, filterChain);
    assertThat(currentUserInFilter[0]).isNotNull();
    assertThat(SessionUtils.getCurrentUser().getUsername()).isEqualTo("anonymous");
    assertThat(SessionUtils.getCurrentUser().getAuthorities()).containsExactly(GoAuthority.ROLE_ANONYMOUS.asAuthority());
}
Also used : MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(com.thoughtworks.go.http.mocks.MockFilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) UsernamePassword(com.thoughtworks.go.server.newsecurity.models.UsernamePassword) MockHttpServletRequest(com.thoughtworks.go.http.mocks.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) OncePerRequestFilter(org.springframework.web.filter.OncePerRequestFilter) Servlet(javax.servlet.Servlet) GoUserPrinciple(com.thoughtworks.go.server.security.userdetail.GoUserPrinciple) MockFilterChain(com.thoughtworks.go.http.mocks.MockFilterChain) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockFilterChain (com.thoughtworks.go.http.mocks.MockFilterChain)1 MockHttpServletRequest (com.thoughtworks.go.http.mocks.MockHttpServletRequest)1 MockHttpServletResponse (com.thoughtworks.go.http.mocks.MockHttpServletResponse)1 UsernamePassword (com.thoughtworks.go.server.newsecurity.models.UsernamePassword)1 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)1 FilterChain (javax.servlet.FilterChain)1 Servlet (javax.servlet.Servlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.junit.jupiter.api.Test)1 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)1