3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.auth.bitbucket;
22 import org.junit.Test;
23 import org.sonar.api.config.internal.MapSettings;
24 import org.sonar.api.server.authentication.OAuth2IdentityProvider;
26 import static org.assertj.core.api.Assertions.assertThat;
27 import static org.assertj.core.api.Assertions.assertThatThrownBy;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 public class BitbucketIdentityProviderTest {
35 private final MapSettings settings = new MapSettings();
36 private final BitbucketSettings bitbucketSettings = new BitbucketSettings(settings.asConfig());
37 private final UserIdentityFactory userIdentityFactory = mock(UserIdentityFactory.class);
38 private final BitbucketScribeApi scribeApi = new BitbucketScribeApi(bitbucketSettings);
39 private final BitbucketIdentityProvider underTest = new BitbucketIdentityProvider(bitbucketSettings, userIdentityFactory, scribeApi);
42 public void check_fields() {
43 assertThat(underTest.getKey()).isEqualTo("bitbucket");
44 assertThat(underTest.getName()).isEqualTo("Bitbucket");
45 assertThat(underTest.getDisplay().getIconPath()).isEqualTo("/images/alm/bitbucket-white.svg");
46 assertThat(underTest.getDisplay().getBackgroundColor()).isEqualTo("#0052cc");
50 public void is_enabled() {
51 enableBitbucketAuthentication(true);
52 assertThat(underTest.isEnabled()).isTrue();
54 settings.setProperty("sonar.auth.bitbucket.enabled", false);
55 assertThat(underTest.isEnabled()).isFalse();
60 enableBitbucketAuthentication(true);
61 OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
62 when(context.generateCsrfState()).thenReturn("state");
63 when(context.getCallbackUrl()).thenReturn("http://localhost/callback");
65 underTest.init(context);
67 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");
71 public void fail_to_init_when_disabled() {
72 enableBitbucketAuthentication(false);
73 OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
75 assertThatThrownBy(() -> underTest.init(context))
76 .isInstanceOf(IllegalStateException.class)
77 .hasMessage("Bitbucket authentication is disabled");
80 private void enableBitbucketAuthentication(boolean enabled) {
82 settings.setProperty("sonar.auth.bitbucket.clientId.secured", "id");
83 settings.setProperty("sonar.auth.bitbucket.clientSecret.secured", "secret");
84 settings.setProperty("sonar.auth.bitbucket.enabled", true);
86 settings.setProperty("sonar.auth.bitbucket.enabled", false);