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.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.MultipleAlmFeatureProvider;
32 import org.sonar.server.component.ComponentFinder;
33 import org.sonar.server.exceptions.BadRequestException;
34 import org.sonar.server.exceptions.ForbiddenException;
35 import org.sonar.server.tester.UserSessionRule;
36 import org.sonar.server.ws.TestRequest;
37 import org.sonar.server.ws.WsActionTester;
39 import static org.assertj.core.api.Assertions.assertThat;
40 import static org.assertj.core.api.Assertions.assertThatThrownBy;
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 CreateGitlabActionTest {
48 public UserSessionRule userSession = UserSessionRule.standalone();
50 public DbTester db = DbTester.create();
52 private static String GITLAB_URL = "gitlab.com/api/v4";
54 private final Encryption encryption = mock(Encryption.class);
55 private final MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
57 private WsActionTester ws = new WsActionTester(new CreateGitlabAction(db.getDbClient(), userSession,
58 new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeatureProvider)));
61 public void before() {
62 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
66 public void create_without_url() {
67 UserDto user = db.users().insertUser();
68 userSession.logIn(user).setSystemAdministrator();
70 TestRequest request = ws.newRequest()
71 .setParam("key", "Gitlab - Dev Team")
72 .setParam("personalAccessToken", "98765432100");
74 Assertions.assertThatThrownBy(request::execute)
75 .isInstanceOf(IllegalArgumentException.class)
76 .hasMessage("The 'url' parameter is missing");
80 public void create_with_url() {
81 UserDto user = db.users().insertUser();
82 userSession.logIn(user).setSystemAdministrator();
85 .setParam("key", "Gitlab - Dev Team")
86 .setParam("url", GITLAB_URL)
87 .setParam("personalAccessToken", "98765432100")
90 assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
91 .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, s -> s.getDecryptedPersonalAccessToken(encryption))
92 .containsOnly(tuple("Gitlab - Dev Team", GITLAB_URL, "98765432100"));
96 public void create_with_empty_url() {
97 UserDto user = db.users().insertUser();
98 userSession.logIn(user).setSystemAdministrator();
100 TestRequest request = ws.newRequest()
101 .setParam("key", "Gitlab - Dev Team")
103 .setParam("personalAccessToken", "98765432100");
105 Assertions.assertThatThrownBy(request::execute)
106 .isInstanceOf(IllegalArgumentException.class)
107 .hasMessage("The 'url' parameter is missing");
111 public void fail_when_key_is_already_used() {
112 when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
113 UserDto user = db.users().insertUser();
114 userSession.logIn(user).setSystemAdministrator();
115 AlmSettingDto gitlabAlmSetting = db.almSettings().insertGitlabAlmSetting();
117 assertThatThrownBy(() -> ws.newRequest()
118 .setParam("key", gitlabAlmSetting.getKey())
119 .setParam("personalAccessToken", "98765432100")
120 .setParam("url", GITLAB_URL)
122 .isInstanceOf(IllegalArgumentException.class)
123 .hasMessageContaining(String.format("An ALM setting with key '%s' already exist", gitlabAlmSetting.getKey()));
127 public void fail_when_no_multiple_instance_allowed() {
128 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
129 UserDto user = db.users().insertUser();
130 userSession.logIn(user).setSystemAdministrator();
131 db.almSettings().insertGitlabAlmSetting();
133 assertThatThrownBy(() -> ws.newRequest()
134 .setParam("key", "anotherKey")
135 .setParam("personalAccessToken", "98765432100")
136 .setParam("url", GITLAB_URL)
138 .isInstanceOf(BadRequestException.class)
139 .hasMessageContaining("A GITLAB setting is already defined");
143 public void fail_when_missing_administer_system_permission() {
144 UserDto user = db.users().insertUser();
145 userSession.logIn(user);
147 assertThatThrownBy(() -> ws.newRequest()
148 .setParam("key", "Gitlab - Dev Team")
149 .setParam("personalAccessToken", "98765432100")
151 .isInstanceOf(ForbiddenException.class);
155 public void definition() {
156 WebService.Action def = ws.getDef();
158 assertThat(def.since()).isEqualTo("8.1");
159 assertThat(def.changelog()).hasSize(1);
160 assertThat(def.isPost()).isTrue();
161 assertThat(def.params())
162 .extracting(WebService.Param::key, WebService.Param::isRequired)
163 .containsExactlyInAnyOrder(tuple("key", true), tuple("personalAccessToken", true), tuple("url", true));