1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.adapters;
17
18 import junit.framework.TestCase;
19
20 import org.acegisecurity.GrantedAuthority;
21 import org.acegisecurity.GrantedAuthorityImpl;
22
23 import org.acegisecurity.context.SecurityContextHolder;
24
25 import org.acegisecurity.util.MockFilterChain;
26
27 import org.springframework.mock.web.MockHttpServletRequest;
28 import org.springframework.mock.web.MockHttpServletResponse;
29
30
31 /***
32 * Tests {@link HttpRequestIntegrationFilter}.
33 *
34 * @author Ben Alex
35 * @version $Id: HttpRequestIntegrationFilterTests.java,v 1.9 2005/11/25 00:26:29 benalex Exp $
36 */
37 public class HttpRequestIntegrationFilterTests extends TestCase {
38
39
40 public HttpRequestIntegrationFilterTests() {
41 super();
42 }
43
44 public HttpRequestIntegrationFilterTests(String arg0) {
45 super(arg0);
46 }
47
48
49
50 public static void main(String[] args) {
51 junit.textui.TestRunner.run(HttpRequestIntegrationFilterTests.class);
52 }
53
54 public void testCorrectOperation() throws Exception {
55 HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
56 PrincipalAcegiUserToken principal = new PrincipalAcegiUserToken("key",
57 "someone", "password",
58 new GrantedAuthority[] {new GrantedAuthorityImpl("SOME_ROLE")},
59 null);
60
61 MockHttpServletRequest request = new MockHttpServletRequest();
62 request.setUserPrincipal(principal);
63
64 MockHttpServletResponse response = new MockHttpServletResponse();
65 MockFilterChain chain = new MockFilterChain(true);
66
67 filter.doFilter(request, response, chain);
68
69 if (!(SecurityContextHolder.getContext().getAuthentication() instanceof PrincipalAcegiUserToken)) {
70 System.out.println(SecurityContextHolder.getContext()
71 .getAuthentication());
72 fail("Should have returned PrincipalAcegiUserToken");
73 }
74
75 PrincipalAcegiUserToken castResult = (PrincipalAcegiUserToken) SecurityContextHolder.getContext()
76 .getAuthentication();
77 assertEquals(principal, castResult);
78 }
79
80 public void testHandlesIfHttpRequestIsNullForSomeReason()
81 throws Exception {
82 HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
83
84 try {
85 filter.doFilter(null, null, null);
86 fail("Should have thrown IllegalArgumentException");
87 } catch (IllegalArgumentException expected) {
88 assertTrue(true);
89 }
90 }
91
92 public void testHandlesIfThereIsNoPrincipal() throws Exception {
93 HttpRequestIntegrationFilter filter = new HttpRequestIntegrationFilter();
94 MockHttpServletRequest request = new MockHttpServletRequest();
95 MockHttpServletResponse response = new MockHttpServletResponse();
96 MockFilterChain chain = new MockFilterChain(true);
97
98 assertNull(SecurityContextHolder.getContext().getAuthentication());
99 filter.doFilter(request, response, chain);
100 assertNull(SecurityContextHolder.getContext().getAuthentication());
101 }
102
103 protected void setUp() throws Exception {
104 super.setUp();
105 SecurityContextHolder.getContext().setAuthentication(null);
106 }
107
108 protected void tearDown() throws Exception {
109 super.tearDown();
110 SecurityContextHolder.getContext().setAuthentication(null);
111 }
112 }