]> source.dussan.org Git - sonarqube.git/blob
132d8d2dde6cfe2e62b126ba4d86c6bdb5670187
[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.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;
38
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;
44
45 public class UpdateGitlabActionTest {
46
47   @Rule
48   public ExpectedException expectedException = ExpectedException.none();
49   @Rule
50   public UserSessionRule userSession = UserSessionRule.standalone();
51   @Rule
52   public DbTester db = DbTester.create();
53
54   private static String GITLAB_URL = "gitlab.com/api/v4";
55
56   private MultipleAlmFeatureProvider multipleAlmFeatureProvider = mock(MultipleAlmFeatureProvider.class);
57
58   private WsActionTester ws = new WsActionTester(new UpdateGitlabAction(db.getDbClient(), userSession,
59     new AlmSettingsSupport(db.getDbClient(), userSession, new ComponentFinder(db.getDbClient(), null), multipleAlmFeatureProvider)));
60
61   @Before
62   public void before() {
63     when(multipleAlmFeatureProvider.enabled()).thenReturn(true);
64   }
65
66   @Test
67   public void update_without_url() {
68     UserDto user = db.users().insertUser();
69     userSession.logIn(user).setSystemAdministrator();
70
71     TestRequest request = ws.newRequest()
72       .setParam("key", "Gitlab - Dev Team")
73       .setParam("personalAccessToken", "98765432100");
74
75     Assertions.assertThatThrownBy(() -> request.execute())
76       .isInstanceOf(IllegalArgumentException.class)
77       .hasMessage("The 'url' parameter is missing");
78   }
79
80   @Test
81   public void update_with_url() {
82     UserDto user = db.users().insertUser();
83     userSession.logIn(user).setSystemAdministrator();
84
85     AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
86
87     ws.newRequest()
88       .setParam("key", almSettingDto.getKey())
89       .setParam("url", GITLAB_URL)
90       .setParam("personalAccessToken", "10987654321")
91       .execute();
92     assertThat(db.getDbClient().almSettingDao().selectAll(db.getSession()))
93       .extracting(AlmSettingDto::getKey, AlmSettingDto::getUrl, AlmSettingDto::getPersonalAccessToken)
94       .containsOnly(tuple(almSettingDto.getKey(), GITLAB_URL, "10987654321"));
95   }
96
97   @Test
98   public void update_with_new_key() {
99     UserDto user = db.users().insertUser();
100     userSession.logIn(user).setSystemAdministrator();
101
102     AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
103
104     ws.newRequest()
105       .setParam("key", almSettingDto.getKey())
106       .setParam("newKey", "Gitlab - Infra Team")
107       .setParam("personalAccessToken", "0123456789")
108       .setParam("url", GITLAB_URL)
109       .execute();
110
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));
114   }
115
116   @Test
117   public void update_without_pat() {
118     UserDto user = db.users().insertUser();
119     userSession.logIn(user).setSystemAdministrator();
120
121     AlmSettingDto almSettingDto = db.almSettings().insertGitlabAlmSetting();
122
123     ws.newRequest()
124       .setParam("key", almSettingDto.getKey())
125       .setParam("url", GITLAB_URL)
126       .execute();
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()));
130   }
131
132   @Test
133   public void fail_when_key_does_not_match_existing_alm_setting() {
134     UserDto user = db.users().insertUser();
135     userSession.logIn(user).setSystemAdministrator();
136
137     expectedException.expect(NotFoundException.class);
138     expectedException.expectMessage("ALM setting with key 'unknown' cannot be found");
139
140     ws.newRequest()
141       .setParam("key", "unknown")
142       .setParam("personalAccessToken", "0123456789")
143       .setParam("url", GITLAB_URL)
144       .execute();
145   }
146
147   @Test
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();
153
154     expectedException.expect(IllegalArgumentException.class);
155     expectedException.expectMessage(format("An ALM setting with key '%s' already exists", almSetting2.getKey()));
156
157     ws.newRequest()
158       .setParam("key", almSetting1.getKey())
159       .setParam("newKey", almSetting2.getKey())
160       .setParam("personalAccessToken", "0123456789")
161       .setParam("url", GITLAB_URL)
162       .execute();
163   }
164
165   @Test
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();
170
171     expectedException.expect(ForbiddenException.class);
172
173     ws.newRequest()
174       .setParam("key", almSettingDto.getKey())
175       .setParam("newKey", "Gitlab - Infra Team")
176       .setParam("personalAccessToken", "0123456789")
177       .setParam("url", GITLAB_URL)
178       .execute();
179   }
180
181   @Test
182   public void definition() {
183     WebService.Action def = ws.getDef();
184
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));
190   }
191
192 }