Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

BitbucketIdentityProviderTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.bitbucket;
  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 BitbucketIdentityProviderTest {
  31. @Rule
  32. public ExpectedException thrown = ExpectedException.none();
  33. private final MapSettings settings = new MapSettings();
  34. private final BitbucketSettings bitbucketSettings = new BitbucketSettings(settings.asConfig());
  35. private final UserIdentityFactory userIdentityFactory = mock(UserIdentityFactory.class);
  36. private final BitbucketScribeApi scribeApi = new BitbucketScribeApi(bitbucketSettings);
  37. private final BitbucketIdentityProvider underTest = new BitbucketIdentityProvider(bitbucketSettings, userIdentityFactory, scribeApi);
  38. @Test
  39. public void check_fields() {
  40. assertThat(underTest.getKey()).isEqualTo("bitbucket");
  41. assertThat(underTest.getName()).isEqualTo("Bitbucket");
  42. assertThat(underTest.getDisplay().getIconPath()).isEqualTo("/images/alm/bitbucket-white.svg");
  43. assertThat(underTest.getDisplay().getBackgroundColor()).isEqualTo("#0052cc");
  44. }
  45. @Test
  46. public void is_enabled() {
  47. enableBitbucketAuthentication(true);
  48. assertThat(underTest.isEnabled()).isTrue();
  49. settings.setProperty("sonar.auth.bitbucket.enabled", false);
  50. assertThat(underTest.isEnabled()).isFalse();
  51. }
  52. @Test
  53. public void init() {
  54. enableBitbucketAuthentication(true);
  55. OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
  56. when(context.generateCsrfState()).thenReturn("state");
  57. when(context.getCallbackUrl()).thenReturn("http://localhost/callback");
  58. underTest.init(context);
  59. verify(context).redirectTo("https://bitbucket.org/site/oauth2/authorize?response_type=code&client_id=id&redirect_uri=http%3A%2F%2Flocalhost%2Fcallback&scope=account&state=state");
  60. }
  61. @Test
  62. public void fail_to_init_when_disabled() {
  63. enableBitbucketAuthentication(false);
  64. OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
  65. thrown.expect(IllegalStateException.class);
  66. thrown.expectMessage("Bitbucket authentication is disabled");
  67. underTest.init(context);
  68. }
  69. private void enableBitbucketAuthentication(boolean enabled) {
  70. if (enabled) {
  71. settings.setProperty("sonar.auth.bitbucket.clientId.secured", "id");
  72. settings.setProperty("sonar.auth.bitbucket.clientSecret.secured", "secret");
  73. settings.setProperty("sonar.auth.bitbucket.enabled", true);
  74. } else {
  75. settings.setProperty("sonar.auth.bitbucket.enabled", false);
  76. }
  77. }
  78. }