Search in sources :

Example 81 with MockHttpServletRequest

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

the class OpenID4JavaConsumerTests method successfulVerificationReturnsExpectedAuthentication.

@SuppressWarnings("serial")
@Test
public void successfulVerificationReturnsExpectedAuthentication() throws Exception {
    ConsumerManager mgr = mock(ConsumerManager.class);
    OpenID4JavaConsumer consumer = new OpenID4JavaConsumer(mgr, new NullAxFetchListFactory());
    VerificationResult vr = mock(VerificationResult.class);
    DiscoveryInformation di = mock(DiscoveryInformation.class);
    Identifier id = new Identifier() {

        public String getIdentifier() {
            return "id";
        }
    };
    Message msg = mock(Message.class);
    when(mgr.verify(anyString(), any(ParameterList.class), any(DiscoveryInformation.class))).thenReturn(vr);
    when(vr.getVerifiedId()).thenReturn(id);
    when(vr.getAuthResponse()).thenReturn(msg);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(DiscoveryInformation.class.getName(), di);
    request.getSession().setAttribute("SPRING_SECURITY_OPEN_ID_ATTRIBUTES_FETCH_LIST", attributes);
    OpenIDAuthenticationToken auth = consumer.endConsumption(request);
    assertThat(auth.getStatus()).isEqualTo(OpenIDAuthenticationStatus.SUCCESS);
}
Also used : Identifier(org.openid4java.discovery.Identifier) ConsumerManager(org.openid4java.consumer.ConsumerManager) VerificationResult(org.openid4java.consumer.VerificationResult) AxMessage(org.openid4java.message.ax.AxMessage) Message(org.openid4java.message.Message) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DiscoveryInformation(org.openid4java.discovery.DiscoveryInformation) ParameterList(org.openid4java.message.ParameterList)

Example 82 with MockHttpServletRequest

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

the class OpenIDAuthenticationFilterTests method encodesUrlParameters.

/**
	 * Tests that the filter encodes any query parameters on the return_to URL.
	 */
@Test
public void encodesUrlParameters() throws Exception {
    // Arbitrary parameter name and value that will both need to be encoded:
    String paramName = "foo&bar";
    String paramValue = "http://example.com/path?a=b&c=d";
    MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_PATH);
    req.addParameter(paramName, paramValue);
    filter.setReturnToUrlParameters(Collections.singleton(paramName));
    URI returnTo = new URI(filter.buildReturnToUrl(req));
    String query = returnTo.getRawQuery();
    assertThat(count(query, '=')).isEqualTo(1);
    assertThat(count(query, '&')).isEqualTo(0);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) URI(java.net.URI) Test(org.junit.Test)

Example 83 with MockHttpServletRequest

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

the class OpenIDAuthenticationFilterTests method testFilterOperation.

@Test
public void testFilterOperation() throws Exception {
    MockHttpServletRequest req = new MockHttpServletRequest();
    req.setServletPath(REQUEST_PATH);
    req.setRequestURI(REQUEST_PATH);
    req.setServerPort(8080);
    MockHttpServletResponse response = new MockHttpServletResponse();
    req.setParameter("openid_identifier", " " + CLAIMED_IDENTITY_URL);
    req.setRemoteHost("www.example.com");
    filter.setConsumer(new MockOpenIDConsumer() {

        public String beginConsumption(HttpServletRequest req, String claimedIdentity, String returnToUrl, String realm) throws OpenIDConsumerException {
            assertThat(claimedIdentity).isEqualTo(CLAIMED_IDENTITY_URL);
            assertThat(returnToUrl).isEqualTo(DEFAULT_TARGET_URL);
            assertThat(realm).isEqualTo("http://localhost:8080/");
            return REDIRECT_URL;
        }
    });
    FilterChain fc = mock(FilterChain.class);
    filter.doFilter(req, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo(REDIRECT_URL);
    // Filter chain shouldn't proceed
    verify(fc, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 84 with MockHttpServletRequest

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

the class ClientCredentialsTokenEndpointFilterTests method testFailedAuthentication.

@Test(expected = BadCredentialsException.class)
public void testFailedAuthentication() throws Exception {
    filter.setAuthenticationManager(authenticationManager);
    filter.afterPropertiesSet();
    filter.attemptAuthentication(new MockHttpServletRequest(), new MockHttpServletResponse());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 85 with MockHttpServletRequest

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

the class HttpSessionOAuthRememberMeServicesTests method testNoTokensRemembered.

@Test
public void testNoTokensRemembered() {
    MockHttpSession mockHttpSession = new MockHttpSession();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setSession(mockHttpSession);
    HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
    Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
    oAuthRememberMeService.rememberTokens(tokens, request, response);
    Assert.assertEquals(0, oAuthRememberMeService.loadRememberedTokens(request, response).size());
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) OAuthConsumerToken(org.springframework.security.oauth.consumer.OAuthConsumerToken) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)3144 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)1989 Test (org.junit.jupiter.api.Test)1907 lombok.val (lombok.val)1124 Test (org.junit.Test)752 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)482 MockServletContext (org.springframework.mock.web.MockServletContext)471 MockRequestContext (org.springframework.webflow.test.MockRequestContext)468 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)245 MockFilterChain (org.springframework.mock.web.MockFilterChain)238 JEEContext (org.pac4j.core.context.JEEContext)156 Authentication (org.springframework.security.core.Authentication)144 BeforeEach (org.junit.jupiter.api.BeforeEach)132 HashMap (java.util.HashMap)123 FilterChain (jakarta.servlet.FilterChain)117 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)108 MockHttpSession (org.springframework.mock.web.MockHttpSession)98 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)96 Before (org.junit.Before)82 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)78