3 * Copyright (C) 2009-2021 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.resources.ResourceTypes;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.db.DbTester;
27 import org.sonar.db.alm.setting.AlmSettingDto;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.server.almsettings.MultipleAlmFeatureProvider;
30 import org.sonar.server.component.ComponentFinder;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.exceptions.NotFoundException;
33 import org.sonar.server.tester.UserSessionRule;
34 import org.sonar.server.ws.TestRequest;
35 import org.sonar.server.ws.WsActionTester;
37 import static java.lang.String.format;
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.assertj.core.api.Assertions.assertThatThrownBy;
40 import static org.assertj.core.groups.Tuple.tuple;
41 import static org.mockito.Mockito.mock;
43 public class UpdateBitbucketCloudActionTest {
45 public UserSessionRule userSession = UserSessionRule.standalone();
47 public DbTester db = DbTester.create();
49 private final WsActionTester ws = new WsActionTester(new UpdateBitbucketCloudAction(db.getDbClient(), userSession,
50 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), mock(ResourceTypes.class)),
51 mock(MultipleAlmFeatureProvider.class))));
54 public void update() {
55 UserDto user = db.users().insertUser();
56 userSession.logIn(user).setSystemAdministrator();
57 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
60 .setParam("key", almSettingDto.getKey())
61 .setParam("workspace", "workspace")
62 .setParam("clientId", "id")
63 .setParam("clientSecret", "secret")
66 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
67 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
68 .containsOnly(tuple(almSettingDto.getKey(), "id", "secret", "workspace"));
72 public void update_with_new_key() {
73 UserDto user = db.users().insertUser();
74 userSession.logIn(user).setSystemAdministrator();
76 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
79 .setParam("key", almSettingDto.getKey())
80 .setParam("newKey", "Bitbucket Server - Infra Team")
81 .setParam("workspace", "workspace")
82 .setParam("clientId", "id")
83 .setParam("clientSecret", "secret")
85 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
86 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
87 .containsOnly(tuple("Bitbucket Server - Infra Team", "id", "secret", "workspace"));
91 public void update_binding_without_changing_the_key() {
92 UserDto user = db.users().insertUser();
93 userSession.logIn(user).setSystemAdministrator();
94 AlmSettingDto almSetting = db.almSettings().insertBitbucketAlmSetting();
97 .setParam("key", almSetting.getKey())
98 .setParam("newKey", almSetting.getKey())
99 .setParam("workspace", "workspace")
100 .setParam("clientId", "id")
101 .setParam("clientSecret", "secret")
104 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
105 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
106 .containsOnly(tuple(almSetting.getKey(), "id", "secret", "workspace"));
110 public void update_without_secret() {
111 UserDto user = db.users().insertUser();
112 userSession.logIn(user).setSystemAdministrator();
114 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
117 .setParam("key", almSettingDto.getKey())
118 .setParam("workspace", "workspace")
119 .setParam("clientId", "id")
121 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
122 .extracting(AlmSettingDto::getKey, AlmSettingDto::getClientId, AlmSettingDto::getClientSecret, AlmSettingDto::getAppId)
123 .containsOnly(tuple(almSettingDto.getKey(), "id", almSettingDto.getClientSecret(), "workspace"));
127 public void fail_when_key_does_not_match_existing_alm_setting() {
128 UserDto user = db.users().insertUser();
129 userSession.logIn(user).setSystemAdministrator();
130 TestRequest request = ws.newRequest()
131 .setParam("key", "unknown")
132 .setParam("workspace", "workspace")
133 .setParam("clientId", "id")
134 .setParam("clientSecret", "secret");
136 assertThatThrownBy(request::execute)
137 .isInstanceOf(NotFoundException.class)
138 .hasMessage("ALM setting with key 'unknown' cannot be found");
142 public void fail_when_new_key_matches_existing_alm_setting() {
143 UserDto user = db.users().insertUser();
144 userSession.logIn(user).setSystemAdministrator();
145 AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting();
146 AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting();
147 TestRequest request = ws.newRequest()
148 .setParam("key", almSetting1.getKey())
149 .setParam("newKey", almSetting2.getKey())
150 .setParam("workspace", "workspace")
151 .setParam("clientId", "id")
152 .setParam("clientSecret", "secret");
154 assertThatThrownBy(request::execute)
155 .isInstanceOf(IllegalArgumentException.class)
156 .hasMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
160 public void fail_when_missing_administer_system_permission() {
161 UserDto user = db.users().insertUser();
162 userSession.logIn(user);
163 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
164 TestRequest request = ws.newRequest()
165 .setParam("key", almSettingDto.getKey())
166 .setParam("newKey", "Bitbucket Server - Infra Team")
167 .setParam("workspace", "workspace")
168 .setParam("clientId", "id")
169 .setParam("clientSecret", "secret");
171 assertThatThrownBy(request::execute)
172 .isInstanceOf(ForbiddenException.class);
176 public void definition() {
177 WebService.Action def = ws.getDef();
179 assertThat(def.since()).isEqualTo("8.7");
180 assertThat(def.isPost()).isTrue();
181 assertThat(def.params())
182 .extracting(WebService.Param::key, WebService.Param::isRequired)
183 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("workspace", true),
184 tuple("clientId", true), tuple("clientSecret", false));