Search in sources :

Example 11 with RequestContext

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

the class AuthenticationRequiredHandlerImplTest method testRedirectToLoginFormUrl.

@Test
public void testRedirectToLoginFormUrl() throws Exception {
    handler.setLoginFormUrl(LOGIN_FORM_URL);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    handler.handle(context, new AuthenticationRequiredException(""));
    verify(requestCache).saveRequest(request, response);
    assertEquals(LOGIN_FORM_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) AuthenticationRequiredException(org.craftercms.security.exception.AuthenticationRequiredException) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 12 with RequestContext

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

the class LoginSuccessHandleImplTest method testRedirectToSavedRequest.

@Test
public void testRedirectToSavedRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    SavedRequest savedRequest = mock(SavedRequest.class);
    when(savedRequest.getRedirectUrl()).thenReturn(SAVED_REQUEST_URL);
    when(requestCache.getRequest(request, response)).thenReturn(savedRequest);
    handler.handle(context, mock(Authentication.class));
    assertEquals(SAVED_REQUEST_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) SavedRequest(org.springframework.security.web.savedrequest.SavedRequest) Test(org.junit.Test)

Example 13 with RequestContext

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

the class RememberMeManagerImplTest method testAutoLoginWithInvalidId.

@Test
public void testAutoLoginWithInvalidId() 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, getSerializedLoginWithInvalidId()));
    Authentication auth = rememberMeManager.autoLogin(context);
    assertNull(auth);
    assertNull(response.getCookie(REMEMBER_ME_COOKIE_NAME).getValue());
}
Also used : Cookie(javax.servlet.http.Cookie) 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 14 with RequestContext

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

the class RememberMeManagerImplTest method testEnableRememberMe.

@Test
public void testEnableRememberMe() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    rememberMeManager.enableRememberMe(getAuthentication(), context);
    String cookieValue = response.getCookie(REMEMBER_ME_COOKIE_NAME).getValue();
    assertEquals(getSerializedLogin(), cookieValue);
}
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 15 with RequestContext

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

the class RememberMeManagerImplTest method testAutoLoginWithInvalidToken.

@Test(expected = CookieTheftException.class)
public void testAutoLoginWithInvalidToken() 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, getSerializedLoginWithInvalidToken()));
    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)

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