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.Date;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.config.MapSettings;
29 import org.sonar.api.config.Settings;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.utils.System2;
32 import org.sonar.api.web.UserRole;
33 import org.sonar.db.DbClient;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.component.ResourceTypesRule;
37 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
38 import org.sonar.db.permission.template.PermissionTemplateDto;
39 import org.sonar.db.user.GroupDto;
40 import org.sonar.db.user.UserDto;
41 import org.sonar.server.exceptions.UnauthorizedException;
42 import org.sonar.server.i18n.I18nRule;
43 import org.sonar.server.permission.ws.BasePermissionWsTest;
44 import org.sonar.server.permission.ws.PermissionsWsAction;
45 import org.sonar.server.tester.UserSessionRule;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
49 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
50 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
51 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_03;
52 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
53 import static org.sonar.db.user.GroupTesting.newGroupDto;
54 import static org.sonar.db.user.UserTesting.newUserDto;
55 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
56 import static org.sonar.test.JsonAssert.assertJson;
58 public class SearchTemplatesActionTest extends BasePermissionWsTest {
60 public DbTester db = DbTester.create(System2.INSTANCE);
63 public ExpectedException expectedException = ExpectedException.none();
66 public UserSessionRule userSession = UserSessionRule.standalone();
68 private I18nRule i18n = new I18nRule();
69 private DbClient dbClient = db.getDbClient();
70 private DbSession dbSession = db.getSession();
71 private ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
74 protected PermissionsWsAction buildWsAction() {
76 Settings settings = new MapSettings();
77 settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01);
78 settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.VIEW), UUID_EXAMPLE_02);
79 settings.setProperty(defaultRootQualifierTemplateProperty("DEV"), UUID_EXAMPLE_03);
81 DefaultPermissionTemplateFinder defaultPermissionTemplateFinder = new DefaultPermissionTemplateFinder(settings, resourceTypes);
82 SearchTemplatesDataLoader dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder);
83 return new SearchTemplatesAction(userSession, i18n, dataLoader);
88 i18n.setProjectPermissions();
93 public void search_project_permissions() {
94 PermissionTemplateDto projectTemplate = insertProjectTemplate();
95 PermissionTemplateDto viewsTemplate = insertViewsTemplate();
96 PermissionTemplateDto developerTemplate = insertDeveloperTemplate();
98 UserDto user1 = insertUser(newUserDto());
99 UserDto user2 = insertUser(newUserDto());
100 UserDto user3 = insertUser(newUserDto());
102 GroupDto group1 = insertGroup(newGroupDto());
103 GroupDto group2 = insertGroup(newGroupDto());
104 GroupDto group3 = insertGroup(newGroupDto());
106 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.ISSUE_ADMIN);
107 addUserToTemplate(projectTemplate.getId(), user2.getId(), UserRole.ISSUE_ADMIN);
108 addUserToTemplate(projectTemplate.getId(), user3.getId(), UserRole.ISSUE_ADMIN);
109 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.CODEVIEWER);
110 addGroupToTemplate(projectTemplate.getId(), group1.getId(), UserRole.ADMIN);
111 addPermissionTemplateWithProjectCreator(projectTemplate.getId(), UserRole.ADMIN);
113 addUserToTemplate(viewsTemplate.getId(), user1.getId(), UserRole.USER);
114 addUserToTemplate(viewsTemplate.getId(), user2.getId(), UserRole.USER);
115 addGroupToTemplate(viewsTemplate.getId(), group1.getId(), UserRole.ISSUE_ADMIN);
116 addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
117 addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
119 addGroupToTemplate(developerTemplate.getId(), group1.getId(), UserRole.USER);
123 String result = newRequest().execute().getInput();
126 .withStrictArrayOrder()
127 .isSimilarTo(getClass().getResource("search_templates-example.json"));
131 public void empty_result() {
132 String result = newRequest().execute().getInput();
135 .withStrictArrayOrder()
136 .ignoreFields("permissions")
137 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/empty.json"));
141 public void search_by_name() {
142 insertProjectTemplate();
143 insertViewsTemplate();
144 insertDeveloperTemplate();
147 String result = newRequest()
148 .setParam(TEXT_QUERY, "views")
152 assertThat(result).contains("Default template for Views")
153 .doesNotContain("projects")
154 .doesNotContain("developers");
158 public void fail_if_not_logged_in() {
159 expectedException.expect(UnauthorizedException.class);
160 userSession.anonymous();
162 newRequest().execute();
166 public void display_all_project_permissions() {
167 String result = newRequest().execute().getInput();
170 .withStrictArrayOrder()
171 .ignoreFields("defaultTemplates", "permissionTemplates")
172 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/display_all_project_permissions.json"));
175 private PermissionTemplateDto insertProjectTemplate() {
176 return insertTemplate(newPermissionTemplateDto()
177 .setOrganizationUuid(db.getDefaultOrganization().getUuid())
178 .setUuid(UUID_EXAMPLE_01)
179 .setName("Default template for Projects")
180 .setDescription("Template for new projects")
182 .setCreatedAt(new Date(1_000_000_000_000L))
183 .setUpdatedAt(new Date(1_000_000_000_000L)));
186 private PermissionTemplateDto insertViewsTemplate() {
187 return insertTemplate(newPermissionTemplateDto()
188 .setOrganizationUuid(db.getDefaultOrganization().getUuid())
189 .setUuid(UUID_EXAMPLE_02)
190 .setName("Default template for Views")
191 .setDescription("Template for new views")
192 .setKeyPattern(".*sonar.views.*")
193 .setCreatedAt(new Date(1_000_000_000_000L))
194 .setUpdatedAt(new Date(1_100_000_000_000L)));
197 private PermissionTemplateDto insertDeveloperTemplate() {
198 return insertTemplate(newPermissionTemplateDto()
199 .setOrganizationUuid(db.getDefaultOrganization().getUuid())
200 .setUuid(UUID_EXAMPLE_03)
201 .setName("Default template for Developers")
202 .setKeyPattern(".*sonar.developer.*")
203 .setDescription(null)
204 .setCreatedAt(new Date(1_100_500_000_000L))
205 .setUpdatedAt(new Date(1_100_900_000_000L)));
208 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
209 return dbClient.permissionTemplateDao().insert(db.getSession(), template);
212 private GroupDto insertGroup(GroupDto groupDto) {
213 return dbClient.groupDao().insert(db.getSession(), groupDto);
216 private UserDto insertUser(UserDto userDto) {
217 return dbClient.userDao().insert(db.getSession(), userDto.setActive(true));
220 private void addGroupToTemplate(long templateId, @Nullable Long groupId, String permission) {
221 dbClient.permissionTemplateDao().insertGroupPermission(db.getSession(), templateId, groupId, permission);
224 private void addUserToTemplate(long templateId, long userId, String permission) {
225 dbClient.permissionTemplateDao().insertUserPermission(db.getSession(), templateId, userId, permission);
228 private void addPermissionTemplateWithProjectCreator(long templateId, String permission) {
229 dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
230 .setWithProjectCreator(true)
231 .setTemplateId(templateId)
232 .setPermission(permission)
233 .setCreatedAt(1_000_000_000L)
234 .setUpdatedAt(2_000_000_000L));