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;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.mockito.Mockito;
25 import org.sonar.api.server.ws.WebService;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.qualityprofile.QProfileDto;
29 import org.sonar.db.user.GroupDto;
30 import org.sonar.db.user.UserDto;
31 import org.sonar.server.exceptions.BadRequestException;
32 import org.sonar.server.exceptions.ForbiddenException;
33 import org.sonar.server.exceptions.UnauthorizedException;
34 import org.sonar.server.qualityprofile.QProfileRules;
35 import org.sonar.server.rule.ws.RuleQueryFactory;
36 import org.sonar.server.tester.UserSessionRule;
37 import org.sonar.server.ws.TestRequest;
38 import org.sonar.server.ws.WsActionTester;
40 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.assertj.core.api.Assertions.assertThatThrownBy;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.verify;
46 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
47 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
48 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
50 public class ActivateRulesActionIT {
53 public DbTester db = DbTester.create();
55 public UserSessionRule userSession = UserSessionRule.standalone();
57 private DbClient dbClient = db.getDbClient();
58 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
59 private RuleQueryFactory ruleQueryFactory = mock(RuleQueryFactory.class, Mockito.RETURNS_MOCKS);
61 private QProfileRules qProfileRules = mock(QProfileRules.class, Mockito.RETURNS_DEEP_STUBS);
62 private WsActionTester ws = new WsActionTester(new ActivateRulesAction(ruleQueryFactory, userSession, qProfileRules, wsSupport, dbClient));
65 public void define_bulk_activate_rule_action() {
66 WebService.Action definition = ws.getDef();
67 assertThat(definition).isNotNull();
68 assertThat(definition.isPost()).isTrue();
69 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder(
94 "sonarsourceSecurity",
95 "cleanCodeAttributeCategories",
96 "impactSoftwareQualities",
101 public void as_global_qprofile_admin() {
102 userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
103 QProfileDto qualityProfile = db.qualityProfiles().insert();
107 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
110 verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any());
114 public void as_qprofile_editor() {
115 UserDto user = db.users().insertUser();
116 GroupDto group = db.users().insertGroup();
117 QProfileDto qualityProfile = db.qualityProfiles().insert();
118 db.qualityProfiles().addGroupPermission(qualityProfile, group);
119 userSession.logIn(user).setGroups(group);
123 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
126 verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any());
130 public void fail_if_not_logged_in() {
131 TestRequest request = ws.newRequest()
133 .setParam(PARAM_TARGET_KEY, randomAlphanumeric(UUID_SIZE));
135 assertThatThrownBy(() -> request.execute())
136 .isInstanceOf(UnauthorizedException.class);
140 public void fail_if_built_in_profile() {
141 userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
142 QProfileDto qualityProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true));
143 TestRequest request = ws.newRequest()
145 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee());
147 assertThatThrownBy(() -> request.execute())
148 .isInstanceOf(BadRequestException.class);
152 public void fail_if_not_enough_permission() {
153 userSession.logIn(db.users().insertUser());
154 QProfileDto qualityProfile = db.qualityProfiles().insert();
156 assertThatThrownBy(() -> {
159 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
162 .isInstanceOf(ForbiddenException.class);