From e82dfd3f1a24efdfd372fbbee9c83d84041778ff Mon Sep 17 00:00:00 2001 From: Luke Taylor Date: Thu, 31 Jan 2008 17:44:52 +0000 Subject: [PATCH] Added some further tests for LDAP searching with a different user search base. --- ...apUserServiceBeanDefinitionParserTests.java | 10 ++++++++++ .../search/FilterBasedLdapUserSearchTests.java | 18 ++++++++++++------ core/src/test/resources/test-server.ldif | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java b/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java index 3dbf5efd15..357fdb7c37 100644 --- a/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java +++ b/core/src/test/java/org/springframework/security/config/LdapUserServiceBeanDefinitionParserTests.java @@ -38,6 +38,16 @@ public class LdapUserServiceBeanDefinitionParserTests { assertEquals(2, ben.getAuthorities().length); } + @Test + public void differentUserSearchBaseWorksAsExpected() throws Exception { + setContext(""); + + UserDetailsService uds = (UserDetailsService) appCtx.getBean("ldapUDS"); + UserDetails joe = uds.loadUserByUsername("Joe Smeth"); + + assertEquals("Joe Smeth", joe.getUsername()); + } + private void setContext(String context) { appCtx = new InMemoryXmlApplicationContext(context); } diff --git a/core/src/test/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearchTests.java b/core/src/test/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearchTests.java index 7559b38782..1e43fa9c02 100644 --- a/core/src/test/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearchTests.java +++ b/core/src/test/java/org/springframework/security/ldap/search/FilterBasedLdapUserSearchTests.java @@ -44,7 +44,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests } @Test - public void testBasicSearch() { + public void basicSearchSucceeds() { FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory); locator.setSearchSubtree(false); locator.setSearchTimeLimit(0); @@ -58,7 +58,7 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests // Try some funny business with filters. @Test - public void testExtraFilterPartToExcludeBob() throws Exception { + public void extraFilterPartToExcludeBob() throws Exception { FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(&(cn=*)(!(|(uid={0})(uid=rod))))", dirCtxFactory); @@ -70,19 +70,19 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests } @Test(expected=IncorrectResultSizeDataAccessException.class) - public void testFailsOnMultipleMatches() { + public void searchFailsOnMultipleMatches() { FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(cn=*)", dirCtxFactory); locator.searchForUser("Ignored"); } @Test(expected=UsernameNotFoundException.class) - public void testSearchForInvalidUserFails() { + public void searchForInvalidUserFails() { FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=people", "(uid={0})", dirCtxFactory); locator.searchForUser("Joe"); } @Test - public void testSubTreeSearchSucceeds() { + public void subTreeSearchSucceeds() { // Don't set the searchBase, so search from the root. FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("", "(cn={0})", dirCtxFactory); locator.setSearchSubtree(true); @@ -93,5 +93,11 @@ public class FilterBasedLdapUserSearchTests extends AbstractLdapIntegrationTests assertEquals(new DistinguishedName("uid=ben,ou=people"), ben.getDn()); } - // TODO: Add test with non-uid username + @Test + public void searchWithDifferentSearchBaseIsSuccessful() throws Exception { + FilterBasedLdapUserSearch locator = new FilterBasedLdapUserSearch("ou=otherpeople", "(cn={0})", dirCtxFactory); + DirContextOperations joe = locator.searchForUser("Joe Smeth"); + assertEquals("Joe Smeth", joe.getStringAttribute("cn")); + } + } diff --git a/core/src/test/resources/test-server.ldif b/core/src/test/resources/test-server.ldif index f2caf17ca8..ebb2edc9d3 100644 --- a/core/src/test/resources/test-server.ldif +++ b/core/src/test/resources/test-server.ldif @@ -13,6 +13,11 @@ objectclass: top objectclass: organizationalUnit ou: people +dn: ou=otherpeople,dc=springframework,dc=org +objectclass: top +objectclass: organizationalUnit +ou: otherpeople + dn: uid=ben,ou=people,dc=springframework,dc=org objectclass: top objectclass: person @@ -33,6 +38,16 @@ sn: Hamilton uid: bob userPassword: bobspassword +dn: uid=joe,ou=otherpeople,dc=springframework,dc=org +objectclass: top +objectclass: person +objectclass: organizationalPerson +objectclass: inetOrgPerson +cn: Joe Smeth +sn: Smeth +uid: joe +userPassword: joespassword + dn: cn=developers,ou=groups,dc=springframework,dc=org objectclass: top objectclass: groupOfNames