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 java.net.HttpURLConnection;
23 import java.util.Collection;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.mockito.ArgumentCaptor;
29 import org.mockito.MockitoAnnotations;
30 import org.sonar.api.rule.RuleKey;
31 import org.sonar.api.server.ws.WebService;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.permission.GlobalPermission;
36 import org.sonar.db.qualityprofile.QProfileDto;
37 import org.sonar.db.rule.RuleDefinitionDto;
38 import org.sonar.db.rule.RuleTesting;
39 import org.sonar.db.user.UserDto;
40 import org.sonar.server.exceptions.BadRequestException;
41 import org.sonar.server.exceptions.UnauthorizedException;
42 import org.sonar.server.qualityprofile.QProfileRules;
43 import org.sonar.server.tester.UserSessionRule;
44 import org.sonar.server.ws.TestRequest;
45 import org.sonar.server.ws.TestResponse;
46 import org.sonar.server.ws.WsActionTester;
48 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.mockito.ArgumentMatchers.any;
51 import static org.mockito.ArgumentMatchers.anyCollection;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.verify;
54 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
55 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
56 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
58 public class DeactivateRuleActionTest {
60 public DbTester db = DbTester.create();
62 public UserSessionRule userSession = UserSessionRule.standalone();
64 public ExpectedException expectedException = ExpectedException.none();
66 private ArgumentCaptor<Collection<String>> ruleUuidsCaptor = ArgumentCaptor.forClass(Collection.class);
67 private DbClient dbClient = db.getDbClient();
68 private QProfileRules qProfileRules = mock(QProfileRules.class);
69 private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
70 private DeactivateRuleAction underTest = new DeactivateRuleAction(dbClient, qProfileRules, userSession, wsSupport);
71 private WsActionTester ws = new WsActionTester(underTest);
74 public void before() {
75 MockitoAnnotations.initMocks(this);
79 public void definition() {
80 WebService.Action definition = ws.getDef();
81 assertThat(definition).isNotNull();
82 assertThat(definition.isPost()).isTrue();
83 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("key", "rule");
87 public void deactivate_rule_in_default_organization() {
88 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
89 QProfileDto qualityProfile = db.qualityProfiles().insert();
90 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
91 TestRequest request = ws.newRequest()
93 .setParam(PARAM_RULE, rule.getKey().toString())
94 .setParam(PARAM_KEY, qualityProfile.getKee());
96 TestResponse response = request.execute();
98 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
99 ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
100 verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleUuidsCaptor.capture());
101 assertThat(ruleUuidsCaptor.getValue()).containsExactly(rule.getUuid());
102 assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
106 public void as_qprofile_editor() {
107 UserDto user = db.users().insertUser();
108 QProfileDto qualityProfile = db.qualityProfiles().insert();
109 db.qualityProfiles().addUserPermission(qualityProfile, user);
110 userSession.logIn(user);
111 RuleKey ruleKey = RuleTesting.randomRuleKey();
112 db.rules().insert(ruleKey);
116 .setParam(PARAM_RULE, ruleKey.toString())
117 .setParam(PARAM_KEY, qualityProfile.getKee())
120 verify(qProfileRules).deactivateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());
124 public void fail_if_not_logged_in() {
125 TestRequest request = ws.newRequest()
127 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
128 .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
130 expectedException.expect(UnauthorizedException.class);
136 public void fail_activate_external_rule() {
137 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
138 QProfileDto qualityProfile = db.qualityProfiles().insert();
139 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
141 TestRequest request = ws.newRequest()
143 .setParam(PARAM_RULE, rule.getKey().toString())
144 .setParam(PARAM_KEY, qualityProfile.getKee());
146 expectedException.expect(BadRequestException.class);
147 expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
153 public void fail_deactivate_if_built_in_profile() {
154 RuleDefinitionDto rule = db.rules().insert();
155 userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
157 QProfileDto qualityProfile = db.qualityProfiles().insert(profile -> profile.setIsBuiltIn(true));
158 TestRequest request = ws.newRequest()
160 .setParam(PARAM_RULE, rule.getKey().toString())
161 .setParam(PARAM_KEY, qualityProfile.getKee());
163 expectedException.expect(BadRequestException.class);