3 * Copyright (C) 2009-2024 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;
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.RegisterExtension;
25 import org.mockito.Mockito;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbTester;
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.UnauthorizedException;
35 import org.sonar.server.qualityprofile.QProfileRules;
36 import org.sonar.server.rule.ws.RuleQueryFactory;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestRequest;
39 import org.sonar.server.ws.WsActionTester;
41 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.mockito.ArgumentMatchers.any;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.verify;
47 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
48 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
49 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
51 class ActivateRulesActionIT {
54 public DbTester db = DbTester.create();
56 public UserSessionRule userSession = UserSessionRule.standalone();
58 private DbClient dbClient = db.getDbClient();
59 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
60 private RuleQueryFactory ruleQueryFactory = mock(RuleQueryFactory.class, Mockito.RETURNS_MOCKS);
62 private QProfileRules qProfileRules = mock(QProfileRules.class, Mockito.RETURNS_DEEP_STUBS);
63 private WsActionTester ws = new WsActionTester(new ActivateRulesAction(ruleQueryFactory, userSession, qProfileRules, wsSupport,
67 void define_bulk_activate_rule_action() {
68 WebService.Action definition = ws.getDef();
69 assertThat(definition).isNotNull();
70 assertThat(definition.isPost()).isTrue();
71 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder(
97 "sonarsourceSecurity",
98 "cleanCodeAttributeCategories",
99 "impactSoftwareQualities",
104 void as_global_qprofile_admin() {
105 userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
106 QProfileDto qualityProfile = db.qualityProfiles().insert();
110 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
113 verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any(), any());
117 void as_qprofile_editor() {
118 UserDto user = db.users().insertUser();
119 GroupDto group = db.users().insertGroup();
120 QProfileDto qualityProfile = db.qualityProfiles().insert();
121 db.qualityProfiles().addGroupPermission(qualityProfile, group);
122 userSession.logIn(user).setGroups(group);
126 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
129 verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any(), any());
133 void fail_if_not_logged_in() {
134 TestRequest request = ws.newRequest()
136 .setParam(PARAM_TARGET_KEY, randomAlphanumeric(UUID_SIZE));
138 assertThatThrownBy(() -> request.execute())
139 .isInstanceOf(UnauthorizedException.class);
143 void fail_if_built_in_profile() {
144 userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
145 QProfileDto qualityProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true));
146 TestRequest request = ws.newRequest()
148 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee());
150 assertThatThrownBy(() -> request.execute())
151 .isInstanceOf(BadRequestException.class);
155 void fail_if_not_enough_permission() {
156 userSession.logIn(db.users().insertUser());
157 QProfileDto qualityProfile = db.qualityProfiles().insert();
159 assertThatThrownBy(() -> {
162 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
165 .isInstanceOf(ForbiddenException.class);