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

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