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