]> source.dussan.org Git - sonarqube.git/blob
225b156e40fa3f8134f4cc75f9b0b1218b1e0c1e
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.server.almsettings.ws;
21
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.config.internal.Encryption;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.alm.setting.AlmSettingDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.almsettings.MultipleAlmFeature;
31 import org.sonar.server.component.ComponentFinder;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.tester.UserSessionRule;
35 import org.sonar.server.ws.WsActionTester;
36
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.api.Assertions.assertThatThrownBy;
39 import static org.assertj.core.groups.Tuple.tuple;
40 import static org.mockito.Mockito.mock;
41 import static org.mockito.Mockito.when;
42
43 public class CreateBitbucketActionIT {
44
45   @Rule
46   public UserSessionRule userSession = UserSessionRule.standalone();
47   @Rule
48   public DbTester db = DbTester.create();
49
50   private final Encryption encryption = mock(Encryption.class);
51   private final MultipleAlmFeature multipleAlmFeature = mock(MultipleAlmFeature.class);
52
53   private WsActionTester ws = new WsActionTester(new CreateBitBucketAction(db.getDbClient(), userSession,
54     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
55       multipleAlmFeature)));
56
57   @Before
58   public void before() {
59     when(multipleAlmFeature.isEnabled()).thenReturn(false);
60   }
61
62   @Test
63   public void create() {
64     UserDto user = db.users().insertUser();
65     userSession.logIn(user).setSystemAdministrator();
66
67     ws.newRequest()
68       .setParam("key", "Bitbucket Server - Dev Team")
69       .setParam("url", "https://bitbucket.enterprise.com")
70       .setParam("personalAccessToken", "98765432100")
71       .execute();
72
73     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
74       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
75       .containsOnly(tuple("Bitbucket Server - Dev Team", "https://bitbucket.enterprise.com", "98765432100"));
76   }
77
78   @Test
79   public void fail_when_key_is_already_used() {
80     when(multipleAlmFeature.isEnabled()).thenReturn(true);
81     UserDto user = db.users().insertUser();
82     userSession.logIn(user).setSystemAdministrator();
83     AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting();
84
85     assertThatThrownBy(() -> ws.newRequest()
86       .setParam("key", bitbucketAlmSetting.getKey())
87       .setParam("url", "https://bitbucket.enterprise.com")
88       .setParam("personalAccessToken", "98765432100")
89       .execute())
90       .isInstanceOf(IllegalArgumentException.class)
91       .hasMessageContaining(String.format("An DevOps Platform setting with key '%s' already exist", bitbucketAlmSetting.getKey()));
92   }
93
94   @Test
95   public void fail_when_no_multiple_instance_allowed() {
96     when(multipleAlmFeature.isEnabled()).thenReturn(false);
97     UserDto user = db.users().insertUser();
98     userSession.logIn(user).setSystemAdministrator();
99     db.almSettings().insertBitbucketAlmSetting();
100
101     assertThatThrownBy(() -> ws.newRequest()
102       .setParam("key", "otherKey")
103       .setParam("url", "https://bitbucket.enterprise.com")
104       .setParam("personalAccessToken", "98765432100")
105       .execute())
106       .isInstanceOf(BadRequestException.class)
107       .hasMessageContaining("A BITBUCKET setting is already defined");
108   }
109
110   @Test
111   public void fail_when_no_multiple_instance_allowed_and_bitbucket_cloud_exists() {
112     when(multipleAlmFeature.isEnabled()).thenReturn(false);
113     UserDto user = db.users().insertUser();
114     userSession.logIn(user).setSystemAdministrator();
115     db.almSettings().insertBitbucketCloudAlmSetting();
116
117     assertThatThrownBy(() -> ws.newRequest()
118       .setParam("key", "otherKey")
119       .setParam("url", "https://bitbucket.enterprise.com")
120       .setParam("personalAccessToken", "98765432100")
121       .execute())
122       .isInstanceOf(BadRequestException.class)
123       .hasMessageContaining("A BITBUCKET_CLOUD setting is already defined");
124   }
125
126   @Test
127   public void fail_when_missing_administer_system_permission() {
128     UserDto user = db.users().insertUser();
129     userSession.logIn(user);
130
131     assertThatThrownBy(() -> ws.newRequest()
132       .setParam("key", "Bitbucket Server - Dev Team")
133       .setParam("url", "https://bitbucket.enterprise.com")
134       .setParam("personalAccessToken", "98765432100")
135       .setParam("personalAccessToken", "98765432100")
136       .execute())
137       .isInstanceOf(ForbiddenException.class);
138   }
139
140   @Test
141   public void definition() {
142     WebService.Action def = ws.getDef();
143
144     assertThat(def.since()).isEqualTo("8.1");
145     assertThat(def.isPost()).isTrue();
146     assertThat(def.params())
147       .extracting(WebService.Param::key, WebService.Param::isRequired)
148       .containsExactlyInAnyOrder(tuple("key", true), tuple("url", true), tuple("personalAccessToken", true));
149   }
150 }