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.

SamlSettingsTest.java 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.saml;
  21. import com.tngtech.java.junit.dataprovider.DataProvider;
  22. import com.tngtech.java.junit.dataprovider.DataProviderRunner;
  23. import com.tngtech.java.junit.dataprovider.UseDataProvider;
  24. import org.junit.Test;
  25. import org.junit.runner.RunWith;
  26. import org.sonar.api.config.PropertyDefinitions;
  27. import org.sonar.api.config.internal.MapSettings;
  28. import org.sonar.api.utils.System2;
  29. import static org.assertj.core.api.Assertions.assertThat;
  30. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  31. @RunWith(DataProviderRunner.class)
  32. public class SamlSettingsTest {
  33. private MapSettings settings = new MapSettings(new PropertyDefinitions(System2.INSTANCE, SamlSettings.definitions()));
  34. private SamlSettings underTest = new SamlSettings(settings.asConfig());
  35. @Test
  36. public void return_application_id() {
  37. settings.setProperty("sonar.auth.saml.applicationId", "MyApp");
  38. assertThat(underTest.getApplicationId()).isEqualTo("MyApp");
  39. }
  40. @Test
  41. public void return_default_value_of_application_id() {
  42. assertThat(underTest.getApplicationId()).isEqualTo("sonarqube");
  43. }
  44. @Test
  45. public void return_provider_name() {
  46. settings.setProperty("sonar.auth.saml.providerName", "MyProviderName");
  47. assertThat(underTest.getProviderName()).isEqualTo("MyProviderName");
  48. }
  49. @Test
  50. public void return_default_value_of_application_name() {
  51. assertThat(underTest.getProviderName()).isEqualTo("SAML");
  52. }
  53. @Test
  54. public void return_provider_id() {
  55. settings.setProperty("sonar.auth.saml.applicationId", "http://localhost:8080/auth/realms/sonarqube");
  56. assertThat(underTest.getApplicationId()).isEqualTo("http://localhost:8080/auth/realms/sonarqube");
  57. }
  58. @Test
  59. public void return_login_url() {
  60. settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/");
  61. assertThat(underTest.getLoginUrl()).isEqualTo("http://localhost:8080/");
  62. settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080");
  63. assertThat(underTest.getLoginUrl()).isEqualTo("http://localhost:8080");
  64. }
  65. @Test
  66. public void return_certificate() {
  67. settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
  68. assertThat(underTest.getCertificate()).isEqualTo("ABCDEFG");
  69. }
  70. @Test
  71. public void return_user_login_attribute() {
  72. settings.setProperty("sonar.auth.saml.user.login", "userLogin");
  73. assertThat(underTest.getUserLogin()).isEqualTo("userLogin");
  74. }
  75. @Test
  76. public void return_user_name_attribute() {
  77. settings.setProperty("sonar.auth.saml.user.name", "userName");
  78. assertThat(underTest.getUserName()).isEqualTo("userName");
  79. }
  80. @Test
  81. public void return_user_email_attribute() {
  82. settings.setProperty("sonar.auth.saml.user.email", "userEmail");
  83. assertThat(underTest.getUserEmail().get()).isEqualTo("userEmail");
  84. }
  85. @Test
  86. public void return_empty_user_email_when_no_setting() {
  87. assertThat(underTest.getUserEmail()).isNotPresent();
  88. }
  89. @Test
  90. public void return_group_name_attribute() {
  91. settings.setProperty("sonar.auth.saml.group.name", "groupName");
  92. assertThat(underTest.getGroupName().get()).isEqualTo("groupName");
  93. }
  94. @Test
  95. public void return_empty_group_name_when_no_setting() {
  96. assertThat(underTest.getGroupName()).isNotPresent();
  97. }
  98. @Test
  99. public void is_enabled() {
  100. settings.setProperty("sonar.auth.saml.applicationId", "MyApp");
  101. settings.setProperty("sonar.auth.saml.providerId", "http://localhost:8080/auth/realms/sonarqube");
  102. settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/auth/realms/sonarqube/protocol/saml");
  103. settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
  104. settings.setProperty("sonar.auth.saml.user.login", "login");
  105. settings.setProperty("sonar.auth.saml.user.name", "name");
  106. settings.setProperty("sonar.auth.saml.enabled", true);
  107. assertThat(underTest.isEnabled()).isTrue();
  108. settings.setProperty("sonar.auth.saml.enabled", false);
  109. assertThat(underTest.isEnabled()).isFalse();
  110. }
  111. @Test
  112. public void is_enabled_using_default_values() {
  113. settings.setProperty("sonar.auth.saml.providerId", "http://localhost:8080/auth/realms/sonarqube");
  114. settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/auth/realms/sonarqube/protocol/saml");
  115. settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
  116. settings.setProperty("sonar.auth.saml.user.login", "login");
  117. settings.setProperty("sonar.auth.saml.user.name", "name");
  118. settings.setProperty("sonar.auth.saml.enabled", true);
  119. assertThat(underTest.isEnabled()).isTrue();
  120. }
  121. @DataProvider
  122. public static Object[][] settingsRequiredToEnablePlugin() {
  123. return new Object[][] {
  124. {"sonar.auth.saml.providerId"},
  125. {"sonar.auth.saml.loginUrl"},
  126. {"sonar.auth.saml.certificate.secured"},
  127. {"sonar.auth.saml.user.login"},
  128. {"sonar.auth.saml.user.name"},
  129. {"sonar.auth.saml.enabled"},
  130. };
  131. }
  132. @Test
  133. @UseDataProvider("settingsRequiredToEnablePlugin")
  134. public void is_enabled_return_false_when_one_required_setting_is_missing(String setting) {
  135. initAllSettings();
  136. settings.setProperty(setting, (String) null);
  137. assertThat(underTest.isEnabled()).isFalse();
  138. }
  139. @Test
  140. public void fail_to_get_provider_id_when_null() {
  141. assertThatThrownBy(() -> underTest.getProviderId())
  142. .isInstanceOf(IllegalArgumentException.class)
  143. .hasMessage("Provider ID is missing");
  144. }
  145. @Test
  146. public void fail_to_get_login_url_when_null() {
  147. assertThatThrownBy(() -> underTest.getLoginUrl())
  148. .isInstanceOf(IllegalArgumentException.class)
  149. .hasMessage("Login URL is missing");
  150. }
  151. @Test
  152. public void fail_to_get_certificate_when_null() {
  153. assertThatThrownBy(() -> underTest.getCertificate())
  154. .isInstanceOf(IllegalArgumentException.class)
  155. .hasMessage("Certificate is missing");
  156. }
  157. @Test
  158. public void fail_to_get_user_login_attribute_when_null() {
  159. assertThatThrownBy(() -> underTest.getUserLogin())
  160. .isInstanceOf(IllegalArgumentException.class)
  161. .hasMessage("User login attribute is missing");
  162. }
  163. @Test
  164. public void fail_to_get_user_name_attribute_when_null() {
  165. assertThatThrownBy(() -> underTest.getUserName())
  166. .isInstanceOf(IllegalArgumentException.class)
  167. .hasMessage("User name attribute is missing");
  168. }
  169. private void initAllSettings() {
  170. settings.setProperty("sonar.auth.saml.applicationId", "MyApp");
  171. settings.setProperty("sonar.auth.saml.providerId", "http://localhost:8080/auth/realms/sonarqube");
  172. settings.setProperty("sonar.auth.saml.loginUrl", "http://localhost:8080/auth/realms/sonarqube/protocol/saml");
  173. settings.setProperty("sonar.auth.saml.certificate.secured", "ABCDEFG");
  174. settings.setProperty("sonar.auth.saml.user.login", "login");
  175. settings.setProperty("sonar.auth.saml.user.name", "name");
  176. settings.setProperty("sonar.auth.saml.enabled", true);
  177. }
  178. }