]> source.dussan.org Git - sonarqube.git/blob
d87093c067a8f0f9bc80b167599e29a8b59bbf3c
[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.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;
38
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;
44
45 public class CreateGitlabActionTest {
46
47   @Rule
48   public UserSessionRule userSession = UserSessionRule.standalone();
49   @Rule
50   public DbTester db = DbTester.create();
51
52   private static String GITLAB_URL = "gitlab.com/api/v4";
53
54   private final Encryption encryption = mock(Encryption.class);
55   private final MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
56
57   private WsActionTester ws = new WsActionTester(new CreateGitlabAction(db.getDbClient(), userSession,
58     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeatureProvider)));
59
60   @Before
61   public void before() {
62     when(multipleAlmFeatureProvider.enabled()).thenReturn(false);
63   }
64
65   @Test
66   public void create_without_url() {
67     UserDto user = db.users().insertUser();
68     userSession.logIn(user).setSystemAdministrator();
69
70     TestRequest request = ws.newRequest()
71       .setParam("key", "Gitlab - Dev Team")
72       .setParam("personalAccessToken", "98765432100");
73
74     Assertions.assertThatThrownBy(request::execute)
75       .isInstanceOf(IllegalArgumentException.class)
76       .hasMessage("The 'url' parameter is missing");
77   }
78
79   @Test
80   public void create_with_url() {
81     UserDto user = db.users().insertUser();
82     userSession.logIn(user).setSystemAdministrator();
83
84     ws.newRequest()
85       .setParam("key", "Gitlab - Dev Team")
86       .setParam("url", GITLAB_URL)
87       .setParam("personalAccessToken", "98765432100")
88       .execute();
89
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"));
93   }
94
95   @Test
96   public void create_with_empty_url() {
97     UserDto user = db.users().insertUser();
98     userSession.logIn(user).setSystemAdministrator();
99
100     TestRequest request = ws.newRequest()
101       .setParam("key", "Gitlab - Dev Team")
102       .setParam("url", "")
103       .setParam("personalAccessToken", "98765432100");
104
105     Assertions.assertThatThrownBy(request::execute)
106       .isInstanceOf(IllegalArgumentException.class)
107       .hasMessage("The 'url' parameter is missing");
108   }
109
110   @Test
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();
116
117     assertThatThrownBy(() -> ws.newRequest()
118       .setParam("key", gitlabAlmSetting.getKey())
119       .setParam("personalAccessToken", "98765432100")
120       .setParam("url", GITLAB_URL)
121       .execute())
122       .isInstanceOf(IllegalArgumentException.class)
123       .hasMessageContaining(String.format("An ALM setting with key '%s' already exist", gitlabAlmSetting.getKey()));
124   }
125
126   @Test
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();
132
133     assertThatThrownBy(() -> ws.newRequest()
134       .setParam("key", "anotherKey")
135       .setParam("personalAccessToken", "98765432100")
136       .setParam("url", GITLAB_URL)
137       .execute())
138       .isInstanceOf(BadRequestException.class)
139       .hasMessageContaining("A GITLAB setting is already defined");
140   }
141
142   @Test
143   public void fail_when_missing_administer_system_permission() {
144     UserDto user = db.users().insertUser();
145     userSession.logIn(user);
146
147     assertThatThrownBy(() -> ws.newRequest()
148       .setParam("key", "Gitlab - Dev Team")
149       .setParam("personalAccessToken", "98765432100")
150       .execute())
151       .isInstanceOf(ForbiddenException.class);
152   }
153
154   @Test
155   public void definition() {
156     WebService.Action def = ws.getDef();
157
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));
164   }
165 }