use of org.craftercms.commons.http.RequestContext in project profile by craftercms.
the class AddSecurityCookiesProcessorTest method testAddCookiesLoggedIn.
@Test
public void testAddCookiesLoggedIn() throws Exception {
String ticket = new ObjectId().toString();
Date lastModified = new Date();
Profile profile = new Profile();
profile.setLastModified(lastModified);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
RequestContext context = new RequestContext(request, response, null);
RequestSecurityProcessor flushResponseProcessor = new RequestSecurityProcessor() {
@Override
public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain) throws Exception {
context.getResponse().getOutputStream().flush();
}
};
RequestSecurityProcessorChain chain = new RequestSecurityProcessorChainImpl(Arrays.asList(processor, flushResponseProcessor).iterator());
Authentication auth = new DefaultAuthentication(ticket, profile);
SecurityUtils.setAuthentication(request, auth);
processor.processRequest(context, chain);
Cookie ticketCookie = response.getCookie(SecurityUtils.TICKET_COOKIE_NAME);
assertNotNull(ticketCookie);
assertEquals(ticket, ticketCookie.getValue());
Cookie profileLastModifiedCookie = response.getCookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME);
assertNotNull(profileLastModifiedCookie);
assertEquals(profile.getLastModified().getTime(), Long.parseLong(profileLastModifiedCookie.getValue()));
}
use of org.craftercms.commons.http.RequestContext in project profile by craftercms.
the class LoginProcessorTest method testLoginSuccess.
@Test
public void testLoginSuccess() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(LoginProcessor.DEFAULT_LOGIN_METHOD, LoginProcessor.DEFAULT_LOGIN_URL);
MockHttpServletResponse response = new MockHttpServletResponse();
HttpSession session = request.getSession(true);
RequestContext context = new RequestContext(request, response, null);
RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
request.setParameter(LoginProcessor.DEFAULT_USERNAME_PARAM, USERNAME);
request.setParameter(LoginProcessor.DEFAULT_PASSWORD_PARAM, VALID_PASSWORD);
session.setAttribute(SecurityUtils.BAD_CREDENTIALS_EXCEPTION_SESSION_ATTRIBUTE, new BadCredentialsException());
session.setAttribute(SecurityUtils.AUTHENTICATION_EXCEPTION_SESSION_ATTRIBUTE, new AuthenticationSystemException());
processor.processRequest(context, chain);
verify(chain, never()).processRequest(context);
/**
* Removed Session are invalidated after login is ok.
* assertNull(session.getAttribute(SecurityUtils.BAD_CREDENTIALS_EXCEPTION_SESSION_ATTRIBUTE));
* assertNull(session.getAttribute(SecurityUtils.AUTHENTICATION_EXCEPTION_SESSION_ATTRIBUTE));
*/
Authentication auth = SecurityUtils.getAuthentication(request);
assertNotNull(auth);
assertEquals(TICKET, auth.getTicket());
assertNotNull(auth.getProfile());
assertEquals(USERNAME, auth.getProfile().getUsername());
verify(authenticationManager).authenticateUser(TENANTS, USERNAME, VALID_PASSWORD);
verify(rememberMeManager).disableRememberMe(context);
verify(loginSuccessHandler).handle(context, auth);
request.setParameter(LoginProcessor.DEFAULT_REMEMBER_ME_PARAM, "true");
processor.processRequest(context, chain);
auth = SecurityUtils.getAuthentication(request);
assertNotNull(auth);
verify(rememberMeManager).enableRememberMe(auth, context);
}
use of org.craftercms.commons.http.RequestContext in project profile by craftercms.
the class MellonAutoLoginProcessorTest method testProcess.
@Test
public void testProcess() throws Exception {
RequestContext context = getRequestContext();
RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
processor.processRequest(context, chain);
Authentication auth = SecurityUtils.getAuthentication(context.getRequest());
assertNotNull(auth);
assertEquals(TICKET, auth.getTicket());
assertEquals(PROFILE_ID, auth.getProfile().getId());
assertEquals(USERNAME, auth.getProfile().getUsername());
assertEquals(EMAIL, auth.getProfile().getEmail());
assertTrue(auth.getProfile().isEnabled());
assertEquals(TENANT_NAME, auth.getProfile().getTenant());
assertEquals(getAttributes(), auth.getProfile().getAttributes());
}
use of org.craftercms.commons.http.RequestContext in project profile by craftercms.
the class MellonAutoLoginProcessorTest method getRequestContext.
private RequestContext getRequestContext() {
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.addHeader(MellonAutoLoginProcessor.DEFAULT_USERNAME_HEADER_NAME, USERNAME);
request.addHeader(MellonAutoLoginProcessor.DEFAULT_EMAIL_HEADER_NAME, EMAIL);
request.addHeader(MellonAutoLoginProcessor.DEFAULT_MELLON_HEADER_PREFIX + FIRST_NAME_ATTRIB_NAME, FIRST_NAME);
request.addHeader(MellonAutoLoginProcessor.DEFAULT_MELLON_HEADER_PREFIX + LAST_NAME_ATTRIB_NAME, LAST_NAME);
return new RequestContext(request, response, null);
}
use of org.craftercms.commons.http.RequestContext in project profile by craftercms.
the class SecurityExceptionProcessorTest method testAccessDeniedNoAuthentication.
@Test
public void testAccessDeniedNoAuthentication() 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);
processor.processRequest(context, chain);
verify(chain).processRequest(context);
verify(authenticationRequiredHandler).handle(eq(context), any(AuthenticationRequiredException.class));
}
Aggregations