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.WsTester;
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.CONTROLLER;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_ID;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_GROUP_NAME;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
49 public class AddGroupToTemplateActionTest extends BasePermissionWsTest<AddGroupToTemplateAction> {
51 private PermissionTemplateDto template;
52 private GroupDto group;
55 protected AddGroupToTemplateAction buildWsAction() {
56 return new AddGroupToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
61 template = insertTemplate();
62 group = db.users().insertGroup(db.getDefaultOrganization(), "group-name");
66 public void add_group_to_template() throws Exception {
67 loginAsAdminOnDefaultOrganization();
69 newRequest(group.getName(), template.getUuid(), CODEVIEWER);
71 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(group.getName());
75 public void add_group_to_template_by_name() throws Exception {
76 loginAsAdminOnDefaultOrganization();
79 .setParam(PARAM_GROUP_NAME, group.getName())
80 .setParam(PARAM_PERMISSION, CODEVIEWER)
81 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
84 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(group.getName());
88 public void add_with_group_id() throws Exception {
89 loginAsAdminOnDefaultOrganization();
92 .setParam(PARAM_TEMPLATE_ID, template.getUuid())
93 .setParam(PARAM_PERMISSION, CODEVIEWER)
94 .setParam(PARAM_GROUP_ID, String.valueOf(group.getId()))
97 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(group.getName());
101 public void does_not_add_a_group_twice() throws Exception {
102 loginAsAdminOnDefaultOrganization();
104 newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
105 newRequest(group.getName(), template.getUuid(), ISSUE_ADMIN);
107 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), ISSUE_ADMIN)).containsExactly(group.getName());
111 public void add_anyone_group_to_template() throws Exception {
112 loginAsAdminOnDefaultOrganization();
114 newRequest(ANYONE, template.getUuid(), CODEVIEWER);
116 assertThat(getGroupNamesInTemplateAndPermission(template.getId(), CODEVIEWER)).containsExactly(ANYONE);
120 public void fail_if_add_anyone_group_to_admin_permission() throws Exception {
121 loginAsAdminOnDefaultOrganization();
123 expectedException.expect(BadRequestException.class);
124 expectedException.expectMessage(String.format("It is not possible to add the '%s' permission to the group 'Anyone'", UserRole.ADMIN));
126 newRequest(ANYONE, template.getUuid(), ADMIN);
130 public void fail_if_not_a_project_permission() throws Exception {
131 loginAsAdminOnDefaultOrganization();
133 expectedException.expect(IllegalArgumentException.class);
135 newRequest(group.getName(), template.getUuid(), GlobalPermissions.PROVISIONING);
139 public void fail_if_not_admin_of_default_organization() throws Exception {
142 expectedException.expect(ForbiddenException.class);
144 newRequest(group.getName(), template.getUuid(), CODEVIEWER);
148 public void fail_if_group_params_missing() throws Exception {
149 loginAsAdminOnDefaultOrganization();
151 expectedException.expect(BadRequestException.class);
153 newRequest(null, template.getUuid(), CODEVIEWER);
157 public void fail_if_permission_missing() throws Exception {
158 loginAsAdminOnDefaultOrganization();
160 expectedException.expect(IllegalArgumentException.class);
162 newRequest(group.getName(), template.getUuid(), null);
166 public void fail_if_template_uuid_and_name_missing() throws Exception {
167 loginAsAdminOnDefaultOrganization();
169 expectedException.expect(BadRequestException.class);
171 newRequest(group.getName(), null, CODEVIEWER);
175 public void fail_if_group_does_not_exist() throws Exception {
176 loginAsAdminOnDefaultOrganization();
178 expectedException.expect(NotFoundException.class);
179 expectedException.expectMessage("No group with name 'unknown-group-name'");
181 newRequest("unknown-group-name", template.getUuid(), CODEVIEWER);
185 public void fail_if_template_key_does_not_exist() throws Exception {
186 loginAsAdminOnDefaultOrganization();
188 expectedException.expect(NotFoundException.class);
189 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
191 newRequest(group.getName(), "unknown-key", CODEVIEWER);
194 private void newRequest(@Nullable String groupName, @Nullable String templateKey, @Nullable String permission) throws Exception {
195 WsTester.TestRequest request = newRequest();
196 if (groupName != null) {
197 request.setParam(PARAM_GROUP_NAME, groupName);
199 if (templateKey != null) {
200 request.setParam(PARAM_TEMPLATE_ID, templateKey);
202 if (permission != null) {
203 request.setParam(PARAM_PERMISSION, permission);
209 private List<String> getGroupNamesInTemplateAndPermission(long templateId, String permission) {
210 PermissionQuery query = PermissionQuery.builder().setPermission(permission).build();
211 return db.getDbClient().permissionTemplateDao()
212 .selectGroupNamesByQueryAndTemplate(db.getSession(), query, templateId);
215 private WsTester.TestRequest newRequest() {
216 return wsTester.newPostRequest(CONTROLLER, "add_group_to_template");