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());
}
Aggregations