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.core.util.UuidFactory;
28 import org.sonar.core.util.UuidFactoryFast;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.permission.GlobalPermission;
31 import org.sonar.db.qualityprofile.QProfileDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.exceptions.BadRequestException;
34 import org.sonar.server.exceptions.ForbiddenException;
35 import org.sonar.server.exceptions.NotFoundException;
36 import org.sonar.server.language.LanguageTesting;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestResponse;
39 import org.sonar.server.ws.WsActionTester;
41 import static java.lang.String.format;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
44 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LANGUAGE;
45 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_LOGIN;
46 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_QUALITY_PROFILE;
48 public class AddUserActionTest {
50 private static final String XOO = "xoo";
51 private static final Languages LANGUAGES = LanguageTesting.newLanguages(XOO);
54 public ExpectedException expectedException = ExpectedException.none();
56 public UserSessionRule userSession = UserSessionRule.standalone();
58 public DbTester db = DbTester.create();
60 private final QProfileWsSupport wsSupport = new QProfileWsSupport(db.getDbClient(), userSession);
61 private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
62 private final WsActionTester ws = new WsActionTester(new AddUserAction(db.getDbClient(), uuidFactory, wsSupport, LANGUAGES));
65 public void test_definition() {
66 WebService.Action def = ws.getDef();
67 assertThat(def.key()).isEqualTo("add_user");
68 assertThat(def.isPost()).isTrue();
69 assertThat(def.isInternal()).isTrue();
70 assertThat(def.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("qualityProfile", "language", "login");
74 public void add_user() {
75 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
76 UserDto user = db.users().insertUser();
77 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
79 TestResponse response = ws.newRequest()
80 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
81 .setParam(PARAM_LANGUAGE, XOO)
82 .setParam(PARAM_LOGIN, user.getLogin())
85 assertThat(response.getStatus()).isEqualTo(204);
86 assertThat(db.getDbClient().qProfileEditUsersDao().exists(db.getSession(), profile, user)).isTrue();
90 public void does_nothing_when_user_can_already_edit_profile() {
91 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
92 UserDto user = db.users().insertUser();
93 db.qualityProfiles().addUserPermission(profile, user);
94 assertThat(db.getDbClient().qProfileEditUsersDao().exists(db.getSession(), profile, user)).isTrue();
95 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
98 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
99 .setParam(PARAM_LANGUAGE, XOO)
100 .setParam(PARAM_LOGIN, user.getLogin())
103 assertThat(db.getDbClient().qProfileEditUsersDao().exists(db.getSession(), profile, user)).isTrue();
107 public void qp_administers_can_add_user() {
108 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
109 UserDto user = db.users().insertUser();
110 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
113 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
114 .setParam(PARAM_LANGUAGE, XOO)
115 .setParam(PARAM_LOGIN, user.getLogin())
118 assertThat(db.getDbClient().qProfileEditUsersDao().exists(db.getSession(), profile, user)).isTrue();
122 public void qp_editors_can_add_user() {
123 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
124 UserDto user = db.users().insertUser();
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_LOGIN, user.getLogin())
135 assertThat(db.getDbClient().qProfileEditUsersDao().exists(db.getSession(), profile, user)).isTrue();
139 public void uses_default_organization_when_no_organization() {
140 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
141 UserDto user = db.users().insertUser();
142 userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
145 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
146 .setParam(PARAM_LANGUAGE, XOO)
147 .setParam(PARAM_LOGIN, user.getLogin())
150 assertThat(db.getDbClient().qProfileEditUsersDao().exists(db.getSession(), profile, user)).isTrue();
154 public void fail_when_user_does_not_exist() {
155 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
156 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
158 expectedException.expect(NotFoundException.class);
159 expectedException.expectMessage("User with login 'unknown' is not found");
162 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
163 .setParam(PARAM_LANGUAGE, XOO)
164 .setParam(PARAM_LOGIN, "unknown")
169 public void fail_when_qprofile_does_not_exist() {
170 UserDto user = db.users().insertUser();
171 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
173 expectedException.expect(NotFoundException.class);
174 expectedException.expectMessage("Quality Profile for language 'xoo' and name 'unknown' does not exist");
177 .setParam(PARAM_QUALITY_PROFILE, "unknown")
178 .setParam(PARAM_LANGUAGE, XOO)
179 .setParam(PARAM_LOGIN, user.getLogin())
184 public void fail_when_wrong_language() {
185 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage("unknown"));
186 UserDto user = db.users().insertUser();
187 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
189 expectedException.expect(NotFoundException.class);
190 expectedException.expectMessage(format("Quality Profile for language 'xoo' and name '%s' does not exist", profile.getName()));
192 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
193 .setParam(PARAM_LANGUAGE, XOO)
194 .setParam(PARAM_LOGIN, user.getLogin())
199 public void fail_when_qp_is_built_in() {
200 UserDto user = db.users().insertUser();
201 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO).setIsBuiltIn(true));
202 userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
204 expectedException.expect(BadRequestException.class);
205 expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s' with language 'xoo'", profile.getName()));
208 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
209 .setParam(PARAM_LANGUAGE, XOO)
210 .setParam(PARAM_LOGIN, user.getLogin())
215 public void fail_when_not_enough_permission() {
216 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(XOO));
217 UserDto user = db.users().insertUser();
218 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_GATES);
220 expectedException.expect(ForbiddenException.class);
223 .setParam(PARAM_QUALITY_PROFILE, profile.getName())
224 .setParam(PARAM_LANGUAGE, XOO)
225 .setParam(PARAM_LOGIN, user.getLogin())