3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.ws;
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.project.ProjectDto;
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.tester.UserSessionRule;
43 import org.sonar.server.ws.TestRequest;
44 import org.sonar.server.ws.TestResponse;
45 import org.sonar.server.ws.WsActionTester;
47 import static java.lang.String.format;
48 import static org.assertj.core.api.Assertions.assertThat;
49 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
51 public class RemoveProjectActionTest {
52 private static final String LANGUAGE_1 = "xoo";
53 private static final String LANGUAGE_2 = "foo";
56 public DbTester db = DbTester.create();
58 public ExpectedException expectedException = ExpectedException.none();
60 public UserSessionRule userSession = UserSessionRule.standalone();
62 private DbClient dbClient = db.getDbClient();
63 private Languages languages = LanguageTesting.newLanguages(LANGUAGE_1, LANGUAGE_2);
64 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
66 private RemoveProjectAction underTest = new RemoveProjectAction(dbClient, userSession, languages,
67 new ComponentFinder(dbClient, new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT)), wsSupport);
68 private WsActionTester ws = new WsActionTester(underTest);
71 public void definition() {
72 WebService.Action definition = ws.getDef();
74 assertThat(definition.since()).isEqualTo("5.2");
75 assertThat(definition.isPost()).isTrue();
76 assertThat(definition.key()).isEqualTo("remove_project");
78 assertThat(definition.params()).extracting(WebService.Param::key).containsOnly("qualityProfile", "project", "language");
79 WebService.Param languageParam = definition.param("language");
80 assertThat(languageParam.possibleValues()).containsOnly(LANGUAGE_1, LANGUAGE_2);
81 assertThat(languageParam.exampleValue()).isNull();
82 assertThat(languageParam.deprecatedSince()).isNullOrEmpty();
83 WebService.Param profileName = definition.param("qualityProfile");
84 assertThat(profileName.deprecatedSince()).isNullOrEmpty();
88 public void remove_profile_from_project() {
89 logInAsProfileAdmin();
91 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
92 QProfileDto profileLang1 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
93 QProfileDto profileLang2 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_2));
94 db.qualityProfiles().associateWithProject(project, profileLang1);
95 db.qualityProfiles().associateWithProject(project, profileLang2);
97 TestResponse response = call(project, profileLang1);
98 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
100 assertProjectIsNotAssociatedToProfile(project, profileLang1);
101 assertProjectIsAssociatedToProfile(project, profileLang2);
105 public void removal_does_not_fail_if_profile_is_not_associated_to_project() {
106 logInAsProfileAdmin();
108 ProjectDto project = db.components().insertPrivateProjectDto();
109 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
111 TestResponse response = call(project, profile);
112 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
114 assertProjectIsNotAssociatedToProfile(project, profile);
118 public void project_administrator_can_remove_profile() {
119 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
120 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
121 db.qualityProfiles().associateWithProject(project, profile);
122 userSession.logIn(db.users().insertUser()).addProjectPermission(UserRole.ADMIN, project);
124 call(project, profile);
126 assertProjectIsNotAssociatedToProfile(project, profile);
130 public void as_qprofile_editor() {
131 ProjectDto project = db.components().insertPrivateProjectDto();
132 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
133 db.qualityProfiles().associateWithProject(project, profile);
134 UserDto user = db.users().insertUser();
135 db.qualityProfiles().addUserPermission(profile, user);
136 userSession.logIn(user);
138 call(project, profile);
140 assertProjectIsNotAssociatedToProfile(project, profile);
144 public void fail_if_not_enough_permissions() {
145 userSession.logIn(db.users().insertUser());
146 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
147 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
149 expectedException.expect(ForbiddenException.class);
150 expectedException.expectMessage("Insufficient privileges");
152 call(project, profile);
156 public void fail_if_not_logged_in() {
157 userSession.anonymous();
158 ProjectDto project = db.components().insertPrivateProjectDto();
159 QProfileDto profile = db.qualityProfiles().insert();
161 expectedException.expect(UnauthorizedException.class);
162 expectedException.expectMessage("Authentication is required");
164 call(project, profile);
168 public void fail_if_project_does_not_exist() {
169 logInAsProfileAdmin();
170 QProfileDto profile = db.qualityProfiles().insert();
172 expectedException.expect(NotFoundException.class);
173 expectedException.expectMessage("Project 'unknown' not found");
176 .setParam("project", "unknown")
177 .setParam("profileKey", profile.getKee())
182 public void fail_if_profile_does_not_exist() {
183 logInAsProfileAdmin();
184 ComponentDto project = db.components().insertPrivateProject();
186 expectedException.expect(NotFoundException.class);
187 expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
190 .setParam("project", project.getDbKey())
191 .setParam("language", "xoo")
192 .setParam("qualityProfile", "unknown")
197 public void fail_when_using_branch_db_key() {
198 ComponentDto project = db.components().insertPublicProject();
199 userSession.logIn().addProjectPermission(UserRole.ADMIN, project);
200 ComponentDto branch = db.components().insertProjectBranch(project);
201 QProfileDto profile = db.qualityProfiles().insert();
203 expectedException.expect(NotFoundException.class);
204 expectedException.expectMessage(format("Project '%s' not found", branch.getDbKey()));
207 .setParam("project", branch.getDbKey())
208 .setParam("language", profile.getLanguage())
209 .setParam("qualityProfile", profile.getName())
213 private void assertProjectIsAssociatedToProfile(ProjectDto project, QProfileDto profile) {
214 QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
215 assertThat(loaded.getKee()).isEqualTo(profile.getKee());
218 private void assertProjectIsNotAssociatedToProfile(ProjectDto project, QProfileDto profile) {
219 QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
220 assertThat(loaded == null || !loaded.getKee().equals(profile.getKee())).isTrue();
223 private void logInAsProfileAdmin() {
224 userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
227 private TestResponse call(ProjectDto project, QProfileDto qualityProfile) {
228 TestRequest request = ws.newRequest()
229 .setParam("project", project.getKey())
230 .setParam("language", qualityProfile.getLanguage())
231 .setParam("qualityProfile", qualityProfile.getName());
232 return request.execute();