[1240] | 1 | package gov.va.med.edp.springframework.security.ui.vistalink;
|
---|
| 2 |
|
---|
| 3 | import gov.va.med.edp.springframework.security.providers.vistalink.VistaAuthenticationToken;
|
---|
| 4 | import gov.va.med.edp.springframework.security.userdetails.vistalink.VistaUser;
|
---|
| 5 | import junit.framework.TestCase;
|
---|
| 6 | import org.springframework.security.Authentication;
|
---|
| 7 | import org.springframework.security.AuthenticationManager;
|
---|
| 8 | import org.springframework.security.GrantedAuthority;
|
---|
| 9 | import org.easymock.MockControl;
|
---|
| 10 | import org.easymock.AbstractMatcher;
|
---|
| 11 | import org.springframework.mock.web.MockFilterChain;
|
---|
| 12 | import org.springframework.mock.web.MockHttpServletRequest;
|
---|
| 13 | import org.springframework.mock.web.MockHttpServletResponse;
|
---|
| 14 |
|
---|
| 15 | public 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
|
---|
| 73 | import gov.va.med.edp.springframework.security.providers.vistalink.VistaAuthenticationToken;
|
---|
| 74 | import gov.va.med.edp.springframework.security.ui.vistalink.VistaAuthenticationProcessingFilter;
|
---|
| 75 | import gov.va.med.edp.springframework.security.userdetails.vistalink.VistaUser;
|
---|
| 76 | import junit.framework.TestCase;
|
---|
| 77 | import org.springframework.security.Authentication;
|
---|
| 78 | import org.springframework.security.AuthenticationManager;
|
---|
| 79 | import org.springframework.security.GrantedAuthority;
|
---|
| 80 | import org.easymock.EasyMock;
|
---|
| 81 | import static org.easymock.EasyMock.*;
|
---|
| 82 | import org.easymock.IArgumentMatcher;
|
---|
| 83 | import org.springframework.mock.web.MockFilterChain;
|
---|
| 84 | import org.springframework.mock.web.MockHttpServletRequest;
|
---|
| 85 | import org.springframework.mock.web.MockHttpServletResponse;
|
---|
| 86 |
|
---|
| 87 | public 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 | */
|
---|