]> source.dussan.org Git - sonarqube.git/blob
85fc13c707c4f81a7fa0a23a310b216f7d5c79fa
[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 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;
39
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;
45
46 public class RemoveGroupActionTest {
47
48   private static final String XOO = "xoo";
49   private static final Languages LANGUAGES = LanguageTesting.newLanguages(XOO);
50
51   @Rule
52   public ExpectedException expectedException = ExpectedException.none();
53   @Rule
54   public UserSessionRule userSession = UserSessionRule.standalone();
55   @Rule
56   public DbTester db = DbTester.create();
57
58   private final QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession);
59
60   private WsActionTester ws = new WsActionTester(new RemoveGroupAction(db.getDbClient(), wsSupport, LANGUAGES));
61
62   @Test
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");
69   }
70
71   @Test
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);
77
78     TestResponse response = ws.newRequest()
79       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
80       .setParam(PARAM_LANGUAGE, XOO)
81       .setParam(PARAM_GROUP, group.getName())
82       .execute();
83
84     assertThat(response.getStatus()).isEqualTo(204);
85     assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
86   }
87
88   @Test
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);
94
95     ws.newRequest()
96       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
97       .setParam(PARAM_LANGUAGE, XOO)
98       .setParam(PARAM_GROUP, group.getName())
99       .execute();
100
101     assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
102   }
103
104   @Test
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);
110
111     ws.newRequest()
112       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
113       .setParam(PARAM_LANGUAGE, XOO)
114       .setParam(PARAM_GROUP, group.getName())
115       .execute();
116
117     assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
118   }
119
120   @Test
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);
128
129     ws.newRequest()
130       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
131       .setParam(PARAM_LANGUAGE, XOO)
132       .setParam(PARAM_GROUP, group.getName())
133       .execute();
134
135     assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
136   }
137
138   @Test
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);
144
145     ws.newRequest()
146       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
147       .setParam(PARAM_LANGUAGE, XOO)
148       .setParam(PARAM_GROUP, group.getName())
149       .execute();
150
151     assertThat(db.getDbClient().qProfileEditGroupsDao().exists(db.getSession(), profile, group)).isFalse();
152   }
153
154   @Test
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);
158
159     expectedException.expect(NotFoundException.class);
160     expectedException.expectMessage("No group with name 'unknown'");
161
162     ws.newRequest()
163       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
164       .setParam(PARAM_LANGUAGE, XOO)
165       .setParam(PARAM_GROUP, "unknown")
166       .execute();
167   }
168
169   @Test
170   public void fail_when_qprofile_does_not_exist() {
171     GroupDto group = db.users().insertGroup();
172     userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
173
174     expectedException.expect(NotFoundException.class);
175     expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
176
177     ws.newRequest()
178       .setParam(PARAM_QUALITY_PROFILE, "unknown")
179       .setParam(PARAM_LANGUAGE, XOO)
180       .setParam(PARAM_GROUP, group.getName())
181       .execute();
182   }
183
184   @Test
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);
189
190     expectedException.expect(NotFoundException.class);
191     expectedException.expectMessage(format("Quality Profile for language 'xoo' and name '%s' does not exist", profile.getName()));
192
193     ws.newRequest()
194       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
195       .setParam(PARAM_LANGUAGE, XOO)
196       .setParam(PARAM_GROUP, group.getName())
197       .execute();
198   }
199
200   @Test
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);
205
206     expectedException.expect(BadRequestException.class);
207     expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s' with language 'xoo'", profile.getName()));
208
209     ws.newRequest()
210       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
211       .setParam(PARAM_LANGUAGE, XOO)
212       .setParam(PARAM_GROUP, group.getName())
213       .execute();
214   }
215
216   @Test
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);
221
222     expectedException.expect(ForbiddenException.class);
223
224     ws.newRequest()
225       .setParam(PARAM_QUALITY_PROFILE, profile.getName())
226       .setParam(PARAM_LANGUAGE, XOO)
227       .setParam(PARAM_GROUP, group.getName())
228       .execute();
229   }
230 }