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.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.groups.Tuple.tuple;
41 import static org.mockito.Mockito.mock;
42 import static org.mockito.Mockito.when;
44 public class CreateGitlabActionTest {
47 public ExpectedException expectedException = ExpectedException.none();
49 public UserSessionRule userSession = UserSessionRule.standalone();
51 public DbTester db = DbTester.create();
53 private static String GITLAB_URL = "gitlab.com/api/v4";
55 private 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, AlmSettingDto::getPersonalAccessToken)
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 expectedException.expect(IllegalArgumentException.class);
118 expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", gitlabAlmSetting.getKey()));
121 .setParam("key", gitlabAlmSetting.getKey())
122 .setParam("personalAccessToken", "98765432100")
123 .setParam("url", GITLAB_URL)
128 public void fail_when_no_multiple_instance_allowed() {
129 when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
130 UserDto user = db.users().insertUser();
131 userSession.logIn(user).setSystemAdministrator();
132 db.almSettings().insertGitlabAlmSetting();
134 expectedException.expect(BadRequestException.class);
135 expectedException.expectMessage("A GITLAB setting is already defined");
138 .setParam("key", "anotherKey")
139 .setParam("personalAccessToken", "98765432100")
140 .setParam("url", GITLAB_URL)
145 public void fail_when_missing_administer_system_permission() {
146 UserDto user = db.users().insertUser();
147 userSession.logIn(user);
149 expectedException.expect(ForbiddenException.class);
152 .setParam("key", "Gitlab - Dev Team")
153 .setParam("personalAccessToken", "98765432100")
158 public void definition() {
159 WebService.Action def = ws.getDef();
161 assertThat(def.since()).isEqualTo("8.1");
162 assertThat(def.changelog()).hasSize(1);
163 assertThat(def.isPost()).isTrue();
164 assertThat(def.params())
165 .extracting(WebService.Param::key, WebService.Param::isRequired)
166 .containsExactlyInAnyOrder(tuple("key", true), tuple("personalAccessToken", true), tuple("url", true));