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.exceptions.BadRequestException;
36 import org.sonar.server.exceptions.NotFoundException;
37 import org.sonar.server.i18n.I18nRule;
38 import org.sonar.server.permission.PermissionTemplateService;
39 import org.sonar.server.permission.index.PermissionIndexer;
40 import org.sonar.server.permission.ws.BasePermissionWsTest;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.sonar.db.component.ComponentTesting.newDeveloper;
45 import static org.sonar.db.component.ComponentTesting.newProjectDto;
46 import static org.sonar.db.component.ComponentTesting.newView;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
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 BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyTemplateAction> {
53 private UserDto user1;
54 private UserDto user2;
55 private GroupDto group1;
56 private GroupDto group2;
57 private PermissionTemplateDto template1;
58 private PermissionTemplateDto template2;
59 private PermissionIndexer issuePermissionIndexer = mock(PermissionIndexer.class);
62 protected BulkApplyTemplateAction buildWsAction() {
63 PermissionRepository repository = new PermissionRepository(db.getDbClient(), new MapSettings());
64 PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(), repository, issuePermissionIndexer, userSession);
65 return new BulkApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport(), new I18nRule(), newRootResourceTypes());
70 user1 = db.users().insertUser("user-login-1");
71 user2 = db.users().insertUser("user-login-2");
72 OrganizationDto defaultOrg = db.getDefaultOrganization();
73 group1 = db.users().insertGroup(defaultOrg, "group-name-1");
74 group2 = db.users().insertGroup(defaultOrg, "group-name-2");
77 template1 = insertTemplate();
78 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
79 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
80 addGroupToTemplate(group1, template1, UserRole.ADMIN);
81 addGroupToTemplate(group2, template1, UserRole.USER);
83 template2 = insertTemplate();
84 addUserToTemplate(user1, template2, UserRole.USER);
85 addUserToTemplate(user2, template2, UserRole.USER);
86 addGroupToTemplate(group1, template2, UserRole.USER);
87 addGroupToTemplate(group2, template2, UserRole.USER);
91 public void bulk_apply_template_by_template_uuid() throws Exception {
92 ComponentDto project = db.components().insertComponent(newProjectDto());
93 ComponentDto view = db.components().insertComponent(newView());
94 ComponentDto developer = db.components().insertComponent(newDeveloper("developer-name"));
95 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, developer);
96 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, developer);
97 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, developer);
98 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, developer);
99 loginAsAdminOnDefaultOrganization();
101 newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()).execute();
103 assertTemplate1AppliedToProject(project);
104 assertTemplate1AppliedToProject(view);
105 assertTemplate1AppliedToProject(developer);
109 public void bulk_apply_template_by_template_name() throws Exception {
110 ComponentDto project = db.components().insertComponent(newProjectDto());
111 loginAsAdminOnDefaultOrganization();
113 newRequest().setParam(PARAM_TEMPLATE_NAME, template1.getName()).execute();
115 assertTemplate1AppliedToProject(project);
119 public void apply_template_by_qualifier() throws Exception {
120 ComponentDto project = db.components().insertComponent(newProjectDto());
121 ComponentDto view = db.components().insertComponent(newView());
122 loginAsAdminOnDefaultOrganization();
125 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
126 .setParam(PARAM_QUALIFIER, project.qualifier()).execute();
128 assertTemplate1AppliedToProject(project);
129 assertNoPermissionOnProject(view);
133 public void apply_template_by_query_on_name_and_key() throws Exception {
134 ComponentDto projectFoundByKey = newProjectDto().setKey("sonar");
135 db.components().insertProjectAndSnapshot(projectFoundByKey);
136 ComponentDto projectFoundByName = newProjectDto().setName("name-sonar-name");
137 db.components().insertProjectAndSnapshot(projectFoundByName);
138 // match must be exact on key
139 ComponentDto projectUntouched = newProjectDto().setKey("new-sonar").setName("project-name");
140 db.components().insertProjectAndSnapshot(projectUntouched);
141 db.components().indexAllComponents();
142 loginAsAdminOnDefaultOrganization();
145 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
146 .setParam(Param.TEXT_QUERY, "sonar")
149 assertTemplate1AppliedToProject(projectFoundByKey);
150 assertTemplate1AppliedToProject(projectFoundByName);
151 assertNoPermissionOnProject(projectUntouched);
155 public void fail_if_no_template_parameter() throws Exception {
156 loginAsAdminOnDefaultOrganization();
158 expectedException.expect(BadRequestException.class);
159 expectedException.expectMessage("Template name or template id must be provided, not both.");
161 newRequest().execute();
165 public void fail_if_template_name_is_incorrect() throws Exception {
166 loginAsAdminOnDefaultOrganization();
168 expectedException.expect(NotFoundException.class);
169 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
171 newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid").execute();
174 private void assertTemplate1AppliedToProject(ComponentDto project) throws Exception {
175 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
176 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
177 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
178 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
179 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
182 private void assertNoPermissionOnProject(ComponentDto project) throws Exception {
183 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty();
184 assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty();
185 assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty();
186 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
187 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
188 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
189 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).isEmpty();
190 assertThat(selectProjectPermissionUsers(project, UserRole.USER)).isEmpty();
193 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
194 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
198 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
199 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
203 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
204 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
205 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
208 private List<Long> selectProjectPermissionUsers(ComponentDto project, String permission) {
209 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
210 return db.getDbClient().userPermissionDao().selectUserIds(db.getSession(), db.getDefaultOrganization().getUuid(), query);