3 * Copyright (C) 2009-2017 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 org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.server.ws.WebService.Param;
26 import org.sonar.api.web.UserRole;
27 import org.sonar.db.component.ComponentDto;
28 import org.sonar.db.component.ComponentTesting;
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.newView;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
49 public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyTemplateAction> {
52 public DefaultTemplatesResolverRule defaultTemplatesResolver = DefaultTemplatesResolverRule.withoutGovernance();
54 private UserDto user1;
55 private UserDto user2;
56 private GroupDto group1;
57 private GroupDto group2;
58 private OrganizationDto organization;
59 private PermissionTemplateDto template1;
60 private PermissionTemplateDto template2;
61 private PermissionIndexer issuePermissionIndexer = mock(PermissionIndexer.class);
64 protected BulkApplyTemplateAction buildWsAction() {
65 PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
66 issuePermissionIndexer, userSession, defaultTemplatesResolver);
67 return new BulkApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport(), new I18nRule(), newRootResourceTypes());
72 organization = db.organizations().insert();
74 user1 = db.users().insertUser();
75 user2 = db.users().insertUser();
76 group1 = db.users().insertGroup(organization);
77 group2 = db.users().insertGroup(organization);
79 db.organizations().addMember(organization, user1);
80 db.organizations().addMember(organization, user2);
82 // template 1 for org 1
83 template1 = db.permissionTemplates().insertTemplate(organization);
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 = db.permissionTemplates().insertTemplate(organization);
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 // this project should not be applied the template
99 OrganizationDto otherOrganization = db.organizations().insert();
100 db.components().insertPrivateProject(otherOrganization);
102 ComponentDto privateProject = db.components().insertPrivateProject(organization);
103 ComponentDto publicProject = db.components().insertPublicProject(organization);
104 ComponentDto view = db.components().insertView(organization);
105 loginAsAdmin(organization);
107 newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()).execute();
109 assertTemplate1AppliedToPrivateProject(privateProject);
110 assertTemplate1AppliedToPublicProject(publicProject);
111 assertTemplate1AppliedToPublicProject(view);
115 public void request_throws_NotFoundException_if_template_with_specified_name_does_not_exist_in_specified_organization() throws Exception {
116 OrganizationDto otherOrganization = db.organizations().insert();
117 loginAsAdmin(otherOrganization);
119 expectedException.expect(NotFoundException.class);
120 expectedException.expectMessage("Permission template with name '" + template1.getName()
121 + "' is not found (case insensitive) in organization with key '" + otherOrganization.getKey() + "'");
124 .setParam(PARAM_ORGANIZATION, otherOrganization.getKey())
125 .setParam(PARAM_TEMPLATE_NAME, template1.getName())
130 public void bulk_apply_template_by_template_name() throws Exception {
131 ComponentDto privateProject = db.components().insertPrivateProject(organization);
132 ComponentDto publicProject = db.components().insertPublicProject(organization);
133 loginAsAdmin(organization);
136 .setParam(PARAM_ORGANIZATION, organization.getKey())
137 .setParam(PARAM_TEMPLATE_NAME, template1.getName())
140 assertTemplate1AppliedToPrivateProject(privateProject);
141 assertTemplate1AppliedToPublicProject(publicProject);
145 public void apply_template_by_qualifier() throws Exception {
146 ComponentDto publicProject = db.components().insertPublicProject(organization);
147 ComponentDto privateProject = db.components().insertPrivateProject(organization);
148 ComponentDto view = db.components().insertComponent(newView(organization));
149 loginAsAdmin(organization);
152 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
153 .setParam(PARAM_QUALIFIER, privateProject.qualifier()).execute();
155 assertTemplate1AppliedToPrivateProject(privateProject);
156 assertTemplate1AppliedToPublicProject(publicProject);
157 assertNoPermissionOnProject(view);
161 public void apply_template_by_query_on_name_and_key_public_project() throws Exception {
162 ComponentDto publicProjectFoundByKey = ComponentTesting.newPublicProjectDto(organization).setKey("sonar");
163 db.components().insertProjectAndSnapshot(publicProjectFoundByKey);
164 ComponentDto publicProjectFoundByName = ComponentTesting.newPublicProjectDto(organization).setName("name-sonar-name");
165 db.components().insertProjectAndSnapshot(publicProjectFoundByName);
166 // match must be exact on key
167 ComponentDto projectUntouched = ComponentTesting.newPublicProjectDto(organization).setKey("new-sonar").setName("project-name");
168 db.components().insertProjectAndSnapshot(projectUntouched);
169 loginAsAdmin(organization);
172 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
173 .setParam(Param.TEXT_QUERY, "sonar")
176 assertTemplate1AppliedToPublicProject(publicProjectFoundByKey);
177 assertTemplate1AppliedToPublicProject(publicProjectFoundByName);
178 assertNoPermissionOnProject(projectUntouched);
182 public void apply_template_by_query_on_name_and_key() throws Exception {
183 ComponentDto privateProjectFoundByKey = ComponentTesting.newPrivateProjectDto(organization).setKey("sonar");
184 db.components().insertProjectAndSnapshot(privateProjectFoundByKey);
185 ComponentDto privateProjectFoundByName = ComponentTesting.newPrivateProjectDto(organization).setName("name-sonar-name");
186 db.components().insertProjectAndSnapshot(privateProjectFoundByName);
187 // match must be exact on key
188 ComponentDto projectUntouched = ComponentTesting.newPublicProjectDto(organization).setKey("new-sonar").setName("project-name");
189 db.components().insertProjectAndSnapshot(projectUntouched);
190 loginAsAdmin(organization);
193 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
194 .setParam(Param.TEXT_QUERY, "sonar")
197 assertTemplate1AppliedToPrivateProject(privateProjectFoundByKey);
198 assertTemplate1AppliedToPrivateProject(privateProjectFoundByName);
199 assertNoPermissionOnProject(projectUntouched);
203 public void fail_if_no_template_parameter() throws Exception {
204 loginAsAdmin(db.getDefaultOrganization());
206 expectedException.expect(BadRequestException.class);
207 expectedException.expectMessage("Template name or template id must be provided, not both.");
209 newRequest().execute();
213 public void fail_if_template_name_is_incorrect() throws Exception {
214 loginAsAdmin(db.getDefaultOrganization());
216 expectedException.expect(NotFoundException.class);
217 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
219 newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid").execute();
222 private void assertTemplate1AppliedToPublicProject(ComponentDto project) throws Exception {
223 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
224 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
225 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
226 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
227 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
230 private void assertTemplate1AppliedToPrivateProject(ComponentDto project) throws Exception {
231 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
232 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
233 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
234 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
235 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
238 private void assertNoPermissionOnProject(ComponentDto project) throws Exception {
239 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty();
240 assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty();
241 assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty();
242 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
243 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
244 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
245 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).isEmpty();
246 assertThat(selectProjectPermissionUsers(project, UserRole.USER)).isEmpty();
249 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
250 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
254 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
255 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
259 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
260 PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(project.getOrganizationUuid()).setPermission(permission).setComponentUuid(project.uuid()).build();
261 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), query);
264 private List<Integer> selectProjectPermissionUsers(ComponentDto project, String permission) {
265 PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(project.getOrganizationUuid()).setPermission(permission).setComponentUuid(project.uuid()).build();
266 return db.getDbClient().userPermissionDao().selectUserIdsByQuery(db.getSession(), query);