Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

GitHubSettingsTest.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.github;
  21. import org.junit.Test;
  22. import org.sonar.api.config.PropertyDefinitions;
  23. import org.sonar.api.config.internal.MapSettings;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.sonar.auth.github.GitHubSettings.LOGIN_STRATEGY_DEFAULT_VALUE;
  26. import static org.sonar.auth.github.GitHubSettings.LOGIN_STRATEGY_PROVIDER_ID;
  27. public class GitHubSettingsTest {
  28. private MapSettings settings = new MapSettings(new PropertyDefinitions(GitHubSettings.definitions()));
  29. private GitHubSettings underTest = new GitHubSettings(settings.asConfig());
  30. @Test
  31. public void is_enabled() {
  32. settings.setProperty("sonar.auth.github.clientId.secured", "id");
  33. settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
  34. settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_DEFAULT_VALUE);
  35. settings.setProperty("sonar.auth.github.enabled", true);
  36. assertThat(underTest.isEnabled()).isTrue();
  37. settings.setProperty("sonar.auth.github.enabled", false);
  38. assertThat(underTest.isEnabled()).isFalse();
  39. }
  40. @Test
  41. public void is_enabled_always_return_false_when_client_id_is_null() {
  42. settings.setProperty("sonar.auth.github.enabled", true);
  43. settings.setProperty("sonar.auth.github.clientId.secured", (String) null);
  44. settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
  45. settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_DEFAULT_VALUE);
  46. assertThat(underTest.isEnabled()).isFalse();
  47. }
  48. @Test
  49. public void is_enabled_always_return_false_when_client_secret_is_null() {
  50. settings.setProperty("sonar.auth.github.enabled", true);
  51. settings.setProperty("sonar.auth.github.clientId.secured", "id");
  52. settings.setProperty("sonar.auth.github.clientSecret.secured", (String) null);
  53. settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_DEFAULT_VALUE);
  54. assertThat(underTest.isEnabled()).isFalse();
  55. }
  56. @Test
  57. public void return_client_id() {
  58. settings.setProperty("sonar.auth.github.clientId.secured", "id");
  59. assertThat(underTest.clientId()).isEqualTo("id");
  60. }
  61. @Test
  62. public void return_client_secret() {
  63. settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
  64. assertThat(underTest.clientSecret()).isEqualTo("secret");
  65. }
  66. @Test
  67. public void return_login_strategy() {
  68. settings.setProperty("sonar.auth.github.loginStrategy", LOGIN_STRATEGY_PROVIDER_ID);
  69. assertThat(underTest.loginStrategy()).isEqualTo(LOGIN_STRATEGY_PROVIDER_ID);
  70. }
  71. @Test
  72. public void allow_users_to_sign_up() {
  73. settings.setProperty("sonar.auth.github.allowUsersToSignUp", "true");
  74. assertThat(underTest.allowUsersToSignUp()).isTrue();
  75. settings.setProperty("sonar.auth.github.allowUsersToSignUp", "false");
  76. assertThat(underTest.allowUsersToSignUp()).isFalse();
  77. // default value
  78. settings.setProperty("sonar.auth.github.allowUsersToSignUp", (String) null);
  79. assertThat(underTest.allowUsersToSignUp()).isTrue();
  80. }
  81. @Test
  82. public void sync_groups() {
  83. settings.setProperty("sonar.auth.github.groupsSync", "true");
  84. assertThat(underTest.syncGroups()).isTrue();
  85. settings.setProperty("sonar.auth.github.groupsSync", "false");
  86. assertThat(underTest.syncGroups()).isFalse();
  87. // default value
  88. settings.setProperty("sonar.auth.github.groupsSync", (String) null);
  89. assertThat(underTest.syncGroups()).isFalse();
  90. }
  91. @Test
  92. public void apiUrl_must_have_ending_slash() {
  93. settings.setProperty("sonar.auth.github.apiUrl", "https://github.com");
  94. assertThat(underTest.apiURL()).isEqualTo("https://github.com/");
  95. settings.setProperty("sonar.auth.github.apiUrl", "https://github.com/");
  96. assertThat(underTest.apiURL()).isEqualTo("https://github.com/");
  97. }
  98. @Test
  99. public void webUrl_must_have_ending_slash() {
  100. settings.setProperty("sonar.auth.github.webUrl", "https://github.com");
  101. assertThat(underTest.webURL()).isEqualTo("https://github.com/");
  102. settings.setProperty("sonar.auth.github.webUrl", "https://github.com/");
  103. assertThat(underTest.webURL()).isEqualTo("https://github.com/");
  104. }
  105. @Test
  106. public void return_organizations_single() {
  107. String setting = "example";
  108. settings.setProperty("sonar.auth.github.organizations", setting);
  109. String[] expected = new String[] {"example"};
  110. String[] actual = underTest.organizations();
  111. assertThat(actual).isEqualTo(expected);
  112. }
  113. @Test
  114. public void return_organizations_multiple() {
  115. String setting = "example0,example1";
  116. settings.setProperty("sonar.auth.github.organizations", setting);
  117. String[] expected = new String[] {"example0", "example1"};
  118. String[] actual = underTest.organizations();
  119. assertThat(actual).isEqualTo(expected);
  120. }
  121. @Test
  122. public void return_organizations_empty_list() {
  123. String[] setting = null;
  124. settings.setProperty("sonar.auth.github.organizations", setting);
  125. String[] expected = new String[] {};
  126. String[] actual = underTest.organizations();
  127. assertThat(actual).isEqualTo(expected);
  128. }
  129. @Test
  130. public void definitions() {
  131. assertThat(GitHubSettings.definitions()).hasSize(9);
  132. }
  133. }