@Test public void testGetCredentialsValid() { ComponentContext context = configureForCookie(); HttpServletRequest request = createMock(HttpServletRequest.class); Cookie[] cookies = new Cookie[] { new Cookie("sdfsd", "fsdfs"), new Cookie("sdfsd1", "fsdfs"), null, new Cookie("sdfsd3", "fsdfs"), new Cookie("sdfsd4", "fsdfs"), }; EasyMock.expect(request.getCookies()).andReturn(cookies); HttpServletResponse response = createMock(HttpServletResponse.class); replay(); trustedTokenService.activate(context); Cookie secureCookie = new Cookie("secure-cookie", trustedTokenService.encodeCookie("ieb")); cookies[2] = secureCookie; Credentials ieb = trustedTokenService.getCredentials(request, response); Assert.assertTrue(ieb instanceof SimpleCredentials); SimpleCredentials sc = (SimpleCredentials) ieb; TrustedUser tu = (TrustedUser) sc.getAttribute(TrustedTokenService.CA_AUTHENTICATION_USER); Assert.assertNotNull(tu); Assert.assertEquals("ieb", tu.getUser()); verify(); }
@Test public void testGetCredentialsNone() { ComponentContext context = configureForCookie(); HttpServletRequest request = createMock(HttpServletRequest.class); Cookie[] cookies = new Cookie[] { new Cookie("sdfsd", "fsdfs"), new Cookie("sdfsd1", "fsdfs"), new Cookie("sdfsd2", "fsdfs"), new Cookie("sdfsd3", "fsdfs"), new Cookie("sdfsd4", "fsdfs"), }; EasyMock.expect(request.getCookies()).andReturn(cookies); HttpServletResponse response = createMock(HttpServletResponse.class); replay(); trustedTokenService.activate(context); Credentials none = trustedTokenService.getCredentials(request, response); Assert.assertNull(none); verify(); }
@Test public void testGetCredentialsValidSession() { ComponentContext context = configureForSession(); HttpServletRequest request = createMock(HttpServletRequest.class); HttpSession session = createMock(HttpSession.class); EasyMock.expect(request.getSession(true)).andReturn(session); Principal principal = createMock(Principal.class); EasyMock.expect(request.getUserPrincipal()).andReturn(principal); EasyMock.expect(principal.getName()).andReturn(null); EasyMock.expect(request.getRemoteUser()).andReturn("ieb"); Capture<SimpleCredentials> attributeValue = new Capture<SimpleCredentials>(); Capture<String> attributeName = new Capture<String>(); session.setAttribute(EasyMock.capture(attributeName), EasyMock.capture(attributeValue)); HttpServletResponse response = createMock(HttpServletResponse.class); replay(); trustedTokenService.activate(context); trustedTokenService.injectToken(request, response); Assert.assertTrue(attributeName.hasCaptured()); Assert.assertTrue(attributeValue.hasCaptured()); Credentials credentials = attributeValue.getValue(); verify(); reset(); EasyMock.expect(request.getSession(false)).andReturn(session); EasyMock.expect(session.getAttribute(TrustedTokenService.SA_AUTHENTICATION_CREDENTIALS)) .andReturn(credentials); replay(); Credentials ieb = trustedTokenService.getCredentials(request, response); Assert.assertTrue(ieb instanceof SimpleCredentials); SimpleCredentials sc = (SimpleCredentials) ieb; TrustedUser tu = (TrustedUser) sc.getAttribute(TrustedTokenService.CA_AUTHENTICATION_USER); Assert.assertNotNull(tu); Assert.assertEquals("ieb", tu.getUser()); verify(); }