package org.springframework.security.openid;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.MockAuthenticationManager;
import org.springframework.security.openid.OpenIDAuthenticationFilter;
import org.springframework.security.openid.OpenIDConsumerException;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;

public class OpenIDAuthenticationFilterTests {

    OpenIDAuthenticationFilter filter;
    private static final String REDIRECT_URL = "http://www.example.com/redirect";
    private static final String CLAIMED_IDENTITY_URL = "http://www.example.com/identity";
    private static final String REQUEST_PATH = "/j_spring_openid_security_check";
    private static final String FILTER_PROCESS_URL = "http://localhost:80" + REQUEST_PATH;
    private static final String DEFAULT_TARGET_URL = FILTER_PROCESS_URL;

    @Before
    public void setUp() throws Exception {
        filter = new OpenIDAuthenticationFilter();
        filter.setConsumer(new MockOpenIDConsumer(REDIRECT_URL));
        SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
        filter.setAuthenticationSuccessHandler(new SavedRequestAwareAuthenticationSuccessHandler());
        successHandler.setDefaultTargetUrl(DEFAULT_TARGET_URL);
        filter.setAuthenticationManager(new MockAuthenticationManager());
        filter.afterPropertiesSet();
    }

    @Test
    public void testFilterOperation() throws Exception {
        MockHttpServletRequest req = new MockHttpServletRequest("GET", REQUEST_PATH);
        MockHttpServletResponse response = new MockHttpServletResponse();

        req.setParameter("j_username", CLAIMED_IDENTITY_URL);
        req.setRemoteHost("www.example.com");

        filter.setConsumer(new MockOpenIDConsumer() {
            public String beginConsumption(HttpServletRequest req, String claimedIdentity, String returnToUrl, String realm) throws OpenIDConsumerException {
                assertEquals(CLAIMED_IDENTITY_URL, claimedIdentity);
                assertEquals(DEFAULT_TARGET_URL, returnToUrl);
                assertEquals("http://localhost:80/", realm);
                return REDIRECT_URL;
            }
        });

        FilterChain fc = mock(FilterChain.class);
        filter.doFilter(req, response, fc);
        assertEquals(REDIRECT_URL, response.getRedirectedUrl());
        // Filter chain shouldn't proceed
        verify(fc, never()).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
    }
}

@@ -495,21 +495,17 @@
 ecurity.
-MockA
+a
 uthentic
@@ -513,66 +513,9 @@
 tion
-Manager;%0Aimport org.springframework.security.openid.OpenID
+.
 Auth
@@ -524,20 +524,21 @@
 tication
-Filt
+Manag
 er;%0Aimpo
@@ -573,34 +573,23 @@
 ity.
-openid.OpenIDConsumerExcep
+core.Authentica
 tion
@@ -1704,20 +1704,16 @@
 ger(new 
-Mock
 Authenti
@@ -1723,24 +1723,143 @@
 ionManager()
+ %7B%0A            public Authentication authenticate(Authentication a) %7B%0A                return a;%0A            %7D%0A        %7D
 );%0A        f

Remove dependency on MockAuthenticationManager