1   package com.atlassian.security.auth.trustedapps.seraph.filter;
2   
3   import com.atlassian.security.auth.trustedapps.filter.AuthenticationController;
4   import com.atlassian.seraph.auth.RoleMapper;
5   import com.atlassian.seraph.filter.BaseLoginFilter;
6   import com.mockobjects.dynamic.C;
7   import com.mockobjects.dynamic.Mock;
8   import com.mockobjects.servlet.MockHttpServletRequest;
9   import junit.framework.TestCase;
10  
11  import java.security.Principal;
12  
13  /**
14   * Tests for {@link SeraphAuthenticationController}
15   */
16  public class TestSeraphAuthenticationController extends TestCase
17  {
18      private AuthenticationController authenticationController;
19  
20      private Mock mockRoleMapper;
21  
22      protected void setUp() throws Exception
23      {
24          mockRoleMapper = new Mock(RoleMapper.class);
25          authenticationController = new SeraphAuthenticationController((RoleMapper) mockRoleMapper.proxy());
26      }
27  
28      protected void tearDown() throws Exception
29      {
30          mockRoleMapper = null;
31          authenticationController = null;
32      }
33  
34      public void testCreateSeraphAuthenticationControllerWithNullRoleMapperThrowsIllegalArgumentException()
35      {
36          try
37          {
38              new SeraphAuthenticationController(null);
39              fail("Should throw exception");
40          }
41          catch (IllegalArgumentException e)
42          {
43              // expected
44          }
45      }
46  
47      public void testShouldAttemptAuthenticationWithSeraphAuthenticationStatusAttributeNotPresent()
48      {
49          final MockHttpServletRequest request = new MockHttpServletRequest();
50          request.addExpectedGetAttributeName(BaseLoginFilter.OS_AUTHSTATUS_KEY);
51          request.setupGetAttribute(null);
52          assertTrue(authenticationController.shouldAttemptAuthentication(request));
53      }
54  
55      public void testShouldAttemptAuthenticationWithSeraphAuthenticationStatusAttributePresent()
56      {
57          final MockHttpServletRequest request = new MockHttpServletRequest();
58          request.addExpectedGetAttributeName(BaseLoginFilter.OS_AUTHSTATUS_KEY);
59          request.setupGetAttribute("some.value");
60          assertFalse(authenticationController.shouldAttemptAuthentication(request));
61      }
62  
63      public void testCanLoginWithRoleMapperDenyingLogin()
64      {
65          canLogin(false);
66      }
67  
68      public void testCanLoginWithRoleMapperAllowingLogin()
69      {
70          canLogin(true);
71      }
72  
73      private void canLogin(boolean roleMapperCanLogin)
74      {
75          final MockPrincipal principal = new MockPrincipal();
76          final MockHttpServletRequest request = new MockHttpServletRequest();
77  
78          mockRoleMapper.matchAndReturn("canLogin", C.eq(principal, request), roleMapperCanLogin);
79          assertEquals(roleMapperCanLogin, authenticationController.canLogin(principal, request));
80      }
81  
82      private static class MockPrincipal implements Principal
83      {
84          public String getName()
85          {
86              return "principal";
87          }
88      }
89  }