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.

LdapUserMappingTest.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth.ldap;
  21. import org.junit.Test;
  22. import org.sonar.api.config.internal.MapSettings;
  23. import static org.assertj.core.api.Assertions.assertThat;
  24. public class LdapUserMappingTest {
  25. @Test
  26. public void defaults() {
  27. LdapUserMapping userMapping = new LdapUserMapping(new MapSettings(), "ldap");
  28. assertThat(userMapping.getBaseDn()).isNull();
  29. assertThat(userMapping.getRequest()).isEqualTo("(&(objectClass=inetOrgPerson)(uid={0}))");
  30. assertThat(userMapping.getRealNameAttribute()).isEqualTo("cn");
  31. assertThat(userMapping.getEmailAttribute()).isEqualTo("mail");
  32. assertThat(userMapping.toString()).isEqualTo("LdapUserMapping{" +
  33. "baseDn=null," +
  34. " request=(&(objectClass=inetOrgPerson)(uid={0}))," +
  35. " realNameAttribute=cn," +
  36. " emailAttribute=mail}");
  37. }
  38. @Test
  39. public void activeDirectory() {
  40. MapSettings settings = new MapSettings()
  41. .setProperty("ldap.user.baseDn", "cn=users")
  42. .setProperty("ldap.user.objectClass", "user")
  43. .setProperty("ldap.user.loginAttribute", "sAMAccountName");
  44. LdapUserMapping userMapping = new LdapUserMapping(settings, "ldap");
  45. LdapSearch search = userMapping.createSearch(null, "tester");
  46. assertThat(search.getBaseDn()).isEqualTo("cn=users");
  47. assertThat(search.getRequest()).isEqualTo("(&(objectClass=user)(sAMAccountName={0}))");
  48. assertThat(search.getParameters()).isEqualTo(new String[] {"tester"});
  49. assertThat(search.getReturningAttributes()).isNull();
  50. assertThat(userMapping.toString()).isEqualTo("LdapUserMapping{" +
  51. "baseDn=cn=users," +
  52. " request=(&(objectClass=user)(sAMAccountName={0}))," +
  53. " realNameAttribute=cn," +
  54. " emailAttribute=mail}");
  55. }
  56. @Test
  57. public void realm() {
  58. MapSettings settings = new MapSettings()
  59. .setProperty("ldap.realm", "example.org")
  60. .setProperty("ldap.userObjectClass", "user")
  61. .setProperty("ldap.loginAttribute", "sAMAccountName");
  62. LdapUserMapping userMapping = new LdapUserMapping(settings, "ldap");
  63. assertThat(userMapping.getBaseDn()).isEqualTo("dc=example,dc=org");
  64. }
  65. }