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.core.permission.GlobalPermissions;
27 import org.sonar.db.permission.PermissionQuery;
28 import org.sonar.db.permission.template.PermissionTemplateDto;
29 import org.sonar.db.user.UserDto;
30 import org.sonar.server.exceptions.BadRequestException;
31 import org.sonar.server.exceptions.ForbiddenException;
32 import org.sonar.server.exceptions.NotFoundException;
33 import org.sonar.server.permission.ws.BasePermissionWsTest;
34 import org.sonar.server.ws.WsTester;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.sonar.api.web.UserRole.CODEVIEWER;
38 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
39 import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
40 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PERMISSION;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
45 public class AddUserToTemplateActionTest extends BasePermissionWsTest<AddUserToTemplateAction> {
47 private static final String ACTION = "add_user_to_template";
50 private PermissionTemplateDto permissionTemplate;
53 protected AddUserToTemplateAction buildWsAction() {
54 return new AddUserToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
59 user = db.users().insertUser("user-login");
60 permissionTemplate = insertTemplate();
64 public void add_user_to_template() throws Exception {
65 loginAsAdminOnDefaultOrganization();
67 newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
69 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(user.getLogin());
73 public void add_user_to_template_by_name() throws Exception {
74 loginAsAdminOnDefaultOrganization();
76 wsTester.newPostRequest(CONTROLLER, ACTION)
77 .setParam(PARAM_USER_LOGIN, user.getLogin())
78 .setParam(PARAM_PERMISSION, CODEVIEWER)
79 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
82 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(user.getLogin());
86 public void does_not_add_a_user_twice() throws Exception {
87 loginAsAdminOnDefaultOrganization();
89 newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
90 newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
92 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
96 public void fail_if_not_a_project_permission() throws Exception {
97 loginAsAdminOnDefaultOrganization();
99 expectedException.expect(IllegalArgumentException.class);
101 newRequest(user.getLogin(), permissionTemplate.getUuid(), GlobalPermissions.PROVISIONING);
105 public void fail_if_not_admin_of_default_organization() throws Exception {
106 userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_PROFILE_ADMIN);
108 expectedException.expect(ForbiddenException.class);
110 newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
114 public void fail_if_user_missing() throws Exception {
115 loginAsAdminOnDefaultOrganization();
117 expectedException.expect(IllegalArgumentException.class);
119 newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
123 public void fail_if_permission_missing() throws Exception {
124 loginAsAdminOnDefaultOrganization();
126 expectedException.expect(IllegalArgumentException.class);
128 newRequest(user.getLogin(), permissionTemplate.getUuid(), null);
132 public void fail_if_template_uuid_and_name_are_missing() throws Exception {
133 loginAsAdminOnDefaultOrganization();
135 expectedException.expect(BadRequestException.class);
137 newRequest(user.getLogin(), null, CODEVIEWER);
141 public void fail_if_user_does_not_exist() throws Exception {
142 loginAsAdminOnDefaultOrganization();
144 expectedException.expect(NotFoundException.class);
145 expectedException.expectMessage("User with login 'unknown-login' is not found");
147 newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER);
151 public void fail_if_template_key_does_not_exist() throws Exception {
152 loginAsAdminOnDefaultOrganization();
154 expectedException.expect(NotFoundException.class);
155 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
157 newRequest(user.getLogin(), "unknown-key", CODEVIEWER);
160 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) throws Exception {
161 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
162 if (userLogin != null) {
163 request.setParam(PARAM_USER_LOGIN, userLogin);
165 if (templateKey != null) {
166 request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateKey);
168 if (permission != null) {
169 request.setParam(PARAM_PERMISSION, permission);
175 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
176 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
177 return db.getDbClient().permissionTemplateDao()
178 .selectUserLoginsByQueryAndTemplate(db.getSession(), permissionQuery, templateId);