use of org.apereo.cas.util.MockWebServer in project cas by apereo.
the class RestfulAuthenticationPolicyTests method assertPolicyFails.
private static void assertPolicyFails(final int port, final HttpStatus status, final Class<? extends Throwable> exceptionClass) {
val applicationContext = new StaticApplicationContext();
applicationContext.refresh();
try (val webServer = new MockWebServer(port, new ByteArrayResource(StringUtils.EMPTY.getBytes(StandardCharsets.UTF_8), "Output"), status)) {
webServer.start();
val props = new RestAuthenticationPolicyProperties();
props.setUrl("http://localhost:" + port);
val policy = new RestfulAuthenticationPolicy(props);
assertThrowsWithRootCause(GeneralSecurityException.class, exceptionClass, () -> policy.isSatisfiedBy(CoreAuthenticationTestUtils.getAuthentication("casuser"), new LinkedHashSet<>(), applicationContext, Optional.empty()));
}
}
use of org.apereo.cas.util.MockWebServer in project cas by apereo.
the class AzureActiveDirectoryAuthenticationHandlerTests method verifyOperationFailsLogin.
@Test
public void verifyOperationFailsLogin() throws Exception {
val handler = getMockAzureActiveDirectoryAuthenticationHandler(7787);
val entity = MAPPER.writeValueAsString(RegisteredServiceTestUtils.getTestAttributes());
try (val webServer = new MockWebServer(7787, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "Output"), HttpStatus.UNAUTHORIZED)) {
webServer.start();
val c = CoreAuthenticationTestUtils.getCredentialsWithDifferentUsernameAndPassword("castest@hotmail.onmicrosoft.com", "1234567890");
assertThrows(FailedLoginException.class, () -> handler.authenticate(c));
}
}
use of org.apereo.cas.util.MockWebServer in project cas by apereo.
the class RestAcceptableUsagePolicyRepositoryTests method verify.
@Test
public void verify() {
val context = new MockRequestContext();
val request = new MockHttpServletRequest();
val response = new MockHttpServletResponse();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
RequestContextHolder.setRequestContext(context);
ExternalContextHolder.setExternalContext(context.getExternalContext());
WebUtils.putServiceIntoFlowScope(context, RegisteredServiceTestUtils.getService());
WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication("casuser"), context);
val ticketRegistrySupport = mock(TicketRegistrySupport.class);
val props = new AcceptableUsagePolicyProperties();
props.getRest().setUrl("http://localhost:9298");
props.getCore().setAupAttributeName("givenName");
val r = new RestAcceptableUsagePolicyRepository(ticketRegistrySupport, props);
val data = StringUtils.EMPTY;
try (val webServer = new MockWebServer(9298, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
webServer.start();
assertFalse(r.isUsagePolicyAcceptedBy(CoreAuthenticationTestUtils.getPrincipal()));
assertTrue(r.submit(context));
}
}
use of org.apereo.cas.util.MockWebServer in project cas by apereo.
the class RestAcceptableUsagePolicyRepositoryTests method verifyFetch.
@Test
public void verifyFetch() throws Exception {
val ticketRegistrySupport = mock(TicketRegistrySupport.class);
val props = new AcceptableUsagePolicyProperties();
props.getRest().setUrl("http://localhost:9198");
props.getCore().setAupAttributeName("givenName");
val r = new RestAcceptableUsagePolicyRepository(ticketRegistrySupport, props);
val input = AcceptableUsagePolicyTerms.builder().code("example").defaultText("hello world").build();
val data = MAPPER.writeValueAsString(input);
try (val webServer = new MockWebServer(9198, new ByteArrayResource(data.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
webServer.start();
val context = new MockRequestContext();
WebUtils.putAuthentication(RegisteredServiceTestUtils.getAuthentication("casuser"), context);
val request = new MockHttpServletRequest();
context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, new MockHttpServletResponse()));
val terms = r.fetchPolicy(context);
assertTrue(terms.isPresent());
assertEquals(terms.get(), input);
}
}
use of org.apereo.cas.util.MockWebServer in project cas by apereo.
the class RestfulDelegatedClientFactoryTests method verifyAction.
@Test
public void verifyAction() throws Exception {
val clients = new HashMap<String, Object>();
clients.put("callbackUrl", "https://sso.example.org/cas/login");
clients.put("properties", getProperties());
val props = new CasConfigurationProperties();
props.getAuthn().getPac4j().getRest().setUrl("http://localhost:9212");
val factory = new RestfulDelegatedClientFactory(props);
var clientsFound = factory.build();
assertTrue(clientsFound.isEmpty());
val entity = MAPPER.writeValueAsString(clients);
try (val webServer = new MockWebServer(9212, new ByteArrayResource(entity.getBytes(StandardCharsets.UTF_8), "REST Output"), MediaType.APPLICATION_JSON_VALUE)) {
webServer.start();
clientsFound = factory.build();
assertNotNull(clientsFound);
assertEquals(3, clientsFound.size());
/*
* Try the cache once the list is retrieved...
*/
clientsFound = factory.build();
assertNotNull(clientsFound);
assertEquals(3, clientsFound.size());
}
}
Aggregations