3 * Copyright (C) 2009-2019 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.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;
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;
52 public class RemoveProjectActionTest {
53 private static final String LANGUAGE_1 = "xoo";
54 private static final String LANGUAGE_2 = "foo";
57 public DbTester db = DbTester.create();
59 public ExpectedException expectedException = ExpectedException.none();
61 public UserSessionRule userSession = UserSessionRule.standalone();
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));
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);
72 public void definition() {
73 WebService.Action definition = ws.getDef();
75 assertThat(definition.since()).isEqualTo("5.2");
76 assertThat(definition.isPost()).isTrue();
77 assertThat(definition.key()).isEqualTo("remove_project");
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");
98 public void remove_profile_from_project_in_default_organization() {
99 logInAsProfileAdmin();
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);
107 TestResponse response = call(project, profileLang1);
108 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
110 assertProjectIsNotAssociatedToProfile(project, profileLang1);
111 assertProjectIsAssociatedToProfile(project, profileLang2);
115 public void removal_does_not_fail_if_profile_is_not_associated_to_project() {
116 logInAsProfileAdmin();
118 ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
119 QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
121 TestResponse response = call(project, profile);
122 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
124 assertProjectIsNotAssociatedToProfile(project, profile);
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);
134 call(project, profile);
136 assertProjectIsNotAssociatedToProfile(project, profile);
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);
149 call(project, profile);
151 assertProjectIsNotAssociatedToProfile(project, profile);
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());
160 expectedException.expect(ForbiddenException.class);
161 expectedException.expectMessage("Insufficient privileges");
163 call(project, profile);
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());
172 expectedException.expect(UnauthorizedException.class);
173 expectedException.expectMessage("Authentication is required");
175 call(project, profile);
179 public void fail_if_project_does_not_exist() {
180 logInAsProfileAdmin();
181 QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization());
183 expectedException.expect(NotFoundException.class);
184 expectedException.expectMessage("Component id 'unknown' not found");
187 .setParam("projectUuid", "unknown")
188 .setParam("profileKey", profile.getKee())
193 public void fail_if_profile_does_not_exist() {
194 logInAsProfileAdmin();
195 ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
197 expectedException.expect(NotFoundException.class);
198 expectedException.expectMessage("Quality Profile with key 'unknown' does not exist");
201 .setParam("projectUuid", project.uuid())
202 .setParam("profileKey", "unknown")
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);
214 expectedException.expect(NotFoundException.class);
215 expectedException.expectMessage(format("Component key '%s' not found", branch.getDbKey()));
218 .setParam("project", branch.getDbKey())
219 .setParam("profileKey", profile.getKee())
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);
231 expectedException.expect(NotFoundException.class);
232 expectedException.expectMessage(format("Component id '%s' not found", branch.uuid()));
235 .setParam("projectUuid", branch.uuid())
236 .setParam("profileKey", profile.getKee())
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());
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();
250 private void logInAsProfileAdmin() {
251 userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES, db.getDefaultOrganization());
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();