3 * Copyright (C) 2009-2018 SonarSource SA
4 * mailto:info 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.resources.Qualifiers;
28 import org.sonar.api.resources.ResourceTypes;
29 import org.sonar.api.web.UserRole;
30 import org.sonar.db.component.ComponentDto;
31 import org.sonar.db.component.ResourceTypesRule;
32 import org.sonar.db.permission.PermissionQuery;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.db.user.GroupDto;
35 import org.sonar.db.user.UserDto;
36 import org.sonar.server.es.TestProjectIndexers;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.permission.PermissionService;
41 import org.sonar.server.permission.PermissionServiceImpl;
42 import org.sonar.server.permission.PermissionTemplateService;
43 import org.sonar.server.permission.ws.BasePermissionWsTest;
44 import org.sonar.server.ws.TestRequest;
45 import org.sonar.server.ws.TestResponse;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.sonar.db.permission.OrganizationPermission.ADMINISTER;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_ID;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY;
51 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
52 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
54 public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateAction> {
57 public DefaultTemplatesResolverRule defaultTemplatesResolver = DefaultTemplatesResolverRule.withoutGovernance();
58 private UserDto user1;
59 private UserDto user2;
60 private GroupDto group1;
61 private GroupDto group2;
62 private ComponentDto project;
63 private PermissionTemplateDto template1;
64 private PermissionTemplateDto template2;
66 private ResourceTypes resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
67 private PermissionService permissionService = new PermissionServiceImpl(resourceTypes);
69 private PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
70 new TestProjectIndexers(), userSession, defaultTemplatesResolver, permissionService);
73 protected ApplyTemplateAction buildWsAction() {
74 return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
79 user1 = db.users().insertUser();
80 db.organizations().addMember(db.getDefaultOrganization(), user1);
81 user2 = db.users().insertUser();
82 db.organizations().addMember(db.getDefaultOrganization(), user2);
83 group1 = db.users().insertGroup();
84 group2 = db.users().insertGroup();
87 template1 = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
88 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
89 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
90 addGroupToTemplate(group1, template1, UserRole.ADMIN);
91 addGroupToTemplate(group2, template1, UserRole.USER);
93 template2 = db.permissionTemplates().insertTemplate(db.getDefaultOrganization());
94 addUserToTemplate(user1, template2, UserRole.USER);
95 addUserToTemplate(user2, template2, UserRole.USER);
96 addGroupToTemplate(group1, template2, UserRole.USER);
97 addGroupToTemplate(group2, template2, UserRole.USER);
99 project = db.components().insertPrivateProject();
100 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, project);
101 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, project);
102 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, project);
103 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, project);
107 public void apply_template_with_project_uuid() throws Exception {
108 loginAsAdmin(db.getDefaultOrganization());
110 newRequest(template1.getUuid(), project.uuid(), null);
112 assertTemplate1AppliedToProject();
116 public void apply_template_with_project_uuid_by_template_name() {
117 loginAsAdmin(db.getDefaultOrganization());
120 .setParam(PARAM_TEMPLATE_NAME, template1.getName().toUpperCase())
121 .setParam(PARAM_PROJECT_ID, project.uuid())
124 assertTemplate1AppliedToProject();
128 public void apply_template_with_project_key() throws Exception {
129 loginAsAdmin(db.getDefaultOrganization());
131 newRequest(template1.getUuid(), null, project.getDbKey());
133 assertTemplate1AppliedToProject();
137 public void fail_when_unknown_template() throws Exception {
138 loginAsAdmin(db.getDefaultOrganization());
140 expectedException.expect(NotFoundException.class);
141 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
143 newRequest("unknown-template-uuid", project.uuid(), null);
147 public void fail_when_unknown_project_uuid() throws Exception {
148 loginAsAdmin(db.getDefaultOrganization());
150 expectedException.expect(NotFoundException.class);
151 expectedException.expectMessage("Project id 'unknown-project-uuid' not found");
153 newRequest(template1.getUuid(), "unknown-project-uuid", null);
157 public void fail_when_unknown_project_key() throws Exception {
158 loginAsAdmin(db.getDefaultOrganization());
160 expectedException.expect(NotFoundException.class);
161 expectedException.expectMessage("Project key 'unknown-project-key' not found");
163 newRequest(template1.getUuid(), null, "unknown-project-key");
167 public void fail_when_template_is_not_provided() throws Exception {
168 loginAsAdmin(db.getDefaultOrganization());
170 expectedException.expect(BadRequestException.class);
172 newRequest(null, project.uuid(), null);
176 public void fail_when_project_uuid_and_key_not_provided() throws Exception {
177 loginAsAdmin(db.getDefaultOrganization());
179 expectedException.expect(BadRequestException.class);
180 expectedException.expectMessage("Project id or project key can be provided, not both.");
182 newRequest(template1.getUuid(), null, null);
186 public void fail_when_not_admin_of_organization() throws Exception {
187 userSession.logIn().addPermission(ADMINISTER, "otherOrg");
189 expectedException.expect(ForbiddenException.class);
191 newRequest(template1.getUuid(), project.uuid(), null);
194 private void assertTemplate1AppliedToProject() {
195 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
196 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
197 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
198 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
199 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
202 private TestResponse newRequest(@Nullable String templateUuid, @Nullable String projectUuid, @Nullable String projectKey) {
203 TestRequest request = newRequest();
204 if (templateUuid != null) {
205 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
207 if (projectUuid != null) {
208 request.setParam(PARAM_PROJECT_ID, projectUuid);
210 if (projectKey != null) {
211 request.setParam(PARAM_PROJECT_KEY, projectKey);
214 return request.execute();
217 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
218 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
222 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
223 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
227 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
228 PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(project.getOrganizationUuid()).setPermission(permission).setComponentUuid(project.uuid()).build();
229 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), query);
232 private List<Integer> selectProjectPermissionUsers(ComponentDto project, String permission) {
233 PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(project.getOrganizationUuid()).setPermission(permission).setComponentUuid(project.uuid()).build();
234 return db.getDbClient().userPermissionDao().selectUserIdsByQuery(db.getSession(), query);