]> source.dussan.org Git - sonarqube.git/blob
8b7aa5a1505d927bf24a957f3beffe85f4e400ce
[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.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;
45
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;
49
50 public class AddProjectActionTest {
51
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 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);
67
68   @Test
69   public void definition() {
70     WebService.Action definition = tester.getDef();
71     assertThat(definition.since()).isEqualTo("5.2");
72     assertThat(definition.isPost()).isTrue();
73
74     // parameters
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();
82   }
83
84   @Test
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"));
89
90     TestResponse response = call(project, profile);
91     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
92
93     assertProjectIsAssociatedToProfile(project, profile);
94   }
95
96   @Test
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);
103
104     call(project, qualityProfile);
105
106     assertProjectIsAssociatedToProfile(project, qualityProfile);
107   }
108
109   @Test
110   public void change_association_in_default_organization() {
111     logInAsProfileAdmin();
112
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);
118
119     call(project, profile2);
120
121     assertProjectIsNotAssociatedToProfile(project, profile1);
122     assertProjectIsAssociatedToProfile(project, profile2);
123   }
124
125   @Test
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);
133
134     call(project, profile3Language1);
135
136     assertProjectIsAssociatedToProfile(project, profile3Language1);
137     assertProjectIsAssociatedToProfile(project, profile2Language2);
138   }
139
140   @Test
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);
145
146     call(project, profile);
147
148     assertProjectIsAssociatedToProfile(project, profile);
149   }
150
151   @Test
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"));
156
157     expectedException.expect(ForbiddenException.class);
158     expectedException.expectMessage("Insufficient privileges");
159
160     call(project, profile);
161   }
162
163   @Test
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();
168
169     expectedException.expect(UnauthorizedException.class);
170     expectedException.expectMessage("Authentication is required");
171
172     call(project, profile);
173   }
174
175   @Test
176   public void throw_NotFoundException_if_project_does_not_exist() {
177     logInAsProfileAdmin();
178     QProfileDto profile = db.qualityProfiles().insert();
179
180     expectedException.expect(NotFoundException.class);
181     expectedException.expectMessage("Project 'unknown' not found");
182
183     tester.newRequest()
184       .setParam("project", "unknown")
185       .setParam("profileKey", profile.getKee())
186       .execute();
187   }
188
189   @Test
190   public void throw_NotFoundException_if_profile_does_not_exist() {
191     logInAsProfileAdmin();
192     ComponentDto project = db.components().insertPrivateProject(db.getDefaultOrganization());
193
194     expectedException.expect(NotFoundException.class);
195     expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
196
197     tester.newRequest()
198       .setParam("project", project.getKey())
199       .setParam("language", "xoo")
200       .setParam("qualityProfile", "unknown")
201       .execute();
202   }
203
204   @Test
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();
210
211     expectedException.expect(NotFoundException.class);
212     expectedException.expectMessage(format("Project '%s' not found", branch.getDbKey()));
213
214     tester.newRequest()
215       .setParam("project", branch.getDbKey())
216       .setParam("profileKey", profile.getKee())
217       .execute();
218   }
219
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());
223   }
224
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();
228   }
229
230   private void logInAsProfileAdmin() {
231     userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
232   }
233
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();
240   }
241
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();
249   }
250 }