1   package com.atlassian.seraph.service.rememberme;
2   
3   import javax.servlet.ServletContext;
4   import javax.servlet.http.HttpServletRequest;
5   import javax.servlet.http.HttpSession;
6   
7   import com.atlassian.seraph.config.SecurityConfig;
8   
9   import junit.framework.TestCase;
10  
11  import static org.mockito.Mockito.mock;
12  import static org.mockito.Mockito.when;
13  
14  /**
15   */
16  public class TestDefaultRememberMeConfiguration extends TestCase
17  {
18      public static final int TWO_WEEKS = 2 * 7 * 24 * 60 * 60;
19      private static final String COOKIE_NAME = "atlassian.seraph.remember.me";
20  
21      private SecurityConfig securityConfig;
22      private HttpServletRequest servletRequest;
23      private HttpSession httpSession;
24      private ServletContext servletContext;
25      private DefaultRememberMeConfiguration configuration;
26  
27      @Override
28      protected void setUp() throws Exception
29      {
30          securityConfig = mock(SecurityConfig.class);
31          servletRequest = mock(HttpServletRequest.class);
32          httpSession = mock(HttpSession.class);
33          servletContext = mock(ServletContext.class);
34          configuration = new DefaultRememberMeConfiguration(securityConfig);
35  
36          when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/6.0.0");
37          when(httpSession.getServletContext()).thenReturn(servletContext);
38          when(servletRequest.getSession()).thenReturn(httpSession);
39      }
40  
41      public void testDefaults()
42      {
43          when(securityConfig.getLoginCookieKey()).thenReturn(COOKIE_NAME);
44          when(securityConfig.getLoginCookiePath()).thenReturn(null);
45          when(securityConfig.isInsecureCookie()).thenReturn(false);
46  
47          assertEquals(COOKIE_NAME, configuration.getCookieName());
48          assertEquals(false, configuration.isInsecureCookieAlwaysUsed());
49          assertEquals(null, configuration.getCookieDomain(null));
50          assertEquals(TWO_WEEKS, configuration.getCookieMaxAgeInSeconds());
51          assertEquals(false, configuration.isCookieHttpOnly(servletRequest));
52      }
53  
54      public void testSetPaths_NotSpecified_NoContext()
55      {
56          when(securityConfig.getLoginCookiePath()).thenReturn(null);
57          when(servletRequest.getContextPath()).thenReturn("");
58  
59          assertEquals("/", configuration.getCookiePath(servletRequest));
60      }
61  
62      public void testSetPaths_NotSpecified_RootContext()
63      {
64          when(securityConfig.getLoginCookiePath()).thenReturn(null);
65          when(servletRequest.getContextPath()).thenReturn("/");
66  
67          assertEquals("/", configuration.getCookiePath(servletRequest));
68      }
69  
70      public void testSetPaths_NotSpecified_WithContext()
71      {
72          when(securityConfig.getLoginCookiePath()).thenReturn(null);
73          when(servletRequest.getContextPath()).thenReturn("/context");
74          assertEquals("/context", configuration.getCookiePath(servletRequest));
75      }
76  
77      public void testSetPaths_Specified()
78      {
79          when(securityConfig.getLoginCookiePath()).thenReturn("/specified");
80          assertEquals("/specified", configuration.getCookiePath(servletRequest));
81      }
82  
83      public void testIsCookieHttpOnlyForNonTomcatServer()
84      {
85          when(servletContext.getServerInfo()).thenReturn("Some Random Server/6.0.20");
86          assertEquals(false, configuration.isCookieHttpOnly(servletRequest));
87      }
88  
89      public void testIsCookieHttpOnlyForTomcat6019PlusServer()
90      {
91          when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/6.0.20");
92          assertEquals(true, configuration.isCookieHttpOnly(servletRequest));
93      }
94  
95      public void testIsCookieHttpOnlyForTomcat6019Server()
96      {
97          when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/6.0.19");
98          assertEquals(true, configuration.isCookieHttpOnly(servletRequest));
99      }
100 
101     public void testIsCookieHttpOnlyForTomcat6019MinusServer()
102     {
103         when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/6.0.18");
104         assertEquals(false, configuration.isCookieHttpOnly(servletRequest));
105     }
106 
107     public void testIsCookieHttpOnlyForTomcat5528PlusServer()
108     {
109         when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/5.5.29");
110         assertEquals(true, configuration.isCookieHttpOnly(servletRequest));
111     }
112 
113     public void testIsCookieHttpOnlyForTomcat5528Server()
114     {
115         when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/5.5.28");
116         assertEquals(true, configuration.isCookieHttpOnly(servletRequest));
117     }
118 
119     public void testIsCookieHttpOnlyForTomcat5528MinusServer()
120     {
121         when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/5.5.27");
122         assertEquals(false, configuration.isCookieHttpOnly(servletRequest));
123     }
124 
125     public void testIsCookieHttpOnlyForTomcat4Server()
126     {
127         when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/4.0.0");
128         assertEquals(false, configuration.isCookieHttpOnly(servletRequest));
129     }
130 
131     /**
132      * When we start supporting Tomcat 7 we will have to check
133      * which minor version first supports httpOnly.
134      */
135     public void testIsCookieHttpOnlyForTomcat7Server()
136     {
137         when(servletContext.getServerInfo()).thenReturn("Apache Tomcat/7.0.0");
138         assertEquals(false, configuration.isCookieHttpOnly(servletRequest));
139     }
140 }