Search in sources :

Example 26 with RequestContext

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

the class RememberMeAutoLoginProcessorTest method testProcessRequestWithPreviousAuthentication.

@Test
public void testProcessRequestWithPreviousAuthentication() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    SecurityUtils.setAuthentication(request, authentication);
    processor.processRequest(context, chain);
    verify(rememberMeManager, never()).autoLogin(context);
    verify(chain).processRequest(context);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 27 with RequestContext

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

the class UrlAccessRestrictionCheckingProcessorTest method testAllowedAccess.

@Test
public void testAllowedAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    Profile profile = new Profile();
    profile.setRoles(SetUtils.asSet(ADMIN_ROLE));
    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), profile));
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) 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 28 with RequestContext

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

the class LoginSuccessHandleImplTest method testRedirectToDefaultTargetUrl.

@Test
public void testRedirectToDefaultTargetUrl() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    handler.handle(context, mock(Authentication.class));
    assertEquals(DEFAULT_TARGET_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.craftercms.security.authentication.Authentication) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 29 with RequestContext

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

the class RememberMeManagerImplTest method testAutoLoginWithInvalidProfile.

@Test(expected = InvalidCookieException.class)
public void testAutoLoginWithInvalidProfile() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    request.setCookies(new Cookie(REMEMBER_ME_COOKIE_NAME, getSerializedLoginWithInvalidProfile()));
    rememberMeManager.autoLogin(context);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 30 with RequestContext

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

the class RestLoginFailureHandlerTest method testHandle.

@Test
public void testHandle() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/login.json");
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    handler.handle(context, new BadCredentialsException(ERROR_MESSAGE));
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, response.getStatus());
    assertEquals(EXPECTED_RESPONSE_CONTENT, response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) BadCredentialsException(org.craftercms.security.exception.BadCredentialsException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

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