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.Rule;
26 import org.junit.Test;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.core.permission.GlobalPermissions;
29 import org.sonar.db.component.ComponentDto;
30 import org.sonar.db.permission.PermissionQuery;
31 import org.sonar.db.permission.template.PermissionTemplateDto;
32 import org.sonar.db.user.GroupDto;
33 import org.sonar.db.user.UserDto;
34 import org.sonar.server.exceptions.BadRequestException;
35 import org.sonar.server.exceptions.ForbiddenException;
36 import org.sonar.server.exceptions.NotFoundException;
37 import org.sonar.server.permission.PermissionTemplateService;
38 import org.sonar.server.permission.index.PermissionIndexer;
39 import org.sonar.server.permission.ws.BasePermissionWsTest;
40 import org.sonar.server.ws.TestRequest;
41 import org.sonar.server.ws.TestResponse;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.mockito.Mockito.mock;
45 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
51 public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateAction> {
54 public DefaultTemplatesResolverRule defaultTemplatesResolver = DefaultTemplatesResolverRule.withoutGovernance();
55 private UserDto user1;
56 private UserDto user2;
57 private GroupDto group1;
58 private GroupDto group2;
59 private ComponentDto project;
60 private PermissionTemplateDto template1;
61 private PermissionTemplateDto template2;
63 private PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
64 mock(PermissionIndexer.class), userSession, defaultTemplatesResolver);
67 protected ApplyTemplateAction buildWsAction() {
68 return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
73 user1 = db.users().insertUser();
74 user2 = db.users().insertUser();
75 group1 = db.users().insertGroup();
76 group2 = db.users().insertGroup();
79 template1 = insertTemplate();
80 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
81 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
82 addGroupToTemplate(group1, template1, UserRole.ADMIN);
83 addGroupToTemplate(group2, template1, UserRole.USER);
85 template2 = insertTemplate();
86 addUserToTemplate(user1, template2, UserRole.USER);
87 addUserToTemplate(user2, template2, UserRole.USER);
88 addGroupToTemplate(group1, template2, UserRole.USER);
89 addGroupToTemplate(group2, template2, UserRole.USER);
91 project = db.components().insertProject();
92 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
93 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
94 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, project);
95 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
99 public void apply_template_with_project_uuid() throws Exception {
100 loginAsAdminOnDefaultOrganization();
102 newRequest(template1.getUuid(), project.uuid(), null);
104 assertTemplate1AppliedToProject();
108 public void apply_template_with_project_uuid_by_template_name() throws Exception {
109 loginAsAdminOnDefaultOrganization();
112 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
113 .setParam(PARAM_PROJECT_ID, project.uuid())
116 assertTemplate1AppliedToProject();
120 public void apply_template_with_project_key() throws Exception {
121 loginAsAdminOnDefaultOrganization();
123 newRequest(template1.getUuid(), null, project.key());
125 assertTemplate1AppliedToProject();
129 public void fail_when_unknown_template() throws Exception {
130 loginAsAdminOnDefaultOrganization();
132 expectedException.expect(NotFoundException.class);
133 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
135 newRequest("unknown-template-uuid", project.uuid(), null);
139 public void fail_when_unknown_project_uuid() throws Exception {
140 loginAsAdminOnDefaultOrganization();
142 expectedException.expect(NotFoundException.class);
143 expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
145 newRequest(template1.getUuid(), "unknown-project-uuid", null);
149 public void fail_when_unknown_project_key() throws Exception {
150 loginAsAdminOnDefaultOrganization();
152 expectedException.expect(NotFoundException.class);
153 expectedException.expectMessage("Project key 'unknown-project-key' not found");
155 newRequest(template1.getUuid(), null, "unknown-project-key");
159 public void fail_when_template_is_not_provided() throws Exception {
160 loginAsAdminOnDefaultOrganization();
162 expectedException.expect(BadRequestException.class);
164 newRequest(null, project.uuid(), null);
168 public void fail_when_project_uuid_and_key_not_provided() throws Exception {
169 loginAsAdminOnDefaultOrganization();
171 expectedException.expect(BadRequestException.class);
172 expectedException.expectMessage("Project id or project key can be provided, not both.");
174 newRequest(template1.getUuid(), null, null);
178 public void fail_when_not_admin_of_organization() throws Exception {
179 userSession.logIn().addOrganizationPermission("otherOrg", SYSTEM_ADMIN);
181 expectedException.expect(ForbiddenException.class);
182 userSession.logIn().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
184 newRequest(template1.getUuid(), project.uuid(), null);
187 private void assertTemplate1AppliedToProject() {
188 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
189 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
190 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
191 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
192 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
195 private TestResponse newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) throws Exception {
196 TestRequest request = newRequest();
197 if (templateUuid != null) {
198 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
200 if (projectUuid != null) {
201 request.setParam(PARAM_PROJECT_ID, projectUuid);
203 if (projectKey != null) {
204 request.setParam(PARAM_PROJECT_KEY, projectKey);
207 return request.execute();
210 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
211 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
215 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
216 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
220 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
221 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
222 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
225 private List<Long> selectProjectPermissionUsers(ComponentDto project, String permission) {
226 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
227 return db.getDbClient().userPermissionDao().selectUserIds(db.getSession(), db.getDefaultOrganization().getUuid(), query);