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.
21 package org.sonar.server.permission.ws.template;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.sonar.db.component.ComponentTesting.newDeveloper;
26 import static org.sonar.db.component.ComponentTesting.newProjectDto;
27 import static org.sonar.db.component.ComponentTesting.newView;
28 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
29 import static org.sonar.db.user.GroupMembershipQuery.IN;
30 import static org.sonar.db.user.GroupTesting.newGroupDto;
31 import static org.sonar.db.user.UserTesting.newUserDto;
32 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
33 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
34 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
36 import com.google.common.base.Predicate;
37 import com.google.common.collect.FluentIterable;
38 import java.util.List;
39 import javax.annotation.Nullable;
40 import org.junit.Before;
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.rules.ExpectedException;
44 import org.sonar.api.config.Settings;
45 import org.sonar.api.resources.Qualifiers;
46 import org.sonar.api.server.ws.WebService.Param;
47 import org.sonar.api.utils.System2;
48 import org.sonar.api.web.UserRole;
49 import org.sonar.core.permission.GlobalPermissions;
50 import org.sonar.db.DbClient;
51 import org.sonar.db.DbSession;
52 import org.sonar.db.DbTester;
53 import org.sonar.db.component.ComponentDbTester;
54 import org.sonar.db.component.ComponentDto;
55 import org.sonar.db.component.ResourceTypesRule;
56 import org.sonar.db.permission.GroupWithPermissionDto;
57 import org.sonar.db.permission.OldPermissionQuery;
58 import org.sonar.db.permission.PermissionRepository;
59 import org.sonar.db.permission.template.PermissionTemplateDto;
60 import org.sonar.db.permission.UserWithPermissionDto;
61 import org.sonar.db.user.GroupDbTester;
62 import org.sonar.db.user.GroupDto;
63 import org.sonar.db.user.GroupRoleDto;
64 import org.sonar.db.user.UserDbTester;
65 import org.sonar.db.user.UserDto;
66 import org.sonar.db.user.UserRoleDto;
67 import org.sonar.server.component.ComponentFinder;
68 import org.sonar.server.exceptions.BadRequestException;
69 import org.sonar.server.exceptions.NotFoundException;
70 import org.sonar.server.i18n.I18nRule;
71 import org.sonar.server.issue.index.IssueAuthorizationIndexer;
72 import org.sonar.server.permission.PermissionService;
73 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
74 import org.sonar.server.tester.UserSessionRule;
75 import org.sonar.server.usergroups.ws.UserGroupFinder;
76 import org.sonar.server.ws.TestRequest;
77 import org.sonar.server.ws.WsActionTester;
79 public class BulkApplyTemplateActionTest {
81 public UserSessionRule userSession = UserSessionRule.standalone().login("login").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
83 public ExpectedException expectedException = ExpectedException.none();
85 public DbTester db = DbTester.create(System2.INSTANCE);
86 ComponentDbTester componentDb = new ComponentDbTester(db);
87 UserDbTester userDb = new UserDbTester(db);
88 GroupDbTester groupDb = new GroupDbTester(db);
89 DbClient dbClient = db.getDbClient();
90 DbSession dbSession = db.getSession();
92 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
93 I18nRule i18n = new I18nRule();
100 PermissionTemplateDto template1;
101 PermissionTemplateDto template2;
102 IssueAuthorizationIndexer issueAuthorizationIndexer = mock(IssueAuthorizationIndexer.class);
105 public void setUp() {
106 PermissionRepository repository = new PermissionRepository(dbClient, new Settings());
107 ComponentFinder componentFinder = new ComponentFinder(dbClient);
108 PermissionService permissionService = new PermissionService(dbClient, repository, issueAuthorizationIndexer, userSession, componentFinder);
109 PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder, new UserGroupFinder(dbClient), resourceTypes);
111 BulkApplyTemplateAction underTest = new BulkApplyTemplateAction(dbClient, permissionService, permissionDependenciesFinder, i18n, resourceTypes);
112 ws = new WsActionTester(underTest);
114 user1 = userDb.insertUser(newUserDto().setLogin("user-login-1"));
115 user2 = userDb.insertUser(newUserDto().setLogin("user-login-2"));
116 group1 = groupDb.insertGroup(newGroupDto().setName("group-name-1"));
117 group2 = groupDb.insertGroup(newGroupDto().setName("group-name-2"));
120 template1 = insertTemplate(newPermissionTemplateDto().setUuid("permission-template-uuid-1"));
121 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
122 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
123 addGroupToTemplate(group1, template1, UserRole.ADMIN);
124 addGroupToTemplate(group2, template1, UserRole.USER);
126 template2 = insertTemplate(newPermissionTemplateDto().setUuid("permission-template-uuid-2"));
127 addUserToTemplate(user1, template2, UserRole.USER);
128 addUserToTemplate(user2, template2, UserRole.USER);
129 addGroupToTemplate(group1, template2, UserRole.USER);
130 addGroupToTemplate(group2, template2, UserRole.USER);
136 public void bulk_apply_template_by_template_uuid() {
137 ComponentDto project = componentDb.insertComponent(newProjectDto());
138 ComponentDto view = componentDb.insertComponent(newView());
139 ComponentDto developer = componentDb.insertComponent(newDeveloper("developer-name"));
140 addUserPermissionToProject(user1, developer, UserRole.ADMIN);
141 addUserPermissionToProject(user2, developer, UserRole.ADMIN);
142 addGroupPermissionToProject(group1, developer, UserRole.ADMIN);
143 addGroupPermissionToProject(group2, developer, UserRole.ADMIN);
146 call(ws.newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()));
148 assertTemplate1AppliedToProject(project);
149 assertTemplate1AppliedToProject(view);
150 assertTemplate1AppliedToProject(developer);
154 public void bulk_apply_template_by_template_name() {
155 ComponentDto project = componentDb.insertComponent(newProjectDto());
157 call(ws.newRequest().setParam(PARAM_TEMPLATE_NAME, template1.getName()));
159 assertTemplate1AppliedToProject(project);
163 public void apply_template_by_qualifier() {
164 ComponentDto project = componentDb.insertComponent(newProjectDto());
165 ComponentDto view = componentDb.insertComponent(newView());
168 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
169 .setParam(PARAM_QUALIFIER, project.qualifier()));
171 assertTemplate1AppliedToProject(project);
172 assertNoPermissionOnProject(view);
176 public void apply_template_by_query_on_name_and_key() {
177 ComponentDto projectFoundByKey = newProjectDto().setKey("sonar");
178 componentDb.insertProjectAndSnapshot(projectFoundByKey);
179 ComponentDto projectFoundByName = newProjectDto().setName("name-sonar-name");
180 componentDb.insertProjectAndSnapshot(projectFoundByName);
181 // match must be exact on key
182 ComponentDto projectUntouched = newProjectDto().setKey("new-sonar").setName("project-name");
183 componentDb.insertProjectAndSnapshot(projectUntouched);
184 componentDb.indexAllComponents();
187 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
188 .setParam(Param.TEXT_QUERY, "sonar"));
190 assertTemplate1AppliedToProject(projectFoundByKey);
191 assertTemplate1AppliedToProject(projectFoundByName);
192 assertNoPermissionOnProject(projectUntouched);
196 public void fail_if_no_template_parameter() {
197 expectedException.expect(BadRequestException.class);
198 expectedException.expectMessage("Template name or template id must be provided, not both.");
200 call(ws.newRequest());
204 public void fail_if_template_name_is_incorrect() {
205 expectedException.expect(NotFoundException.class);
206 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
208 call(ws.newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid"));
211 private void call(TestRequest request) {
216 private void assertTemplate1AppliedToProject(ComponentDto project) {
217 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).extracting("name").containsExactly(group1.getName());
218 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).extracting("name").containsExactly(group2.getName());
219 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
220 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).extracting("login").containsExactly(user1.getLogin());
221 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).extracting("login").containsExactly(user2.getLogin());
224 private void assertNoPermissionOnProject(ComponentDto project) {
225 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty();
226 assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty();
227 assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty();
228 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
229 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
230 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
231 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).isEmpty();
232 assertThat(selectProjectPermissionUsers(project, UserRole.USER)).isEmpty();
235 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
236 return dbClient.permissionTemplateDao().insert(dbSession, template);
239 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
240 dbClient.permissionTemplateDao().insertUserPermission(dbSession, permissionTemplate.getId(), user.getId(), permission);
243 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
244 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, permissionTemplate.getId(), group.getId(), permission);
247 private void addUserPermissionToProject(UserDto user, ComponentDto project, String permission) {
248 dbClient.roleDao().insertUserRole(dbSession, new UserRoleDto()
250 .setUserId(user.getId())
251 .setResourceId(project.getId()));
254 private void addGroupPermissionToProject(GroupDto group, ComponentDto project, String permission) {
255 dbClient.roleDao().insertGroupRole(dbSession, new GroupRoleDto()
257 .setResourceId(project.getId())
258 .setGroupId(group.getId()));
261 private List<GroupWithPermissionDto> selectProjectPermissionGroups(ComponentDto project, String permission) {
262 return FluentIterable.from(dbClient.permissionDao().selectGroups(dbSession, query(permission), project.getId()))
263 .filter(new PermissionNotNull())
267 private List<UserWithPermissionDto> selectProjectPermissionUsers(ComponentDto project, String permission) {
268 return dbClient.permissionDao().selectUsers(dbSession, query(permission), project.getId(), 0, Integer.MAX_VALUE);
271 private void commit() {
275 private static OldPermissionQuery query(String permission) {
276 return OldPermissionQuery.builder().membership(IN).permission(permission).build();
279 private static class PermissionNotNull implements Predicate<GroupWithPermissionDto> {
281 public boolean apply(@Nullable GroupWithPermissionDto input) {
282 return input.getPermission() != null;