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.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.NotFoundException;
36 import org.sonar.server.i18n.I18nRule;
37 import org.sonar.server.permission.PermissionTemplateService;
38 import org.sonar.server.permission.index.PermissionIndexer;
39 import org.sonar.server.permission.ws.BasePermissionWsTest;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.mock;
43 import static org.sonar.db.component.ComponentTesting.newDeveloper;
44 import static org.sonar.db.component.ComponentTesting.newProjectDto;
45 import static org.sonar.db.component.ComponentTesting.newView;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
50 public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyTemplateAction> {
52 private UserDto user1;
53 private UserDto user2;
54 private GroupDto group1;
55 private GroupDto group2;
56 private PermissionTemplateDto template1;
57 private PermissionTemplateDto template2;
58 private PermissionIndexer issuePermissionIndexer = mock(PermissionIndexer.class);
61 protected BulkApplyTemplateAction buildWsAction() {
62 PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(), new MapSettings(), issuePermissionIndexer, userSession);
63 return new BulkApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport(), new I18nRule(), newRootResourceTypes());
68 user1 = db.users().insertUser("user-login-1");
69 user2 = db.users().insertUser("user-login-2");
70 OrganizationDto defaultOrg = db.getDefaultOrganization();
71 group1 = db.users().insertGroup(defaultOrg, "group-name-1");
72 group2 = db.users().insertGroup(defaultOrg, "group-name-2");
75 template1 = insertTemplate();
76 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
77 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
78 addGroupToTemplate(group1, template1, UserRole.ADMIN);
79 addGroupToTemplate(group2, template1, UserRole.USER);
81 template2 = insertTemplate();
82 addUserToTemplate(user1, template2, UserRole.USER);
83 addUserToTemplate(user2, template2, UserRole.USER);
84 addGroupToTemplate(group1, template2, UserRole.USER);
85 addGroupToTemplate(group2, template2, UserRole.USER);
89 public void bulk_apply_template_by_template_uuid() throws Exception {
90 ComponentDto project = db.components().insertComponent(newProjectDto());
91 ComponentDto view = db.components().insertComponent(newView());
92 ComponentDto developer = db.components().insertComponent(newDeveloper("developer-name"));
93 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, developer);
94 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, developer);
95 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, developer);
96 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, developer);
97 loginAsAdminOnDefaultOrganization();
99 newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()).execute();
101 assertTemplate1AppliedToProject(project);
102 assertTemplate1AppliedToProject(view);
103 assertTemplate1AppliedToProject(developer);
107 public void bulk_apply_template_by_template_name() throws Exception {
108 ComponentDto project = db.components().insertComponent(newProjectDto());
109 loginAsAdminOnDefaultOrganization();
111 newRequest().setParam(PARAM_TEMPLATE_NAME, template1.getName()).execute();
113 assertTemplate1AppliedToProject(project);
117 public void apply_template_by_qualifier() throws Exception {
118 ComponentDto project = db.components().insertComponent(newProjectDto());
119 ComponentDto view = db.components().insertComponent(newView());
120 loginAsAdminOnDefaultOrganization();
123 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
124 .setParam(PARAM_QUALIFIER, project.qualifier()).execute();
126 assertTemplate1AppliedToProject(project);
127 assertNoPermissionOnProject(view);
131 public void apply_template_by_query_on_name_and_key() throws Exception {
132 ComponentDto projectFoundByKey = newProjectDto().setKey("sonar");
133 db.components().insertProjectAndSnapshot(projectFoundByKey);
134 ComponentDto projectFoundByName = newProjectDto().setName("name-sonar-name");
135 db.components().insertProjectAndSnapshot(projectFoundByName);
136 // match must be exact on key
137 ComponentDto projectUntouched = newProjectDto().setKey("new-sonar").setName("project-name");
138 db.components().insertProjectAndSnapshot(projectUntouched);
139 db.components().indexAllComponents();
140 loginAsAdminOnDefaultOrganization();
143 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
144 .setParam(Param.TEXT_QUERY, "sonar")
147 assertTemplate1AppliedToProject(projectFoundByKey);
148 assertTemplate1AppliedToProject(projectFoundByName);
149 assertNoPermissionOnProject(projectUntouched);
153 public void fail_if_no_template_parameter() throws Exception {
154 loginAsAdminOnDefaultOrganization();
156 expectedException.expect(BadRequestException.class);
157 expectedException.expectMessage("Template name or template id must be provided, not both.");
159 newRequest().execute();
163 public void fail_if_template_name_is_incorrect() throws Exception {
164 loginAsAdminOnDefaultOrganization();
166 expectedException.expect(NotFoundException.class);
167 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
169 newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid").execute();
172 private void assertTemplate1AppliedToProject(ComponentDto project) throws Exception {
173 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
174 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
175 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
176 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
177 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
180 private void assertNoPermissionOnProject(ComponentDto project) throws Exception {
181 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty();
182 assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty();
183 assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty();
184 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
185 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
186 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
187 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).isEmpty();
188 assertThat(selectProjectPermissionUsers(project, UserRole.USER)).isEmpty();
191 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
192 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
196 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
197 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
201 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
202 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
203 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
206 private List<Long> selectProjectPermissionUsers(ComponentDto project, String permission) {
207 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
208 return db.getDbClient().userPermissionDao().selectUserIds(db.getSession(), db.getDefaultOrganization().getUuid(), query);