Search in sources :

Example 26 with FilterChain

use of javax.servlet.FilterChain in project OpenAM by OpenRock.

the class AMSetupFilterTest method filterShouldRedirectRequestsToSetupPageIfNotConfigured.

@Test
public void filterShouldRedirectRequestsToSetupPageIfNotConfigured() throws Exception {
    //Given
    initializeFilter();
    HttpServletRequest request = mockRequest("REQUEST_URI");
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mock(FilterChain.class);
    systemIsNotConfigured();
    withWritePermissionsOnBootstrapRootDirectory();
    //When
    setupFilter.doFilter(request, response, chain);
    //Then
    verify(response).sendRedirect("SCHEME://SERVER_NAME:8080/CONTEXT_PATH/config/options.htm");
    verifyZeroInteractions(chain);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.testng.annotations.Test)

Example 27 with FilterChain

use of javax.servlet.FilterChain in project nhin-d by DirectProject.

the class BasicAuthFilter_doFilterTest method testDoFilter_sessionsNotAllowed.

@Test
public void testDoFilter_sessionsNotAllowed() throws Exception {
    final HttpSession session = mock(HttpSession.class);
    final BasicAuthFilter filter = buildFilter();
    filter.setAllowSessions(false);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final FilterChain chain = mock(FilterChain.class);
    when(request.getHeader("Authorization")).thenReturn(buildRawCredential("gm2552", "password"));
    when(request.getSession(true)).thenReturn(session);
    filter.doFilter(request, response, chain);
    verify(chain, times(1)).doFilter((HttpServletRequest) any(), eq(response));
    verify(session, never()).setAttribute(eq("NHINDAuthPrincipalAttr"), (Principal) any());
    verify(response, never()).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 28 with FilterChain

use of javax.servlet.FilterChain in project nhin-d by DirectProject.

the class BasicAuthFilter_doFilterTest method testDoFilter_nonBasicAuthScheme_assertUnauthorized.

@Test
public void testDoFilter_nonBasicAuthScheme_assertUnauthorized() throws Exception {
    final HttpSession session = mock(HttpSession.class);
    final BasicAuthFilter filter = buildFilter();
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final FilterChain chain = mock(FilterChain.class);
    when(request.getHeader("Authorization")).thenReturn("OAuth 39843");
    when(request.getSession(true)).thenReturn(session);
    filter.doFilter(request, response, chain);
    verify(chain, never()).doFilter((HttpServletRequest) any(), eq(response));
    verify(request, times(1)).getHeader(eq("Authorization"));
    verify(session, never()).setAttribute(eq("NHINDAuthPrincipalAttr"), (Principal) any());
    verify(response, times(1)).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 29 with FilterChain

use of javax.servlet.FilterChain in project nhin-d by DirectProject.

the class BasicAuthFilter_doFilterTest method testDoFilter_nonSSLConnectionDisallowed_assertForbidden.

@Test
public void testDoFilter_nonSSLConnectionDisallowed_assertForbidden() throws Exception {
    final HttpSession session = mock(HttpSession.class);
    final BasicAuthFilter filter = buildFilter();
    filter.setForceSSL(true);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, response, chain);
    verify(chain, never()).doFilter((HttpServletRequest) any(), eq(response));
    verify(session, never()).setAttribute(eq("NHINDAuthPrincipalAttr"), (Principal) any());
    verify(response, times(1)).sendError(eq(HttpServletResponse.SC_FORBIDDEN));
    verify(request, never()).getHeader(eq("Authorization"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 30 with FilterChain

use of javax.servlet.FilterChain in project nhin-d by DirectProject.

the class BasicAuthFilter_doFilterTest method testDoFilter_exsitingPrincipal.

@Test
public void testDoFilter_exsitingPrincipal() throws Exception {
    final HttpSession session = mock(HttpSession.class);
    final BasicAuthFilter filter = buildFilter();
    final HttpServletRequest request = mock(HttpServletRequest.class);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    final FilterChain chain = mock(FilterChain.class);
    when(request.getUserPrincipal()).thenReturn(new NHINDPrincipal("gm2552", "admin"));
    filter.doFilter(request, response, chain);
    verify(chain, times(1)).doFilter((HttpServletRequest) any(), eq(response));
    verify(request, never()).getSession(eq(true));
    verify(session, never()).setAttribute(eq("NHINDAuthPrincipalAttr"), (Principal) any());
    verify(response, never()).sendError(eq(HttpServletResponse.SC_UNAUTHORIZED));
    verify(request, never()).getHeader(eq("Authorization"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) NHINDPrincipal(org.nhindirect.common.rest.auth.NHINDPrincipal) Test(org.junit.Test)

Aggregations

FilterChain (javax.servlet.FilterChain)418 HttpServletRequest (javax.servlet.http.HttpServletRequest)317 HttpServletResponse (javax.servlet.http.HttpServletResponse)269 Test (org.junit.Test)246 ServletResponse (javax.servlet.ServletResponse)135 ServletRequest (javax.servlet.ServletRequest)118 FilterConfig (javax.servlet.FilterConfig)80 Filter (javax.servlet.Filter)68 ServletException (javax.servlet.ServletException)54 IOException (java.io.IOException)48 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)46 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 Injector (com.google.inject.Injector)32 ServletTestUtils.newFakeHttpServletRequest (com.google.inject.servlet.ServletTestUtils.newFakeHttpServletRequest)25 ServletContext (javax.servlet.ServletContext)25 Test (org.testng.annotations.Test)25 HttpSession (javax.servlet.http.HttpSession)24 MockFilterChain (org.springframework.mock.web.MockFilterChain)24 InvocationOnMock (org.mockito.invocation.InvocationOnMock)22 Properties (java.util.Properties)19