]> source.dussan.org Git - sonarqube.git/blob
28f385073b957669a0e4902e63725cf162a04ecf
[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.Captor;
30 import org.mockito.MockitoAnnotations;
31 import org.sonar.api.rule.RuleKey;
32 import org.sonar.api.rule.Severity;
33 import org.sonar.api.server.ws.WebService;
34 import org.sonar.db.DbClient;
35 import org.sonar.db.DbSession;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.organization.OrganizationDto;
38 import org.sonar.db.permission.OrganizationPermission;
39 import org.sonar.db.qualityprofile.QProfileDto;
40 import org.sonar.db.rule.RuleDefinitionDto;
41 import org.sonar.db.rule.RuleTesting;
42 import org.sonar.db.user.UserDto;
43 import org.sonar.server.exceptions.BadRequestException;
44 import org.sonar.server.exceptions.ForbiddenException;
45 import org.sonar.server.exceptions.UnauthorizedException;
46 import org.sonar.server.organization.TestDefaultOrganizationProvider;
47 import org.sonar.server.qualityprofile.QProfileRules;
48 import org.sonar.server.qualityprofile.RuleActivation;
49 import org.sonar.server.tester.UserSessionRule;
50 import org.sonar.server.ws.TestRequest;
51 import org.sonar.server.ws.TestResponse;
52 import org.sonar.server.ws.WsActionTester;
53
54 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.mockito.ArgumentMatchers.any;
57 import static org.mockito.ArgumentMatchers.anyCollection;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.verify;
60 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
61 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
62 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
63
64 public class ActivateRuleActionTest {
65
66   @Rule
67   public DbTester db = DbTester.create();
68   @Rule
69   public UserSessionRule userSession = UserSessionRule.standalone();
70   @Rule
71   public ExpectedException expectedException = ExpectedException.none();
72   @Captor
73   public ArgumentCaptor<Collection<RuleActivation>> ruleActivationCaptor;
74
75   private DbClient dbClient = db.getDbClient();
76   private QProfileRules qProfileRules = mock(QProfileRules.class);
77   private QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db));
78
79   private WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, qProfileRules, userSession, wsSupport));
80
81   private OrganizationDto defaultOrganization;
82   private OrganizationDto organization;
83
84   @Before
85   public void before() {
86     MockitoAnnotations.initMocks(this);
87     defaultOrganization = db.getDefaultOrganization();
88     organization = db.organizations().insert();
89   }
90
91   @Test
92   public void define_activate_rule_action() {
93     WebService.Action definition = ws.getDef();
94     assertThat(definition).isNotNull();
95     assertThat(definition.isPost()).isTrue();
96     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "key", "reset", "rule", "params");
97   }
98
99   @Test
100   public void fail_if_not_logged_in() {
101     TestRequest request = ws.newRequest()
102       .setMethod("POST")
103       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
104       .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
105
106     expectedException.expect(UnauthorizedException.class);
107
108     request.execute();
109   }
110
111   @Test
112   public void fail_if_not_organization_quality_profile_administrator() {
113     userSession.logIn(db.users().insertUser());
114     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
115     TestRequest request = ws.newRequest()
116       .setMethod("POST")
117       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
118       .setParam(PARAM_KEY, qualityProfile.getKee());
119
120     expectedException.expect(ForbiddenException.class);
121
122     request.execute();
123   }
124
125   @Test
126   public void fail_activate_if_built_in_profile() {
127     userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
128
129     QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo"));
130     TestRequest request = ws.newRequest()
131       .setMethod("POST")
132       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
133       .setParam(PARAM_KEY, qualityProfile.getKee());
134
135     expectedException.expect(BadRequestException.class);
136     expectedException.expectMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
137
138     request.execute();
139   }
140
141   @Test
142   public void fail_activate_external_rule() {
143     userSession.logIn(db.users().insertUser()).addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
144     QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
145     RuleDefinitionDto rule = db.rules().insert(r -> r.setIsExternal(true));
146
147     TestRequest request = ws.newRequest()
148       .setMethod("POST")
149       .setParam(PARAM_RULE, rule.getKey().toString())
150       .setParam(PARAM_KEY, qualityProfile.getKee());
151
152     expectedException.expect(BadRequestException.class);
153     expectedException.expectMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
154
155     request.execute();
156   }
157
158   @Test
159   public void activate_rule_in_default_organization() {
160     userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, defaultOrganization);
161     QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization);
162     RuleDefinitionDto rule = db.rules().insert(RuleTesting.randomRuleKey());
163     TestRequest request = ws.newRequest()
164       .setMethod("POST")
165       .setParam(PARAM_RULE, rule.getKey().toString())
166       .setParam(PARAM_KEY, qualityProfile.getKee())
167       .setParam("severity", "BLOCKER")
168       .setParam("params", "key1=v1;key2=v2")
169       .setParam("reset", "false");
170
171     TestResponse response = request.execute();
172
173     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
174     verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
175
176     Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
177     assertThat(activations).hasSize(1);
178
179     RuleActivation activation = activations.iterator().next();
180     assertThat(activation.getRuleId()).isEqualTo(rule.getId());
181     assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
182     assertThat(activation.isReset()).isFalse();
183   }
184
185   @Test
186   public void activate_rule_in_specific_organization() {
187     userSession.logIn().addPermission(OrganizationPermission.ADMINISTER_QUALITY_PROFILES, organization);
188     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
189     RuleKey ruleKey = RuleTesting.randomRuleKey();
190     Integer ruleId = db.rules().insert(ruleKey).getId();
191     TestRequest request = ws.newRequest()
192       .setMethod("POST")
193       .setParam(PARAM_RULE, ruleKey.toString())
194       .setParam(PARAM_KEY, qualityProfile.getKee())
195       .setParam("severity", "BLOCKER")
196       .setParam("params", "key1=v1;key2=v2")
197       .setParam("reset", "false");
198
199     TestResponse response = request.execute();
200
201     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
202     verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
203
204     Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
205     assertThat(activations).hasSize(1);
206     RuleActivation activation = activations.iterator().next();
207     assertThat(activation.getRuleId()).isEqualTo(ruleId);
208     assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
209     assertThat(activation.isReset()).isFalse();
210   }
211
212   @Test
213   public void as_qprofile_editor() {
214     UserDto user = db.users().insertUser();
215     QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
216     db.qualityProfiles().addUserPermission(qualityProfile, user);
217     userSession.logIn(user);
218     RuleKey ruleKey = RuleTesting.randomRuleKey();
219     db.rules().insert(ruleKey);
220
221     ws.newRequest()
222       .setMethod("POST")
223       .setParam(PARAM_RULE, ruleKey.toString())
224       .setParam(PARAM_KEY, qualityProfile.getKee())
225       .setParam("severity", "BLOCKER")
226       .setParam("params", "key1=v1;key2=v2")
227       .setParam("reset", "false")
228       .execute();
229
230     verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());
231   }
232 }