]> source.dussan.org Git - sonarqube.git/blob
7323ec7f2f1de12150a11f454832267e2f8f8491
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2021 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.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;
38
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;
43
44 public class CreateGitlabActionTest {
45
46   @Rule
47   public ExpectedException expectedException = ExpectedException.none();
48   @Rule
49   public UserSessionRule userSession = UserSessionRule.standalone();
50   @Rule
51   public DbTester db = DbTester.create();
52
53   private static String GITLAB_URL = "gitlab.com/api/v4";
54
55   private 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, AlmSettingDto::getPersonalAccessToken)
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     expectedException.expect(IllegalArgumentException.class);
118     expectedException.expectMessage(String.format("An ALM setting with key '%s' already exist", gitlabAlmSetting.getKey()));
119
120     ws.newRequest()
121       .setParam("key", gitlabAlmSetting.getKey())
122       .setParam("personalAccessToken", "98765432100")
123       .setParam("url", GITLAB_URL)
124       .execute();
125   }
126
127   @Test
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();
133
134     expectedException.expect(BadRequestException.class);
135     expectedException.expectMessage("A GITLAB setting is already defined");
136
137     ws.newRequest()
138       .setParam("key", "anotherKey")
139       .setParam("personalAccessToken", "98765432100")
140       .setParam("url", GITLAB_URL)
141       .execute();
142   }
143
144   @Test
145   public void fail_when_missing_administer_system_permission() {
146     UserDto user = db.users().insertUser();
147     userSession.logIn(user);
148
149     expectedException.expect(ForbiddenException.class);
150
151     ws.newRequest()
152       .setParam("key", "Gitlab - Dev Team")
153       .setParam("personalAccessToken", "98765432100")
154       .execute();
155   }
156
157   @Test
158   public void definition() {
159     WebService.Action def = ws.getDef();
160
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));
167   }
168 }