source: EDIS/tags/ed/tracking-server-vista/src/test/java/gov/va/med/edp/springframework/security/ui/vistalink/VistaAuthenticationProcessingFilterTest.java@ 1240

Last change on this file since 1240 was 1240, checked in by George Lilly, 13 years ago

new version from the VA

File size: 7.5 KB
Line 
1package gov.va.med.edp.springframework.security.ui.vistalink;
2
3import gov.va.med.edp.springframework.security.providers.vistalink.VistaAuthenticationToken;
4import gov.va.med.edp.springframework.security.userdetails.vistalink.VistaUser;
5import junit.framework.TestCase;
6import org.springframework.security.Authentication;
7import org.springframework.security.AuthenticationManager;
8import org.springframework.security.GrantedAuthority;
9import org.easymock.MockControl;
10import org.easymock.AbstractMatcher;
11import org.springframework.mock.web.MockFilterChain;
12import org.springframework.mock.web.MockHttpServletRequest;
13import org.springframework.mock.web.MockHttpServletResponse;
14
15public class VistaAuthenticationProcessingFilterTest extends TestCase {
16
17 private MockHttpServletRequest request = new MockHttpServletRequest();
18 private MockHttpServletResponse response = new MockHttpServletResponse();
19 private MockFilterChain filterChain = new MockFilterChain();
20
21 private MockControl mockAuthenticationManagerControl;
22 private AuthenticationManager mockAuthenticationManager;
23
24 protected void setUp() throws Exception {
25 mockAuthenticationManagerControl = MockControl.createControl(AuthenticationManager.class);
26 mockAuthenticationManager = (AuthenticationManager) mockAuthenticationManagerControl.getMock();
27 }
28
29 public void testAttemptAuthentication() throws Exception {
30 VistaAuthenticationProcessingFilter f = new VistaAuthenticationProcessingFilter();
31 f.setDefaultTargetUrl("/welcome.jsp");
32 f.setAuthenticationFailureUrl("/authenticationFailed.jsp");
33 f.setAuthenticationManager(mockAuthenticationManager);
34 f.afterPropertiesSet();
35
36 request.addParameter(VistaAuthenticationProcessingFilter.STATION_NUMBER_KEY, "983A");
37 request.addParameter(VistaAuthenticationProcessingFilter.ACCESS_CODE_KEY, "10VEHU");
38 request.addParameter(VistaAuthenticationProcessingFilter.VERIFY_CODE_KEY, "VEHU10");
39 request.setRemoteAddr("10.0.1.34");
40 request.setRequestURI(f.getFilterProcessesUrl());
41
42 VistaAuthenticationToken authRequest = new VistaAuthenticationToken("983A", "10VEHU", "VEHU10", "10.0.1.34");
43 mockAuthenticationManager.authenticate(authRequest);
44 mockAuthenticationManagerControl.setMatcher(new AuthenticationEquals());
45 mockAuthenticationManagerControl.setReturnValue(new VistaAuthenticationToken(new VistaUser("54321.123456789", "983A", "12345", "10VEHU", "VEHU10", "Vehu,Ten", "Ten Vehu", "TEN", null, "VEHU", null, null, null, true, true, true, true, new GrantedAuthority[]{}), "10VEHU", "VEHU10", "10.0.1.34", new GrantedAuthority[]{}));
46 mockAuthenticationManagerControl.replay();
47
48 f.doFilter(request, response, filterChain);
49
50 assertNull(filterChain.getRequest());
51 assertNull(filterChain.getResponse());
52
53 mockAuthenticationManagerControl.verify();
54 }
55
56 static class AuthenticationEquals extends AbstractMatcher {
57
58 protected boolean argumentMatches(Object o, Object o1) {
59 if (!(o instanceof Authentication)) return false;
60 if (!(o1 instanceof Authentication)) return false;
61 Authentication expected = (Authentication) o;
62 Authentication actual = (Authentication) o1;
63 return expected.getPrincipal().equals(actual.getPrincipal()) &&
64 expected.getName().equals(actual.getName()) &&
65 expected.getCredentials().equals(actual.getCredentials()) &&
66 expected.getDetails().equals(actual.getDetails()) &&
67 expected.isAuthenticated() == actual.isAuthenticated();
68 }
69 }
70}
71
72/* version for java 5 and later easymock
73import gov.va.med.edp.springframework.security.providers.vistalink.VistaAuthenticationToken;
74import gov.va.med.edp.springframework.security.ui.vistalink.VistaAuthenticationProcessingFilter;
75import gov.va.med.edp.springframework.security.userdetails.vistalink.VistaUser;
76import junit.framework.TestCase;
77import org.springframework.security.Authentication;
78import org.springframework.security.AuthenticationManager;
79import org.springframework.security.GrantedAuthority;
80import org.easymock.EasyMock;
81import static org.easymock.EasyMock.*;
82import org.easymock.IArgumentMatcher;
83import org.springframework.mock.web.MockFilterChain;
84import org.springframework.mock.web.MockHttpServletRequest;
85import org.springframework.mock.web.MockHttpServletResponse;
86
87public class VistaAuthenticationProcessingFilterTest extends TestCase {
88
89 private MockHttpServletRequest request = new MockHttpServletRequest();
90 private MockHttpServletResponse response = new MockHttpServletResponse();
91 private MockFilterChain filterChain = new MockFilterChain();
92 private AuthenticationManager mockAuthenticationManager;
93
94 protected void setUp() throws Exception {
95 mockAuthenticationManager = createMock(AuthenticationManager.class);
96 }
97
98 public void testAttemptAuthentication() throws Exception {
99 VistaAuthenticationProcessingFilter f = new VistaAuthenticationProcessingFilter();
100 f.setDefaultTargetUrl("/welcome.jsp");
101 f.setAuthenticationFailureUrl("/authenticationFailed.jsp");
102 f.setAuthenticationManager(mockAuthenticationManager);
103 f.afterPropertiesSet();
104
105 request.addParameter(VistaAuthenticationProcessingFilter.STATION_NUMBER_KEY, "983A");
106 request.addParameter(VistaAuthenticationProcessingFilter.ACCESS_CODE_KEY, "10VEHU");
107 request.addParameter(VistaAuthenticationProcessingFilter.VERIFY_CODE_KEY, "VEHU10");
108 request.setRemoteAddr("10.0.1.34");
109 request.setRequestURI(f.getFilterProcessesUrl());
110
111 VistaAuthenticationToken authRequest = new VistaAuthenticationToken("983A", "10VEHU", "VEHU10", "10.0.1.34");
112 expect(mockAuthenticationManager.authenticate(eqAuth(authRequest))).andReturn(new VistaAuthenticationToken(new VistaUser("54321.123456789", "983A", "12345", "10VEHU", "VEHU10", "Vehu,Ten", "Ten Vehu", "TEN", null, "VEHU", null, null, null, true, true, true, true, new GrantedAuthority[]{}), "10VEHU", "VEHU10", "10.0.1.34", new GrantedAuthority[]{}));
113 replay(mockAuthenticationManager);
114
115 f.doFilter(request, response, filterChain);
116
117 assertNull(filterChain.getRequest());
118 assertNull(filterChain.getResponse());
119
120 verify(mockAuthenticationManager);
121 }
122
123 static class AuthenticationEquals implements IArgumentMatcher {
124
125 private Authentication expected;
126
127 public AuthenticationEquals(Authentication expected) {
128 this.expected = expected;
129 }
130
131 public boolean matches(Object o) {
132 if (!(o instanceof Authentication)) return false;
133
134 Authentication actual = (Authentication) o;
135
136 return expected.getPrincipal().equals(actual.getPrincipal()) &&
137 expected.getName().equals(actual.getName()) &&
138 expected.getCredentials().equals(actual.getCredentials()) &&
139 expected.getDetails().equals(actual.getDetails()) &&
140 expected.isAuthenticated() == actual.isAuthenticated();
141 }
142
143 public void appendTo(StringBuffer stringBuffer) {
144 //To change body of implemented methods use File | Settings | File Templates.
145 }
146 }
147
148 public static <T extends Authentication> T eqAuth(T in) {
149 EasyMock.reportMatcher(new AuthenticationEquals(in));
150 return null;
151 }
152}
153*/
Note: See TracBrowser for help on using the repository browser.