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 org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.resources.Languages;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.permission.GlobalPermission;
29 import org.sonar.db.qualityprofile.QProfileDto;
30 import org.sonar.db.user.GroupDto;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.language.LanguageTesting;
36 import org.sonar.server.tester.UserSessionRule;
37 import org.sonar.server.ws.TestResponse;
38 import org.sonar.server.ws.WsActionTester;
40 import static java.lang.String.format;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_GROUP;
43 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
44 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
46 public class RemoveGroupActionTest {
48 private static final String XOO = "xoo";
49 private static final Languages LANGUAGES = LanguageTesting.newLanguages(XOO);
52 public ExpectedException expectedException = ExpectedException.none();
54 public UserSessionRule userSession = UserSessionRule.standalone();
56 public DbTester db = DbTester.create();
58 private final QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession);
60 private WsActionTester ws = new WsActionTester(new RemoveGroupAction(db.getDbClient(), wsSupport, LANGUAGES));
63 public void test_definition() {
64 WebService.Action def = ws.getDef();
65 assertThat(def.key()).isEqualTo("remove_group");
66 assertThat(def.isPost()).isTrue();
67 assertThat(def.isInternal()).isTrue();
68 assertThat(def.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("qualityProfile", "language", "group");
72 public void remove_group() {
73 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
74 GroupDto group = db.users().insertGroup();
75 db.qualityProfiles().addGroupPermission(profile, group);
76 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
78 TestResponse response = ws.newRequest()
79 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
80 .setParam(PARAM_LANGUAGE, XOO)
81 .setParam(PARAM_GROUP, group.getName())
84 assertThat(response.getStatus()).isEqualTo(204);
85 assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
89 public void does_nothing_when_group_cannot_edit_profile() {
90 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
91 GroupDto group = db.users().insertGroup();
92 assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
93 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
96 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
97 .setParam(PARAM_LANGUAGE, XOO)
98 .setParam(PARAM_GROUP, group.getName())
101 assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
105 public void qp_administers_can_remove_group() {
106 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
107 GroupDto group = db.users().insertGroup();
108 db.qualityProfiles().addGroupPermission(profile, group);
109 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
112 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
113 .setParam(PARAM_LANGUAGE, XOO)
114 .setParam(PARAM_GROUP, group.getName())
117 assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
121 public void qp_editors_can_remove_group() {
122 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
123 GroupDto group = db.users().insertGroup();
124 db.qualityProfiles().addGroupPermission(profile, group);
125 UserDto userAllowedToEditProfile = db.users().insertUser();
126 db.qualityProfiles().addUserPermission(profile, userAllowedToEditProfile);
127 userSession.logIn(userAllowedToEditProfile);
130 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
131 .setParam(PARAM_LANGUAGE, XOO)
132 .setParam(PARAM_GROUP, group.getName())
135 assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
139 public void uses_default_organization_when_no_organization() {
140 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
141 GroupDto group = db.users().insertGroup();
142 db.qualityProfiles().addGroupPermission(profile, group);
143 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
146 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
147 .setParam(PARAM_LANGUAGE, XOO)
148 .setParam(PARAM_GROUP, group.getName())
151 assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
155 public void fail_when_group_does_not_exist() {
156 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
157 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
159 expectedException.expect(NotFoundException.class);
160 expectedException.expectMessage("No group with name 'unknown'");
163 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
164 .setParam(PARAM_LANGUAGE, XOO)
165 .setParam(PARAM_GROUP, "unknown")
170 public void fail_when_qprofile_does_not_exist() {
171 GroupDto group = db.users().insertGroup();
172 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
174 expectedException.expect(NotFoundException.class);
175 expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
178 .setParam(PARAM_QUALITY_PROFILE, "unknown")
179 .setParam(PARAM_LANGUAGE, XOO)
180 .setParam(PARAM_GROUP, group.getName())
185 public void fail_when_wrong_language() {
186 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage("unknown"));
187 GroupDto group = db.users().insertGroup();
188 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
190 expectedException.expect(NotFoundException.class);
191 expectedException.expectMessage(format("Quality Profile for language 'xoo' and name '%s' does not exist", profile.getName()));
194 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
195 .setParam(PARAM_LANGUAGE, XOO)
196 .setParam(PARAM_GROUP, group.getName())
201 public void fail_when_qp_is_built_in() {
202 GroupDto group = db.users().insertGroup();
203 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO).setIsBuiltIn(true));
204 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
206 expectedException.expect(BadRequestException.class);
207 expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s' with language 'xoo'", profile.getName()));
210 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
211 .setParam(PARAM_LANGUAGE, XOO)
212 .setParam(PARAM_GROUP, group.getName())
217 public void fail_when_not_enough_permission() {
218 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
219 GroupDto group = db.users().insertGroup();
220 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_GATES);
222 expectedException.expect(ForbiddenException.class);
225 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
226 .setParam(PARAM_LANGUAGE, XOO)
227 .setParam(PARAM_GROUP, group.getName())