]> source.dussan.org Git - sonarqube.git/blob
9a397d0b482a01400435f173b261afda41e1e797
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.resources.Qualifiers;
28 import org.sonar.api.server.ws.WebService;
29 import org.sonar.api.web.UserRole;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbTester;
32 import org.sonar.db.component.ComponentDto;
33 import org.sonar.db.component.ResourceTypesRule;
34 import org.sonar.db.organization.OrganizationDto;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.db.user.UserDto;
37 import org.sonar.server.component.ComponentFinder;
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.organization.TestDefaultOrganizationProvider;
43 import org.sonar.server.tester.UserSessionRule;
44 import org.sonar.server.ws.TestRequest;
45 import org.sonar.server.ws.TestResponse;
46 import org.sonar.server.ws.WsActionTester;
47
48 import static java.lang.String.format;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
51
52 public class RemoveProjectActionTest {
53   private static final String LANGUAGE_1 = "xoo";
54   private static final String LANGUAGE_2 = "foo";
55
56   @Rule
57   public DbTester db = DbTester.create();
58   @Rule
59   public ExpectedException expectedException = ExpectedException.none();
60   @Rule
61   public UserSessionRule userSession = UserSessionRule.standalone();
62
63   private DbClient dbClient = db.getDbClient();
64   private Languages languages = LanguageTesting.newLanguages(LANGUAGE_1, LANGUAGE_2);
65   private QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db));
66
67   private RemoveProjectAction underTest = new RemoveProjectAction(dbClient, userSession, languages,
68       new ComponentFinder(dbClient, new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT)), wsSupport);
69   private WsActionTester ws = new WsActionTester(underTest);
70
71   @Test
72   public void definition() {
73     WebService.Action definition = ws.getDef();
74
75     assertThat(definition.since()).isEqualTo("5.2");
76     assertThat(definition.isPost()).isTrue();
77     assertThat(definition.key()).isEqualTo("remove_project");
78
79     assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("key", "qualityProfile", "project", "language", "projectUuid", "organization");
80     WebService.Param languageParam = definition.param("language");
81     assertThat(languageParam.possibleValues()).containsOnly(LANGUAGE_1, LANGUAGE_2);
82     assertThat(languageParam.exampleValue()).isNull();
83     assertThat(languageParam.deprecatedSince()).isNullOrEmpty();
84     WebService.Param organizationParam = definition.param("organization");
85     assertThat(organizationParam.since()).isEqualTo("6.4");
86     assertThat(organizationParam.isInternal()).isTrue();
87     WebService.Param profile = definition.param("key");
88     assertThat(profile.deprecatedKey()).isEqualTo("profileKey");
89     WebService.Param profileName = definition.param("qualityProfile");
90     assertThat(profileName.deprecatedSince()).isNullOrEmpty();
91     WebService.Param project = definition.param("project");
92     assertThat(project.deprecatedKey()).isEqualTo("projectKey");
93     WebService.Param projectUuid = definition.param("projectUuid");
94     assertThat(projectUuid.deprecatedSince()).isEqualTo("6.5");
95   }
96
97   @Test
98   public void remove_profile_from_project_in_default_organization() {
99     logInAsProfileAdmin();
100
101     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
102     QProfileDto profileLang1 = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE_1));
103     QProfileDto profileLang2 = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE_2));
104     db.qualityProfiles().associateWithProject(project, profileLang1);
105     db.qualityProfiles().associateWithProject(project, profileLang2);
106
107     TestResponse response = call(project, profileLang1);
108     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
109
110     assertProjectIsNotAssociatedToProfile(project, profileLang1);
111     assertProjectIsAssociatedToProfile(project, profileLang2);
112   }
113
114   @Test
115   public void removal_does_not_fail_if_profile_is_not_associated_to_project() {
116     logInAsProfileAdmin();
117
118     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
119     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
120
121     TestResponse response = call(project, profile);
122     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
123
124     assertProjectIsNotAssociatedToProfile(project, profile);
125   }
126
127   @Test
128   public void project_administrator_can_remove_profile() {
129     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
130     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
131     db.qualityProfiles().associateWithProject(project, profile);
132     userSession.logIn(db.users().insertUser()).addProjectPermission(UserRole.ADMIN, project);
133
134     call(project, profile);
135
136     assertProjectIsNotAssociatedToProfile(project, profile);
137   }
138
139   @Test
140   public void as_qprofile_editor() {
141     OrganizationDto organization = db.organizations().insert();
142     ComponentDto project = db.components().insertPrivateProject(organization);
143     QProfileDto profile = db.qualityProfiles().insert(organization, p -> p.setLanguage(LANGUAGE_1));
144     db.qualityProfiles().associateWithProject(project, profile);
145     UserDto user = db.users().insertUser();
146     db.qualityProfiles().addUserPermission(profile, user);
147     userSession.logIn(user);
148
149     call(project, profile);
150
151     assertProjectIsNotAssociatedToProfile(project, profile);
152   }
153
154   @Test
155   public void fail_if_not_enough_permissions() {
156     userSession.logIn(db.users().insertUser());
157     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
158     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
159
160     expectedException.expect(ForbiddenException.class);
161     expectedException.expectMessage("Insufficient privileges");
162
163     call(project, profile);
164   }
165
166   @Test
167   public void fail_if_not_logged_in() {
168     userSession.anonymous();
169     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
170     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
171
172     expectedException.expect(UnauthorizedException.class);
173     expectedException.expectMessage("Authentication is required");
174
175     call(project, profile);
176   }
177
178   @Test
179   public void fail_if_project_does_not_exist() {
180     logInAsProfileAdmin();
181     QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
182
183     expectedException.expect(NotFoundException.class);
184     expectedException.expectMessage("Component id 'unknown' not found");
185
186     ws.newRequest()
187       .setParam("projectUuid", "unknown")
188       .setParam("profileKey", profile.getKee())
189       .execute();
190   }
191
192   @Test
193   public void fail_if_profile_does_not_exist() {
194     logInAsProfileAdmin();
195     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
196
197     expectedException.expect(NotFoundException.class);
198     expectedException.expectMessage("Quality Profile with key 'unknown' does not exist");
199
200     ws.newRequest()
201       .setParam("projectUuid", project.uuid())
202       .setParam("profileKey", "unknown")
203       .execute();
204   }
205
206   @Test
207   public void fail_when_using_branch_db_key() throws Exception {
208     OrganizationDto organization = db.organizations().insert();
209     ComponentDto project = db.components().insertMainBranch(organization);
210     userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
211     ComponentDto branch = db.components().insertProjectBranch(project);
212     QProfileDto profile = db.qualityProfiles().insert(organization);
213
214     expectedException.expect(NotFoundException.class);
215     expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
216
217     ws.newRequest()
218       .setParam("project", branch.getDbKey())
219       .setParam("profileKey", profile.getKee())
220       .execute();
221   }
222
223   @Test
224   public void fail_when_using_branch_uuid() {
225     OrganizationDto organization = db.organizations().insert();
226     ComponentDto project = db.components().insertMainBranch(organization);
227     userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
228     ComponentDto branch = db.components().insertProjectBranch(project);
229     QProfileDto profile = db.qualityProfiles().insert(organization);
230
231     expectedException.expect(NotFoundException.class);
232     expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
233
234     ws.newRequest()
235       .setParam("projectUuid", branch.uuid())
236       .setParam("profileKey", profile.getKee())
237       .execute();
238   }
239
240   private void assertProjectIsAssociatedToProfile(ComponentDto project, QProfileDto profile) {
241     QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
242     assertThat(loaded.getKee()).isEqualTo(profile.getKee());
243   }
244
245   private void assertProjectIsNotAssociatedToProfile(ComponentDto project, QProfileDto profile) {
246     QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
247     assertThat(loaded == null || !loaded.getKee().equals(profile.getKee())).isTrue();
248   }
249
250   private void logInAsProfileAdmin() {
251     userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES, db.getDefaultOrganization());
252   }
253
254   private TestResponse call(ComponentDto project, QProfileDto qualityProfile) {
255     TestRequest request = ws.newRequest()
256       .setParam("projectUuid", project.uuid())
257       .setParam("profileKey", qualityProfile.getKee());
258     return request.execute();
259   }
260 }