use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedAuthenticationClientFinishLogoutActionTests method verifyOperationWithRelay.
@Test
public void verifyOperationWithRelay() throws Exception {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
request.addParameter(SamlProtocolConstants.PARAMETER_SAML_RELAY_STATE, "SAML2Client");
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
val samlClient = (SAML2Client) builtClients.findClient("SAML2Client").get();
val handler = mock(SAML2ProfileHandler.class);
when(handler.receive(any())).thenThrow(new IllegalArgumentException());
samlClient.setLogoutProfileHandler(handler);
val result = delegatedAuthenticationClientFinishLogoutAction.execute(context);
assertNull(result);
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedAuthenticationClientFinishLogoutActionTests method verifyOperationNoLogoutRedirectUrl.
@Test
public void verifyOperationNoLogoutRedirectUrl() throws Exception {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
WebUtils.putDelegatedAuthenticationClientName(context, "SAML2Client");
val samlClient = (SAML2Client) builtClients.findClient("SAML2Client").get();
samlClient.getLogoutValidator().setPostLogoutURL("https://google.com");
val result = delegatedAuthenticationClientFinishLogoutAction.execute(context);
assertNull(result);
assertEquals("https://google.com", samlClient.getLogoutValidator().getPostLogoutURL());
assertNull(WebUtils.getLogoutRedirectUrl(context, String.class));
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifySaml2LogoutResponse.
@Test
public void verifySaml2LogoutResponse() throws Exception {
val client = builtClients.findClient("SAML2Client").get();
val request = new MockHttpServletRequest();
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, client.getName());
val webContext = new JEEContext(request, new MockHttpServletResponse());
request.setMethod("POST");
val logoutResponse = getLogoutResponse();
request.setContent(EncodingUtils.encodeBase64(logoutResponse).getBytes(StandardCharsets.UTF_8));
val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
request.addParameter(Pac4jConstants.LOGOUT_ENDPOINT_PARAMETER, "https://httpbin.org/post");
val context = new MockRequestContext();
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val event = delegatedAuthenticationAction.execute(context);
assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, event.getId());
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifySsoAuthenticationWithInvalidTicketFails.
@Test
public void verifySsoAuthenticationWithInvalidTicketFails() throws Exception {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
request.addParameter("error_message", "Auth+failed");
val response = new MockHttpServletResponse();
val client = builtClients.findClient("FacebookClient").get();
val webContext = new JEEContext(request, new MockHttpServletResponse());
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
val service = CoreAuthenticationTestUtils.getService("https://delegated2.example.org");
servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val tgt = new MockTicketGrantingTicket("casuser");
centralAuthenticationService.addTicket(tgt);
WebUtils.putTicketGrantingTicketInScopes(context, new MockTicketGrantingTicket("otheruser"));
assertEquals(CasWebflowConstants.TRANSITION_ID_STOP, delegatedAuthenticationAction.execute(context).getId());
}
use of org.apereo.cas.util.MockServletContext in project cas by apereo.
the class DelegatedClientAuthenticationActionTests method verifyFinishAuthentication.
@Test
public void verifyFinishAuthentication() throws Exception {
val request = new MockHttpServletRequest();
request.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
request.addParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, "theme");
request.addParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, Locale.getDefault().getCountry());
request.addParameter(CasProtocolConstants.PARAMETER_METHOD, HttpMethod.POST.name());
val service = RegisteredServiceTestUtils.getService(UUID.randomUUID().toString());
servicesManager.save(RegisteredServiceTestUtils.getRegisteredService(service.getId(), Map.of()));
request.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
val client = builtClients.findClient("FacebookClient").get();
val webContext = new JEEContext(request, new MockHttpServletResponse());
val ticket = delegatedClientAuthenticationWebflowManager.store(webContext, client);
request.addParameter(DefaultDelegatedClientAuthenticationWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
val context = new MockRequestContext();
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
setRequestContext(context);
setExternalContext(context.getExternalContext());
val event = delegatedAuthenticationAction.execute(context);
assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, event.getId());
assertEquals("theme", request.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(Locale.getDefault().getCountry(), request.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
assertEquals(HttpMethod.POST.name(), request.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
assertEquals(service.getId(), request.getAttribute(CasProtocolConstants.PARAMETER_SERVICE));
val flowScope = context.getFlowScope();
assertEquals(service.getId(), ((Service) flowScope.get(CasProtocolConstants.PARAMETER_SERVICE)).getId());
val credential = flowScope.get(CasWebflowConstants.VAR_ID_CREDENTIAL, ClientCredential.class);
assertNotNull(credential);
assertEquals(credential.getId(), "casuser");
}
Aggregations