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.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.WsActionTester;
36 import static java.lang.String.format;
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;
42 public class UpdateBitbucketActionTest {
45 public UserSessionRule userSession = UserSessionRule.standalone();
47 public DbTester db = DbTester.create();
49 private final Encryption encryption = mock(Encryption.class);
51 private WsActionTester ws = new WsActionTester(new UpdateBitbucketAction(db.getDbClient(), userSession,
52 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
53 mock(MultipleAlmFeatureProvider.class))));
56 public void update() {
57 UserDto user = db.users().insertUser();
58 userSession.logIn(user).setSystemAdministrator();
59 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
62 .setParam("key", almSettingDto.getKey())
63 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
64 .setParam("personalAccessToken", "10987654321")
67 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
68 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
69 .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", "10987654321"));
73 public void update_with_new_key() {
74 UserDto user = db.users().insertUser();
75 userSession.logIn(user).setSystemAdministrator();
77 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
80 .setParam("key", almSettingDto.getKey())
81 .setParam("newKey", "Bitbucket Server - Infra Team")
82 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
83 .setParam("personalAccessToken", "0123456789")
85 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
86 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
87 .containsOnly(tuple("Bitbucket Server - Infra Team", "https://bitbucket.enterprise-unicorn.com", "0123456789"));
91 public void update_without_pat() {
92 UserDto user = db.users().insertUser();
93 userSession.logIn(user).setSystemAdministrator();
95 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
98 .setParam("key", almSettingDto.getKey())
99 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
101 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
102 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
103 .containsOnly(tuple(almSettingDto.getKey(), "https://bitbucket.enterprise-unicorn.com", almSettingDto.getDecryptedPersonalAccessToken(encryption)));
107 public void fail_when_key_does_not_match_existing_alm_setting() {
108 UserDto user = db.users().insertUser();
109 userSession.logIn(user).setSystemAdministrator();
111 assertThatThrownBy(() -> ws.newRequest()
112 .setParam("key", "unknown")
113 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
114 .setParam("personalAccessToken", "0123456789")
116 .isInstanceOf(NotFoundException.class)
117 .hasMessageContaining("ALM setting with key 'unknown' cannot be found");
121 public void fail_when_new_key_matches_existing_alm_setting() {
122 UserDto user = db.users().insertUser();
123 userSession.logIn(user).setSystemAdministrator();
124 AlmSettingDto almSetting1 = db.almSettings().insertBitbucketAlmSetting();
125 AlmSettingDto almSetting2 = db.almSettings().insertBitbucketAlmSetting();
127 assertThatThrownBy(() -> ws.newRequest()
128 .setParam("key", almSetting1.getKey())
129 .setParam("newKey", almSetting2.getKey())
130 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
131 .setParam("personalAccessToken", "0123456789")
133 .isInstanceOf(IllegalArgumentException.class)
134 .hasMessageContaining(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
138 public void fail_when_missing_administer_system_permission() {
139 UserDto user = db.users().insertUser();
140 userSession.logIn(user);
141 AlmSettingDto almSettingDto = db.almSettings().insertBitbucketAlmSetting();
143 assertThatThrownBy(() -> ws.newRequest()
144 .setParam("key", almSettingDto.getKey())
145 .setParam("newKey", "Bitbucket Server - Infra Team")
146 .setParam("url", "https://bitbucket.enterprise-unicorn.com")
147 .setParam("personalAccessToken", "0123456789")
149 .isInstanceOf(ForbiddenException.class);
153 public void definition() {
154 WebService.Action def = ws.getDef();
156 assertThat(def.since()).isEqualTo("8.1");
157 assertThat(def.isPost()).isTrue();
158 assertThat(def.params())
159 .extracting(WebService.Param::key, WebService.Param::isRequired)
160 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("url", true), tuple("personalAccessToken", false));