3 * Copyright (C) 2009-2022 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.server.almsettings.ws;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.config.internal.Encryption;
25 import org.sonar.api.resources.ResourceTypes;
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.MultipleAlmFeatureProvider;
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.exceptions.NotFoundException;
35 import org.sonar.server.tester.UserSessionRule;
36 import org.sonar.server.ws.TestRequest;
37 import org.sonar.server.ws.WsActionTester;
39 import static java.lang.String.format;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatNoException;
42 import static org.assertj.core.api.Assertions.assertThatThrownBy;
43 import static org.assertj.core.groups.Tuple.tuple;
44 import static org.mockito.ArgumentMatchers.any;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.when;
48 public class UpdateBitbucketCloudActionTest {
50 public UserSessionRule userSession = UserSessionRule.standalone();
52 public DbTester db = DbTester.create();
54 private final Encryption encryption = mock(Encryption.class);
56 private final WsActionTester ws = new WsActionTester(new UpdateBitbucketCloudAction(db.getDbClient(), userSession,
57 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), mock(ResourceTypes.class)),
58 mock(MultipleAlmFeatureProvider.class))));
61 public void update() {
62 when(encryption.isEncrypted(any())).thenReturn(false);
63 UserDto user = db.users().insertUser();
64 userSession.logIn(user).setSystemAdministrator();
65 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
68 .setParam("key", almSettingDto.getKey())
69 .setParam("workspace", "workspace")
70 .setParam("clientId", "id")
71 .setParam("clientSecret", "secret")
74 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
75 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId,
76 s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId)
77 .containsOnly(tuple(almSettingDto.getKey(), "id", "secret", "workspace"));
81 public void update_with_new_key() {
82 when(encryption.isEncrypted(any())).thenReturn(false);
84 UserDto user = db.users().insertUser();
85 userSession.logIn(user).setSystemAdministrator();
87 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
90 .setParam("key", almSettingDto.getKey())
91 .setParam("newKey", "Bitbucket Server - Infra Team")
92 .setParam("workspace", "workspace")
93 .setParam("clientId", "id")
94 .setParam("clientSecret", "secret")
96 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
97 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId,
98 s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId)
99 .containsOnly(tuple("Bitbucket Server - Infra Team", "id", "secret", "workspace"));
103 public void update_binding_without_changing_the_key() {
104 when(encryption.isEncrypted(any())).thenReturn(false);
106 UserDto user = db.users().insertUser();
107 userSession.logIn(user).setSystemAdministrator();
108 AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
111 .setParam("key", almSetting.getKey())
112 .setParam("newKey", almSetting.getKey())
113 .setParam("workspace", "workspace")
114 .setParam("clientId", "id")
115 .setParam("clientSecret", "secret")
118 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
119 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId,
120 s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId)
121 .containsOnly(tuple(almSetting.getKey(), "id", "secret", "workspace"));
125 public void update_without_secret() {
126 when(encryption.isEncrypted(any())).thenReturn(false);
128 UserDto user = db.users().insertUser();
129 userSession.logIn(user).setSystemAdministrator();
131 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
134 .setParam("key", almSettingDto.getKey())
135 .setParam("workspace", "workspace")
136 .setParam("clientId", "id")
138 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
139 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId,
140 s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId)
141 .containsOnly(tuple(almSettingDto.getKey(), "id", almSettingDto.getDecryptedPrivateKey(encryption), "workspace"));
145 public void fail_when_key_does_not_match_existing_alm_setting() {
146 UserDto user = db.users().insertUser();
147 userSession.logIn(user).setSystemAdministrator();
148 TestRequest request = ws.newRequest()
149 .setParam("key", "unknown")
150 .setParam("workspace", "workspace")
151 .setParam("clientId", "id")
152 .setParam("clientSecret", "secret");
154 assertThatThrownBy(request::execute)
155 .isInstanceOf(NotFoundException.class)
156 .hasMessage("ALM setting with key 'unknown' cannot be found");
160 public void fail_when_new_key_matches_existing_alm_setting() {
161 UserDto user = db.users().insertUser();
162 userSession.logIn(user).setSystemAdministrator();
163 AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting();
164 AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting();
165 TestRequest request = ws.newRequest()
166 .setParam("key", almSetting1.getKey())
167 .setParam("newKey", almSetting2.getKey())
168 .setParam("workspace", "workspace")
169 .setParam("clientId", "id")
170 .setParam("clientSecret", "secret");
172 assertThatThrownBy(request::execute)
173 .isInstanceOf(IllegalArgumentException.class)
174 .hasMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
178 public void fail_when_missing_administer_system_permission() {
179 UserDto user = db.users().insertUser();
180 userSession.logIn(user);
181 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
182 TestRequest request = ws.newRequest()
183 .setParam("key", almSettingDto.getKey())
184 .setParam("newKey", "Bitbucket Server - Infra Team")
185 .setParam("workspace", "workspace")
186 .setParam("clientId", "id")
187 .setParam("clientSecret", "secret");
189 assertThatThrownBy(request::execute)
190 .isInstanceOf(ForbiddenException.class);
194 public void fail_when_workspace_id_format_is_incorrect() {
195 String workspace = "workspace/name";
196 UserDto user = db.users().insertUser();
197 userSession.logIn(user).setSystemAdministrator();
198 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
200 TestRequest request = ws.newRequest()
201 .setParam("key", almSettingDto.getKey())
202 .setParam("workspace", workspace)
203 .setParam("clientId", "id")
204 .setParam("clientSecret", "secret");
206 assertThatThrownBy(request::execute)
207 .isInstanceOf(BadRequestException.class)
208 .hasMessageContaining(String.format(
209 "Workspace ID '%s' has an incorrect format. Should only contain lowercase letters, numbers, dashes, and underscores.",
215 public void do_not_fail_when_workspace_id_format_is_correct() {
216 String workspace = "work-space_123";
217 UserDto user = db.users().insertUser();
218 userSession.logIn(user).setSystemAdministrator();
219 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
221 TestRequest request = ws.newRequest()
222 .setParam("key", almSettingDto.getKey())
223 .setParam("workspace", workspace)
224 .setParam("clientId", "id")
225 .setParam("clientSecret", "secret");
227 assertThatNoException().isThrownBy(request::execute);
231 public void definition() {
232 WebService.Action def = ws.getDef();
234 assertThat(def.since()).isEqualTo("8.7");
235 assertThat(def.isPost()).isTrue();
236 assertThat(def.params())
237 .extracting(WebService.Param::key, WebService.Param::isRequired)
238 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("workspace", true),
239 tuple("clientId", true), tuple("clientSecret", false));