3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.permission.ws.template;
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.web.UserRole;
27 import org.sonar.core.permission.GlobalPermissions;
28 import org.sonar.db.permission.PermissionQuery;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.db.user.GroupDto;
31 import org.sonar.server.exceptions.BadRequestException;
32 import org.sonar.server.exceptions.ForbiddenException;
33 import org.sonar.server.exceptions.NotFoundException;
34 import org.sonar.server.permission.ws.BasePermissionWsTest;
35 import org.sonar.server.ws.TestRequest;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.sonar.api.security.DefaultGroups.ANYONE;
39 import static org.sonar.api.web.UserRole.ADMIN;
40 import static org.sonar.api.web.UserRole.CODEVIEWER;
41 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
48 public class AddGroupToTemplateActionTest extends BasePermissionWsTest<AddGroupToTemplateAction> {
50 private PermissionTemplateDto template;
51 private GroupDto group;
54 protected AddGroupToTemplateAction buildWsAction() {
55 return new AddGroupToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
60 template = insertTemplate();
61 group = db.users().insertGroup(db.getDefaultOrganization(), "group-name");
65 public void add_group_to_template() throws Exception {
66 loginAsAdminOnDefaultOrganization();
68 newRequest(group.getName(), template.getUuid(), CODEVIEWER);
70 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
74 public void add_group_to_template_by_name() throws Exception {
75 loginAsAdminOnDefaultOrganization();
78 .setParam(PARAM_GROUP_NAME, group.getName())
79 .setParam(PARAM_PERMISSION, CODEVIEWER)
80 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
83 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
87 public void add_with_group_id() throws Exception {
88 loginAsAdminOnDefaultOrganization();
91 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
92 .setParam(PARAM_PERMISSION, CODEVIEWER)
93 .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
96 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(group.getName());
100 public void does_not_add_a_group_twice() throws Exception {
101 loginAsAdminOnDefaultOrganization();
103 newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
104 newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
106 assertThat(getGroupNamesInTemplateAndPermission(template, ISSUE_ADMIN)).containsExactly(group.getName());
110 public void add_anyone_group_to_template() throws Exception {
111 loginAsAdminOnDefaultOrganization();
113 newRequest(ANYONE, template.getUuid(), CODEVIEWER);
115 assertThat(getGroupNamesInTemplateAndPermission(template, CODEVIEWER)).containsExactly(ANYONE);
119 public void fail_if_add_anyone_group_to_admin_permission() throws Exception {
120 loginAsAdminOnDefaultOrganization();
122 expectedException.expect(BadRequestException.class);
123 expectedException.expectMessage(String.format("It is not possible to add the '%s' permission to the group 'Anyone'", UserRole.ADMIN));
125 newRequest(ANYONE, template.getUuid(), ADMIN);
129 public void fail_if_not_a_project_permission() throws Exception {
130 loginAsAdminOnDefaultOrganization();
132 expectedException.expect(IllegalArgumentException.class);
134 newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING);
138 public void fail_if_not_admin_of_default_organization() throws Exception {
141 expectedException.expect(ForbiddenException.class);
143 newRequest(group.getName(), template.getUuid(), CODEVIEWER);
147 public void fail_if_group_params_missing() throws Exception {
148 loginAsAdminOnDefaultOrganization();
150 expectedException.expect(BadRequestException.class);
152 newRequest(null, template.getUuid(), CODEVIEWER);
156 public void fail_if_permission_missing() throws Exception {
157 loginAsAdminOnDefaultOrganization();
159 expectedException.expect(IllegalArgumentException.class);
161 newRequest(group.getName(), template.getUuid(), null);
165 public void fail_if_template_uuid_and_name_missing() throws Exception {
166 loginAsAdminOnDefaultOrganization();
168 expectedException.expect(BadRequestException.class);
170 newRequest(group.getName(), null, CODEVIEWER);
174 public void fail_if_group_does_not_exist() throws Exception {
175 loginAsAdminOnDefaultOrganization();
177 expectedException.expect(NotFoundException.class);
178 expectedException.expectMessage("No group with name 'unknown-group-name'");
180 newRequest("unknown-group-name", template.getUuid(), CODEVIEWER);
184 public void fail_if_template_key_does_not_exist() throws Exception {
185 loginAsAdminOnDefaultOrganization();
187 expectedException.expect(NotFoundException.class);
188 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
190 newRequest(group.getName(), "unknown-key", CODEVIEWER);
193 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) throws Exception {
194 TestRequest request = newRequest();
195 if (groupName != null) {
196 request.setParam(PARAM_GROUP_NAME, groupName);
198 if (templateKey != null) {
199 request.setParam(PARAM_TEMPLATE_ID, templateKey);
201 if (permission != null) {
202 request.setParam(PARAM_PERMISSION, permission);
208 private List<String> getGroupNamesInTemplateAndPermission(PermissionTemplateDto template, String permission) {
209 PermissionQuery query = PermissionQuery.builder().setPermission(permission).build();
210 return db.getDbClient().permissionTemplateDao()
211 .selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getOrganizationUuid(), template.getId());