]> source.dussan.org Git - sonarqube.git/blob
380dee89e9fea7b32b5b0427f73cecb86dc8403d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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
22 import org.junit.Test;
23 import org.sonar.api.config.internal.MapSettings;
24 import org.sonar.api.server.authentication.OAuth2IdentityProvider;
25
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;
31
32 public class BitbucketIdentityProviderTest {
33
34
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);
40
41   @Test
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");
47   }
48
49   @Test
50   public void is_enabled() {
51     enableBitbucketAuthentication(true);
52     assertThat(underTest.isEnabled()).isTrue();
53
54     settings.setProperty("sonar.auth.bitbucket.enabled", false);
55     assertThat(underTest.isEnabled()).isFalse();
56   }
57
58   @Test
59   public void init() {
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");
64
65     underTest.init(context);
66
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");
68   }
69
70   @Test
71   public void fail_to_init_when_disabled() {
72     enableBitbucketAuthentication(false);
73     OAuth2IdentityProvider.InitContext context = mock(OAuth2IdentityProvider.InitContext.class);
74
75     assertThatThrownBy(() -> underTest.init(context))
76       .isInstanceOf(IllegalStateException.class)
77       .hasMessage("Bitbucket authentication is disabled");
78   }
79
80   private void enableBitbucketAuthentication(boolean enabled) {
81     if (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);
85     } else {
86       settings.setProperty("sonar.auth.bitbucket.enabled", false);
87     }
88   }
89
90 }