]> source.dussan.org Git - sonarqube.git/blob
046269a7b5feec9958b6561fd76ee2d7221bb647
[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.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;
46
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;
50
51 public class RemoveProjectActionTest {
52   private static final String LANGUAGE_1 = "xoo";
53   private static final String LANGUAGE_2 = "foo";
54
55   @Rule
56   public DbTester db = DbTester.create();
57   @Rule
58   public ExpectedException expectedException = ExpectedException.none();
59   @Rule
60   public UserSessionRule userSession = UserSessionRule.standalone();
61
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);
65
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);
69
70   @Test
71   public void definition() {
72     WebService.Action definition = ws.getDef();
73
74     assertThat(definition.since()).isEqualTo("5.2");
75     assertThat(definition.isPost()).isTrue();
76     assertThat(definition.key()).isEqualTo("remove_project");
77
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();
85   }
86
87   @Test
88   public void remove_profile_from_project() {
89     logInAsProfileAdmin();
90
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);
96
97     TestResponse response = call(project, profileLang1);
98     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
99
100     assertProjectIsNotAssociatedToProfile(project, profileLang1);
101     assertProjectIsAssociatedToProfile(project, profileLang2);
102   }
103
104   @Test
105   public void removal_does_not_fail_if_profile_is_not_associated_to_project() {
106     logInAsProfileAdmin();
107
108     ProjectDto project = db.components().insertPrivateProjectDto();
109     QProfileDto profile = db.qualityProfiles().insert(qp -> qp.setLanguage("xoo"));
110
111     TestResponse response = call(project, profile);
112     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
113
114     assertProjectIsNotAssociatedToProfile(project, profile);
115   }
116
117   @Test
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);
123
124     call(project, profile);
125
126     assertProjectIsNotAssociatedToProfile(project, profile);
127   }
128
129   @Test
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);
137
138     call(project, profile);
139
140     assertProjectIsNotAssociatedToProfile(project, profile);
141   }
142
143   @Test
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"));
148
149     expectedException.expect(ForbiddenException.class);
150     expectedException.expectMessage("Insufficient privileges");
151
152     call(project, profile);
153   }
154
155   @Test
156   public void fail_if_not_logged_in() {
157     userSession.anonymous();
158     ProjectDto project = db.components().insertPrivateProjectDto();
159     QProfileDto profile = db.qualityProfiles().insert();
160
161     expectedException.expect(UnauthorizedException.class);
162     expectedException.expectMessage("Authentication is required");
163
164     call(project, profile);
165   }
166
167   @Test
168   public void fail_if_project_does_not_exist() {
169     logInAsProfileAdmin();
170     QProfileDto profile = db.qualityProfiles().insert();
171
172     expectedException.expect(NotFoundException.class);
173     expectedException.expectMessage("Project 'unknown' not found");
174
175     ws.newRequest()
176       .setParam("project", "unknown")
177       .setParam("profileKey", profile.getKee())
178       .execute();
179   }
180
181   @Test
182   public void fail_if_profile_does_not_exist() {
183     logInAsProfileAdmin();
184     ComponentDto project = db.components().insertPrivateProject();
185
186     expectedException.expect(NotFoundException.class);
187     expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
188
189     ws.newRequest()
190       .setParam("project", project.getDbKey())
191       .setParam("language", "xoo")
192       .setParam("qualityProfile", "unknown")
193       .execute();
194   }
195
196   @Test
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();
202
203     expectedException.expect(NotFoundException.class);
204     expectedException.expectMessage(format("Project '%s' not found", branch.getDbKey()));
205
206     ws.newRequest()
207       .setParam("project", branch.getDbKey())
208       .setParam("language", profile.getLanguage())
209       .setParam("qualityProfile", profile.getName())
210       .execute();
211   }
212
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());
216   }
217
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();
221   }
222
223   private void logInAsProfileAdmin() {
224     userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
225   }
226
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();
233   }
234 }