Search in sources :

Example 26 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class TicketGrantingTicketCheckActionTests method verifyInvalidTicket.

@Test
public void verifyInvalidTicket() throws Exception {
    final MockRequestContext ctx = new MockRequestContext();
    final MockTicketGrantingTicket tgt = new MockTicketGrantingTicket("user");
    WebUtils.putTicketGrantingTicketInScopes(ctx, tgt);
    final TicketGrantingTicketCheckAction action = new TicketGrantingTicketCheckAction(this.getCentralAuthenticationService());
    final Event event = action.doExecute(ctx);
    assertEquals(TicketGrantingTicketCheckAction.INVALID, event.getId());
}
Also used : MockTicketGrantingTicket(org.apereo.cas.mock.MockTicketGrantingTicket) Event(org.springframework.webflow.execution.Event) MockRequestContext(org.springframework.webflow.test.MockRequestContext) TicketGrantingTicketCheckAction(org.apereo.cas.web.flow.login.TicketGrantingTicketCheckAction) Test(org.junit.Test)

Example 27 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class DelegatedClientAuthenticationActionTests method verifyFinishAuthentication.

@Test
public void verifyFinishAuthentication() throws Exception {
    final MockHttpServletRequest mockRequest = new MockHttpServletRequest();
    mockRequest.setParameter(Pac4jConstants.DEFAULT_CLIENT_NAME_PARAMETER, "FacebookClient");
    mockRequest.addParameter(ThemeChangeInterceptor.DEFAULT_PARAM_NAME, MY_THEME);
    mockRequest.addParameter(LocaleChangeInterceptor.DEFAULT_PARAM_NAME, MY_LOCALE);
    mockRequest.addParameter(CasProtocolConstants.PARAMETER_METHOD, MY_METHOD);
    final Service service = CoreAuthenticationTestUtils.getService(MY_SERVICE);
    mockRequest.addParameter(CasProtocolConstants.PARAMETER_SERVICE, service.getId());
    final ServletExternalContext servletExternalContext = mock(ServletExternalContext.class);
    when(servletExternalContext.getNativeRequest()).thenReturn(mockRequest);
    when(servletExternalContext.getNativeResponse()).thenReturn(new MockHttpServletResponse());
    final MockRequestContext mockRequestContext = new MockRequestContext();
    mockRequestContext.setExternalContext(servletExternalContext);
    final FacebookClient facebookClient = new FacebookClient() {

        @Override
        protected OAuth20Credentials retrieveCredentials(final WebContext context) {
            return new OAuth20Credentials("fakeVerifier");
        }
    };
    facebookClient.setName(FacebookClient.class.getSimpleName());
    final Clients clients = new Clients(MY_LOGIN_URL, facebookClient);
    final TicketGrantingTicket tgt = new TicketGrantingTicketImpl(TGT_ID, mock(Authentication.class), mock(ExpirationPolicy.class));
    final CentralAuthenticationService casImpl = mock(CentralAuthenticationService.class);
    when(casImpl.createTicketGrantingTicket(any())).thenReturn(tgt);
    final AuthenticationTransactionManager transManager = mock(AuthenticationTransactionManager.class);
    final AuthenticationManager authNManager = mock(AuthenticationManager.class);
    when(authNManager.authenticate(any(AuthenticationTransaction.class))).thenReturn(CoreAuthenticationTestUtils.getAuthentication());
    when(transManager.getAuthenticationManager()).thenReturn(authNManager);
    when(transManager.handle(any(AuthenticationTransaction.class), any(AuthenticationResultBuilder.class))).thenReturn(transManager);
    final AuthenticationSystemSupport support = mock(AuthenticationSystemSupport.class);
    when(support.getAuthenticationTransactionManager()).thenReturn(transManager);
    final AuditableExecution enforcer = mock(AuditableExecution.class);
    when(enforcer.execute(any())).thenReturn(new AuditableExecutionResult());
    final DefaultTicketRegistry ticketRegistry = new DefaultTicketRegistry();
    final DelegatedClientWebflowManager manager = new DelegatedClientWebflowManager(ticketRegistry, new DefaultTransientSessionTicketFactory(new HardTimeoutExpirationPolicy(60)), ThemeChangeInterceptor.DEFAULT_PARAM_NAME, LocaleChangeInterceptor.DEFAULT_PARAM_NAME, new WebApplicationServiceFactory(), "https://cas.example.org", new DefaultAuthenticationServiceSelectionPlan(new DefaultAuthenticationServiceSelectionStrategy()));
    final Ticket ticket = manager.store(Pac4jUtils.getPac4jJ2EContext(mockRequest, new MockHttpServletResponse()), facebookClient);
    mockRequest.addParameter(DelegatedClientWebflowManager.PARAMETER_CLIENT_ID, ticket.getId());
    final DelegatedClientAuthenticationAction action = new DelegatedClientAuthenticationAction(clients, support, casImpl, getServicesManagerWith(service, facebookClient), enforcer, manager, new DelegatedSessionCookieManager(mock(CookieRetrievingCookieGenerator.class)));
    final Event event = action.execute(mockRequestContext);
    assertEquals("success", event.getId());
    assertEquals(MY_THEME, mockRequest.getAttribute(ThemeChangeInterceptor.DEFAULT_PARAM_NAME));
    assertEquals(MY_LOCALE, mockRequest.getAttribute(LocaleChangeInterceptor.DEFAULT_PARAM_NAME));
    assertEquals(MY_METHOD, mockRequest.getAttribute(CasProtocolConstants.PARAMETER_METHOD));
    assertEquals(MY_SERVICE, mockRequest.getAttribute(CasProtocolConstants.PARAMETER_SERVICE));
    final MutableAttributeMap flowScope = mockRequestContext.getFlowScope();
    final MutableAttributeMap requestScope = mockRequestContext.getRequestScope();
    assertEquals(service.getId(), ((Service) flowScope.get(CasProtocolConstants.PARAMETER_SERVICE)).getId());
    assertEquals(TGT_ID, flowScope.get(WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID));
    assertEquals(TGT_ID, requestScope.get(WebUtils.PARAMETER_TICKET_GRANTING_TICKET_ID));
}
Also used : WebContext(org.pac4j.core.context.WebContext) FacebookClient(org.pac4j.oauth.client.FacebookClient) DefaultAuthenticationServiceSelectionPlan(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionPlan) AuthenticationResultBuilder(org.apereo.cas.authentication.AuthenticationResultBuilder) DefaultTransientSessionTicketFactory(org.apereo.cas.ticket.factory.DefaultTransientSessionTicketFactory) HardTimeoutExpirationPolicy(org.apereo.cas.ticket.support.HardTimeoutExpirationPolicy) ExpirationPolicy(org.apereo.cas.ticket.ExpirationPolicy) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) DefaultTicketRegistry(org.apereo.cas.ticket.registry.DefaultTicketRegistry) WebApplicationServiceFactory(org.apereo.cas.authentication.principal.WebApplicationServiceFactory) MutableAttributeMap(org.springframework.webflow.core.collection.MutableAttributeMap) TicketGrantingTicketImpl(org.apereo.cas.ticket.TicketGrantingTicketImpl) AuthenticationTransaction(org.apereo.cas.authentication.AuthenticationTransaction) AuditableExecutionResult(org.apereo.cas.audit.AuditableExecutionResult) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) DefaultAuthenticationServiceSelectionStrategy(org.apereo.cas.authentication.DefaultAuthenticationServiceSelectionStrategy) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) Ticket(org.apereo.cas.ticket.Ticket) AuthenticationSystemSupport(org.apereo.cas.authentication.AuthenticationSystemSupport) AuthenticationTransactionManager(org.apereo.cas.authentication.AuthenticationTransactionManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) TicketGrantingTicket(org.apereo.cas.ticket.TicketGrantingTicket) HardTimeoutExpirationPolicy(org.apereo.cas.ticket.support.HardTimeoutExpirationPolicy) CentralAuthenticationService(org.apereo.cas.CentralAuthenticationService) AbstractRegisteredService(org.apereo.cas.services.AbstractRegisteredService) Service(org.apereo.cas.authentication.principal.Service) MockRequestContext(org.springframework.webflow.test.MockRequestContext) Clients(org.pac4j.core.client.Clients) AuditableExecution(org.apereo.cas.audit.AuditableExecution) DelegatedClientWebflowManager(org.apereo.cas.web.DelegatedClientWebflowManager) AuthenticationManager(org.apereo.cas.authentication.AuthenticationManager) DelegatedSessionCookieManager(org.apereo.cas.web.pac4j.DelegatedSessionCookieManager) ServletExternalContext(org.springframework.webflow.context.servlet.ServletExternalContext) Authentication(org.apereo.cas.authentication.Authentication) OAuth20Credentials(org.pac4j.oauth.credentials.OAuth20Credentials) Event(org.springframework.webflow.execution.Event) Test(org.junit.Test)

Example 28 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class SamlMetadataUIParserActionTests method verifyEntityIdUIInfoExists.

@Test
public void verifyEntityIdUIInfoExists() throws Exception {
    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(SamlProtocolConstants.PARAMETER_ENTITY_ID, "https://carmenwiki.osu.edu/shibboleth");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockServletContext sCtx = new MockServletContext();
    ctx.setExternalContext(new ServletExternalContext(sCtx, request, response));
    ctx.getFlowScope().put(CasProtocolConstants.PARAMETER_SERVICE, RegisteredServiceTestUtils.getService());
    samlMetadataUIParserAction.execute(ctx);
    assertNotNull(WebUtils.getServiceUserInterfaceMetadata(ctx, SamlMetadataUIInfo.class));
}
Also used : SamlMetadataUIInfo(org.apereo.cas.support.saml.mdui.SamlMetadataUIInfo) 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.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 29 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class SamlMetadataUIParserDynamicActionTests method verifyEntityIdUIInfoExistsDynamically.

@Test
public void verifyEntityIdUIInfoExistsDynamically() throws Exception {
    final MockRequestContext ctx = new MockRequestContext();
    final MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter(SamlProtocolConstants.PARAMETER_ENTITY_ID, "https://carmenwiki.osu.edu/shibboleth");
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final MockServletContext sCtx = new MockServletContext();
    ctx.setExternalContext(new ServletExternalContext(sCtx, request, response));
    samlMetadataUIParserAction.execute(ctx);
    assertNotNull(WebUtils.getServiceUserInterfaceMetadata(ctx, SamlMetadataUIInfo.class));
}
Also used : SamlMetadataUIInfo(org.apereo.cas.support.saml.mdui.SamlMetadataUIInfo) 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.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 30 with MockRequestContext

use of org.springframework.webflow.test.MockRequestContext in project cas by apereo.

the class InterruptSingleSignOnParticipationStrategyTests method verifyStrategyWithInterruptDisabled.

@Test
public void verifyStrategyWithInterruptDisabled() {
    final InterruptSingleSignOnParticipationStrategy s = new InterruptSingleSignOnParticipationStrategy(mock(ServicesManager.class), true, true);
    final MockRequestContext ctx = new MockRequestContext();
    final InterruptResponse response = new InterruptResponse();
    response.setSsoEnabled(false);
    InterruptUtils.putInterruptIn(ctx, response);
    assertFalse(s.isParticipating(ctx));
}
Also used : ServicesManager(org.apereo.cas.services.ServicesManager) MockRequestContext(org.springframework.webflow.test.MockRequestContext) InterruptResponse(org.apereo.cas.interrupt.InterruptResponse) Test(org.junit.Test)

Aggregations

MockRequestContext (org.springframework.webflow.test.MockRequestContext)73 Test (org.junit.Test)68 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)53 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)53 ServletExternalContext (org.springframework.webflow.context.servlet.ServletExternalContext)50 MockServletContext (org.springframework.mock.web.MockServletContext)46 Event (org.springframework.webflow.execution.Event)34 EventFactorySupport (org.springframework.webflow.action.EventFactorySupport)14 TicketGrantingTicket (org.apereo.cas.ticket.TicketGrantingTicket)10 DeliveryOptions (org.opennms.reporting.core.DeliveryOptions)8 Credential (org.apereo.cas.authentication.Credential)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)7 AuthenticationResult (org.apereo.cas.authentication.AuthenticationResult)5 Service (org.apereo.cas.authentication.principal.Service)5 CentralAuthenticationService (org.apereo.cas.CentralAuthenticationService)4 Clients (org.pac4j.core.client.Clients)4 FacebookClient (org.pac4j.oauth.client.FacebookClient)4 MutableAttributeMap (org.springframework.webflow.core.collection.MutableAttributeMap)4 Authentication (org.apereo.cas.authentication.Authentication)3 SamlMetadataUIInfo (org.apereo.cas.support.saml.mdui.SamlMetadataUIInfo)3