package org.springframework.security.config;

import org.springframework.security.providers.ProviderManager;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.springframework.security.providers.ldap.LdapAuthenticationProvider;
import org.springframework.security.Authentication;
import org.springframework.security.util.InMemoryXmlApplicationContext;
import org.springframework.security.userdetails.ldap.LdapUserDetailsImpl;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.After;


/**
 * @author Luke Taylor
 * @version $Id$
 */
public class LdapProviderBeanDefinitionParserTests {
    InMemoryXmlApplicationContext appCtx;

    @After
    public void closeAppContext() {
        if (appCtx != null) {
            appCtx.close();
            appCtx = null;
        }
    }

    @Test
    public void simpleProviderAuthenticatesCorrectly() {
        setContext("<ldap-server /> <ldap-authentication-provider group-search-filter='member={0}' />");

        LdapAuthenticationProvider provider = getProvider();
        Authentication auth = provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "benspassword"));
        LdapUserDetailsImpl ben = (LdapUserDetailsImpl) auth.getPrincipal();

        assertEquals(2, ben.getAuthorities().length);
    }  
    
    @Test(expected = SecurityConfigurationException.class)
    public void missingServerEltCausesConfigException() {
        setContext("<ldap-authentication-provider />");
    }

    
    @Test
    public void supportsPasswordComparisonAuthentication() {
        setContext("<ldap-server /> " +
                "<ldap-authentication-provider user-dn-pattern='uid={0},ou=people'>" +
                "    <password-compare />" +
                "</ldap-authentication-provider>");
        LdapAuthenticationProvider provider = getProvider();
        provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "benspassword"));        
    }    
    
    
    @Test
    public void supportsPasswordComparisonAuthenticationWithHashAttribute() {
        setContext("<ldap-server /> " +
                "<ldap-authentication-provider user-dn-pattern='uid={0},ou=people'>" +
                "    <password-compare password-attribute='uid' hash='plaintext'/>" +
                "</ldap-authentication-provider>");
        LdapAuthenticationProvider provider = getProvider();
        provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "ben"));        
    }    
    
    @Test
    public void supportsPasswordComparisonAuthenticationWithPasswordEncoder() {
        setContext("<ldap-server /> " +
        		"<ldap-authentication-provider user-dn-pattern='uid={0},ou=people'>" +
        		"    <password-compare password-attribute='uid'>" +
        		"        <password-encoder hash='plaintext'/>" +
        		"    </password-compare>" +
        		"</ldap-authentication-provider>");
        LdapAuthenticationProvider provider = getProvider();
        provider.authenticate(new UsernamePasswordAuthenticationToken("ben", "ben"));        
    }    

    @Test
    public void detectsNonStandardServerId() {
        setContext("<ldap-server id='myServer'/> " +
                "<ldap-authentication-provider />");
    }
    
    private void setContext(String context) {
        appCtx = new InMemoryXmlApplicationContext(context);
    }

    private LdapAuthenticationProvider getProvider() {
        ProviderManager authManager = (ProviderManager) appCtx.getBean(BeanIds.AUTHENTICATION_MANAGER);

        assertEquals(1, authManager.getProviders().size());

        LdapAuthenticationProvider provider = (LdapAuthenticationProvider) authManager.getProviders().get(0);
        return provider;
    }    
}

@@ -39,16 +39,204 @@
 onfig;%0A%0A
+import static org.junit.Assert.assertEquals;%0Aimport static org.junit.Assert.assertTrue;%0A%0Aimport org.junit.After;%0Aimport org.junit.Test;%0Aimport org.springframework.security.Authentication;%0A
 import o
@@ -492,85 +492,36 @@
 ity.
-Authentication;%0Aimport org.springframework.security.util.InMemoryXmlApplicati
+userdetails.ldap.InetOrgPers
 onCo
@@ -525,16 +525,22 @@
 nContext
+Mapper
 ;%0Aimport
@@ -618,80 +618,124 @@
 ort 
-static org.junit.Assert.*;%0Aimport org.junit.Test;%0Aimport org.junit.After
+org.springframework.security.util.FieldUtils;%0Aimport org.springframework.security.util.InMemoryXmlApplicationContext
 ;%0A%0A%0A
@@ -3462,24 +3462,512 @@
 %0A    %7D%0A    %0A
+    @Test%0A    public void inetOrgContextMapperIsSupported() throws Exception %7B%0A        setContext(%0A                %22%3Cldap-server id='someServer' url='ldap://127.0.0.1:343/dc=springframework,dc=org'/%3E%22 +%0A                %22%3Cldap-authentication-provider user-details-class='inetOrgPerson'/%3E%22);%0A        LdapAuthenticationProvider provider = getProvider();%0A        assertTrue(FieldUtils.getFieldValue(provider, %22userDetailsContextMapper%22) instanceof InetOrgPersonContextMapper);%0A    %7D    %0A    %0A
     private 

Allow mapping to a standard Ldap UserDetails through the namespace http://jira.springframework.org/browse/SEC-807. Added extra test for Ldap provider parser.