3 * Copyright (C) 2009-2024 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.assertj.core.api.Assertions;
23 import org.junit.Before;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.config.internal.Encryption;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.alm.setting.AlmSettingDto;
30 import org.sonar.db.user.UserDto;
31 import org.sonar.server.almsettings.MultipleAlmFeature;
32 import org.sonar.server.component.ComponentFinder;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.tester.UserSessionRule;
36 import org.sonar.server.ws.TestRequest;
37 import org.sonar.server.ws.WsActionTester;
39 import static java.lang.String.format;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.assertThatThrownBy;
42 import static org.assertj.core.groups.Tuple.tuple;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
46 public class UpdateGitlabActionTest {
49 public UserSessionRule userSession = UserSessionRule.standalone();
51 public DbTester db = DbTester.create();
53 private static String GITLAB_URL = "gitlab.com/api/v4";
55 private final Encryption encryption = mock(Encryption.class);
56 private final MultipleAlmFeature multipleAlmFeature = mock(MultipleAlmFeature.class);
58 private WsActionTester ws = new WsActionTester(new UpdateGitlabAction(db.getDbClient(), userSession,
59 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeature)));
62 public void before() {
63 when(multipleAlmFeature.isEnabled()).thenReturn(true);
67 public void update_without_url() {
68 UserDto user = db.users().insertUser();
69 userSession.logIn(user).setSystemAdministrator();
71 TestRequest request = ws.newRequest()
72 .setParam("key", "Gitlab - Dev Team")
73 .setParam("personalAccessToken", "98765432100");
75 Assertions.assertThatThrownBy(request::execute)
76 .isInstanceOf(IllegalArgumentException.class)
77 .hasMessage("The 'url' parameter is missing");
81 public void update_with_url() {
82 UserDto user = db.users().insertUser();
83 userSession.logIn(user).setSystemAdministrator();
85 AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
88 .setParam("key", almSettingDto.getKey())
89 .setParam("url", GITLAB_URL)
90 .setParam("personalAccessToken", "10987654321")
92 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
93 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
94 .containsOnly(tuple(almSettingDto.getKey(), GITLAB_URL, "10987654321"));
98 public void update_with_new_key() {
99 UserDto user = db.users().insertUser();
100 userSession.logIn(user).setSystemAdministrator();
102 AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
105 .setParam("key", almSettingDto.getKey())
106 .setParam("newKey", "Gitlab - Infra Team")
107 .setParam("personalAccessToken", "0123456789")
108 .setParam("url", GITLAB_URL)
111 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
112 .extracting(AlmSettingDto::getKey, s -> s.getDecryptedPersonalAccessToken(encryption), AlmSettingDto::getUrl)
113 .containsOnly(tuple("Gitlab - Infra Team", "0123456789", GITLAB_URL));
117 public void update_without_pat() {
118 UserDto user = db.users().insertUser();
119 userSession.logIn(user).setSystemAdministrator();
121 AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
124 .setParam("key", almSettingDto.getKey())
125 .setParam("url", GITLAB_URL)
127 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
128 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
129 .containsOnly(tuple(almSettingDto.getKey(), GITLAB_URL, almSettingDto.getDecryptedPersonalAccessToken(encryption)));
133 public void fail_when_key_does_not_match_existing_alm_setting() {
134 UserDto user = db.users().insertUser();
135 userSession.logIn(user).setSystemAdministrator();
137 assertThatThrownBy(() -> ws.newRequest()
138 .setParam("key", "unknown")
139 .setParam("personalAccessToken", "0123456789")
140 .setParam("url", GITLAB_URL)
142 .isInstanceOf(NotFoundException.class)
143 .hasMessageContaining("DevOps Platform setting with key 'unknown' cannot be found");
147 public void fail_when_new_key_matches_existing_alm_setting() {
148 UserDto user = db.users().insertUser();
149 userSession.logIn(user).setSystemAdministrator();
150 AlmSettingDto almSetting1 = db.almSettings().insertGitlabAlmSetting();
151 AlmSettingDto almSetting2 = db.almSettings().insertGitlabAlmSetting();
153 assertThatThrownBy(() -> ws.newRequest()
154 .setParam("key", almSetting1.getKey())
155 .setParam("newKey", almSetting2.getKey())
156 .setParam("personalAccessToken", "0123456789")
157 .setParam("url", GITLAB_URL)
159 .isInstanceOf(IllegalArgumentException.class)
160 .hasMessageContaining(format("An DevOps Platform setting with key '%s' already exists", almSetting2.getKey()));
164 public void fail_when_missing_administer_system_permission() {
165 UserDto user = db.users().insertUser();
166 userSession.logIn(user);
167 AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
169 assertThatThrownBy(() -> ws.newRequest()
170 .setParam("key", almSettingDto.getKey())
171 .setParam("newKey", "Gitlab - Infra Team")
172 .setParam("personalAccessToken", "0123456789")
173 .setParam("url", GITLAB_URL)
175 .isInstanceOf(ForbiddenException.class);
179 public void definition() {
180 WebService.Action def = ws.getDef();
182 assertThat(def.since()).isEqualTo("8.1");
183 assertThat(def.isPost()).isTrue();
184 assertThat(def.params())
185 .extracting(WebService.Param::key, WebService.Param::isRequired)
186 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("personalAccessToken", false), tuple("url", true));