Search in sources :

Example 41 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class SecurityExceptionProcessorTest method testAccessDeniedWithAuthentication.

@Test
public void testAccessDeniedWithAuthentication() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    doThrow(AccessDeniedException.class).when(chain).processRequest(context);
    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), new Profile()));
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
    verify(accessDeniedHandler).handle(eq(context), any(AccessDeniedException.class));
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) AccessDeniedException(org.craftercms.security.exception.AccessDeniedException) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) ObjectId(org.bson.types.ObjectId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 42 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class SecurityExceptionProcessorTest method testNonSecurityException.

@Test(expected = Exception.class)
public void testNonSecurityException() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    doThrow(Exception.class).when(chain).processRequest(context);
    processor.processRequest(context, chain);
}
Also used : RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 43 with RequestContext

use of org.craftercms.commons.http.RequestContext in project profile by craftercms.

the class RequestSecurityFilterTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    doAnswer(new Answer() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            RequestContext context = (RequestContext) invocation.getArguments()[0];
            RequestSecurityProcessorChain chain = (RequestSecurityProcessorChain) invocation.getArguments()[1];
            chain.processRequest(context);
            return null;
        }
    }).when(processor).processRequest(any(RequestContext.class), any(RequestSecurityProcessorChain.class));
    filter = new RequestSecurityFilter();
    filter.setSecurityEnabled(true);
    filter.setSecurityProcessors(Arrays.asList(processor));
    filter.setUrlsToInclude("/static-assets/paywall/**");
    filter.setUrlsToExclude("/static-assets/**");
}
Also used : Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RequestContext(org.craftercms.commons.http.RequestContext) Before(org.junit.Before)

Example 44 with RequestContext

use of org.craftercms.commons.http.RequestContext in project engine by craftercms.

the class ExecuteControllerDirective method createScriptVariables.

protected Map<String, Object> createScriptVariables(Environment env) throws TemplateException {
    Map<String, Object> variables = new HashMap<String, Object>();
    RequestContext context = RequestContext.getCurrent();
    SiteItem contentModel = getContentModel(env);
    Object templateModel = getTemplateModel(env);
    if (context != null) {
        GroovyScriptUtils.addSiteItemScriptVariables(variables, context.getRequest(), context.getResponse(), servletContext, contentModel, templateModel);
    } else {
        throw new IllegalStateException("No current request context found");
    }
    return variables;
}
Also used : HashMap(java.util.HashMap) RequestContext(org.craftercms.commons.http.RequestContext) SiteItem(org.craftercms.engine.model.SiteItem)

Example 45 with RequestContext

use of org.craftercms.commons.http.RequestContext in project engine by craftercms.

the class RenderComponentDirective method createScriptVariables.

protected Map<String, Object> createScriptVariables(SiteItem component, Map<String, Object> templateModel, Map<String, Object> additionalModel) {
    Map<String, Object> variables = new HashMap<>();
    RequestContext context = RequestContext.getCurrent();
    if (context != null) {
        GroovyScriptUtils.addSiteItemScriptVariables(variables, context.getRequest(), context.getResponse(), servletContext, component, templateModel);
        if (MapUtils.isNotEmpty(additionalModel)) {
            variables.putAll(additionalModel);
        }
    } else {
        throw new IllegalStateException("No current request context found");
    }
    return variables;
}
Also used : HashMap(java.util.HashMap) RequestContext(org.craftercms.commons.http.RequestContext)

Aggregations

RequestContext (org.craftercms.commons.http.RequestContext)47 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)40 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)40 Test (org.junit.Test)37 RequestSecurityProcessorChain (org.craftercms.security.processors.RequestSecurityProcessorChain)17 Authentication (org.craftercms.security.authentication.Authentication)12 DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)11 Cookie (javax.servlet.http.Cookie)9 Profile (org.craftercms.profile.api.Profile)9 ObjectId (org.bson.types.ObjectId)7 AuthenticationRequiredException (org.craftercms.security.exception.AuthenticationRequiredException)5 AccessDeniedException (org.craftercms.security.exception.AccessDeniedException)4 Date (java.util.Date)3 BadCredentialsException (org.craftercms.security.exception.BadCredentialsException)3 RequestSecurityProcessor (org.craftercms.security.processors.RequestSecurityProcessor)3 HashMap (java.util.HashMap)2 HttpSession (javax.servlet.http.HttpSession)2 AuthenticationException (org.craftercms.security.exception.AuthenticationException)2 ExecutionInput (graphql.ExecutionInput)1 ExecutionInput.newExecutionInput (graphql.ExecutionInput.newExecutionInput)1