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