use of org.springframework.mock.web.MockHttpServletRequest 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.MockHttpServletRequest 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.MockHttpServletRequest 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.MockHttpServletRequest 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);
}
use of org.springframework.mock.web.MockHttpServletRequest in project cas by apereo.
the class RegisteredServiceSimpleFormControllerTests method verifyAddRegexRegisteredService.
@Test
public void verifyAddRegexRegisteredService() throws Exception {
final RegexRegisteredService svc = new RegexRegisteredService();
svc.setDescription(DESCRIPTION);
svc.setServiceId("^serviceId");
svc.setName(NAME);
svc.setId(1000);
svc.setEvaluationOrder(1000);
final RegisteredServiceEditBean.ServiceData data = registeredServiceFactory.createServiceData(svc);
this.controller.saveService(new MockHttpServletRequest(), new MockHttpServletResponse(), data, mock(BindingResult.class));
final Collection<RegisteredService> services = this.manager.getAllServices();
assertEquals(1, services.size());
this.manager.getAllServices().forEach(rs -> assertTrue(rs instanceof RegexRegisteredService));
}
Aggregations