Search in sources :

Example 46 with MockServletContext

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

the class OneTimeTokenAccountCheckRegistrationActionTests method verifyExistingAccount.

@Test
public void verifyExistingAccount() {
    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.get(anyString())).thenReturn((Collection) List.of(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_SUCCESS, action.doExecute(context).getId());
}
Also used : lombok.val(lombok.val) 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)

Example 47 with MockServletContext

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

the class OneTimeTokenAccountCreateRegistrationActionTests 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 OneTimeTokenAccountCreateRegistrationAction(repository, "CAS", "CAS");
    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());
    assertTrue(context.getFlowScope().contains(OneTimeTokenAccountCreateRegistrationAction.FLOW_SCOPE_ATTR_ACCOUNT));
    assertTrue(context.getFlowScope().contains(OneTimeTokenAccountCreateRegistrationAction.FLOW_SCOPE_ATTR_QR_IMAGE_BASE64));
}
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)

Example 48 with MockServletContext

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

the class OneTimeTokenAccountSaveRegistrationActionTests method verifyMissingAccount.

@Test
public void verifyMissingAccount() {
    val props = new CasConfigurationProperties();
    val repository = mock(OneTimeTokenCredentialRepository.class);
    val action = new OneTimeTokenAccountSaveRegistrationAction(repository, props);
    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());
    assertEquals(CasWebflowConstants.TRANSITION_ID_ERROR, action.doExecute(context).getId());
}
Also used : lombok.val(lombok.val) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) CasConfigurationProperties(org.apereo.cas.configuration.CasConfigurationProperties) MockRequestContext(org.springframework.webflow.test.MockRequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockServletContext(org.apereo.cas.util.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 49 with MockServletContext

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

the class DefaultLogoutRedirectionStrategyTests method getMockRequestContext.

private static MockRequestContext getMockRequestContext(final MockHttpServletRequest request) {
    val context = new MockRequestContext();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    request.setRequestURI(CasProtocolConstants.ENDPOINT_LOGOUT);
    return context;
}
Also used : lombok.val(lombok.val) 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)

Example 50 with MockServletContext

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

the class DefaultCaptchaActivationStrategyTests method getRequestContext.

private static MockRequestContext getRequestContext(final HttpServletRequest request) {
    val context = new MockRequestContext();
    val response = new MockHttpServletResponse();
    context.setExternalContext(new ServletExternalContext(new MockServletContext(), request, response));
    RequestContextHolder.setRequestContext(context);
    ExternalContextHolder.setExternalContext(context.getExternalContext());
    return context;
}
Also used : lombok.val(lombok.val) 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)

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