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 org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.config.MapSettings;
26 import org.sonar.api.server.ws.WebService.Param;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.component.ComponentDto;
29 import org.sonar.db.organization.OrganizationDto;
30 import org.sonar.db.permission.PermissionQuery;
31 import org.sonar.db.permission.PermissionRepository;
32 import org.sonar.db.permission.template.PermissionTemplateDto;
33 import org.sonar.db.user.GroupDto;
34 import org.sonar.db.user.UserDto;
35 import org.sonar.server.component.ComponentFinder;
36 import org.sonar.server.exceptions.BadRequestException;
37 import org.sonar.server.exceptions.NotFoundException;
38 import org.sonar.server.i18n.I18nRule;
39 import org.sonar.server.permission.PermissionService;
40 import org.sonar.server.permission.index.PermissionIndexer;
41 import org.sonar.server.permission.ws.BasePermissionWsTest;
42 import org.sonar.server.ws.WsTester;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.mockito.Mockito.mock;
46 import static org.sonar.db.component.ComponentTesting.newDeveloper;
47 import static org.sonar.db.component.ComponentTesting.newProjectDto;
48 import static org.sonar.db.component.ComponentTesting.newView;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
50 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
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 BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyTemplateAction> {
56 private static final String ACTION = "bulk_apply_template";
58 private UserDto user1;
59 private UserDto user2;
60 private GroupDto group1;
61 private GroupDto group2;
62 private PermissionTemplateDto template1;
63 private PermissionTemplateDto template2;
64 private PermissionIndexer issuePermissionIndexer = mock(PermissionIndexer.class);
67 protected BulkApplyTemplateAction buildWsAction() {
68 PermissionRepository repository = new PermissionRepository(db.getDbClient(), new MapSettings());
69 ComponentFinder componentFinder = new ComponentFinder(db.getDbClient());
70 PermissionService permissionService = new PermissionService(db.getDbClient(), repository, issuePermissionIndexer, userSession, componentFinder);
71 return new BulkApplyTemplateAction(db.getDbClient(), userSession, permissionService, newPermissionWsSupport(), new I18nRule(), newRootResourceTypes());
76 user1 = db.users().insertUser("user-login-1");
77 user2 = db.users().insertUser("user-login-2");
78 OrganizationDto defaultOrg = db.getDefaultOrganization();
79 group1 = db.users().insertGroup(defaultOrg, "group-name-1");
80 group2 = db.users().insertGroup(defaultOrg, "group-name-2");
83 template1 = insertTemplate();
84 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
85 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
86 addGroupToTemplate(group1, template1, UserRole.ADMIN);
87 addGroupToTemplate(group2, template1, UserRole.USER);
89 template2 = insertTemplate();
90 addUserToTemplate(user1, template2, UserRole.USER);
91 addUserToTemplate(user2, template2, UserRole.USER);
92 addGroupToTemplate(group1, template2, UserRole.USER);
93 addGroupToTemplate(group2, template2, UserRole.USER);
97 public void bulk_apply_template_by_template_uuid() throws Exception {
98 ComponentDto project = db.components().insertComponent(newProjectDto());
99 ComponentDto view = db.components().insertComponent(newView());
100 ComponentDto developer = db.components().insertComponent(newDeveloper("developer-name"));
101 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, developer);
102 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, developer);
103 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, developer);
104 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, developer);
105 loginAsAdminOnDefaultOrganization();
107 newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()).execute();
109 assertTemplate1AppliedToProject(project);
110 assertTemplate1AppliedToProject(view);
111 assertTemplate1AppliedToProject(developer);
115 public void bulk_apply_template_by_template_name() throws Exception {
116 ComponentDto project = db.components().insertComponent(newProjectDto());
117 loginAsAdminOnDefaultOrganization();
119 newRequest().setParam(PARAM_TEMPLATE_NAME, template1.getName()).execute();
121 assertTemplate1AppliedToProject(project);
125 public void apply_template_by_qualifier() throws Exception {
126 ComponentDto project = db.components().insertComponent(newProjectDto());
127 ComponentDto view = db.components().insertComponent(newView());
128 loginAsAdminOnDefaultOrganization();
131 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
132 .setParam(PARAM_QUALIFIER, project.qualifier()).execute();
134 assertTemplate1AppliedToProject(project);
135 assertNoPermissionOnProject(view);
139 public void apply_template_by_query_on_name_and_key() throws Exception {
140 ComponentDto projectFoundByKey = newProjectDto().setKey("sonar");
141 db.components().insertProjectAndSnapshot(projectFoundByKey);
142 ComponentDto projectFoundByName = newProjectDto().setName("name-sonar-name");
143 db.components().insertProjectAndSnapshot(projectFoundByName);
144 // match must be exact on key
145 ComponentDto projectUntouched = newProjectDto().setKey("new-sonar").setName("project-name");
146 db.components().insertProjectAndSnapshot(projectUntouched);
147 db.components().indexAllComponents();
148 loginAsAdminOnDefaultOrganization();
151 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
152 .setParam(Param.TEXT_QUERY, "sonar")
155 assertTemplate1AppliedToProject(projectFoundByKey);
156 assertTemplate1AppliedToProject(projectFoundByName);
157 assertNoPermissionOnProject(projectUntouched);
161 public void fail_if_no_template_parameter() throws Exception {
162 loginAsAdminOnDefaultOrganization();
164 expectedException.expect(BadRequestException.class);
165 expectedException.expectMessage("Template name or template id must be provided, not both.");
167 newRequest().execute();
171 public void fail_if_template_name_is_incorrect() throws Exception {
172 loginAsAdminOnDefaultOrganization();
174 expectedException.expect(NotFoundException.class);
175 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
177 newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid").execute();
180 private void assertTemplate1AppliedToProject(ComponentDto project) throws Exception {
181 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
182 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
183 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
184 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getLogin());
185 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getLogin());
188 private void assertNoPermissionOnProject(ComponentDto project) throws Exception {
189 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty();
190 assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty();
191 assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty();
192 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
193 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
194 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
195 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).isEmpty();
196 assertThat(selectProjectPermissionUsers(project, UserRole.USER)).isEmpty();
199 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
200 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
204 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
205 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
209 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
210 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
211 return db.getDbClient().groupPermissionDao().selectGroupNamesByPermissionQuery(db.getSession(), query);
214 private List<String> selectProjectPermissionUsers(ComponentDto project, String permission) {
215 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
216 return db.getDbClient().userPermissionDao().selectLogins(db.getSession(), query);
219 private WsTester.TestRequest newRequest() {
220 return wsTester.newPostRequest(CONTROLLER, ACTION);