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.

GitLabIdentityProviderTest.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.gitlab;
  21. import org.assertj.core.api.Assertions;
  22. import org.junit.Test;
  23. import org.sonar.api.server.authentication.Display;
  24. import org.sonar.api.server.authentication.OAuth2IdentityProvider;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.mockito.Mockito.mock;
  27. import static org.mockito.Mockito.verify;
  28. import static org.mockito.Mockito.when;
  29. public class GitLabIdentityProviderTest {
  30. @Test
  31. public void test_identity_provider() {
  32. GitLabSettings gitLabSettings = mock(GitLabSettings.class);
  33. when(gitLabSettings.isEnabled()).thenReturn(true);
  34. when(gitLabSettings.allowUsersToSignUp()).thenReturn(true);
  35. GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings, new GitLabRestClient(gitLabSettings),
  36. new ScribeGitLabOauth2Api(gitLabSettings));
  37. assertThat(gitLabIdentityProvider.getKey()).isEqualTo("gitlab");
  38. assertThat(gitLabIdentityProvider.getName()).isEqualTo("GitLab");
  39. Display display = gitLabIdentityProvider.getDisplay();
  40. assertThat(display.getIconPath()).isEqualTo("/images/gitlab-icon-rgb.svg");
  41. assertThat(display.getBackgroundColor()).isEqualTo("#6a4fbb");
  42. assertThat(gitLabIdentityProvider.isEnabled()).isTrue();
  43. assertThat(gitLabIdentityProvider.allowsUsersToSignUp()).isTrue();
  44. }
  45. @Test
  46. public void test_init() {
  47. GitLabSettings gitLabSettings = mock(GitLabSettings.class);
  48. when(gitLabSettings.isEnabled()).thenReturn(true);
  49. when(gitLabSettings.allowUsersToSignUp()).thenReturn(true);
  50. when(gitLabSettings.applicationId()).thenReturn("123");
  51. when(gitLabSettings.secret()).thenReturn("456");
  52. when(gitLabSettings.url()).thenReturn("http://server");
  53. when(gitLabSettings.syncUserGroups()).thenReturn(true);
  54. GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings, new GitLabRestClient(gitLabSettings),
  55. new ScribeGitLabOauth2Api(gitLabSettings));
  56. OAuth2IdentityProvider.InitContext initContext = mock(OAuth2IdentityProvider.InitContext.class);
  57. when(initContext.getCallbackUrl()).thenReturn("http://server/callback");
  58. gitLabIdentityProvider.init(initContext);
  59. verify(initContext).redirectTo("http://server/oauth/authorize?response_type=code&client_id=123&redirect_uri=http%3A%2F%2Fserver%2Fcallback&scope=api");
  60. }
  61. @Test
  62. public void test_init_without_sync() {
  63. GitLabSettings gitLabSettings = mock(GitLabSettings.class);
  64. when(gitLabSettings.isEnabled()).thenReturn(true);
  65. when(gitLabSettings.allowUsersToSignUp()).thenReturn(true);
  66. when(gitLabSettings.applicationId()).thenReturn("123");
  67. when(gitLabSettings.secret()).thenReturn("456");
  68. when(gitLabSettings.url()).thenReturn("http://server");
  69. when(gitLabSettings.syncUserGroups()).thenReturn(false);
  70. GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings, new GitLabRestClient(gitLabSettings),
  71. new ScribeGitLabOauth2Api(gitLabSettings));
  72. OAuth2IdentityProvider.InitContext initContext = mock(OAuth2IdentityProvider.InitContext.class);
  73. when(initContext.getCallbackUrl()).thenReturn("http://server/callback");
  74. gitLabIdentityProvider.init(initContext);
  75. verify(initContext).redirectTo("http://server/oauth/authorize?response_type=code&client_id=123&redirect_uri=http%3A%2F%2Fserver%2Fcallback&scope=read_user");
  76. }
  77. @Test
  78. public void fail_to_init() {
  79. GitLabSettings gitLabSettings = mock(GitLabSettings.class);
  80. when(gitLabSettings.isEnabled()).thenReturn(false);
  81. when(gitLabSettings.allowUsersToSignUp()).thenReturn(true);
  82. when(gitLabSettings.applicationId()).thenReturn("123");
  83. when(gitLabSettings.secret()).thenReturn("456");
  84. when(gitLabSettings.url()).thenReturn("http://server");
  85. GitLabIdentityProvider gitLabIdentityProvider = new GitLabIdentityProvider(gitLabSettings, new GitLabRestClient(gitLabSettings),
  86. new ScribeGitLabOauth2Api(gitLabSettings));
  87. OAuth2IdentityProvider.InitContext initContext = mock(OAuth2IdentityProvider.InitContext.class);
  88. when(initContext.getCallbackUrl()).thenReturn("http://server/callback");
  89. Assertions.assertThatThrownBy(() -> gitLabIdentityProvider.init(initContext))
  90. .hasMessage("GitLab authentication is disabled")
  91. .isInstanceOf(IllegalStateException.class);
  92. }
  93. }