1   /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  
16  package org.acegisecurity.taglibs.velocity;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.GrantedAuthority;
21  import org.acegisecurity.GrantedAuthorityImpl;
22  
23  import org.acegisecurity.context.SecurityContextHolder;
24  
25  import org.acegisecurity.providers.TestingAuthenticationToken;
26  
27  
28  /**
29   * DOCUMENT ME!
30   */
31  public class AuthzImplAuthorizeTagTest extends TestCase {
32      //~ Instance fields ================================================================================================
33  
34      private Authz authz = new AuthzImpl();
35      private TestingAuthenticationToken currentUser;
36  
37      //~ Methods ========================================================================================================
38  
39      protected void setUp() throws Exception {
40          super.setUp();
41  
42          currentUser = new TestingAuthenticationToken("abc", "123",
43                  new GrantedAuthority[] {
44                      new GrantedAuthorityImpl("ROLE_SUPERVISOR"), new GrantedAuthorityImpl("ROLE_TELLER"),
45                  });
46  
47          SecurityContextHolder.getContext().setAuthentication(currentUser);
48      }
49  
50      protected void tearDown() throws Exception {
51          SecurityContextHolder.clearContext();
52      }
53  
54      public void testAlwaysReturnsUnauthorizedIfNoUserFound() {
55          SecurityContextHolder.getContext().setAuthentication(null);
56  
57          //prevents request - no principal in Context
58          assertFalse(authz.allGranted("ROLE_TELLER"));
59      }
60  
61      public void testDefaultsToNotOutputtingBodyWhenNoRequiredAuthorities() {
62          //prevents body output - no authorities granted
63          assertFalse(authz.allGranted(""));
64          assertFalse(authz.anyGranted(""));
65          assertFalse(authz.noneGranted(""));
66      }
67  
68      public void testOutputsBodyIfOneRolePresent() {
69          //authorized - ROLE_TELLER in both sets
70          assertTrue(authz.anyGranted("ROLE_TELLER"));
71      }
72  
73      public void testOutputsBodyWhenAllGranted() {
74          // allows request - all required roles granted on principal
75          assertTrue(authz.allGranted("ROLE_SUPERVISOR,ROLE_TELLER"));
76      }
77  
78      public void testOutputsBodyWhenNotGrantedSatisfied() {
79          // allows request - principal doesn't have ROLE_BANKER
80          assertTrue(authz.noneGranted("ROLE_BANKER"));
81      }
82  
83      public void testPreventsBodyOutputIfNoSecureContext() {
84          SecurityContextHolder.getContext().setAuthentication(null);
85  
86          // prevents output - no context defined
87          assertFalse(authz.anyGranted("ROLE_BANKER"));
88      }
89  
90      public void testSkipsBodyIfNoAnyRolePresent() {
91          // unauthorized - ROLE_BANKER not in granted authorities
92          assertFalse(authz.anyGranted("ROLE_BANKER"));
93      }
94  
95      public void testSkipsBodyWhenMissingAnAllGranted() {
96          //  prevents request - missing ROLE_BANKER on principal
97          assertFalse(authz.allGranted("ROLE_SUPERVISOR,ROLE_TELLER,ROLE_BANKER"));
98      }
99  
100     public void testSkipsBodyWhenNotGrantedUnsatisfied() {
101         //  prevents request - principal has ROLE_TELLER
102         assertFalse(authz.noneGranted("ROLE_TELLER"));
103     }
104 }