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 UpdateAzureActionTest {
45 public UserSessionRule userSession = UserSessionRule.standalone();
47 public DbTester db = DbTester.create();
49 private static String AZURE_URL = "https://ado.sonarqube.com/";
51 private final Encryption encryption = mock(Encryption.class);
53 private WsActionTester ws = new WsActionTester(new UpdateAzureAction(db.getDbClient(), userSession,
54 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
55 mock(MultipleAlmFeatureProvider.class))));
58 public void update() {
59 UserDto user = db.users().insertUser();
60 userSession.logIn(user).setSystemAdministrator();
62 AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
65 .setParam("key", almSettingDto.getKey())
66 .setParam("personalAccessToken", "10987654321")
67 .setParam("url", AZURE_URL)
69 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
70 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
71 .containsOnly(tuple(almSettingDto.getKey(), AZURE_URL, "10987654321"));
75 public void update_with_new_key() {
76 UserDto user = db.users().insertUser();
77 userSession.logIn(user).setSystemAdministrator();
79 AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
82 .setParam("key", almSettingDto.getKey())
83 .setParam("newKey", "Azure Server - Infra Team")
84 .setParam("personalAccessToken", "0123456789")
85 .setParam("url", AZURE_URL)
87 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
88 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
89 .containsOnly(tuple("Azure Server - Infra Team", AZURE_URL, "0123456789"));
93 public void update_without_pat() {
94 UserDto user = db.users().insertUser();
95 userSession.logIn(user).setSystemAdministrator();
97 AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
100 .setParam("key", almSettingDto.getKey())
101 .setParam("url", AZURE_URL)
103 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
104 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
105 .containsOnly(tuple(almSettingDto.getKey(), AZURE_URL, almSettingDto.getDecryptedPersonalAccessToken(encryption)));
109 public void fail_when_key_does_not_match_existing_alm_setting() {
110 UserDto user = db.users().insertUser();
111 userSession.logIn(user).setSystemAdministrator();
113 assertThatThrownBy(() -> ws.newRequest()
114 .setParam("key", "unknown")
115 .setParam("personalAccessToken", "0123456789")
116 .setParam("url", AZURE_URL)
118 .isInstanceOf(NotFoundException.class)
119 .hasMessageContaining("ALM setting with key 'unknown' cannot be found");
123 public void fail_when_new_key_matches_existing_alm_setting() {
124 UserDto user = db.users().insertUser();
125 userSession.logIn(user).setSystemAdministrator();
126 AlmSettingDto almSetting1 = db.almSettings().insertAzureAlmSetting();
127 AlmSettingDto almSetting2 = db.almSettings().insertAzureAlmSetting();
129 assertThatThrownBy(() -> ws.newRequest()
130 .setParam("key", almSetting1.getKey())
131 .setParam("newKey", almSetting2.getKey())
132 .setParam("personalAccessToken", "0123456789")
133 .setParam("url", AZURE_URL)
135 .isInstanceOf(IllegalArgumentException.class)
136 .hasMessageContaining(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
140 public void fail_when_missing_administer_system_permission() {
141 UserDto user = db.users().insertUser();
142 userSession.logIn(user);
143 AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
145 assertThatThrownBy(() -> ws.newRequest()
146 .setParam("key", almSettingDto.getKey())
147 .setParam("newKey", "Azure Server - Infra Team")
148 .setParam("personalAccessToken", "0123456789")
150 .isInstanceOf(ForbiddenException.class);
154 public void definition() {
155 WebService.Action def = ws.getDef();
157 assertThat(def.since()).isEqualTo("8.1");
158 assertThat(def.isPost()).isTrue();
159 assertThat(def.params())
160 .extracting(WebService.Param::key, WebService.Param::isRequired)
161 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("personalAccessToken", false), tuple("url", true));