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.WsActionTester;
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;
41 import static org.mockito.Mockito.when;
43 public class CreateAzureActionTest {
46 public UserSessionRule userSession = UserSessionRule.standalone();
48 public DbTester db = DbTester.create();
50 private final Encryption encryption = mock(Encryption.class);
51 private final MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
53 private WsActionTester ws = new WsActionTester(new CreateAzureAction(db.getDbClient(), userSession,
54 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null),
55 multipleAlmFeatureProvider)));
58 public void before() {
59 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
63 public void create() {
64 UserDto user = db.users().insertUser();
65 userSession.logIn(user).setSystemAdministrator();
68 .setParam("key", "Azure Server - Dev Team")
69 .setParam("personalAccessToken", "98765432100")
70 .setParam("url", "https://ado.sonarqube.com/")
73 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
74 .extracting(AlmSettingDto::getKey,
75 s -> s.getDecryptedPersonalAccessToken(encryption),
76 AlmSettingDto::getUrl)
77 .containsOnly(tuple("Azure Server - Dev Team", "98765432100", "https://ado.sonarqube.com/"));
81 public void fail_when_key_is_already_used() {
82 when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
83 UserDto user = db.users().insertUser();
84 userSession.logIn(user).setSystemAdministrator();
85 AlmSettingDto azureAlmSetting = db.almSettings().insertAzureAlmSetting();
87 assertThatThrownBy(() -> ws.newRequest()
88 .setParam("key", azureAlmSetting.getKey())
89 .setParam("personalAccessToken", "98765432100")
90 .setParam("url", "https://ado.sonarqube.com/")
92 .isInstanceOf(IllegalArgumentException.class)
93 .hasMessageContaining(String.format("An ALM setting with key '%s' already exist", azureAlmSetting.getKey()));
97 public void fail_when_no_multiple_instance_allowed() {
98 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
99 UserDto user = db.users().insertUser();
100 userSession.logIn(user).setSystemAdministrator();
101 db.almSettings().insertAzureAlmSetting();
103 assertThatThrownBy(() -> ws.newRequest()
104 .setParam("key", "key")
105 .setParam("personalAccessToken", "98765432100")
106 .setParam("url", "https://ado.sonarqube.com/")
108 .isInstanceOf(BadRequestException.class)
109 .hasMessageContaining("A AZURE_DEVOPS setting is already defined");
113 public void fail_when_missing_administer_system_permission() {
114 UserDto user = db.users().insertUser();
115 userSession.logIn(user);
117 assertThatThrownBy(() -> ws.newRequest()
118 .setParam("key", "Azure Server - Dev Team")
119 .setParam("personalAccessToken", "98765432100")
121 .isInstanceOf(ForbiddenException.class);
125 public void definition() {
126 WebService.Action def = ws.getDef();
128 assertThat(def.since()).isEqualTo("8.1");
129 assertThat(def.isPost()).isTrue();
130 assertThat(def.params())
131 .extracting(WebService.Param::key, WebService.Param::isRequired)
132 .containsExactlyInAnyOrder(tuple("key", true), tuple("personalAccessToken", true), tuple("url", true));