1   /* Copyright 2004, 2005 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.authz;
17  
18  import junit.framework.TestCase;
19  
20  import org.acegisecurity.Authentication;
21  import org.acegisecurity.GrantedAuthority;
22  import org.acegisecurity.context.SecurityContextHolder;
23  import org.acegisecurity.providers.TestingAuthenticationToken;
24  import org.acegisecurity.userdetails.User;
25  
26  import javax.servlet.jsp.JspException;
27  import javax.servlet.jsp.tagext.Tag;
28  
29  
30  /***
31   * Tests {@link AuthenticationTag}.
32   *
33   * @author Ben Alex
34   * @version $Id: AuthenticationTagTests.java,v 1.9 2005/11/29 13:10:11 benalex Exp $
35   */
36  public class AuthenticationTagTests extends TestCase {
37      //~ Instance fields ========================================================
38  
39      private final MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
40  
41      //~ Methods ================================================================
42  
43      public void testOperationAndMethodPrefixWhenPrincipalIsAUserDetailsInstance()
44          throws JspException {
45          Authentication auth = new TestingAuthenticationToken(new User(
46                      "marissaUserDetails", "koala", true, true, true, true,
47                      new GrantedAuthority[] {}), "koala",
48                  new GrantedAuthority[] {});
49          SecurityContextHolder.getContext().setAuthentication(auth);
50  
51          authenticationTag.setOperation("username");
52          authenticationTag.setMethodPrefix("get");
53          assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
54          assertEquals("marissaUserDetails", authenticationTag.getLastMessage());
55      }
56  
57      public void testOperationWhenPrincipalIsAString() throws JspException {
58          Authentication auth = new TestingAuthenticationToken("marissaAsString",
59                  "koala", new GrantedAuthority[] {});
60          SecurityContextHolder.getContext().setAuthentication(auth);
61  
62          authenticationTag.setOperation("principal");
63          assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
64          assertEquals("marissaAsString", authenticationTag.getLastMessage());
65      }
66  
67      public void testOperationWhenPrincipalIsAUserDetailsInstance()
68          throws JspException {
69          Authentication auth = new TestingAuthenticationToken(new User(
70                      "marissaUserDetails", "koala", true, true, true, true,
71                      new GrantedAuthority[] {}), "koala",
72                  new GrantedAuthority[] {});
73          SecurityContextHolder.getContext().setAuthentication(auth);
74  
75          authenticationTag.setOperation("username");
76          assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
77          assertEquals("marissaUserDetails", authenticationTag.getLastMessage());
78      }
79  
80      public void testOperationWhenPrincipalIsNull() throws JspException {
81          Authentication auth = new TestingAuthenticationToken(null, "koala",
82                  new GrantedAuthority[] {});
83          SecurityContextHolder.getContext().setAuthentication(auth);
84  
85          authenticationTag.setOperation("principal");
86          assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
87      }
88  
89      public void testOperationWhenSecurityContextIsNull()
90          throws JspException {
91          SecurityContextHolder.getContext().setAuthentication(null);
92  
93          authenticationTag.setOperation("principal");
94          assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
95          assertEquals(null, authenticationTag.getLastMessage());
96  
97          SecurityContextHolder.getContext().setAuthentication(null);
98      }
99  
100     public void testSkipsBodyIfNullOrEmptyOperation() throws Exception {
101         authenticationTag.setOperation("");
102         assertEquals("", authenticationTag.getOperation());
103         assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
104     }
105 
106     public void testThrowsExceptionForUnrecognisedMethodPrefix() {
107         Authentication auth = new TestingAuthenticationToken(new User(
108                     "marissaUserDetails", "koala", true, true, true, true,
109                     new GrantedAuthority[] {}), "koala",
110                 new GrantedAuthority[] {});
111         SecurityContextHolder.getContext().setAuthentication(auth);
112         authenticationTag.setOperation("username");
113         authenticationTag.setMethodPrefix("qrq");
114 
115         try {
116             authenticationTag.doStartTag();
117             fail("Should have thrown a JspException");
118         } catch (JspException expected) {
119             assertTrue(true);
120         }
121     }
122 
123     public void testThrowsExceptionForUnrecognisedOperation() {
124         Authentication auth = new TestingAuthenticationToken(new User(
125                     "marissaUserDetails", "koala", true, true, true, true,
126                     new GrantedAuthority[] {}), "koala",
127                 new GrantedAuthority[] {});
128         SecurityContextHolder.getContext().setAuthentication(auth);
129         authenticationTag.setOperation("qsq");
130 
131         try {
132             authenticationTag.doStartTag();
133             fail("Should have throwns JspException");
134         } catch (JspException expected) {
135             assertTrue(true);
136         }
137     }
138 
139     //~ Inner Classes ==========================================================
140 
141     private class MyAuthenticationTag extends AuthenticationTag {
142         String lastMessage = null;
143 
144         public String getLastMessage() {
145             return lastMessage;
146         }
147 
148         protected void writeMessage(String msg) throws JspException {
149             lastMessage = msg;
150         }
151     }
152 }