]> source.dussan.org Git - sonarqube.git/blob
85f27004939258ed1c007c9104c05e826a0dbd29
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.qualityprofile.ws;
21
22 import java.net.HttpURLConnection;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.resources.Languages;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.api.server.ws.WebService.Param;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.util.UuidFactoryFast;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.project.ProjectDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.exceptions.UnauthorizedException;
41 import org.sonar.server.language.LanguageTesting;
42 import org.sonar.server.qualityprofile.QProfileFactoryImpl;
43 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.TestResponse;
46 import org.sonar.server.ws.WsActionTester;
47
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.mockito.Mockito.mock;
50 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
51 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
52 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
53 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
54
55 public class DeleteActionTest {
56
57   private static final String A_LANGUAGE = "xoo";
58
59   @Rule
60   public DbTester db = DbTester.create(System2.INSTANCE);
61   @Rule
62   public ExpectedException expectedException = ExpectedException.none();
63   @Rule
64   public UserSessionRule userSession = UserSessionRule.standalone();
65
66   private DbClient dbClient = db.getDbClient();
67   private DbSession dbSession = db.getSession();
68   private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
69
70   private DeleteAction underTest = new DeleteAction(
71     new Languages(LanguageTesting.newLanguage(A_LANGUAGE)),
72     new QProfileFactoryImpl(dbClient, UuidFactoryFast.getInstance(), System2.INSTANCE, activeRuleIndexer), dbClient, userSession,
73     new QProfileWsSupport(dbClient, userSession));
74   private WsActionTester ws = new WsActionTester(underTest);
75
76   @Test
77   public void delete_profile_by_language_and_name_in_default_organization() {
78     ProjectDto project = db.components().insertPrivateProjectDto();
79     QProfileDto profile1 = createProfile();
80     QProfileDto profile2 = createProfile();
81     db.qualityProfiles().associateWithProject(project, profile1);
82
83     logInAsQProfileAdministrator();
84
85     TestResponse response = ws.newRequest()
86       .setMethod("POST")
87       .setParam(PARAM_LANGUAGE, profile1.getLanguage())
88       .setParam(PARAM_QUALITY_PROFILE, profile1.getName())
89       .execute();
90
91     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
92
93     verifyProfileDoesNotExist(profile1);
94     verifyProfileExists(profile2);
95   }
96
97   @Test
98   public void delete_profile_by_language_and_name_in_specified_organization() {
99     ProjectDto project = db.components().insertPrivateProjectDto();
100     QProfileDto profile1 = createProfile();
101     QProfileDto profile2 = createProfile();
102     db.qualityProfiles().associateWithProject(project, profile1);
103     logInAsQProfileAdministrator();
104
105     TestResponse response = ws.newRequest()
106       .setMethod("POST")
107       .setParam(PARAM_LANGUAGE, profile1.getLanguage())
108       .setParam(PARAM_QUALITY_PROFILE, profile1.getName())
109       .execute();
110     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
111
112     verifyProfileDoesNotExist(profile1);
113     verifyProfileExists(profile2);
114   }
115
116   @Test
117   public void as_qprofile_editor() {
118     QProfileDto profile = createProfile();
119     UserDto user = db.users().insertUser();
120     db.qualityProfiles().addUserPermission(profile, user);
121     userSession.logIn(user);
122
123     TestResponse response = ws.newRequest()
124       .setMethod("POST")
125       .setParam(PARAM_LANGUAGE, profile.getLanguage())
126       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
127       .execute();
128     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
129
130     verifyProfileDoesNotExist(profile);
131   }
132
133   @Test
134   public void fail_if_built_in_profile() {
135     QProfileDto profile1 = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true).setLanguage(A_LANGUAGE));
136     logInAsQProfileAdministrator();
137
138     expectedException.expect(BadRequestException.class);
139
140     ws.newRequest()
141       .setMethod("POST")
142       .setParam(PARAM_LANGUAGE, profile1.getLanguage())
143       .setParam(PARAM_QUALITY_PROFILE, profile1.getName())
144       .execute();
145   }
146
147   @Test
148   public void fail_if_not_profile_administrator() {
149     QProfileDto qprofile = createProfile();
150     userSession.logIn(db.users().insertUser());
151
152     expectedException.expect(ForbiddenException.class);
153     expectedException.expectMessage("Insufficient privileges");
154
155     ws.newRequest()
156       .setMethod("POST")
157       .setParam(PARAM_LANGUAGE, qprofile.getLanguage())
158       .setParam(PARAM_QUALITY_PROFILE, qprofile.getName())
159       .execute();
160   }
161
162   @Test
163   public void fail_if_not_logged_in() {
164     QProfileDto profile = createProfile();
165
166     expectedException.expect(UnauthorizedException.class);
167
168     ws.newRequest()
169       .setMethod("POST")
170       .setParam(PARAM_KEY, profile.getKee())
171       .execute();
172   }
173
174   @Test
175   public void fail_if_missing_parameters() {
176     userSession.logIn();
177
178     expectedException.expect(IllegalArgumentException.class);
179     expectedException.expectMessage("The 'language' parameter is missing");
180
181     ws.newRequest()
182       .setMethod("POST")
183       .execute();
184   }
185
186   @Test
187   public void fail_if_missing_language_parameter() {
188     QProfileDto profile = createProfile();
189     logInAsQProfileAdministrator();
190
191     expectedException.expect(IllegalArgumentException.class);
192     expectedException.expectMessage("The 'language' parameter is missing");
193
194     ws.newRequest()
195       .setMethod("POST")
196       .setParam("profileName", profile.getName())
197       .execute();
198   }
199
200   @Test
201   public void fail_if_missing_name_parameter() {
202     QProfileDto profile = createProfile();
203     logInAsQProfileAdministrator();
204
205     expectedException.expect(IllegalArgumentException.class);
206     expectedException.expectMessage("The 'qualityProfile' parameter is missing");
207
208     ws.newRequest()
209       .setMethod("POST")
210       .setParam(PARAM_LANGUAGE, profile.getLanguage())
211       .execute();
212   }
213
214   @Test
215   public void fail_if_profile_does_not_exist() {
216     userSession.logIn();
217
218     expectedException.expect(NotFoundException.class);
219     expectedException.expectMessage("Quality Profile for language 'xoo' and name 'does_not_exist' does not exist");
220
221     ws.newRequest()
222       .setMethod("POST")
223       .setParam(PARAM_QUALITY_PROFILE, "does_not_exist")
224       .setParam(PARAM_LANGUAGE, "xoo")
225       .execute();
226   }
227
228   @Test
229   public void fail_if_deleting_default_profile() {
230     QProfileDto profile = createProfile();
231     db.qualityProfiles().setAsDefault(profile);
232     logInAsQProfileAdministrator();
233
234     expectedException.expect(IllegalArgumentException.class);
235     expectedException.expectMessage("Profile '" + profile.getName() + "' cannot be deleted because it is marked as default");
236
237     ws.newRequest()
238       .setMethod("POST")
239       .setParam(PARAM_LANGUAGE, profile.getLanguage())
240       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
241       .execute();
242   }
243
244   @Test
245   public void fail_if_a_descendant_is_marked_as_default() {
246     QProfileDto parentProfile = createProfile();
247     QProfileDto childProfile = db.qualityProfiles().insert(p -> p.setLanguage(A_LANGUAGE).setParentKee(parentProfile.getKee()));
248     db.qualityProfiles().setAsDefault(childProfile);
249     logInAsQProfileAdministrator();
250
251     expectedException.expect(IllegalArgumentException.class);
252     expectedException.expectMessage("Profile '" + parentProfile.getName() + "' cannot be deleted because its descendant named '" + childProfile.getName() +
253       "' is marked as default");
254
255     ws.newRequest()
256       .setMethod("POST")
257       .setParam(PARAM_LANGUAGE, parentProfile.getLanguage())
258       .setParam(PARAM_QUALITY_PROFILE, parentProfile.getName())
259       .execute();
260   }
261
262   @Test
263   public void definition() {
264     WebService.Action definition = ws.getDef();
265
266     assertThat(definition.isPost()).isTrue();
267     assertThat(definition.params()).extracting(Param::key).containsExactlyInAnyOrder("language", "qualityProfile");
268   }
269
270   private void logInAsQProfileAdministrator() {
271     userSession
272       .logIn(db.users().insertUser())
273       .addPermission(ADMINISTER_QUALITY_PROFILES);
274   }
275
276   private void verifyProfileDoesNotExist(QProfileDto profile) {
277     assertThat(dbClient.qualityProfileDao().selectByUuid(dbSession, profile.getKee())).isNull();
278     assertThat(dbClient.qualityProfileDao().selectSelectedProjects(dbSession, profile, null)).isEmpty();
279   }
280
281   private void verifyProfileExists(QProfileDto profile) {
282     assertThat(dbClient.qualityProfileDao().selectByUuid(dbSession, profile.getKee())).isNotNull();
283   }
284
285   private QProfileDto createProfile() {
286     return db.qualityProfiles().insert(p -> p.setLanguage(A_LANGUAGE));
287   }
288 }