From 3d2bb8ac5e7bcc9188039fc9c983f6217c9c7b08 Mon Sep 17 00:00:00 2001 From: Mattias Arthursson Date: Wed, 12 Nov 2008 07:44:42 +0000 Subject: [PATCH] Added test case verifying simple authentication mechanism. --- .../core/support/LdapContextSourcelITest.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java index 7d6e5f4a..f50b3b93 100644 --- a/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java +++ b/test/integration-tests/src/test/java/org/springframework/ldap/core/support/LdapContextSourcelITest.java @@ -18,8 +18,10 @@ package org.springframework.ldap.core.support; import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertNotNull; import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import java.util.Hashtable; +import java.util.List; import javax.naming.Context; import javax.naming.NamingException; @@ -27,8 +29,14 @@ import javax.naming.directory.DirContext; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.ldap.AbstractLdapTemplateIntegrationTest; import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.core.DirContextOperations; +import org.springframework.ldap.core.LdapTemplate; +import org.springframework.ldap.core.simple.AbstractParameterizedContextMapper; +import org.springframework.ldap.filter.EqualsFilter; +import org.springframework.ldap.support.LdapUtils; import org.springframework.test.context.ContextConfiguration; /** @@ -42,6 +50,9 @@ public class LdapContextSourcelITest extends AbstractLdapTemplateIntegrationTest @Autowired private ContextSource tested; + @Autowired + private LdapTemplate ldapTemplate; + @Test public void testGetReadOnlyContext() throws NamingException { DirContext ctx = null; @@ -117,4 +128,36 @@ public class LdapContextSourcelITest extends AbstractLdapTemplateIntegrationTest } } } + + @SuppressWarnings("unchecked") + @Test + public void verifyAuthenticate() { + EqualsFilter filter = new EqualsFilter("cn", "Some Person2"); + List results = ldapTemplate.search("", filter.toString(), new DnContextMapper()); + if (results.size() != 1) { + throw new IncorrectResultSizeDataAccessException(1, results.size()); + } + + DirContext ctx = null; + try { + ctx = tested.getContext(results.get(0), "password"); + assertTrue(true); + } + catch (Exception e) { + fail("Authentication failed"); + } + finally { + LdapUtils.closeContext(ctx); + } + + BaseLdapPathSource ldapPathSource = (BaseLdapPathSource) tested; + System.out.println(ldapPathSource.getBaseLdapPathAsString()); + } + + private final static class DnContextMapper extends AbstractParameterizedContextMapper { + @Override + protected String doMapFromContext(DirContextOperations ctx) { + return ctx.getNameInNamespace(); + } + } }