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.3KB

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