]> source.dussan.org Git - sonarqube.git/blob
82fec864e276687a04fe88f7d87f6ee74e5dee5d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.permission.ws.template;
21
22 import java.util.List;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.resources.Qualifiers;
27 import org.sonar.api.resources.ResourceTypes;
28 import org.sonar.api.server.ws.Change;
29 import org.sonar.api.server.ws.WebService.Action;
30 import org.sonar.api.web.UserRole;
31 import org.sonar.core.permission.GlobalPermissions;
32 import org.sonar.db.component.ResourceTypesRule;
33 import org.sonar.db.permission.PermissionQuery;
34 import org.sonar.db.permission.template.PermissionTemplateDto;
35 import org.sonar.db.user.GroupDto;
36 import org.sonar.server.exceptions.BadRequestException;
37 import org.sonar.server.exceptions.ForbiddenException;
38 import org.sonar.server.exceptions.NotFoundException;
39 import org.sonar.server.permission.PermissionService;
40 import org.sonar.server.permission.PermissionServiceImpl;
41 import org.sonar.server.permission.ws.BasePermissionWsTest;
42 import org.sonar.server.permission.ws.WsParameters;
43 import org.sonar.server.ws.TestRequest;
44
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.assertj.core.api.Assertions.assertThatThrownBy;
47 import static org.assertj.core.api.Assertions.tuple;
48 import static org.sonar.api.security.DefaultGroups.ANYONE;
49 import static org.sonar.api.web.UserRole.ADMIN;
50 import static org.sonar.api.web.UserRole.CODEVIEWER;
51 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
53 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
54 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
55 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
56
57 public class AddGroupToTemplateActionTest extends BasePermissionWsTest<AddGroupToTemplateAction> {
58
59   private PermissionTemplateDto template;
60   private GroupDto group;
61   private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
62   private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
63   private WsParameters wsParameters = new WsParameters(permissionService);
64
65   @Override
66   protected AddGroupToTemplateAction buildWsAction() {
67     return new AddGroupToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession, wsParameters);
68   }
69
70   @Before
71   public void setUp() {
72     template = db.permissionTemplates().insertTemplate();
73     group = db.users().insertGroup("group-name");
74   }
75
76   @Test
77   public void verify_definition() {
78     Action wsDef = wsTester.getDef();
79
80     assertThat(wsDef.isInternal()).isFalse();
81     assertThat(wsDef.since()).isEqualTo("5.2");
82     assertThat(wsDef.isPost()).isTrue();
83     assertThat(wsDef.changelog()).extracting(Change::getVersion, Change::getDescription).containsOnly(
84       tuple("8.4", "Parameter 'groupId' is deprecated. Format changes from integer to string. Use 'groupName' instead."),
85       tuple("10.0", "Parameter 'groupId' is removed. Use 'groupName' instead."));
86   }
87
88   @Test
89   public void add_group_to_template() {
90     loginAsAdmin();
91
92     newRequest(group.getName(), template.getUuid(), CODEVIEWER);
93
94     assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
95   }
96
97   @Test
98   public void add_group_to_template_by_name() {
99     loginAsAdmin();
100
101     newRequest()
102       .setParam(PARAM_GROUP_NAME, group.getName())
103       .setParam(PARAM_PERMISSION, CODEVIEWER)
104       .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
105       .execute();
106
107     assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
108   }
109
110   @Test
111   public void does_not_add_a_group_twice() {
112     loginAsAdmin();
113
114     newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
115     newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
116
117     assertThat(getGroupNamesInTemplateAndPermission(template, ISSUE_ADMIN)).containsExactly(group.getName());
118   }
119
120   @Test
121   public void add_anyone_group_to_template() {
122     loginAsAdmin();
123
124     newRequest(ANYONE, template.getUuid(), CODEVIEWER);
125
126     assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(ANYONE);
127   }
128
129   @Test
130   public void fail_if_add_anyone_group_to_admin_permission() {
131     loginAsAdmin();
132
133     assertThatThrownBy(() -> newRequest(ANYONE, template.getUuid(), ADMIN))
134       .isInstanceOf(BadRequestException.class)
135       .hasMessage(String.format("It is not possible to add the '%s' permission to the group 'Anyone'.", UserRole.ADMIN));
136   }
137
138   @Test
139   public void fail_if_not_a_project_permission() {
140     loginAsAdmin();
141
142     assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING))
143       .isInstanceOf(IllegalArgumentException.class);
144   }
145
146   @Test
147   public void fail_if_not_admin() {
148     userSession.logIn();
149
150     assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), CODEVIEWER))
151       .isInstanceOf(ForbiddenException.class);
152   }
153
154   @Test
155   public void fail_if_group_params_missing() {
156     loginAsAdmin();
157
158     assertThatThrownBy(() -> newRequest(null, template.getUuid(), CODEVIEWER))
159       .isInstanceOf(IllegalArgumentException.class);
160   }
161
162   @Test
163   public void fail_if_permission_missing() {
164     loginAsAdmin();
165
166     assertThatThrownBy(() -> newRequest(group.getName(), template.getUuid(), null))
167       .isInstanceOf(IllegalArgumentException.class);
168   }
169
170   @Test
171   public void fail_if_template_uuid_and_name_missing() {
172     loginAsAdmin();
173
174     assertThatThrownBy(() -> newRequest(group.getName(), null, CODEVIEWER))
175       .isInstanceOf(BadRequestException.class);
176   }
177
178   @Test
179   public void fail_if_group_does_not_exist() {
180     loginAsAdmin();
181
182     assertThatThrownBy(() -> newRequest("unknown-group-name", template.getUuid(), CODEVIEWER))
183       .isInstanceOf(NotFoundException.class)
184       .hasMessage("No group with name 'unknown-group-name'");
185   }
186
187   @Test
188   public void fail_if_template_key_does_not_exist() {
189     loginAsAdmin();
190
191     assertThatThrownBy(() -> newRequest(group.getName(), "unknown-key", CODEVIEWER))
192       .isInstanceOf(NotFoundException.class)
193       .hasMessage("Permission template with id 'unknown-key' is not found");
194   }
195
196   private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) {
197     TestRequest request = newRequest();
198     if (groupName != null) {
199       request.setParam(PARAM_GROUP_NAME, groupName);
200     }
201     if (templateKey != null) {
202       request.setParam(PARAM_TEMPLATE_ID, templateKey);
203     }
204     if (permission != null) {
205       request.setParam(PARAM_PERMISSION, permission);
206     }
207
208     request.execute();
209   }
210
211   private List<String> getGroupNamesInTemplateAndPermission(PermissionTemplateDto template, String permission) {
212     PermissionQuery query = PermissionQuery.builder().setPermission(permission).build();
213     return db.getDbClient().permissionTemplateDao()
214       .selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getUuid());
215   }
216 }