]> source.dussan.org Git - sonarqube.git/blob
99c77e27f4b45efa7fb0710dc1f1faefdd2171e3
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.almsettings.ws;
21
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;
35
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;
41
42 public class UpdateAzureActionTest {
43
44   @Rule
45   public UserSessionRule userSession = UserSessionRule.standalone();
46   @Rule
47   public DbTester db = DbTester.create();
48
49   private static String AZURE_URL = "https://ado.sonarqube.com/";
50
51   private final Encryption encryption = mock(Encryption.class);
52
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))));
56
57   @Test
58   public void update() {
59     UserDto user = db.users().insertUser();
60     userSession.logIn(user).setSystemAdministrator();
61
62     AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
63
64     ws.newRequest()
65       .setParam("key", almSettingDto.getKey())
66       .setParam("personalAccessToken", "10987654321")
67       .setParam("url", AZURE_URL)
68       .execute();
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"));
72   }
73
74   @Test
75   public void update_with_new_key() {
76     UserDto user = db.users().insertUser();
77     userSession.logIn(user).setSystemAdministrator();
78
79     AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
80
81     ws.newRequest()
82       .setParam("key", almSettingDto.getKey())
83       .setParam("newKey", "Azure Server - Infra Team")
84       .setParam("personalAccessToken", "0123456789")
85       .setParam("url", AZURE_URL)
86       .execute();
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"));
90   }
91
92   @Test
93   public void update_without_pat() {
94     UserDto user = db.users().insertUser();
95     userSession.logIn(user).setSystemAdministrator();
96
97     AlmSettingDto almSettingDto = db.almSettings().insertAzureAlmSetting();
98
99     ws.newRequest()
100       .setParam("key", almSettingDto.getKey())
101       .setParam("url", AZURE_URL)
102       .execute();
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)));
106   }
107
108   @Test
109   public void fail_when_key_does_not_match_existing_alm_setting() {
110     UserDto user = db.users().insertUser();
111     userSession.logIn(user).setSystemAdministrator();
112
113     assertThatThrownBy(() -> ws.newRequest()
114       .setParam("key", "unknown")
115       .setParam("personalAccessToken", "0123456789")
116       .setParam("url", AZURE_URL)
117       .execute())
118       .isInstanceOf(NotFoundException.class)
119       .hasMessageContaining("ALM setting with key 'unknown' cannot be found");
120   }
121
122   @Test
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();
128
129     assertThatThrownBy(() -> ws.newRequest()
130       .setParam("key", almSetting1.getKey())
131       .setParam("newKey", almSetting2.getKey())
132       .setParam("personalAccessToken", "0123456789")
133       .setParam("url", AZURE_URL)
134       .execute())
135       .isInstanceOf(IllegalArgumentException.class)
136       .hasMessageContaining(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
137   }
138
139   @Test
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();
144
145     assertThatThrownBy(() -> ws.newRequest()
146       .setParam("key", almSettingDto.getKey())
147       .setParam("newKey", "Azure Server - Infra Team")
148       .setParam("personalAccessToken", "0123456789")
149       .execute())
150       .isInstanceOf(ForbiddenException.class);
151   }
152
153   @Test
154   public void definition() {
155     WebService.Action def = ws.getDef();
156
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));
162   }
163
164 }