|
23 | 23 | import java.net.InetAddress; |
24 | 24 | import java.security.cert.X509Certificate; |
25 | 25 |
|
| 26 | +import javax.servlet.http.Cookie; |
26 | 27 | import javax.servlet.http.HttpServletRequest; |
27 | 28 | import javax.servlet.http.HttpServletResponse; |
28 | 29 | import javax.servlet.http.HttpSession; |
|
35 | 36 | import org.junit.Assert; |
36 | 37 | import org.junit.Test; |
37 | 38 | import org.junit.runner.RunWith; |
| 39 | +import org.mockito.ArgumentCaptor; |
38 | 40 | import org.mockito.Mock; |
39 | 41 | import org.mockito.Mockito; |
40 | 42 | import org.mockito.junit.MockitoJUnitRunner; |
@@ -81,6 +83,30 @@ public void testAuthenticate() throws Exception { |
81 | 83 | Mockito.verify(session, Mockito.atLeastOnce()).getAttribute(Mockito.anyString()); |
82 | 84 | } |
83 | 85 |
|
| 86 | + @Test |
| 87 | + public void testAuthenticateClearsSessionCookiesBeforeRedirect() throws Exception { |
| 88 | + SAML2LogoutAPIAuthenticatorCmd cmd = new SAML2LogoutAPIAuthenticatorCmd(); |
| 89 | + |
| 90 | + Field apiServerField = SAML2LogoutAPIAuthenticatorCmd.class.getDeclaredField("_apiServer"); |
| 91 | + apiServerField.setAccessible(true); |
| 92 | + apiServerField.set(cmd, apiServer); |
| 93 | + |
| 94 | + Field managerField = SAML2LogoutAPIAuthenticatorCmd.class.getDeclaredField("_samlAuthManager"); |
| 95 | + managerField.setAccessible(true); |
| 96 | + managerField.set(cmd, samlAuthManager); |
| 97 | + |
| 98 | + Mockito.when(session.getAttribute(Mockito.anyString())).thenReturn(null); |
| 99 | + Cookie sessionKeyCookie = new Cookie("sessionkey", "someKey"); |
| 100 | + Mockito.when(req.getCookies()).thenReturn(new Cookie[]{sessionKeyCookie}); |
| 101 | + |
| 102 | + cmd.authenticate("command", null, session, InetAddress.getByName("127.0.0.1"), HttpUtils.RESPONSE_TYPE_JSON, new StringBuilder(), req, resp); |
| 103 | + |
| 104 | + ArgumentCaptor<Cookie> cookieCaptor = ArgumentCaptor.forClass(Cookie.class); |
| 105 | + Mockito.verify(resp, Mockito.times(1)).addCookie(cookieCaptor.capture()); |
| 106 | + Assert.assertEquals(0, cookieCaptor.getValue().getMaxAge()); |
| 107 | + Assert.assertEquals("", cookieCaptor.getValue().getValue()); |
| 108 | + } |
| 109 | + |
84 | 110 | @Test |
85 | 111 | public void testGetAPIType() throws Exception { |
86 | 112 | Assert.assertTrue(new SAML2LogoutAPIAuthenticatorCmd().getAPIType() == APIAuthenticationType.LOGOUT_API); |
|
0 commit comments