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.TestRequest;
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.PARAM_PERMISSION;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_USER_LOGIN;
44 public class AddUserToTemplateActionTest extends BasePermissionWsTest<AddUserToTemplateAction> {
47 private PermissionTemplateDto permissionTemplate;
50 protected AddUserToTemplateAction buildWsAction() {
51 return new AddUserToTemplateAction(db.getDbClient(), newPermissionWsSupport(), userSession);
56 user = db.users().insertUser("user-login");
57 permissionTemplate = insertTemplate();
61 public void add_user_to_template() throws Exception {
62 loginAsAdminOnDefaultOrganization();
64 newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
66 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(user.getLogin());
70 public void add_user_to_template_by_name() throws Exception {
71 loginAsAdminOnDefaultOrganization();
74 .setParam(PARAM_USER_LOGIN, user.getLogin())
75 .setParam(PARAM_PERMISSION, CODEVIEWER)
76 .setParam(PARAM_TEMPLATE_NAME, permissionTemplate.getName().toUpperCase())
79 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), CODEVIEWER)).containsExactly(user.getLogin());
83 public void does_not_add_a_user_twice() throws Exception {
84 loginAsAdminOnDefaultOrganization();
86 newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
87 newRequest(user.getLogin(), permissionTemplate.getUuid(), ISSUE_ADMIN);
89 assertThat(getLoginsInTemplateAndPermission(permissionTemplate.getId(), ISSUE_ADMIN)).containsExactly(user.getLogin());
93 public void fail_if_not_a_project_permission() throws Exception {
94 loginAsAdminOnDefaultOrganization();
96 expectedException.expect(IllegalArgumentException.class);
98 newRequest(user.getLogin(), permissionTemplate.getUuid(), GlobalPermissions.PROVISIONING);
102 public void fail_if_not_admin_of_default_organization() throws Exception {
103 userSession.logIn().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_PROFILE_ADMIN);
105 expectedException.expect(ForbiddenException.class);
107 newRequest(user.getLogin(), permissionTemplate.getUuid(), CODEVIEWER);
111 public void fail_if_user_missing() throws Exception {
112 loginAsAdminOnDefaultOrganization();
114 expectedException.expect(IllegalArgumentException.class);
116 newRequest(null, permissionTemplate.getUuid(), CODEVIEWER);
120 public void fail_if_permission_missing() throws Exception {
121 loginAsAdminOnDefaultOrganization();
123 expectedException.expect(IllegalArgumentException.class);
125 newRequest(user.getLogin(), permissionTemplate.getUuid(), null);
129 public void fail_if_template_uuid_and_name_are_missing() throws Exception {
130 loginAsAdminOnDefaultOrganization();
132 expectedException.expect(BadRequestException.class);
134 newRequest(user.getLogin(), null, CODEVIEWER);
138 public void fail_if_user_does_not_exist() throws Exception {
139 loginAsAdminOnDefaultOrganization();
141 expectedException.expect(NotFoundException.class);
142 expectedException.expectMessage("User with login 'unknown-login' is not found");
144 newRequest("unknown-login", permissionTemplate.getUuid(), CODEVIEWER);
148 public void fail_if_template_key_does_not_exist() throws Exception {
149 loginAsAdminOnDefaultOrganization();
151 expectedException.expect(NotFoundException.class);
152 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
154 newRequest(user.getLogin(), "unknown-key", CODEVIEWER);
157 private void newRequest(@Nullable String userLogin, @Nullable String templateKey, @Nullable String permission) throws Exception {
158 TestRequest request = newRequest();
159 if (userLogin != null) {
160 request.setParam(PARAM_USER_LOGIN, userLogin);
162 if (templateKey != null) {
163 request.setParam(org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID, templateKey);
165 if (permission != null) {
166 request.setParam(PARAM_PERMISSION, permission);
172 private List<String> getLoginsInTemplateAndPermission(long templateId, String permission) {
173 PermissionQuery permissionQuery = PermissionQuery.builder().setPermission(permission).build();
174 return db.getDbClient().permissionTemplateDao()
175 .selectUserLoginsByQueryAndTemplate(db.getSession(), permissionQuery, templateId);