SEC-449: Mostly changes to aid moving towards compatibility with spring-ldap.

This commit is contained in:
Luke Taylor
2007-09-07 19:55:45 +00:00
parent 98a91f5ac1
commit 9b71b5aa00
16 changed files with 236 additions and 112 deletions

View File

@@ -17,6 +17,8 @@ package org.acegisecurity.ldap;
import org.acegisecurity.AcegiMessageSource;
import org.acegisecurity.BadCredentialsException;
import org.springframework.ldap.UncategorizedLdapException;
import org.springframework.ldap.support.DirContextAdapter;
import java.util.Hashtable;
@@ -112,7 +114,7 @@ public class DefaultInitialDirContextFactoryTests extends AbstractLdapServerTest
try {
idf.newInitialDirContext();
fail("Connection succeeded unexpectedly");
} catch (LdapDataAccessException expected) {}
} catch (UncategorizedLdapException expected) {}
}
public void testEnvironment() {

View File

@@ -15,6 +15,9 @@
package org.acegisecurity.ldap;
import org.springframework.ldap.ContextExecutor;
import org.springframework.ldap.UncategorizedLdapException;
import java.util.Set;
import javax.naming.NamingException;
@@ -64,21 +67,19 @@ public class LdapTemplateTests extends AbstractLdapServerTestCase {
public void testNamingExceptionIsTranslatedCorrectly() {
try {
template.execute(new LdapCallback() {
public Object doInDirContext(DirContext dirContext)
throws NamingException {
throw new NamingException();
}
});
fail("Expected LdapDataAccessException on NamingException");
} catch (LdapDataAccessException expected) {
}
template.executeReadOnly(new ContextExecutor() {
public Object executeWithContext(DirContext dirContext) throws NamingException {
throw new NamingException();
}
});
fail("Expected UncategorizedLdapException on NamingException");
} catch (UncategorizedLdapException expected) {}
}
public void testSearchForSingleAttributeValues() {
String param = "uid=ben,ou=people,dc=acegisecurity,dc=org";
Set values = template.searchForSingleAttributeValues("ou=groups", "(member={0})", new String[]{param}, "ou");
Set values = template.searchForSingleAttributeValues("ou=groups", "(member={0})", new String[] {param}, "ou");
assertEquals("Expected 3 results from search", 3, values.size());
assertTrue(values.contains("developer"));

View File

@@ -30,9 +30,6 @@ import javax.naming.directory.DirContext;
* @version $Id$
*/
public class LdapUtilsTests extends MockObjectTestCase {
//~ Instance fields ================================================================================================
private final LdapDataAccessException tempCoverageBoost = new LdapDataAccessException("");
//~ Methods ========================================================================================================
@@ -63,6 +60,16 @@ public class LdapUtilsTests extends MockObjectTestCase {
LdapUtils.getRelativeName("cn=jane,dc=acegisecurity,dc=org", (Context) mockCtx.proxy()));
}
public void testGetRelativeNameWorksWithArbitrarySpaces()
throws Exception {
Mock mockCtx = mock(DirContext.class);
mockCtx.expects(atLeastOnce()).method("getNameInNamespace").will(returnValue("dc=acegisecurity,dc = org"));
assertEquals("cn=jane smith",
LdapUtils.getRelativeName("cn=jane smith, dc = acegisecurity , dc=org", (Context) mockCtx.proxy()));
}
public void testRootDnsAreParsedFromUrlsCorrectly() {
assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine"));
assertEquals("", LdapUtils.parseRootDnFromUrl("ldap://monkeymachine/"));

View File

@@ -15,12 +15,12 @@
package org.acegisecurity.ldap;
import org.springframework.dao.DataAccessException;
import javax.naming.directory.DirContext;
/**
*
DOCUMENT ME!
*
* @author Luke Taylor
* @version $Id$
@@ -28,8 +28,8 @@ DOCUMENT ME!
public class MockInitialDirContextFactory implements InitialDirContextFactory {
//~ Instance fields ================================================================================================
DirContext ctx;
String baseDn;
private DirContext ctx;
private String baseDn;
//~ Constructors ===================================================================================================
@@ -51,4 +51,12 @@ public class MockInitialDirContextFactory implements InitialDirContextFactory {
public DirContext newInitialDirContext(String username, String password) {
return ctx;
}
public DirContext getReadOnlyContext() throws DataAccessException {
return ctx;
}
public DirContext getReadWriteContext() throws DataAccessException {
return ctx;
}
}

View File

@@ -106,4 +106,6 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapServerTestCase {
// assertEquals("uid=ben,ou=people,dc=acegisecurity,dc=org", ben.getDn());
}
// TODO: Add test with non-uid username
}

View File

@@ -26,8 +26,6 @@ import javax.naming.directory.DirContext;
/**
*
DOCUMENT ME!
*
* @author Luke Taylor
* @version $Id$
@@ -46,15 +44,16 @@ public class PasswordComparisonAuthenticatorMockTests extends MockObjectTestCase
// Get the mock to return an empty attribute set
mockCtx.expects(atLeastOnce()).method("getNameInNamespace").will(returnValue("dc=acegisecurity,dc=org"));
mockCtx.expects(once()).method("lookup").with(eq("cn=Bob,ou=people")).will(returnValue(true));
mockCtx.expects(once()).method("getAttributes").with(eq("cn=Bob,ou=people"), NULL)
mockCtx.expects(once()).method("lookup").with(eq("cn=Bob, ou=people")).will(returnValue(true));
mockCtx.expects(once()).method("getAttributes").with(eq("cn=Bob, ou=people"), NULL)
.will(returnValue(new BasicAttributes()));
// Setup a single return value (i.e. success)
Attributes searchResults = new BasicAttributes("", null);
mockCtx.expects(once()).method("search")
.with(eq("cn=Bob,ou=people"), eq("(userPassword={0})"), NOT_NULL, NOT_NULL)
.will(returnValue(searchResults.getAll()));
mockCtx.expects(once())
.method("search")
.with(eq("cn=Bob, ou=people"), eq("(userPassword={0})"), NOT_NULL, NOT_NULL)
.will(returnValue(searchResults.getAll()));
mockCtx.expects(atLeastOnce()).method("close");
authenticator.authenticate("Bob", "bobspassword");
}