Search in sources :

Example 51 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project cas by apereo.

the class LdapSpnegoKnownClientSystemsFilterActionTests method ensureLdapAttributeShouldDoSpnego.

@Test
public void ensureLdapAttributeShouldDoSpnego() throws Exception {
    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest req = new MockHttpServletRequest();
    req.setRemoteAddr("localhost");
    final ServletExternalContext extCtx = new ServletExternalContext(new MockServletContext(), req, new MockHttpServletResponse());
    ctx.setExternalContext(extCtx);
    final Event ev = action.execute(ctx);
    assertEquals(ev.getId(), new EventFactorySupport().yes(this).getId());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Event(org.springframework.webflow.execution.Event) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockServletContext(org.springframework.mock.web.MockServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) EventFactorySupport(org.springframework.webflow.action.EventFactorySupport) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 52 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testAuthenticateProxyUrl.

@Test
public void testAuthenticateProxyUrl() throws Exception {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setServletPath("/pgtCallback");
    filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
    filter.setProxyReceptorUrl(request.getServletPath());
    assertThat(filter.attemptAuthentication(request, response)).isNull();
}
Also used : ProxyGrantingTicketStorage(org.jasig.cas.client.proxy.ProxyGrantingTicketStorage) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 53 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testChainNotInvokedForProxyReceptor.

// SEC-1592
@Test
public void testChainNotInvokedForProxyReceptor() throws Exception {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain chain = mock(FilterChain.class);
    request.setServletPath("/pgtCallback");
    filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
    filter.setProxyReceptorUrl(request.getServletPath());
    filter.doFilter(request, response, chain);
    verifyZeroInteractions(chain);
}
Also used : ProxyGrantingTicketStorage(org.jasig.cas.client.proxy.ProxyGrantingTicketStorage) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 54 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testRequiresAuthenticationProxyRequest.

@Test
public void testRequiresAuthenticationProxyRequest() {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setServletPath("/pgtCallback");
    assertThat(filter.requiresAuthentication(request, response)).isFalse();
    filter.setProxyReceptorUrl(request.getServletPath());
    assertThat(filter.requiresAuthentication(request, response)).isFalse();
    filter.setProxyGrantingTicketStorage(mock(ProxyGrantingTicketStorage.class));
    assertThat(filter.requiresAuthentication(request, response)).isTrue();
    request.setServletPath("/other");
    assertThat(filter.requiresAuthentication(request, response)).isFalse();
}
Also used : ProxyGrantingTicketStorage(org.jasig.cas.client.proxy.ProxyGrantingTicketStorage) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 55 with MockHttpServletResponse

use of org.springframework.mock.web.MockHttpServletResponse in project spring-security by spring-projects.

the class CasAuthenticationFilterTests method testNullServiceTicketHandledGracefully.

@Test(expected = AuthenticationException.class)
public void testNullServiceTicketHandledGracefully() throws Exception {
    CasAuthenticationFilter filter = new CasAuthenticationFilter();
    filter.setAuthenticationManager(new AuthenticationManager() {

        public Authentication authenticate(Authentication a) {
            throw new BadCredentialsException("Rejected");
        }
    });
    filter.attemptAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse());
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2338 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)1986 Test (org.junit.jupiter.api.Test)1412 lombok.val (lombok.val)946 Test (org.junit.Test)558 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)484 MockServletContext (org.springframework.mock.web.MockServletContext)462 MockRequestContext (org.springframework.webflow.test.MockRequestContext)460 MockFilterChain (org.springframework.mock.web.MockFilterChain)239 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)184 JEEContext (org.pac4j.core.context.JEEContext)159 FilterChain (jakarta.servlet.FilterChain)117 Authentication (org.springframework.security.core.Authentication)116 BeforeEach (org.junit.jupiter.api.BeforeEach)106 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)65 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)64