]> source.dussan.org Git - sonarqube.git/blob
3c3ffc6d4aa1d9e0964398998b411dbdbcd3ecfb
[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
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.RegisterExtension;
25 import org.mockito.Mockito;
26 import org.sonar.api.server.ws.WebService;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.qualityprofile.QProfileDto;
30 import org.sonar.db.user.GroupDto;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.exceptions.BadRequestException;
33 import org.sonar.server.exceptions.ForbiddenException;
34 import org.sonar.server.exceptions.UnauthorizedException;
35 import org.sonar.server.qualityprofile.QProfileRules;
36 import org.sonar.server.rule.ws.RuleQueryFactory;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestRequest;
39 import org.sonar.server.ws.WsActionTester;
40
41 import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.assertj.core.api.Assertions.assertThatThrownBy;
44 import static org.mockito.ArgumentMatchers.any;
45 import static org.mockito.Mockito.mock;
46 import static org.mockito.Mockito.verify;
47 import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_PROFILES;
48 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
49 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
50
51 class ActivateRulesActionIT {
52
53   @RegisterExtension
54   public DbTester db = DbTester.create();
55   @RegisterExtension
56   public UserSessionRule userSession = UserSessionRule.standalone();
57
58   private DbClient dbClient = db.getDbClient();
59   private final QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession);
60   private RuleQueryFactory ruleQueryFactory = mock(RuleQueryFactory.class, Mockito.RETURNS_MOCKS);
61
62   private QProfileRules qProfileRules = mock(QProfileRules.class, Mockito.RETURNS_DEEP_STUBS);
63   private WsActionTester ws = new WsActionTester(new ActivateRulesAction(ruleQueryFactory, userSession, qProfileRules, wsSupport,
64     dbClient));
65
66   @Test
67   void define_bulk_activate_rule_action() {
68     WebService.Action definition = ws.getDef();
69     assertThat(definition).isNotNull();
70     assertThat(definition.isPost()).isTrue();
71     assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder(
72       "types",
73       "template_key",
74       "languages",
75       "is_template",
76       "inheritance",
77       "qprofile",
78       "compareToProfile",
79       "targetSeverity",
80       "tags",
81       "asc",
82       "q",
83       "active_severities",
84       "prioritizedRule",
85       "s",
86       "repositories",
87       "targetKey",
88       "statuses",
89       "rule_key",
90       "available_since",
91       "activation",
92       "severities",
93       "cwe",
94       "owaspTop10",
95       "owaspTop10-2021",
96       "sansTop25",
97       "sonarsourceSecurity",
98       "cleanCodeAttributeCategories",
99       "impactSoftwareQualities",
100       "impactSeverities");
101   }
102
103   @Test
104   void as_global_qprofile_admin() {
105     userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES);
106     QProfileDto qualityProfile = db.qualityProfiles().insert();
107
108     ws.newRequest()
109       .setMethod("POST")
110       .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
111       .execute();
112
113     verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any(), any());
114   }
115
116   @Test
117   void as_qprofile_editor() {
118     UserDto user = db.users().insertUser();
119     GroupDto group = db.users().insertGroup();
120     QProfileDto qualityProfile = db.qualityProfiles().insert();
121     db.qualityProfiles().addGroupPermission(qualityProfile, group);
122     userSession.logIn(user).setGroups(group);
123
124     ws.newRequest()
125       .setMethod("POST")
126       .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
127       .execute();
128
129     verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any(), any());
130   }
131
132   @Test
133   void fail_if_not_logged_in() {
134     TestRequest request = ws.newRequest()
135       .setMethod("POST")
136       .setParam(PARAM_TARGET_KEY, randomAlphanumeric(UUID_SIZE));
137
138     assertThatThrownBy(() -> request.execute())
139       .isInstanceOf(UnauthorizedException.class);
140   }
141
142   @Test
143   void fail_if_built_in_profile() {
144     userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES);
145     QProfileDto qualityProfile = db.qualityProfiles().insert(p -> p.setIsBuiltIn(true));
146     TestRequest request = ws.newRequest()
147       .setMethod("POST")
148       .setParam(PARAM_TARGET_KEY, qualityProfile.getKee());
149
150     assertThatThrownBy(() -> request.execute())
151       .isInstanceOf(BadRequestException.class);
152   }
153
154   @Test
155   void fail_if_not_enough_permission() {
156     userSession.logIn(db.users().insertUser());
157     QProfileDto qualityProfile = db.qualityProfiles().insert();
158
159     assertThatThrownBy(() -> {
160       ws.newRequest()
161         .setMethod("POST")
162         .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
163         .execute();
164     })
165       .isInstanceOf(ForbiddenException.class);
166   }
167 }