1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.vote;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.Authentication;
21 import org.acegisecurity.ConfigAttributeDefinition;
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.SecurityConfig;
25
26 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
27 import org.acegisecurity.providers.anonymous.AnonymousAuthenticationToken;
28 import org.acegisecurity.providers.rememberme.RememberMeAuthenticationToken;
29
30
31
32
33
34
35
36
37 public class AuthenticatedVoterTests extends TestCase {
38
39
40 public AuthenticatedVoterTests() {
41 super();
42 }
43
44 public AuthenticatedVoterTests(String arg0) {
45 super(arg0);
46 }
47
48
49
50 private Authentication createAnonymous() {
51 return new AnonymousAuthenticationToken("ignored", "ignored",
52 new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")});
53 }
54
55 private Authentication createFullyAuthenticated() {
56 return new UsernamePasswordAuthenticationToken("ignored", "ignored",
57 new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")});
58 }
59
60 private Authentication createRememberMe() {
61 return new RememberMeAuthenticationToken("ignored", "ignored",
62 new GrantedAuthority[] {new GrantedAuthorityImpl("ignored")});
63 }
64
65 public static void main(String[] args) {
66 junit.textui.TestRunner.run(AuthenticatedVoterTests.class);
67 }
68
69 public final void setUp() throws Exception {
70 super.setUp();
71 }
72
73 public void testAnonymousWorks() {
74 AuthenticatedVoter voter = new AuthenticatedVoter();
75 ConfigAttributeDefinition def = new ConfigAttributeDefinition();
76 def.addConfigAttribute(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_ANONYMOUSLY));
77 assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createAnonymous(), null, def));
78 assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createRememberMe(), null, def));
79 assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createFullyAuthenticated(), null, def));
80 }
81
82 public void testFullyWorks() {
83 AuthenticatedVoter voter = new AuthenticatedVoter();
84 ConfigAttributeDefinition def = new ConfigAttributeDefinition();
85 def.addConfigAttribute(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_FULLY));
86 assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(createAnonymous(), null, def));
87 assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(createRememberMe(), null, def));
88 assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createFullyAuthenticated(), null, def));
89 }
90
91 public void testRememberMeWorks() {
92 AuthenticatedVoter voter = new AuthenticatedVoter();
93 ConfigAttributeDefinition def = new ConfigAttributeDefinition();
94 def.addConfigAttribute(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_REMEMBERED));
95 assertEquals(AccessDecisionVoter.ACCESS_DENIED, voter.vote(createAnonymous(), null, def));
96 assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createRememberMe(), null, def));
97 assertEquals(AccessDecisionVoter.ACCESS_GRANTED, voter.vote(createFullyAuthenticated(), null, def));
98 }
99
100 public void testSetterRejectsNull() {
101 AuthenticatedVoter voter = new AuthenticatedVoter();
102
103 try {
104 voter.setAuthenticationTrustResolver(null);
105 fail("Expected IAE");
106 } catch (IllegalArgumentException expected) {
107 assertTrue(true);
108 }
109 }
110
111 public void testSupports() {
112 AuthenticatedVoter voter = new AuthenticatedVoter();
113 assertTrue(voter.supports(String.class));
114 assertTrue(voter.supports(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_ANONYMOUSLY)));
115 assertTrue(voter.supports(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_FULLY)));
116 assertTrue(voter.supports(new SecurityConfig(AuthenticatedVoter.IS_AUTHENTICATED_REMEMBERED)));
117 assertFalse(voter.supports(new SecurityConfig("FOO")));
118 }
119 }