Search in sources :

Example 36 with MockHttpServletResponse

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

the class ManageRegisteredServicesMultiActionControllerTests method verifyCustomComponents.

@Test
public void verifyCustomComponents() throws Exception {
    // override the RegisteredServiceMapper
    this.registeredServiceFactory = new DefaultRegisteredServiceFactory(new DefaultAccessStrategyMapper(), policyMapper, new DefaultProxyPolicyMapper(), new CustomRegisteredServiceMapper(), new DefaultUsernameAttributeProviderMapper(), Collections.emptyList());
    this.controller = new ManageRegisteredServicesMultiActionController(this.servicesManager, this.registeredServiceFactory, new WebApplicationServiceFactory(), "https://cas.example.org");
    final RegexRegisteredService r = new RegexRegisteredService();
    r.setId(1200);
    r.setName(NAME);
    r.setDescription(UNIQUE_DESCRIPTION);
    r.setServiceId("test");
    r.setEvaluationOrder(2);
    this.servicesManager.save(r);
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final ModelAndView mv = this.controller.manage(response);
    assertTrue(mv.getModel().containsKey("defaultServiceUrl"));
    assertTrue(mv.getModel().containsKey("status"));
    this.controller.getServices(response);
    final String content = response.getContentAsString();
    assertTrue(content.contains(SERVICES));
    assertTrue(content.contains(UNIQUE_DESCRIPTION));
    assertTrue(content.contains("customComponent1"));
    assertTrue(content.contains("key2"));
}
Also used : WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory) ModelAndView(org.springframework.web.servlet.ModelAndView) DefaultAccessStrategyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultAccessStrategyMapper) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) DefaultRegisteredServiceFactory(org.apereo.cas.mgmt.services.web.factory.DefaultRegisteredServiceFactory) DefaultProxyPolicyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultProxyPolicyMapper) DefaultUsernameAttributeProviderMapper(org.apereo.cas.mgmt.services.web.factory.DefaultUsernameAttributeProviderMapper) ManageRegisteredServicesMultiActionController(org.apereo.cas.mgmt.services.web.ManageRegisteredServicesMultiActionController) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 37 with MockHttpServletResponse

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

the class OAuth20AccessTokenControllerTests method internalVerifyRefreshTokenOk.

private void internalVerifyRefreshTokenOk(final RegisteredService service, final boolean json) throws Exception {
    final Principal principal = createPrincipal();
    final RefreshToken refreshToken = addRefreshToken(principal, service);
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuth20GrantTypes.REFRESH_TOKEN.name().toLowerCase());
    mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
    mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    mockRequest.setParameter(OAuthConstants.REFRESH_TOKEN, refreshToken.getId());
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    requiresAuthenticationInterceptor.preHandle(mockRequest, mockResponse, null);
    oAuth20AccessTokenController.handleRequestInternal(mockRequest, mockResponse);
    //This assert fails because deep down inside Oauth2 access token ctrl the refresh token gets deleted
    //assertNotNull(oAuth20AccessTokenController.getRegistry().getTicket((refreshToken.getId())));
    assertEquals(200, mockResponse.getStatus());
    final String body = mockResponse.getContentAsString();
    final String accessTokenId;
    if (json) {
        assertEquals("application/json", mockResponse.getContentType());
        assertTrue(body.contains('"' + OAuthConstants.ACCESS_TOKEN + "\":\"AT-"));
        assertFalse(body.contains('"' + OAuthConstants.REFRESH_TOKEN + "\":\"RT-"));
        assertTrue(body.contains('"' + OAuthConstants.EXPIRES_IN + "\":7"));
        accessTokenId = StringUtils.substringBetween(body, OAuthConstants.ACCESS_TOKEN + "\":\"", "\",\"");
    } else {
        assertEquals("text/plain", mockResponse.getContentType());
        assertTrue(body.contains(OAuthConstants.ACCESS_TOKEN + '='));
        assertFalse(body.contains(OAuthConstants.REFRESH_TOKEN + '='));
        assertTrue(body.contains(OAuthConstants.EXPIRES_IN + '='));
        accessTokenId = StringUtils.substringBetween(body, OAuthConstants.ACCESS_TOKEN + '=', "&");
    }
    final AccessToken accessToken = oAuth20AccessTokenController.getTicketRegistry().getTicket(accessTokenId, AccessToken.class);
    assertEquals(principal, accessToken.getAuthentication().getPrincipal());
    final int timeLeft = getTimeLeft(body, false, json);
    assertTrue(timeLeft >= TIMEOUT - 10 - DELTA);
}
Also used : RefreshToken(org.apereo.cas.ticket.refreshtoken.RefreshToken) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AccessToken(org.apereo.cas.ticket.accesstoken.AccessToken) Principal(org.apereo.cas.authentication.principal.Principal) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 38 with MockHttpServletResponse

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

the class OAuth20AccessTokenControllerTests method internalVerifyClientOK.

private void internalVerifyClientOK(final RegisteredService service, final boolean basicAuth, final boolean refreshToken, final boolean json) throws Exception {
    final Principal principal = createPrincipal();
    final OAuthCode code = addCode(principal, service);
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest(GET, CONTEXT + OAuthConstants.ACCESS_TOKEN_URL);
    mockRequest.setParameter(OAuthConstants.REDIRECT_URI, REDIRECT_URI);
    mockRequest.setParameter(OAuthConstants.GRANT_TYPE, OAuth20GrantTypes.AUTHORIZATION_CODE.name().toLowerCase());
    if (basicAuth) {
        final String auth = CLIENT_ID + ':' + CLIENT_SECRET;
        final String value = Base64.encodeBase64String(auth.getBytes(StandardCharsets.UTF_8));
        mockRequest.addHeader(HttpConstants.AUTHORIZATION_HEADER, HttpConstants.BASIC_HEADER_PREFIX + value);
    } else {
        mockRequest.setParameter(OAuthConstants.CLIENT_ID, CLIENT_ID);
        mockRequest.setParameter(OAuthConstants.CLIENT_SECRET, CLIENT_SECRET);
    }
    mockRequest.setParameter(OAuthConstants.CODE, code.getId());
    final MockHttpServletResponse mockResponse = new MockHttpServletResponse();
    requiresAuthenticationInterceptor.preHandle(mockRequest, mockResponse, null);
    oAuth20AccessTokenController.handleRequestInternal(mockRequest, mockResponse);
    assertNull(oAuth20AccessTokenController.getTicketRegistry().getTicket(code.getId()));
    assertEquals(HttpStatus.SC_OK, mockResponse.getStatus());
    final String body = mockResponse.getContentAsString();
    final String accessTokenId;
    if (json) {
        assertEquals(MediaType.APPLICATION_JSON_VALUE, mockResponse.getContentType());
        assertTrue(body.contains('"' + OAuthConstants.ACCESS_TOKEN + "\":\"AT-"));
        if (refreshToken) {
            assertTrue(body.contains('"' + OAuthConstants.REFRESH_TOKEN + "\":\"RT-"));
        }
        assertTrue(body.contains('"' + OAuthConstants.EXPIRES_IN + "\":7"));
        accessTokenId = StringUtils.substringBetween(body, OAuthConstants.ACCESS_TOKEN + "\":\"", "\",\"");
    } else {
        assertEquals(MediaType.TEXT_PLAIN_VALUE, mockResponse.getContentType());
        assertTrue(body.contains(OAuthConstants.ACCESS_TOKEN + "=AT-"));
        if (refreshToken) {
            assertTrue(body.contains(OAuthConstants.REFRESH_TOKEN + "=RT-"));
        }
        assertTrue(body.contains(OAuthConstants.EXPIRES_IN + '='));
        accessTokenId = StringUtils.substringBetween(body, OAuthConstants.ACCESS_TOKEN + '=', "&");
    }
    final AccessToken accessToken = oAuth20AccessTokenController.getTicketRegistry().getTicket(accessTokenId, AccessToken.class);
    assertEquals(principal, accessToken.getAuthentication().getPrincipal());
    final int timeLeft = getTimeLeft(body, refreshToken, json);
    assertTrue(timeLeft >= TIMEOUT - 10 - DELTA);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) AccessToken(org.apereo.cas.ticket.accesstoken.AccessToken) OAuthCode(org.apereo.cas.ticket.code.OAuthCode) Principal(org.apereo.cas.authentication.principal.Principal) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 39 with MockHttpServletResponse

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

the class CasWebflowContextConfigurationTests method getMockRequestContext.

private RequestContext getMockRequestContext() {
    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockServletContext sCtx = new MockServletContext();
    ctx.setExternalContext(new ServletExternalContext(sCtx, request, response));
    return ctx;
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.springframework.mock.web.MockServletContext)

Example 40 with MockHttpServletResponse

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

the class RegisteredServiceSimpleFormControllerTests method verifyEditMockRegisteredService.

@Test
public void verifyEditMockRegisteredService() throws Exception {
    this.registeredServiceFactory = new DefaultRegisteredServiceFactory(new DefaultAccessStrategyMapper(), policyMapper, new DefaultProxyPolicyMapper(), new MockRegisteredServiceMapper(), new DefaultUsernameAttributeProviderMapper(), Collections.singletonList(new AttributeFormDataPopulator(this.repository)));
    this.controller = new RegisteredServiceSimpleFormController(this.manager, this.registeredServiceFactory);
    final MockRegisteredService r = new MockRegisteredService();
    r.setId(1000);
    r.setName("Test Service");
    r.setServiceId(TEST_ID);
    r.setDescription(DESCRIPTION);
    this.manager.save(r);
    r.setServiceId("serviceId1");
    final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(r);
    this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
    assertFalse(this.manager.getAllServices().isEmpty());
    final RegisteredService r2 = this.manager.findServiceBy(1000);
    assertEquals("serviceId1", r2.getServiceId());
    assertTrue(r2 instanceof MockRegisteredService);
}
Also used : BindingResult(org.springframework.validation.BindingResult) RegexRegisteredService(org.apereo.cas.services.RegexRegisteredService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) RegisteredService(org.apereo.cas.services.RegisteredService) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) DefaultAccessStrategyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultAccessStrategyMapper) RegisteredServiceEditBean(org.apereo.cas.mgmt.services.web.beans.RegisteredServiceEditBean) DefaultProxyPolicyMapper(org.apereo.cas.mgmt.services.web.factory.DefaultProxyPolicyMapper) RegisteredServiceSimpleFormController(org.apereo.cas.mgmt.services.web.RegisteredServiceSimpleFormController) DefaultRegisteredServiceFactory(org.apereo.cas.mgmt.services.web.factory.DefaultRegisteredServiceFactory) DefaultUsernameAttributeProviderMapper(org.apereo.cas.mgmt.services.web.factory.DefaultUsernameAttributeProviderMapper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) AttributeFormDataPopulator(org.apereo.cas.mgmt.services.web.factory.AttributeFormDataPopulator) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2376 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2002 Test (org.junit.jupiter.api.Test)1416 lombok.val (lombok.val)946 Test (org.junit.Test)582 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)240 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)185 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)107 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)97 HashMap (java.util.HashMap)84 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)83 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)74 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)67 MockHttpSession (org.springframework.mock.web.MockHttpSession)67