3 * Copyright (C) 2009-2021 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.junit.rules.ExpectedException;
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.MultipleAlmFeatureProvider;
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.groups.Tuple.tuple;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class UpdateGitlabActionTest {
48 public ExpectedException expectedException = ExpectedException.none();
50 public UserSessionRule userSession = UserSessionRule.standalone();
52 public DbTester db = DbTester.create();
54 private static String GITLAB_URL = "gitlab.com/api/v4";
56 private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
58 private WsActionTester ws = new WsActionTester(new UpdateGitlabAction(db.getDbClient(), userSession,
59 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeatureProvider)));
62 public void before() {
63 when(multipleAlmFeatureProvider.enabled()).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, AlmSettingDto::getPersonalAccessToken)
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, AlmSettingDto::getPersonalAccessToken, 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, AlmSettingDto::getPersonalAccessToken)
129 .containsOnly(tuple(almSettingDto.getKey(), GITLAB_URL, almSettingDto.getPersonalAccessToken()));
133 public void fail_when_key_does_not_match_existing_alm_setting() {
134 UserDto user = db.users().insertUser();
135 userSession.logIn(user).setSystemAdministrator();
137 expectedException.expect(NotFoundException.class);
138 expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
141 .setParam("key", "unknown")
142 .setParam("personalAccessToken", "0123456789")
143 .setParam("url", GITLAB_URL)
148 public void fail_when_new_key_matches_existing_alm_setting() {
149 UserDto user = db.users().insertUser();
150 userSession.logIn(user).setSystemAdministrator();
151 AlmSettingDto almSetting1 = db.almSettings().insertGitlabAlmSetting();
152 AlmSettingDto almSetting2 = db.almSettings().insertGitlabAlmSetting();
154 expectedException.expect(IllegalArgumentException.class);
155 expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
158 .setParam("key", almSetting1.getKey())
159 .setParam("newKey", almSetting2.getKey())
160 .setParam("personalAccessToken", "0123456789")
161 .setParam("url", GITLAB_URL)
166 public void fail_when_missing_administer_system_permission() {
167 UserDto user = db.users().insertUser();
168 userSession.logIn(user);
169 AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
171 expectedException.expect(ForbiddenException.class);
174 .setParam("key", almSettingDto.getKey())
175 .setParam("newKey", "Gitlab - Infra Team")
176 .setParam("personalAccessToken", "0123456789")
177 .setParam("url", GITLAB_URL)
182 public void definition() {
183 WebService.Action def = ws.getDef();
185 assertThat(def.since()).isEqualTo("8.1");
186 assertThat(def.isPost()).isTrue();
187 assertThat(def.params())
188 .extracting(WebService.Param::key, WebService.Param::isRequired)
189 .containsExactlyInAnyOrder(tuple("key", true), tuple("newKey", false), tuple("personalAccessToken", false), tuple("url", true));