1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.runas;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.Authentication;
21 import org.acegisecurity.BadCredentialsException;
22 import org.acegisecurity.GrantedAuthority;
23 import org.acegisecurity.GrantedAuthorityImpl;
24 import org.acegisecurity.providers.TestingAuthenticationToken;
25 import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26
27
28 /***
29 * Tests {@link RunAsImplAuthenticationProvider}.
30 */
31 public class RunAsImplAuthenticationProviderTests extends TestCase {
32
33
34 public RunAsImplAuthenticationProviderTests() {
35 super();
36 }
37
38 public RunAsImplAuthenticationProviderTests(String arg0) {
39 super(arg0);
40 }
41
42
43
44 public static void main(String[] args) {
45 junit.textui.TestRunner.run(RunAsImplAuthenticationProviderTests.class);
46 }
47
48 public final void setUp() throws Exception {
49 super.setUp();
50 }
51
52 public void testAuthenticationFailDueToWrongKey() {
53 RunAsUserToken token = new RunAsUserToken("WRONG_PASSWORD", "Test",
54 "Password",
55 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
56 "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
57 RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
58 provider.setKey("hello_world");
59
60 try {
61 provider.authenticate(token);
62 fail("Should have thrown BadCredentialsException");
63 } catch (BadCredentialsException expected) {
64 assertTrue(true);
65 }
66 }
67
68 public void testAuthenticationSuccess() {
69 RunAsUserToken token = new RunAsUserToken("my_password", "Test",
70 "Password",
71 new GrantedAuthority[] {new GrantedAuthorityImpl("ROLE_ONE"), new GrantedAuthorityImpl(
72 "ROLE_TWO")}, UsernamePasswordAuthenticationToken.class);
73 RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
74 provider.setKey("my_password");
75
76 Authentication result = provider.authenticate(token);
77
78 if (!(result instanceof RunAsUserToken)) {
79 fail("Should have returned RunAsUserToken");
80 }
81
82 RunAsUserToken resultCast = (RunAsUserToken) result;
83 assertEquals("my_password".hashCode(), resultCast.getKeyHash());
84 }
85
86 public void testStartupFailsIfNoKey() throws Exception {
87 RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
88
89 try {
90 provider.afterPropertiesSet();
91 fail("Should have thrown IllegalArgumentException");
92 } catch (IllegalArgumentException expected) {
93 assertTrue(true);
94 }
95 }
96
97 public void testStartupSuccess() throws Exception {
98 RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
99 provider.setKey("hello_world");
100 assertEquals("hello_world", provider.getKey());
101 provider.afterPropertiesSet();
102 assertTrue(true);
103 }
104
105 public void testSupports() {
106 RunAsImplAuthenticationProvider provider = new RunAsImplAuthenticationProvider();
107 assertTrue(provider.supports(RunAsUserToken.class));
108 assertTrue(!provider.supports(TestingAuthenticationToken.class));
109 }
110 }