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.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.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.tester.UserSessionRule;
35 import org.sonar.server.ws.TestRequest;
36 import org.sonar.server.ws.WsActionTester;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatNoException;
40 import static org.assertj.core.api.Assertions.assertThatThrownBy;
41 import static org.assertj.core.groups.Tuple.tuple;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class CreateBitbucketCloudActionTest {
48 public UserSessionRule userSession = UserSessionRule.standalone();
50 public DbTester db = DbTester.create();
52 private final Encryption encryption = mock(Encryption.class);
53 private final MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
55 private WsActionTester ws = new WsActionTester(new CreateBitbucketCloudAction(db.getDbClient(), userSession,
56 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
57 multipleAlmFeatureProvider)));
60 public void before() {
61 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
65 public void create() {
66 UserDto user = db.users().insertUser();
67 userSession.logIn(user).setSystemAdministrator();
70 .setParam("key", "Bitbucket Server - Dev Team")
71 .setParam("clientId", "id")
72 .setParam("clientSecret", "secret")
73 .setParam("workspace", "workspace1")
76 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
77 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, s -> s.getDecryptedClientSecret(encryption), AlmSettingDto::getAppId)
78 .containsOnly(tuple("Bitbucket Server - Dev Team", "id", "secret", "workspace1"));
82 public void fail_when_key_is_already_used() {
83 when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
84 UserDto user = db.users().insertUser();
85 userSession.logIn(user).setSystemAdministrator();
86 AlmSettingDto bitbucketAlmSetting = db.almSettings().insertBitbucketAlmSetting();
88 assertThatThrownBy(() -> ws.newRequest()
89 .setParam("key", bitbucketAlmSetting.getKey())
90 .setParam("workspace", "workspace1")
91 .setParam("clientId", "id")
92 .setParam("clientSecret", "secret")
94 .isInstanceOf(IllegalArgumentException.class)
95 .hasMessageContaining(String.format("An ALM setting with key '%s' already exist", bitbucketAlmSetting.getKey()));
99 public void fail_when_no_multiple_instance_allowed() {
100 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
101 UserDto user = db.users().insertUser();
102 userSession.logIn(user).setSystemAdministrator();
103 db.almSettings().insertBitbucketCloudAlmSetting();
105 assertThatThrownBy(() -> ws.newRequest()
106 .setParam("key", "otherKey")
107 .setParam("workspace", "workspace1")
108 .setParam("clientId", "id")
109 .setParam("clientSecret", "secret")
111 .isInstanceOf(BadRequestException.class)
112 .hasMessageContaining("A BITBUCKET_CLOUD setting is already defined");
116 public void fail_when_no_multiple_instance_allowed_and_bitbucket_server_exists() {
117 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
118 UserDto user = db.users().insertUser();
119 userSession.logIn(user).setSystemAdministrator();
120 db.almSettings().insertBitbucketAlmSetting();
122 assertThatThrownBy(() -> ws.newRequest()
123 .setParam("key", "otherKey")
124 .setParam("workspace", "workspace1")
125 .setParam("clientId", "id")
126 .setParam("clientSecret", "secret")
128 .isInstanceOf(BadRequestException.class)
129 .hasMessageContaining("A BITBUCKET setting is already defined");
133 public void fail_when_missing_administer_system_permission() {
134 UserDto user = db.users().insertUser();
135 userSession.logIn(user);
137 assertThatThrownBy(() -> ws.newRequest()
138 .setParam("key", "Bitbucket Server - Dev Team")
139 .setParam("clientId", "id")
140 .setParam("clientSecret", "secret")
141 .setParam("workspace", "workspace1")
143 .isInstanceOf(ForbiddenException.class);
147 public void fail_when_workspace_id_format_is_incorrect() {
148 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
149 String workspace = "workspace/name";
150 UserDto user = db.users().insertUser();
151 userSession.logIn(user).setSystemAdministrator();
153 TestRequest request = ws.newRequest()
154 .setParam("key", "another new key")
155 .setParam("workspace", workspace)
156 .setParam("clientId", "id")
157 .setParam("clientSecret", "secret");
159 assertThatThrownBy(request::execute)
160 .isInstanceOf(BadRequestException.class)
161 .hasMessageContaining(String.format(
162 "Workspace ID '%s' has an incorrect format. Should only contain lowercase letters, numbers, dashes, and underscores.",
168 public void do_not_fail_when_workspace_id_format_is_correct() {
169 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
170 String workspace = "work-space_123";
171 UserDto user = db.users().insertUser();
172 userSession.logIn(user).setSystemAdministrator();
174 TestRequest request = ws.newRequest()
175 .setParam("key", "yet another new key")
176 .setParam("workspace", workspace)
177 .setParam("clientId", "id")
178 .setParam("clientSecret", "secret");
180 assertThatNoException().isThrownBy(request::execute);
184 public void definition() {
185 WebService.Action def = ws.getDef();
187 assertThat(def.since()).isEqualTo("8.7");
188 assertThat(def.isPost()).isTrue();
189 assertThat(def.params())
190 .extracting(WebService.Param::key, WebService.Param::isRequired)
191 .containsExactlyInAnyOrder(tuple("key", true), tuple("clientId", true), tuple("clientSecret", true), tuple("workspace", true));