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.server.ws.WebService;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.db.DbClient;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.organization.OrganizationDto;
33 import org.sonar.db.project.ProjectDto;
34 import org.sonar.db.qualityprofile.QProfileDto;
35 import org.sonar.db.user.UserDto;
36 import org.sonar.server.component.TestComponentFinder;
37 import org.sonar.server.exceptions.ForbiddenException;
38 import org.sonar.server.exceptions.NotFoundException;
39 import org.sonar.server.exceptions.UnauthorizedException;
40 import org.sonar.server.language.LanguageTesting;
41 import org.sonar.server.tester.UserSessionRule;
42 import org.sonar.server.ws.TestRequest;
43 import org.sonar.server.ws.TestResponse;
44 import org.sonar.server.ws.WsActionTester;
46 import static java.lang.String.format;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
50 public class AddProjectActionTest {
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 QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
65 private AddProjectAction underTest = new AddProjectAction(dbClient, userSession, languages, TestComponentFinder.from(db), wsSupport);
66 private WsActionTester tester = new WsActionTester(underTest);
69 public void definition() {
70 WebService.Action definition = tester.getDef();
71 assertThat(definition.since()).isEqualTo("5.2");
72 assertThat(definition.isPost()).isTrue();
75 assertThat(definition.params()).extracting(WebService.Param::key)
76 .containsExactlyInAnyOrder("qualityProfile", "project", "language");
77 WebService.Param project = definition.param("project");
78 assertThat(project.isRequired()).isTrue();
79 WebService.Param languageParam = definition.param("language");
80 assertThat(languageParam.possibleValues()).containsOnly(LANGUAGE_1, LANGUAGE_2);
81 assertThat(languageParam.exampleValue()).isNull();
85 public void add_project_on_profile() {
86 logInAsProfileAdmin();
87 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
88 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
90 TestResponse response = call(project, profile);
91 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
93 assertProjectIsAssociatedToProfile(project, profile);
97 public void as_qprofile_editor() {
98 UserDto user = db.users().insertUser();
99 QProfileDto qualityProfile = db.qualityProfiles().insert(qp -> qp.setLanguage(LANGUAGE_1));
100 db.qualityProfiles().addUserPermission(qualityProfile, user);
101 ProjectDto project = db.components().insertPrivateProjectDto();
102 userSession.logIn(user);
104 call(project, qualityProfile);
106 assertProjectIsAssociatedToProfile(project, qualityProfile);
110 public void change_association_in_default_organization() {
111 logInAsProfileAdmin();
113 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
114 // two profiles on same language
115 QProfileDto profile1 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
116 QProfileDto profile2 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
117 db.qualityProfiles().associateWithProject(project, profile1);
119 call(project, profile2);
121 assertProjectIsNotAssociatedToProfile(project, profile1);
122 assertProjectIsAssociatedToProfile(project, profile2);
126 public void changing_association_does_not_change_other_language_associations() {
127 logInAsProfileAdmin();
128 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
129 QProfileDto profile1Language1 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
130 QProfileDto profile2Language2 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_2));
131 QProfileDto profile3Language1 = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE_1));
132 db.qualityProfiles().associateWithProject(project, profile1Language1, profile2Language2);
134 call(project, profile3Language1);
136 assertProjectIsAssociatedToProfile(project, profile3Language1);
137 assertProjectIsAssociatedToProfile(project, profile2Language2);
141 public void project_administrator_can_change_profile() {
142 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
143 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
144 userSession.logIn(db.users().insertUser()).addProjectPermission(UserRole.ADMIN, project);
146 call(project, profile);
148 assertProjectIsAssociatedToProfile(project, profile);
152 public void throw_ForbiddenException_if_not_project_nor_organization_administrator() {
153 userSession.logIn(db.users().insertUser());
154 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
155 QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
157 expectedException.expect(ForbiddenException.class);
158 expectedException.expectMessage("Insufficient privileges");
160 call(project, profile);
164 public void throw_UnauthorizedException_if_not_logged_in() {
165 userSession.anonymous();
166 ProjectDto project = db.components().insertPrivateProjectDto(db.getDefaultOrganization());
167 QProfileDto profile = db.qualityProfiles().insert();
169 expectedException.expect(UnauthorizedException.class);
170 expectedException.expectMessage("Authentication is required");
172 call(project, profile);
176 public void throw_NotFoundException_if_project_does_not_exist() {
177 logInAsProfileAdmin();
178 QProfileDto profile = db.qualityProfiles().insert();
180 expectedException.expect(NotFoundException.class);
181 expectedException.expectMessage("Project 'unknown' not found");
184 .setParam("project", "unknown")
185 .setParam("profileKey", profile.getKee())
190 public void throw_NotFoundException_if_profile_does_not_exist() {
191 logInAsProfileAdmin();
192 ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
194 expectedException.expect(NotFoundException.class);
195 expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
198 .setParam("project", project.getKey())
199 .setParam("language", "xoo")
200 .setParam("qualityProfile", "unknown")
205 public void fail_when_using_branch_db_key() {
206 ComponentDto project = db.components().insertPublicProject();
207 userSession.logIn(db.users().insertUser()).addProjectPermission(UserRole.ADMIN, project);
208 ComponentDto branch = db.components().insertProjectBranch(project);
209 QProfileDto profile = db.qualityProfiles().insert();
211 expectedException.expect(NotFoundException.class);
212 expectedException.expectMessage(format("Project '%s' not found", branch.getDbKey()));
215 .setParam("project", branch.getDbKey())
216 .setParam("profileKey", profile.getKee())
220 private void assertProjectIsAssociatedToProfile(ProjectDto project, QProfileDto profile) {
221 QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
222 assertThat(loaded.getKee()).isEqualTo(profile.getKee());
225 private void assertProjectIsNotAssociatedToProfile(ProjectDto project, QProfileDto profile) {
226 QProfileDto loaded = dbClient.qualityProfileDao().selectAssociatedToProjectAndLanguage(db.getSession(), project, profile.getLanguage());
227 assertThat(loaded == null || !loaded.getKee().equals(profile.getKee())).isTrue();
230 private void logInAsProfileAdmin() {
231 userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
234 private TestResponse call(ProjectDto project, QProfileDto qualityProfile) {
235 TestRequest request = tester.newRequest()
236 .setParam("project", project.getKey())
237 .setParam("language", qualityProfile.getLanguage())
238 .setParam("qualityProfile", qualityProfile.getName());
239 return request.execute();
242 private TestResponse call(OrganizationDto organization, ProjectDto project, QProfileDto qualityProfile) {
243 TestRequest request = tester.newRequest()
244 .setParam("organization", organization.getKey())
245 .setParam("project", project.getKey())
246 .setParam("language", qualityProfile.getLanguage())
247 .setParam("qualityProfile", qualityProfile.getName());
248 return request.execute();