]> source.dussan.org Git - sonarqube.git/blob
78a907c433877c6ce59bd549de65a5b5f372ae7a
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualityprofile.ws;
21
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;
47
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;
57
58 public class DeactivateRuleActionTest {
59   @Rule
60   public DbTester db = DbTester.create();
61   @Rule
62   public UserSessionRule userSession = UserSessionRule.standalone();
63   @Rule
64   public ExpectedException expectedException = ExpectedException.none();
65
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);
72
73   @Before
74   public void before() {
75     MockitoAnnotations.initMocks(this);
76   }
77
78   @Test
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");
84   }
85
86   @Test
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()
92       .setMethod("POST")
93       .setParam(PARAM_RULE, rule.getKey().toString())
94       .setParam(PARAM_KEY, qualityProfile.getKee());
95
96     TestResponse response = request.execute();
97
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());
103   }
104
105   @Test
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);
113
114     ws.newRequest()
115       .setMethod("POST")
116       .setParam(PARAM_RULE, ruleKey.toString())
117       .setParam(PARAM_KEY, qualityProfile.getKee())
118       .execute();
119
120     verify(qProfileRules).deactivateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());
121   }
122
123   @Test
124   public void fail_if_not_logged_in() {
125     TestRequest request = ws.newRequest()
126       .setMethod("POST")
127       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
128       .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
129
130     expectedException.expect(UnauthorizedException.class);
131
132     request.execute();
133   }
134
135   @Test
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));
140
141     TestRequest request = ws.newRequest()
142       .setMethod("POST")
143       .setParam(PARAM_RULE, rule.getKey().toString())
144       .setParam(PARAM_KEY, qualityProfile.getKee());
145
146     expectedException.expect(BadRequestException.class);
147     expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
148
149     request.execute();
150   }
151
152   @Test
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);
156
157     QProfileDto qualityProfile = db.qualityProfiles().insert(profile -> profile.setIsBuiltIn(true));
158     TestRequest request = ws.newRequest()
159       .setMethod("POST")
160       .setParam(PARAM_RULE, rule.getKey().toString())
161       .setParam(PARAM_KEY, qualityProfile.getKee());
162
163     expectedException.expect(BadRequestException.class);
164
165     request.execute();
166   }
167 }