3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.qualityprofile.ws;
22 import org.junit.Before;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.mockito.Mockito;
27 import org.sonar.api.server.ws.WebService;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbTester;
30 import org.sonar.db.organization.OrganizationDto;
31 import org.sonar.db.qualityprofile.QProfileDto;
32 import org.sonar.db.user.GroupDto;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.server.exceptions.BadRequestException;
35 import org.sonar.server.exceptions.ForbiddenException;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.organization.TestDefaultOrganizationProvider;
38 import org.sonar.server.qualityprofile.QProfileRules;
39 import org.sonar.server.rule.ws.RuleQueryFactory;
40 import org.sonar.server.tester.UserSessionRule;
41 import org.sonar.server.ws.TestRequest;
42 import org.sonar.server.ws.WsActionTester;
44 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.Mockito.mock;
48 import static org.mockito.Mockito.verify;
49 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES;
50 import static org.sonar.server.platform.db.migration.def.VarcharColumnDef.UUID_SIZE;
51 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_ORGANIZATION;
52 import static org.sonarqube.ws.client.qualityprofile.QualityProfileWsParameters.PARAM_TARGET_KEY;
54 public class ActivateRulesActionTest {
57 public DbTester db = DbTester.create();
59 public UserSessionRule userSession = UserSessionRule.standalone();
61 public ExpectedException expectedException = ExpectedException.none();
63 private DbClient dbClient = db.getDbClient();
64 private QProfileWsSupport wsSupport = new QProfileWsSupport(dbClient, userSession, TestDefaultOrganizationProvider.from(db));
65 private RuleQueryFactory ruleQueryFactory = mock(RuleQueryFactory.class, Mockito.RETURNS_MOCKS);
67 private QProfileRules qProfileRules = mock(QProfileRules.class, Mockito.RETURNS_DEEP_STUBS);
68 private WsActionTester ws = new WsActionTester(new ActivateRulesAction(ruleQueryFactory, userSession, qProfileRules, wsSupport, dbClient));
70 private OrganizationDto defaultOrganization;
71 private OrganizationDto organization;
74 public void before() {
75 defaultOrganization = db.getDefaultOrganization();
76 organization = db.organizations().insert();
80 public void define_bulk_activate_rule_action() {
81 WebService.Action definition = ws.getDef();
82 assertThat(definition).isNotNull();
83 assertThat(definition.isPost()).isTrue();
84 assertThat(definition.params()).extracting(WebService.Param::key).containsExactlyInAnyOrder(
109 "sonarsourceSecurity");
110 WebService.Param targetProfile = definition.param("targetKey");
111 assertThat(targetProfile.deprecatedKey()).isEqualTo("profile_key");
112 WebService.Param targetSeverity = definition.param("targetSeverity");
113 assertThat(targetSeverity.deprecatedKey()).isEqualTo("activation_severity");
117 public void as_global_qprofile_admin() {
118 userSession.logIn(db.users().insertUser()).addPermission(ADMINISTER_QUALITY_PROFILES, organization);
119 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
123 .setParam(PARAM_ORGANIZATION, organization.getKey())
124 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
127 verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any());
131 public void as_qprofile_editor() {
132 UserDto user = db.users().insertUser();
133 GroupDto group = db.users().insertGroup(organization);
134 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
135 db.organizations().addMember(organization, user);
136 db.qualityProfiles().addGroupPermission(qualityProfile, group);
137 userSession.logIn(user).setGroups(group);
141 .setParam(PARAM_ORGANIZATION, organization.getKey())
142 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())
145 verify(qProfileRules).bulkActivateAndCommit(any(), any(), any(), any());
149 public void fail_if_not_logged_in() {
150 TestRequest request = ws.newRequest()
152 .setParam(PARAM_TARGET_KEY, randomAlphanumeric(UUID_SIZE));
154 expectedException.expect(UnauthorizedException.class);
160 public void fail_if_built_in_profile() {
161 userSession.logIn().addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganization);
162 QProfileDto qualityProfile = db.qualityProfiles().insert(defaultOrganization, p -> p.setIsBuiltIn(true));
163 TestRequest request = ws.newRequest()
165 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee());
167 expectedException.expect(BadRequestException.class);
173 public void fail_if_not_enough_permission() {
174 userSession.logIn(db.users().insertUser());
175 QProfileDto qualityProfile = db.qualityProfiles().insert(organization);
177 expectedException.expect(ForbiddenException.class);
181 .setParam(PARAM_TARGET_KEY, qualityProfile.getKee())