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