1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.adapters.cas;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.MockAuthenticationManager;
21
22 import org.springframework.mock.web.MockHttpServletRequest;
23
24
25 /***
26 * Tests {@link CasPasswordHandler}.
27 *
28 * @author Ben Alex
29 * @version $Id: CasPasswordHandlerTests.java,v 1.3 2005/11/17 00:56:09 benalex Exp $
30 */
31 public class CasPasswordHandlerTests extends TestCase {
32
33
34 public CasPasswordHandlerTests() {
35 super();
36 }
37
38 public CasPasswordHandlerTests(String arg0) {
39 super(arg0);
40 }
41
42
43
44 public final void setUp() throws Exception {
45 super.setUp();
46 }
47
48 public static void main(String[] args) {
49 junit.textui.TestRunner.run(CasPasswordHandlerTests.class);
50 }
51
52 public void testDeniesAccessWhenAuthenticationManagerThrowsException()
53 throws Exception {
54 CasPasswordHandler handler = new CasPasswordHandler();
55 handler.setAuthenticationManager(new MockAuthenticationManager(false));
56 handler.afterPropertiesSet();
57
58 assertFalse(handler.authenticate(new MockHttpServletRequest(),
59 "username", "password"));
60 }
61
62 public void testDetectsEmptyAuthenticationManager()
63 throws Exception {
64 CasPasswordHandler handler = new CasPasswordHandler();
65
66 try {
67 handler.afterPropertiesSet();
68 fail("Should have thrown IllegalArgumentException");
69 } catch (IllegalArgumentException expected) {
70 assertEquals("An AuthenticationManager is required",
71 expected.getMessage());
72 }
73 }
74
75 public void testGettersSetters() {
76 CasPasswordHandler handler = new CasPasswordHandler();
77 handler.setAuthenticationManager(new MockAuthenticationManager(false));
78 assertTrue(handler.getAuthenticationManager() != null);
79 }
80
81 public void testGracefullyHandlesEmptyUsernamesAndPassword()
82 throws Exception {
83 CasPasswordHandler handler = new CasPasswordHandler();
84 handler.setAuthenticationManager(new MockAuthenticationManager(true));
85 handler.afterPropertiesSet();
86
87
88 assertFalse(handler.authenticate(new MockHttpServletRequest(), "",
89 "password"));
90 assertFalse(handler.authenticate(new MockHttpServletRequest(), null,
91 "password"));
92
93
94 assertTrue(handler.authenticate(new MockHttpServletRequest(), "user",
95 null));
96 assertTrue(handler.authenticate(new MockHttpServletRequest(), "user", ""));
97 }
98
99 public void testNormalOperation() throws Exception {
100 CasPasswordHandler handler = new CasPasswordHandler();
101 handler.setAuthenticationManager(new MockAuthenticationManager(true));
102 handler.afterPropertiesSet();
103
104 assertTrue(handler.authenticate(new MockHttpServletRequest(),
105 "username", "password"));
106 }
107 }