1
2
3
4
5
6
7
8
9
10
11
12
13
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
23 import org.acegisecurity.context.SecurityContextHolder;
24
25 import org.acegisecurity.providers.TestingAuthenticationToken;
26
27 import org.acegisecurity.userdetails.User;
28
29 import javax.servlet.jsp.JspException;
30 import javax.servlet.jsp.tagext.Tag;
31
32
33
34
35
36
37
38
39 public class AuthenticationTagTests extends TestCase {
40
41
42 private final MyAuthenticationTag authenticationTag = new MyAuthenticationTag();
43
44
45
46 public void testOperationAndMethodPrefixWhenPrincipalIsAUserDetailsInstance()
47 throws JspException {
48 Authentication auth = new TestingAuthenticationToken(new User("marissaUserDetails", "koala", true, true, true,
49 true, new GrantedAuthority[] {}), "koala", new GrantedAuthority[] {});
50 SecurityContextHolder.getContext().setAuthentication(auth);
51
52 authenticationTag.setOperation("username");
53 authenticationTag.setMethodPrefix("get");
54 assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
55 assertEquals("marissaUserDetails", authenticationTag.getLastMessage());
56 }
57
58 public void testOperationWhenPrincipalIsAString() throws JspException {
59 Authentication auth = new TestingAuthenticationToken("marissaAsString", "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("marissaUserDetails", "koala", true, true, true,
70 true, new GrantedAuthority[] {}), "koala", new GrantedAuthority[] {});
71 SecurityContextHolder.getContext().setAuthentication(auth);
72
73 authenticationTag.setOperation("username");
74 assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
75 assertEquals("marissaUserDetails", authenticationTag.getLastMessage());
76 }
77
78 public void testOperationWhenPrincipalIsNull() throws JspException {
79 Authentication auth = new TestingAuthenticationToken(null, "koala", new GrantedAuthority[] {});
80 SecurityContextHolder.getContext().setAuthentication(auth);
81
82 authenticationTag.setOperation("principal");
83 assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
84 }
85
86 public void testOperationWhenSecurityContextIsNull()
87 throws JspException {
88 SecurityContextHolder.getContext().setAuthentication(null);
89
90 authenticationTag.setOperation("principal");
91 assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
92 assertEquals(null, authenticationTag.getLastMessage());
93
94 SecurityContextHolder.getContext().setAuthentication(null);
95 }
96
97 public void testSkipsBodyIfNullOrEmptyOperation() throws Exception {
98 authenticationTag.setOperation("");
99 assertEquals("", authenticationTag.getOperation());
100 assertEquals(Tag.SKIP_BODY, authenticationTag.doStartTag());
101 }
102
103 public void testThrowsExceptionForUnrecognisedMethodPrefix() {
104 Authentication auth = new TestingAuthenticationToken(new User("marissaUserDetails", "koala", true, true, true,
105 true, new GrantedAuthority[] {}), "koala", new GrantedAuthority[] {});
106 SecurityContextHolder.getContext().setAuthentication(auth);
107 authenticationTag.setOperation("username");
108 authenticationTag.setMethodPrefix("qrq");
109
110 try {
111 authenticationTag.doStartTag();
112 fail("Should have thrown a JspException");
113 } catch (JspException expected) {
114 assertTrue(true);
115 }
116 }
117
118 public void testThrowsExceptionForUnrecognisedOperation() {
119 Authentication auth = new TestingAuthenticationToken(new User("marissaUserDetails", "koala", true, true, true,
120 true, new GrantedAuthority[] {}), "koala", new GrantedAuthority[] {});
121 SecurityContextHolder.getContext().setAuthentication(auth);
122 authenticationTag.setOperation("qsq");
123
124 try {
125 authenticationTag.doStartTag();
126 fail("Should have throwns JspException");
127 } catch (JspException expected) {
128 assertTrue(true);
129 }
130 }
131
132
133
134 private class MyAuthenticationTag extends AuthenticationTag {
135 String lastMessage = null;
136
137 public String getLastMessage() {
138 return lastMessage;
139 }
140
141 protected void writeMessage(String msg) throws JspException {
142 lastMessage = msg;
143 }
144 }
145 }