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.server.ws.WebService.Param;
26 import org.sonar.api.web.UserRole;
27 import org.sonar.db.component.ComponentDto;
28 import org.sonar.db.organization.OrganizationDto;
29 import org.sonar.db.permission.PermissionQuery;
30 import org.sonar.db.permission.template.PermissionTemplateDto;
31 import org.sonar.db.user.GroupDto;
32 import org.sonar.db.user.UserDto;
33 import org.sonar.server.exceptions.BadRequestException;
34 import org.sonar.server.exceptions.NotFoundException;
35 import org.sonar.server.i18n.I18nRule;
36 import org.sonar.server.permission.PermissionTemplateService;
37 import org.sonar.server.permission.index.PermissionIndexer;
38 import org.sonar.server.permission.ws.BasePermissionWsTest;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.mockito.Mockito.mock;
42 import static org.sonar.db.component.ComponentTesting.newProjectDto;
43 import static org.sonar.db.component.ComponentTesting.newView;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
48 public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyTemplateAction> {
51 public DefaultTemplatesResolverRule defaultTemplatesResolver = DefaultTemplatesResolverRule.withoutGovernance();
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 PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
64 issuePermissionIndexer, userSession, defaultTemplatesResolver);
65 return new BulkApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport(), new I18nRule(), newRootResourceTypes());
70 user1 = db.users().insertUser();
71 user2 = db.users().insertUser();
72 group1 = db.users().insertGroup();
73 group2 = db.users().insertGroup();
76 template1 = insertTemplate();
77 addUserToTemplate(user1, template1, UserRole.CODEVIEWER);
78 addUserToTemplate(user2, template1, UserRole.ISSUE_ADMIN);
79 addGroupToTemplate(group1, template1, UserRole.ADMIN);
80 addGroupToTemplate(group2, template1, UserRole.USER);
82 template2 = insertTemplate();
83 addUserToTemplate(user1, template2, UserRole.USER);
84 addUserToTemplate(user2, template2, UserRole.USER);
85 addGroupToTemplate(group1, template2, UserRole.USER);
86 addGroupToTemplate(group2, template2, UserRole.USER);
90 public void bulk_apply_template_by_template_uuid() throws Exception {
91 ComponentDto project = db.components().insertProject();
92 ComponentDto view = db.components().insertView();
93 ComponentDto developer = db.components().insertDeveloper("developer-name");
94 db.users().insertProjectPermissionOnUser(user1, UserRole.ADMIN, developer);
95 db.users().insertProjectPermissionOnUser(user2, UserRole.ADMIN, developer);
96 db.users().insertProjectPermissionOnGroup(group1, UserRole.ADMIN, developer);
97 db.users().insertProjectPermissionOnGroup(group2, UserRole.ADMIN, developer);
98 loginAsAdminOnDefaultOrganization();
100 newRequest().setParam(PARAM_TEMPLATE_ID, template1.getUuid()).execute();
102 assertTemplate1AppliedToProject(project);
103 assertTemplate1AppliedToProject(view);
107 public void bulk_apply_template_by_template_name() throws Exception {
108 ComponentDto project = db.components().insertComponent(newProjectDto(db.getDefaultOrganization()));
109 loginAsAdminOnDefaultOrganization();
111 newRequest().setParam(PARAM_TEMPLATE_NAME, template1.getName()).execute();
113 assertTemplate1AppliedToProject(project);
117 public void apply_template_by_qualifier() throws Exception {
118 OrganizationDto organization = db.getDefaultOrganization();
119 ComponentDto project = db.components().insertComponent(newProjectDto(organization));
120 ComponentDto view = db.components().insertComponent(newView(organization));
121 loginAsAdminOnDefaultOrganization();
124 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
125 .setParam(PARAM_QUALIFIER, project.qualifier()).execute();
127 assertTemplate1AppliedToProject(project);
128 assertNoPermissionOnProject(view);
132 public void apply_template_by_query_on_name_and_key() throws Exception {
133 OrganizationDto organization = db.getDefaultOrganization();
134 ComponentDto projectFoundByKey = newProjectDto(organization).setKey("sonar");
135 db.components().insertProjectAndSnapshot(projectFoundByKey);
136 ComponentDto projectFoundByName = newProjectDto(organization).setName("name-sonar-name");
137 db.components().insertProjectAndSnapshot(projectFoundByName);
138 // match must be exact on key
139 ComponentDto projectUntouched = newProjectDto(organization).setKey("new-sonar").setName("project-name");
140 db.components().insertProjectAndSnapshot(projectUntouched);
141 loginAsAdminOnDefaultOrganization();
144 .setParam(PARAM_TEMPLATE_ID, template1.getUuid())
145 .setParam(Param.TEXT_QUERY, "sonar")
148 assertTemplate1AppliedToProject(projectFoundByKey);
149 assertTemplate1AppliedToProject(projectFoundByName);
150 assertNoPermissionOnProject(projectUntouched);
154 public void fail_if_no_template_parameter() throws Exception {
155 loginAsAdminOnDefaultOrganization();
157 expectedException.expect(BadRequestException.class);
158 expectedException.expectMessage("Template name or template id must be provided, not both.");
160 newRequest().execute();
164 public void fail_if_template_name_is_incorrect() throws Exception {
165 loginAsAdminOnDefaultOrganization();
167 expectedException.expect(NotFoundException.class);
168 expectedException.expectMessage("Permission template with id 'unknown-template-uuid' is not found");
170 newRequest().setParam(PARAM_TEMPLATE_ID, "unknown-template-uuid").execute();
173 private void assertTemplate1AppliedToProject(ComponentDto project) throws Exception {
174 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).containsExactly(group1.getName());
175 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).containsExactly(group2.getName());
176 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
177 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).containsExactly(user1.getId());
178 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).containsExactly(user2.getId());
181 private void assertNoPermissionOnProject(ComponentDto project) throws Exception {
182 assertThat(selectProjectPermissionGroups(project, UserRole.ADMIN)).isEmpty();
183 assertThat(selectProjectPermissionGroups(project, UserRole.CODEVIEWER)).isEmpty();
184 assertThat(selectProjectPermissionGroups(project, UserRole.ISSUE_ADMIN)).isEmpty();
185 assertThat(selectProjectPermissionGroups(project, UserRole.USER)).isEmpty();
186 assertThat(selectProjectPermissionUsers(project, UserRole.ADMIN)).isEmpty();
187 assertThat(selectProjectPermissionUsers(project, UserRole.CODEVIEWER)).isEmpty();
188 assertThat(selectProjectPermissionUsers(project, UserRole.ISSUE_ADMIN)).isEmpty();
189 assertThat(selectProjectPermissionUsers(project, UserRole.USER)).isEmpty();
192 private void addUserToTemplate(UserDto user, PermissionTemplateDto permissionTemplate, String permission) {
193 db.getDbClient().permissionTemplateDao().insertUserPermission(db.getSession(), permissionTemplate.getId(), user.getId(), permission);
197 private void addGroupToTemplate(GroupDto group, PermissionTemplateDto permissionTemplate, String permission) {
198 db.getDbClient().permissionTemplateDao().insertGroupPermission(db.getSession(), permissionTemplate.getId(), group.getId(), permission);
202 private List<String> selectProjectPermissionGroups(ComponentDto project, String permission) {
203 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
204 return db.getDbClient().groupPermissionDao().selectGroupNamesByQuery(db.getSession(), db.getDefaultOrganization().getUuid(), query);
207 private List<Long> selectProjectPermissionUsers(ComponentDto project, String permission) {
208 PermissionQuery query = PermissionQuery.builder().setPermission(permission).setComponentUuid(project.uuid()).build();
209 return db.getDbClient().userPermissionDao().selectUserIds(db.getSession(), db.getDefaultOrganization().getUuid(), query);