Search in sources :

Example 41 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class CasConfigurationServerServletInitializerTests method verifyInitializer.

@Test
public void verifyInitializer() {
    val servletContext = new MockServletContext();
    val servletInitializer = new CasConfigurationServerServletInitializer();
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() throws Throwable {
            servletInitializer.onStartup(servletContext);
        }
    });
}
Also used : lombok.val(lombok.val) Executable(org.junit.jupiter.api.function.Executable) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 42 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class CasWebApplicationServletInitializerTests method verifyInitializr.

@Test
public void verifyInitializr() {
    val servletInitializer = new CasWebApplicationServletInitializer();
    val context = new MockServletContext();
    assertDoesNotThrow(new Executable() {

        @Override
        public void execute() throws Exception {
            servletInitializer.onStartup(context);
        }
    });
}
Also used : lombok.val(lombok.val) Executable(org.junit.jupiter.api.function.Executable) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 43 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class U2FStartAuthenticationActionTests method verifyOperation.

@Test
public void verifyOperation() throws Exception {
    val id = UUID.randomUUID().toString();
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    WebUtils.putAuthentication(CoreAuthenticationTestUtils.getAuthentication(id), context);
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, u2fMultifactorAuthenticationProvider);
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    val cert = CertUtils.readCertificate(new ClassPathResource("cert.crt"));
    val reg1 = new DeviceRegistration("keyhandle11", "publickey1", cert, 1);
    val record = U2FDeviceRegistration.builder().record(deviceRepository.getCipherExecutor().encode(reg1.toJsonWithAttestationCert())).username(id).build();
    deviceRepository.registerDevice(record);
    assertEquals(CasWebflowConstants.TRANSITION_ID_SUCCESS, u2fStartAuthenticationAction.execute(context).getId());
    assertNotNull(context.getFlowScope().get("u2fAuth"));
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) U2FDeviceRegistration(org.apereo.cas.adaptors.u2f.storage.U2FDeviceRegistration) DeviceRegistration(com.yubico.u2f.data.DeviceRegistration) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 44 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class DuoSecurityDetermineUserAccountActionTests method verifyOperation.

@SneakyThrows
private static void verifyOperation(final DuoSecurityUserAccountStatus status, final String eventId) {
    val context = new MockRequestContext();
    val request = new MockHttpServletRequest();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    WebUtils.putServiceIntoFlowScope(context, CoreAuthenticationTestUtils.getWebApplicationService());
    val authentication = CoreAuthenticationTestUtils.getAuthentication();
    WebUtils.putAuthentication(authentication, context);
    val account = new DuoSecurityUserAccount(authentication.getPrincipal().getId());
    account.setStatus(status);
    account.setEnrollPortalUrl("https://example.org");
    val duoService = mock(DuoSecurityAuthenticationService.class);
    when(duoService.getUserAccount(anyString())).thenReturn(account);
    val provider = mock(DuoSecurityMultifactorAuthenticationProvider.class);
    when(provider.getId()).thenReturn(DuoSecurityMultifactorAuthenticationProperties.DEFAULT_IDENTIFIER);
    when(provider.getDuoAuthenticationService()).thenReturn(duoService);
    when(provider.getRegistrationUrl()).thenReturn("https://registration.duo.com");
    when(provider.matches(anyString())).thenReturn(Boolean.TRUE);
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    WebUtils.putMultifactorAuthenticationProviderIdIntoFlowScope(context, provider);
    TestMultifactorAuthenticationProvider.registerProviderIntoApplicationContext(applicationContext, provider);
    val action = new DuoSecurityDetermineUserAccountAction();
    val event = action.execute(context);
    assertEquals(eventId, event.getId());
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) DuoSecurityUserAccount(org.apereo.cas.adaptors.duo.DuoSecurityUserAccount) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) SneakyThrows(lombok.SneakyThrows)

Example 45 with MockServletContext

use of org.apereo.cas.util.MockServletContext in project cas by apereo.

the class OneTimeTokenAccountCheckRegistrationActionTests method verifyCreateAccount.

@Test
public void verifyCreateAccount() {
    val applicationContext = new StaticApplicationContext();
    applicationContext.refresh();
    ApplicationContextProvider.holdApplicationContext(applicationContext);
    ApplicationContextProvider.registerBeanIntoApplicationContext(applicationContext, MultifactorAuthenticationPrincipalResolver.identical(), UUID.randomUUID().toString());
    val account = OneTimeTokenAccount.builder().username("casuser").secretKey(UUID.randomUUID().toString()).validationCode(123456).scratchCodes(List.of()).name(UUID.randomUUID().toString()).build();
    val repository = mock(OneTimeTokenCredentialRepository.class);
    when(repository.create(anyString())).thenReturn(account);
    val action = new OneTimeTokenAccountCheckRegistrationAction(repository);
    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.putAuthentication(RegisteredServiceTestUtils.getAuthentication("casuser"), context);
    assertEquals(CasWebflowConstants.TRANSITION_ID_REGISTER, action.doExecute(context).getId());
}
Also used : lombok.val(lombok.val) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) 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.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test)

Aggregations

lombok.val (lombok.val)50 MockServletContext (org.apereo.cas.util.MockServletContext)50 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)46 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)46 MockRequestContext (org.springframework.webflow.test.MockRequestContext)46 Test (org.junit.jupiter.api.Test)45 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)44 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)30 JEEContext (org.pac4j.core.context.JEEContext)10 MockTicketGrantingTicket (org.apereo.cas.mock.MockTicketGrantingTicket)7 Executable (org.junit.jupiter.api.function.Executable)5 DefaultRegisteredServiceAccessStrategy (org.apereo.cas.services.DefaultRegisteredServiceAccessStrategy)4 SAML2Client (org.pac4j.saml.client.SAML2Client)4 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)4 Flow (org.springframework.webflow.engine.Flow)3 Cookie (javax.servlet.http.Cookie)2 SneakyThrows (lombok.SneakyThrows)2 AuthenticationServiceSelectionPlan (org.apereo.cas.authentication.AuthenticationServiceSelectionPlan)2 UsernamePasswordCredential (org.apereo.cas.authentication.credential.UsernamePasswordCredential)2 CasConfigurationProperties (org.apereo.cas.configuration.CasConfigurationProperties)2