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.

LdapRealmTest.java 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 javax.servlet.http.HttpServletRequest;
  22. import org.junit.ClassRule;
  23. import org.junit.Test;
  24. import org.mockito.Mockito;
  25. import org.sonar.api.config.internal.MapSettings;
  26. import org.sonar.api.security.ExternalGroupsProvider;
  27. import org.sonar.api.security.ExternalUsersProvider;
  28. import org.sonar.auth.ldap.server.LdapServer;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.junit.Assert.fail;
  31. public class LdapRealmTest {
  32. @ClassRule
  33. public static LdapServer server = new LdapServer("/users.example.org.ldif");
  34. @Test
  35. public void normal() {
  36. MapSettings settings = new MapSettings()
  37. .setProperty("ldap.url", server.getUrl());
  38. LdapRealm realm = new LdapRealm(new LdapSettingsManager(settings, new LdapAutodiscovery()));
  39. assertThat(realm.getName()).isEqualTo("LDAP");
  40. realm.init();
  41. assertThat(realm.doGetAuthenticator()).isInstanceOf(LdapAuthenticator.class);
  42. assertThat(realm.getUsersProvider()).isInstanceOf(ExternalUsersProvider.class).isInstanceOf(LdapUsersProvider.class);
  43. assertThat(realm.getGroupsProvider()).isNull();
  44. }
  45. @Test
  46. public void noConnection() {
  47. MapSettings settings = new MapSettings()
  48. .setProperty("ldap.url", "ldap://no-such-host")
  49. .setProperty("ldap.group.baseDn", "cn=groups,dc=example,dc=org");
  50. LdapRealm realm = new LdapRealm(new LdapSettingsManager(settings, new LdapAutodiscovery()));
  51. assertThat(realm.getName()).isEqualTo("LDAP");
  52. try {
  53. realm.init();
  54. fail("Since there is no connection, the init method has to throw an exception.");
  55. } catch (LdapException e) {
  56. assertThat(e).hasMessage("Unable to open LDAP connection");
  57. }
  58. assertThat(realm.doGetAuthenticator()).isInstanceOf(LdapAuthenticator.class);
  59. assertThat(realm.getUsersProvider()).isInstanceOf(ExternalUsersProvider.class).isInstanceOf(LdapUsersProvider.class);
  60. assertThat(realm.getGroupsProvider()).isInstanceOf(ExternalGroupsProvider.class).isInstanceOf(LdapGroupsProvider.class);
  61. try {
  62. realm.getUsersProvider().doGetUserDetails(new ExternalUsersProvider.Context("tester", Mockito.mock(HttpServletRequest.class)));
  63. fail("Since there is no connection, the doGetUserDetails method has to throw an exception.");
  64. } catch (LdapException e) {
  65. assertThat(e.getMessage()).contains("Unable to retrieve details for user tester");
  66. }
  67. try {
  68. realm.getGroupsProvider().doGetGroups(new ExternalGroupsProvider.Context("tester", Mockito.mock(HttpServletRequest.class)));
  69. fail("Since there is no connection, the doGetGroups method has to throw an exception.");
  70. } catch (LdapException e) {
  71. assertThat(e.getMessage()).contains("Unable to retrieve details for user tester");
  72. }
  73. }
  74. }