]> source.dussan.org Git - sonarqube.git/blob
b8e206fed267132410d01ed6e10f7b3949cb2d55
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.mockito.ArgumentCaptor;
28 import org.mockito.MockitoAnnotations;
29 import org.sonar.api.rule.RuleKey;
30 import org.sonar.api.rule.Severity;
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.RuleDto;
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.ForbiddenException;
42 import org.sonar.server.exceptions.UnauthorizedException;
43 import org.sonar.server.qualityprofile.QProfileRules;
44 import org.sonar.server.qualityprofile.RuleActivation;
45 import org.sonar.server.tester.UserSessionRule;
46 import org.sonar.server.ws.TestRequest;
47 import org.sonar.server.ws.TestResponse;
48 import org.sonar.server.ws.WsActionTester;
49
50 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
51 import static org.assertj.core.api.Assertions.assertThat;
52 import static org.assertj.core.api.Assertions.assertThatThrownBy;
53 import static org.mockito.ArgumentMatchers.any;
54 import static org.mockito.ArgumentMatchers.anyCollection;
55 import static org.mockito.Mockito.mock;
56 import static org.mockito.Mockito.verify;
57 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
58 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_KEY;
59 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_RULE;
60
61 public class ActivateRuleActionIT {
62
63   @Rule
64   public DbTester db = DbTester.create();
65   @Rule
66   public UserSessionRule userSession = UserSessionRule.standalone();
67
68   private final ArgumentCaptor<Collection<RuleActivation>> ruleActivationCaptor = ArgumentCaptor.forClass(Collection.class);
69   private final DbClient dbClient = db.getDbClient();
70   private final QProfileRules qProfileRules = mock(QProfileRules.class);
71   private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
72
73   private final WsActionTester ws = new WsActionTester(new ActivateRuleAction(dbClient, qProfileRules, userSession, wsSupport));
74
75   @Before
76   public void before() {
77     MockitoAnnotations.initMocks(this);
78   }
79
80   @Test
81   public void define_activate_rule_action() {
82     WebService.Action definition = ws.getDef();
83     assertThat(definition).isNotNull();
84     assertThat(definition.isPost()).isTrue();
85     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder("severity", "key", "reset", "rule", "params");
86   }
87
88   @Test
89   public void fail_if_not_logged_in() {
90     TestRequest request = ws.newRequest()
91       .setMethod("POST")
92       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
93       .setParam(PARAM_KEY, randomAlphanumeric(UUID_SIZE));
94
95     assertThatThrownBy(() -> request.execute())
96       .isInstanceOf(UnauthorizedException.class);
97   }
98
99   @Test
100   public void fail_if_not_global_quality_profile_administrator() {
101     userSession.logIn(db.users().insertUser());
102     QProfileDto qualityProfile = db.qualityProfiles().insert();
103     TestRequest request = ws.newRequest()
104       .setMethod("POST")
105       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
106       .setParam(PARAM_KEY, qualityProfile.getKee());
107
108     assertThatThrownBy(() -> request.execute())
109       .isInstanceOf(ForbiddenException.class);
110   }
111
112   @Test
113   public void fail_activate_if_built_in_profile() {
114     userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
115
116     QProfileDto qualityProfile = db.qualityProfiles().insert(profile -> profile.setIsBuiltIn(true).setName("Xoo profile").setLanguage("xoo"));
117     TestRequest request = ws.newRequest()
118       .setMethod("POST")
119       .setParam(PARAM_RULE, RuleTesting.newRule().getKey().toString())
120       .setParam(PARAM_KEY, qualityProfile.getKee());
121
122     assertThatThrownBy(() -> request.execute())
123       .isInstanceOf(BadRequestException.class)
124       .hasMessage("Operation forbidden for built-in Quality Profile 'Xoo profile' with language 'xoo'");
125   }
126
127   @Test
128   public void fail_activate_external_rule() {
129     userSession.logIn(db.users().insertUser()).addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
130     QProfileDto qualityProfile = db.qualityProfiles().insert();
131     RuleDto rule = db.rules().insert(r -> r.setIsExternal(true));
132
133     TestRequest request = ws.newRequest()
134       .setMethod("POST")
135       .setParam(PARAM_RULE, rule.getKey().toString())
136       .setParam(PARAM_KEY, qualityProfile.getKee());
137
138     assertThatThrownBy(() -> request.execute())
139       .isInstanceOf(BadRequestException.class)
140       .hasMessage(String.format("Operation forbidden for rule '%s' imported from an external rule engine.", rule.getKey()));
141   }
142
143   @Test
144   public void activate_rule() {
145     userSession.logIn().addPermission(GlobalPermission.ADMINISTER_QUALITY_PROFILES);
146     QProfileDto qualityProfile = db.qualityProfiles().insert();
147     RuleDto rule = db.rules().insert(RuleTesting.randomRuleKey());
148     TestRequest request = ws.newRequest()
149       .setMethod("POST")
150       .setParam(PARAM_RULE, rule.getKey().toString())
151       .setParam(PARAM_KEY, qualityProfile.getKee())
152       .setParam("severity", "BLOCKER")
153       .setParam("params", "key1=v1;key2=v2")
154       .setParam("reset", "false");
155
156     TestResponse response = request.execute();
157
158     assertThat(response.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
159     verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), ruleActivationCaptor.capture());
160
161     Collection<RuleActivation> activations = ruleActivationCaptor.getValue();
162     assertThat(activations).hasSize(1);
163
164     RuleActivation activation = activations.iterator().next();
165     assertThat(activation.getRuleUuid()).isEqualTo(rule.getUuid());
166     assertThat(activation.getSeverity()).isEqualTo(Severity.BLOCKER);
167     assertThat(activation.isReset()).isFalse();
168   }
169
170   @Test
171   public void as_qprofile_editor() {
172     UserDto user = db.users().insertUser();
173     QProfileDto qualityProfile = db.qualityProfiles().insert();
174     db.qualityProfiles().addUserPermission(qualityProfile, user);
175     userSession.logIn(user);
176     RuleKey ruleKey = RuleTesting.randomRuleKey();
177     db.rules().insert(ruleKey);
178
179     ws.newRequest()
180       .setMethod("POST")
181       .setParam(PARAM_RULE, ruleKey.toString())
182       .setParam(PARAM_KEY, qualityProfile.getKee())
183       .setParam("severity", "BLOCKER")
184       .setParam("params", "key1=v1;key2=v2")
185       .setParam("reset", "false")
186       .execute();
187
188     verify(qProfileRules).activateAndCommit(any(DbSession.class), any(QProfileDto.class), anyCollection());
189   }
190 }