You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LdapUserServiceTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2012 John Crygier
  3. * Copyright 2012 gitblit.com
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package com.gitblit.tests;
  18. import static org.junit.Assert.*;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import com.gitblit.LdapUserService;
  24. import com.gitblit.models.UserModel;
  25. import com.gitblit.tests.mock.MemorySettings;
  26. import com.unboundid.ldap.listener.InMemoryDirectoryServer;
  27. import com.unboundid.ldap.listener.InMemoryDirectoryServerConfig;
  28. import com.unboundid.ldap.listener.InMemoryListenerConfig;
  29. import com.unboundid.ldap.sdk.LDAPConnection;
  30. import com.unboundid.ldif.LDIFReader;
  31. /**
  32. * An Integration test for LDAP that tests going against an in-memory UnboundID
  33. * LDAP server.
  34. *
  35. * @author jcrygier
  36. *
  37. */
  38. public class LdapUserServiceTest {
  39. private LdapUserService ldapUserService;
  40. @Before
  41. public void createInMemoryLdapServer() throws Exception {
  42. InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig("dc=MyDomain");
  43. config.addAdditionalBindCredentials("cn=Directory Manager", "password");
  44. config.setListenerConfigs(InMemoryListenerConfig.createLDAPConfig("default", 389));
  45. config.setSchema(null);
  46. InMemoryDirectoryServer ds = new InMemoryDirectoryServer(config);
  47. ds.importFromLDIF(true, new LDIFReader(this.getClass().getResourceAsStream("resources/ldapUserServiceSampleData.ldif")));
  48. ds.startListening();
  49. }
  50. @Before
  51. public void createLdapUserService() {
  52. Map<Object, Object> backingMap = new HashMap<Object, Object>();
  53. backingMap.put("realm.ldap.server", "ldap://localhost:389");
  54. backingMap.put("realm.ldap.domain", "");
  55. backingMap.put("realm.ldap.username", "cn=Directory Manager");
  56. backingMap.put("realm.ldap.password", "password");
  57. backingMap.put("realm.ldap.backingUserService", "users.conf");
  58. backingMap.put("realm.ldap.maintainTeams", "true");
  59. backingMap.put("realm.ldap.accountBase", "OU=Users,OU=UserControl,OU=MyOrganization,DC=MyDomain");
  60. backingMap.put("realm.ldap.accountPattern", "(&(objectClass=person)(sAMAccountName=${username}))");
  61. backingMap.put("realm.ldap.groupBase", "OU=Groups,OU=UserControl,OU=MyOrganization,DC=MyDomain");
  62. backingMap.put("realm.ldap.groupPattern", "(&(objectClass=group)(member=${dn}))");
  63. backingMap.put("realm.ldap.admins", "UserThree @Git_Admins");
  64. MemorySettings ms = new MemorySettings(backingMap);
  65. ldapUserService = new LdapUserService();
  66. ldapUserService.setup(ms);
  67. }
  68. @Test
  69. public void testAuthenticate() {
  70. UserModel userOneModel = ldapUserService.authenticate("UserOne", "userOnePassword".toCharArray());
  71. assertNotNull(userOneModel);
  72. assertNotNull(userOneModel.getTeam("git_admins"));
  73. assertNotNull(userOneModel.getTeam("git_users"));
  74. assertTrue(userOneModel.canAdmin);
  75. UserModel userOneModelFailedAuth = ldapUserService.authenticate("UserOne", "userTwoPassword".toCharArray());
  76. assertNull(userOneModelFailedAuth);
  77. UserModel userTwoModel = ldapUserService.authenticate("UserTwo", "userTwoPassword".toCharArray());
  78. assertNotNull(userTwoModel);
  79. assertNotNull(userTwoModel.getTeam("git_users"));
  80. assertNull(userTwoModel.getTeam("git_admins"));
  81. assertFalse(userTwoModel.canAdmin);
  82. UserModel userThreeModel = ldapUserService.authenticate("UserThree", "userThreePassword".toCharArray());
  83. assertNotNull(userThreeModel);
  84. assertNotNull(userThreeModel.getTeam("git_users"));
  85. assertNull(userThreeModel.getTeam("git_admins"));
  86. assertTrue(userThreeModel.canAdmin);
  87. }
  88. }