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.

GitHubIdentityProviderTest.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.github;
  21. import org.junit.Test;
  22. import org.sonar.api.config.internal.MapSettings;
  23. import org.sonar.api.server.authentication.OAuth2IdentityProvider;
  24. import static org.assertj.core.api.Assertions.assertThat;
  25. import static org.assertj.core.api.Assertions.assertThatThrownBy;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.verify;
  28. import static org.mockito.Mockito.when;
  29. public class GitHubIdentityProviderTest {
  30. private MapSettings settings = new MapSettings();
  31. private GitHubSettings gitHubSettings = new GitHubSettings(settings.asConfig());
  32. private UserIdentityFactoryImpl userIdentityFactory = mock(UserIdentityFactoryImpl.class);
  33. private ScribeGitHubApi scribeApi = new ScribeGitHubApi(gitHubSettings);
  34. private GitHubRestClient gitHubRestClient = new GitHubRestClient(gitHubSettings);
  35. private GitHubIdentityProvider underTest = new GitHubIdentityProvider(gitHubSettings, userIdentityFactory, scribeApi, gitHubRestClient);
  36. @Test
  37. public void check_fields() {
  38. assertThat(underTest.getKey()).isEqualTo("github");
  39. assertThat(underTest.getName()).isEqualTo("GitHub");
  40. assertThat(underTest.getDisplay().getIconPath()).isEqualTo("/images/github.svg");
  41. assertThat(underTest.getDisplay().getBackgroundColor()).isEqualTo("#444444");
  42. }
  43. @Test
  44. public void is_enabled() {
  45. settings.setProperty("sonar.auth.github.clientId.secured", "id");
  46. settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
  47. settings.setProperty("sonar.auth.github.enabled", true);
  48. assertThat(underTest.isEnabled()).isTrue();
  49. settings.setProperty("sonar.auth.github.enabled", false);
  50. assertThat(underTest.isEnabled()).isFalse();
  51. }
  52. @Test
  53. public void should_allow_users_to_signup() {
  54. assertThat(underTest.allowsUsersToSignUp()).as("default").isFalse();
  55. settings.setProperty("sonar.auth.github.allowUsersToSignUp", true);
  56. assertThat(underTest.allowsUsersToSignUp()).isTrue();
  57. }
  58. @Test
  59. public void init() {
  60. setSettings(true);
  61. OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
  62. when(context.generateCsrfState()).thenReturn("state");
  63. when(context.getCallbackUrl()).thenReturn("http://localhost/callback");
  64. settings.setProperty("sonar.auth.github.webUrl", "https://github.com/");
  65. underTest.init(context);
  66. verify(context).redirectTo("https://github.com/login/oauth/authorize" +
  67. "?response_type=code" +
  68. "&client_id=id" +
  69. "&redirect_uri=http%3A%2F%2Flocalhost%2Fcallback&scope=user%3Aemail" +
  70. "&state=state");
  71. }
  72. @Test
  73. public void init_when_group_sync() {
  74. setSettings(true);
  75. settings.setProperty("sonar.auth.github.groupsSync", "true");
  76. settings.setProperty("sonar.auth.github.webUrl", "https://github.com/");
  77. OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
  78. when(context.generateCsrfState()).thenReturn("state");
  79. when(context.getCallbackUrl()).thenReturn("http://localhost/callback");
  80. underTest.init(context);
  81. verify(context).redirectTo("https://github.com/login/oauth/authorize" +
  82. "?response_type=code" +
  83. "&client_id=id" +
  84. "&redirect_uri=http%3A%2F%2Flocalhost%2Fcallback&scope=user%3Aemail%2Cread%3Aorg" +
  85. "&state=state");
  86. }
  87. @Test
  88. public void init_when_organizations() {
  89. setSettings(true);
  90. settings.setProperty("sonar.auth.github.organizations", "example");
  91. settings.setProperty("sonar.auth.github.webUrl", "https://github.com/");
  92. OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
  93. when(context.generateCsrfState()).thenReturn("state");
  94. when(context.getCallbackUrl()).thenReturn("http://localhost/callback");
  95. underTest.init(context);
  96. verify(context).redirectTo("https://github.com/login/oauth/authorize" +
  97. "?response_type=code" +
  98. "&client_id=id" +
  99. "&redirect_uri=http%3A%2F%2Flocalhost%2Fcallback" +
  100. "&scope=user%3Aemail%2Cread%3Aorg" +
  101. "&state=state");
  102. }
  103. @Test
  104. public void fail_to_init_when_disabled() {
  105. setSettings(false);
  106. OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
  107. assertThatThrownBy(() -> underTest.init(context))
  108. .isInstanceOf(IllegalStateException.class)
  109. .hasMessage("GitHub authentication is disabled");
  110. }
  111. @Test
  112. public void scope_includes_org_when_necessary() {
  113. setSettings(false);
  114. settings.setProperty("sonar.auth.github.groupsSync", false);
  115. settings.setProperty("sonar.auth.github.organizations", "");
  116. assertThat(underTest.getScope()).isEqualTo("user:email");
  117. settings.setProperty("sonar.auth.github.groupsSync", true);
  118. settings.setProperty("sonar.auth.github.organizations", "");
  119. assertThat(underTest.getScope()).isEqualTo("user:email,read:org");
  120. settings.setProperty("sonar.auth.github.groupsSync", false);
  121. settings.setProperty("sonar.auth.github.organizations", "example");
  122. assertThat(underTest.getScope()).isEqualTo("user:email,read:org");
  123. settings.setProperty("sonar.auth.github.groupsSync", true);
  124. settings.setProperty("sonar.auth.github.organizations", "example");
  125. assertThat(underTest.getScope()).isEqualTo("user:email,read:org");
  126. }
  127. @Test
  128. public void organization_membership_required() {
  129. setSettings(true);
  130. settings.setProperty("sonar.auth.github.organizations", "example");
  131. assertThat(underTest.isOrganizationMembershipRequired()).isTrue();
  132. settings.setProperty("sonar.auth.github.organizations", "example0, example1");
  133. assertThat(underTest.isOrganizationMembershipRequired()).isTrue();
  134. }
  135. @Test
  136. public void organization_membership_not_required() {
  137. setSettings(true);
  138. settings.setProperty("sonar.auth.github.organizations", "");
  139. assertThat(underTest.isOrganizationMembershipRequired()).isFalse();
  140. }
  141. private void setSettings(boolean enabled) {
  142. if (enabled) {
  143. settings.setProperty("sonar.auth.github.clientId.secured", "id");
  144. settings.setProperty("sonar.auth.github.clientSecret.secured", "secret");
  145. settings.setProperty("sonar.auth.github.enabled", true);
  146. } else {
  147. settings.setProperty("sonar.auth.github.enabled", false);
  148. }
  149. }
  150. }