From afe3f1fd5ba22bd8b5ae017db5f61982726b656c Mon Sep 17 00:00:00 2001 From: Alfred Schmid Date: Fri, 31 Jan 2014 13:36:53 +0100 Subject: [PATCH] Fixed Tests, use local copy of users.conf. Initialize reset ldap before every test. --- .../gitblit/tests/LdapAuthenticationTest.java | 43 +++++++++++++------ src/test/resources/ldap/adduser.ldif | 12 +++--- src/test/resources/ldap/sampledata.ldif | 12 ++++++ 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/src/test/java/com/gitblit/tests/LdapAuthenticationTest.java b/src/test/java/com/gitblit/tests/LdapAuthenticationTest.java index 4c78643d..ce3615e0 100644 --- a/src/test/java/com/gitblit/tests/LdapAuthenticationTest.java +++ b/src/test/java/com/gitblit/tests/LdapAuthenticationTest.java @@ -16,13 +16,17 @@ */ package com.gitblit.tests; +import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.Map; +import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TemporaryFolder; import com.gitblit.Constants.AccountType; import com.gitblit.IStoredSettings; @@ -47,10 +51,14 @@ import com.unboundid.ldif.LDIFReader; * */ public class LdapAuthenticationTest extends GitblitUnitTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); private static final String RESOURCE_DIR = "src/test/resources/ldap/"; - private LdapAuthProvider ldap; + private File usersConf; + + private LdapAuthProvider ldap; static int ldapPort = 1389; @@ -58,6 +66,8 @@ public class LdapAuthenticationTest extends GitblitUnitTest { private IUserManager userManager; + private MemorySettings settings; + @BeforeClass public static void createInMemoryLdapServer() throws Exception { InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=MyDomain"); @@ -66,13 +76,17 @@ public class LdapAuthenticationTest extends GitblitUnitTest { config.setSchema(null); ds = new InMemoryDirectoryServer(config); - ds.importFromLDIF(true, new LDIFReader(new FileInputStream(RESOURCE_DIR + "sampledata.ldif"))); ds.startListening(); } @Before - public void newLdapAuthentication() { - ldap = newLdapAuthentication(getSettings()); + public void init() throws Exception { + ds.clear(); + ds.importFromLDIF(true, new LDIFReader(new FileInputStream(RESOURCE_DIR + "sampledata.ldif"))); + usersConf = folder.newFile("users.conf"); + FileUtils.copyFile(new File(RESOURCE_DIR + "users.conf"), usersConf); + settings = getSettings(); + ldap = newLdapAuthentication(settings); } public LdapAuthProvider newLdapAuthentication(IStoredSettings settings) { @@ -85,7 +99,7 @@ public class LdapAuthenticationTest extends GitblitUnitTest { private MemorySettings getSettings() { Map backingMap = new HashMap(); - backingMap.put("realm.userService", RESOURCE_DIR + "users.conf"); + backingMap.put("realm.userService", usersConf.getAbsolutePath()); backingMap.put("realm.ldap.server", "ldap://localhost:" + ldapPort); backingMap.put("realm.ldap.domain", ""); backingMap.put("realm.ldap.username", "cn=Directory Manager"); @@ -99,9 +113,7 @@ public class LdapAuthenticationTest extends GitblitUnitTest { backingMap.put("realm.ldap.admins", "UserThree @Git_Admins \"@Git Admins\""); backingMap.put("realm.ldap.displayName", "displayName"); backingMap.put("realm.ldap.email", "email"); - backingMap.put("realm.ldap.synchronizeUsers.enable", "true"); backingMap.put("realm.ldap.uid", "sAMAccountName"); - backingMap.put("realm.ldap.ldapCachePeriod", "0 MINUTES"); MemorySettings ms = new MemorySettings(backingMap); return ms; @@ -174,21 +186,28 @@ public class LdapAuthenticationTest extends GitblitUnitTest { } @Test - public void checkIfSevenUsersLoadedFromLdap() throws Exception { + public void checkIfUsersConfContainsAllUsersFromSampleDataLdif() throws Exception { SearchResult searchResult = ds.search("OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain", SearchScope.SUB, "objectClass=person"); assertEquals("Number of ldap users in gitblit user model", searchResult.getEntryCount(), countLdapUsersInUserManager()); } @Test - public void addingUserInLdapShouldUpdateGitBlitUsersAndGroups() throws Exception { + public void addingUserInLdapShouldNotUpdateGitBlitUsersAndGroups() throws Exception { + settings.put("realm.ldap.ldapCachePeriod", "0 MINUTES"); ds.addEntries(LDIFReader.readEntries(RESOURCE_DIR + "adduser.ldif")); - for(String user : userManager.getAllUsernames()) { - System.out.println(user); - } ldap.synchronizeWithLdapService(); assertEquals("Number of ldap users in gitblit user model", 5, countLdapUsersInUserManager()); } + @Test + public void addingUserInLdapShouldUpdateGitBlitUsersAndGroups() throws Exception { + settings.put("realm.ldap.synchronizeUsers.enable", "true"); + settings.put("realm.ldap.ldapCachePeriod", "0 MINUTES"); + ds.addEntries(LDIFReader.readEntries(RESOURCE_DIR + "adduser.ldif")); + ldap.synchronizeWithLdapService(); + assertEquals("Number of ldap users in gitblit user model", 6, countLdapUsersInUserManager()); + } + private int countLdapUsersInUserManager() { int ldapAccountCount = 0; for (UserModel userModel : userManager.getAllUsers()) { diff --git a/src/test/resources/ldap/adduser.ldif b/src/test/resources/ldap/adduser.ldif index e7e3520b..b007a940 100644 --- a/src/test/resources/ldap/adduser.ldif +++ b/src/test/resources/ldap/adduser.ldif @@ -1,11 +1,11 @@ -dn: CN=UserFive,OU=Canada,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain +dn: CN=UserSix,OU=Canada,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain objectClass: user objectClass: person -sAMAccountName: UserFive -userPassword: userFivePassword -displayName: User Five +sAMAccountName: UserSix +userPassword: userSixPassword +displayName: User Six givenName: User -surname: Five +surname: Six personalTitle: Miss -email: userfive@gitblit.com +email: usersix@gitblit.com memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain \ No newline at end of file diff --git a/src/test/resources/ldap/sampledata.ldif b/src/test/resources/ldap/sampledata.ldif index df79333e..6254f19c 100644 --- a/src/test/resources/ldap/sampledata.ldif +++ b/src/test/resources/ldap/sampledata.ldif @@ -105,4 +105,16 @@ givenName: User surname: Four personalTitle: Miss email: userfour@gitblit.com +memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain + +dn: CN=UserFive,OU=Canada,OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain +objectClass: user +objectClass: person +sAMAccountName: UserFive +userPassword: userFivePassword +displayName: User Five +givenName: User +surname: Five +personalTitle: Miss +email: userfive@gitblit.com memberOf: CN=Git_Users,OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain \ No newline at end of file -- 2.39.5