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.Captor;
30 import org.mockito.MockitoAnnotations;
31 import org.sonar.api.rule.RuleKey;
32 import org.sonar.api.server.ws.WebService;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.organization.OrganizationDto;
37 import org.sonar.db.permission.OrganizationPermission;
38 import org.sonar.db.qualityprofile.QProfileDto;
39 import org.sonar.db.rule.RuleDefinitionDto;
40 import org.sonar.db.rule.RuleTesting;
41 import org.sonar.db.user.UserDto;
42 import org.sonar.server.exceptions.BadRequestException;
43 import org.sonar.server.exceptions.ForbiddenException;
44 import org.sonar.server.exceptions.UnauthorizedException;
45 import org.sonar.server.organization.TestDefaultOrganizationProvider;
46 import org.sonar.server.qualityprofile.QProfileRules;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.ws.TestRequest;
49 import org.sonar.server.ws.TestResponse;
50 import org.sonar.server.ws.WsActionTester;
52 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
53 import static org.assertj.core.api.Assertions.assertThat;
54 import static org.mockito.ArgumentMatchers.any;
55 import static org.mockito.ArgumentMatchers.anyCollection;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.verify;
58 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
59 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
60 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
61 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
63 public class DeactivateRuleActionTest {
65 public DbTester db = DbTester.create();
67 public UserSessionRule userSession = UserSessionRule.standalone();
69 public ExpectedException expectedException = ExpectedException.none();
71 public ArgumentCaptor<Collection<Integer>> ruleIdsCaptor;
73 private DbClient dbClient = db.getDbClient();
74 private QProfileRules qProfileRules = mock(QProfileRules.class);
75 private QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db));
76 private DeactivateRuleAction underTest = new DeactivateRuleAction(dbClient, qProfileRules, userSession, wsSupport);
77 private WsActionTester ws = new WsActionTester(underTest);
78 private OrganizationDto defaultOrganization;
79 private OrganizationDto organization;
82 public void before() {
83 MockitoAnnotations.initMocks(this);
84 defaultOrganization = db.getDefaultOrganization();
85 organization = db.organizations().insert();
89 public void definition() {
90 WebService.Action definition = ws.getDef();
91 assertThat(definition).isNotNull();
92 assertThat(definition.isPost()).isTrue();
93 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("key", "rule");
97 public void deactivate_rule_in_default_organization() {
98 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
99 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
100 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
101 TestRequest request = ws.newRequest()
103 .setParam(PARAM_RULE, rule.getKey().toString())
104 .setParam(PARAM_KEY, qualityProfile.getKee());
106 TestResponse response = request.execute();
108 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
109 ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
110 verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleIdsCaptor.capture());
111 assertThat(ruleIdsCaptor.getValue()).containsExactly(rule.getId());
112 assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
116 public void deactivate_rule_in_specific_organization() {
117 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
118 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
119 RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
120 TestRequest request = ws.newRequest()
122 .setParam("organization", organization.getKey())
123 .setParam(PARAM_RULE, rule.getKey().toString())
124 .setParam(PARAM_KEY, qualityProfile.getKee());
126 TestResponse response = request.execute();
128 assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
129 ArgumentCaptor<QProfileDto> qProfileDtoCaptor = ArgumentCaptor.forClass(QProfileDto.class);
130 verify(qProfileRules).deactivateAndCommit(any(DbSession.class), qProfileDtoCaptor.capture(), ruleIdsCaptor.capture());
131 assertThat(ruleIdsCaptor.getValue()).containsExactly(rule.getId());
132 assertThat(qProfileDtoCaptor.getValue().getKee()).isEqualTo(qualityProfile.getKee());
136 public void as_qprofile_editor() {
137 UserDto user = db.users().insertUser();
138 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
139 db.qualityProfiles().addUserPermission(qualityProfile, user);
140 userSession.logIn(user);
141 RuleKey ruleKey = RuleTesting.randomRuleKey();
142 db.rules().insert(ruleKey);
146 .setParam(PARAM_ORGANIZATION, organization.getKey())
147 .setParam(PARAM_RULE, ruleKey.toString())
148 .setParam(PARAM_KEY, qualityProfile.getKee())
151 verify(qProfileRules).deactivateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());
155 public void fail_if_not_logged_in() {
156 TestRequest request = ws.newRequest()
158 .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
159 .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
161 expectedException.expect(UnauthorizedException.class);
167 public void fail_if_not_organization_quality_profile_administrator() {
168 RuleDefinitionDto rule = db.rules().insert();
169 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
170 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
171 TestRequest request = ws.newRequest()
173 .setParam(PARAM_RULE, rule.getKey().toString())
174 .setParam(PARAM_KEY, qualityProfile.getKee());
176 expectedException.expect(ForbiddenException.class);
182 public void fail_activate_external_rule() {
183 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
184 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
185 RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
187 TestRequest request = ws.newRequest()
189 .setParam(PARAM_RULE, rule.getKey().toString())
190 .setParam(PARAM_KEY, qualityProfile.getKee());
192 expectedException.expect(BadRequestException.class);
193 expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
199 public void fail_deactivate_if_built_in_profile() {
200 RuleDefinitionDto rule = db.rules().insert();
201 userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
203 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true));
204 TestRequest request = ws.newRequest()
206 .setParam(PARAM_RULE, rule.getKey().toString())
207 .setParam(PARAM_KEY, qualityProfile.getKee());
209 expectedException.expect(BadRequestException.class);