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 java.util.List;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.ClassRule;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.experimental.categories.Category;
30 import org.junit.rules.ExpectedException;
31 import org.sonar.api.config.Settings;
32 import org.sonar.api.resources.Qualifiers;
33 import org.sonar.api.resources.ResourceType;
34 import org.sonar.api.resources.ResourceTypes;
35 import org.sonar.api.utils.System2;
36 import org.sonar.api.web.UserRole;
37 import org.sonar.core.permission.GlobalPermissions;
38 import org.sonar.db.DbClient;
39 import org.sonar.db.DbSession;
40 import org.sonar.db.DbTester;
41 import org.sonar.db.permission.PermissionTemplateDto;
42 import org.sonar.db.user.GroupDto;
43 import org.sonar.db.user.UserDto;
44 import org.sonar.server.exceptions.ForbiddenException;
45 import org.sonar.server.exceptions.UnauthorizedException;
46 import org.sonar.server.i18n.I18nRule;
47 import org.sonar.server.tester.UserSessionRule;
48 import org.sonar.server.ws.WsActionTester;
49 import org.sonar.test.DbTests;
51 import static java.util.Arrays.asList;
52 import static org.assertj.core.api.Assertions.assertThat;
53 import static org.mockito.Mockito.mock;
54 import static org.mockito.Mockito.when;
55 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
56 import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
57 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
58 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
59 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_03;
60 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
61 import static org.sonar.db.user.GroupTesting.newGroupDto;
62 import static org.sonar.db.user.UserTesting.newUserDto;
63 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
64 import static org.sonar.test.JsonAssert.assertJson;
66 @Category(DbTests.class)
67 public class SearchTemplatesActionTest {
69 public static DbTester db = DbTester.create(System2.INSTANCE);
71 public ExpectedException expectedException = ExpectedException.none();
73 public UserSessionRule userSession = UserSessionRule.standalone();
76 I18nRule i18n = new I18nRule();
77 DbClient dbClient = db.getDbClient();
78 DbSession dbSession = db.getSession();
79 ResourceTypes resourceTypes = mock(ResourceTypes.class);
80 SearchTemplatesDataLoader dataLoader;
82 SearchTemplatesAction underTest;
87 i18n.setProjectPermissions();
88 when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
90 Settings settings = new Settings();
91 settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01);
92 settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.VIEW), UUID_EXAMPLE_02);
93 settings.setProperty(defaultRootQualifierTemplateProperty("DEV"), UUID_EXAMPLE_03);
95 DefaultPermissionTemplateFinder defaultPermissionTemplateFinder = new DefaultPermissionTemplateFinder(settings, resourceTypes);
97 dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder);
98 underTest = new SearchTemplatesAction(dbClient, userSession, i18n, dataLoader);
100 ws = new WsActionTester(underTest);
102 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
106 public void search_project_permissions() {
107 PermissionTemplateDto projectTemplate = insertProjectTemplate();
108 PermissionTemplateDto viewsTemplate = insertViewsTemplate();
109 PermissionTemplateDto developerTemplate = insertDeveloperTemplate();
111 UserDto user1 = insertUser(newUserDto());
112 UserDto user2 = insertUser(newUserDto());
113 UserDto user3 = insertUser(newUserDto());
115 GroupDto group1 = insertGroup(newGroupDto());
116 GroupDto group2 = insertGroup(newGroupDto());
117 GroupDto group3 = insertGroup(newGroupDto());
119 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.ISSUE_ADMIN);
120 addUserToTemplate(projectTemplate.getId(), user2.getId(), UserRole.ISSUE_ADMIN);
121 addUserToTemplate(projectTemplate.getId(), user3.getId(), UserRole.ISSUE_ADMIN);
122 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.CODEVIEWER);
123 addGroupToTemplate(projectTemplate.getId(), group1.getId(), UserRole.ADMIN);
125 addUserToTemplate(viewsTemplate.getId(), user1.getId(), UserRole.USER);
126 addUserToTemplate(viewsTemplate.getId(), user2.getId(), UserRole.USER);
127 addGroupToTemplate(viewsTemplate.getId(), group1.getId(), UserRole.ISSUE_ADMIN);
128 addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
129 addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
131 addGroupToTemplate(developerTemplate.getId(), group1.getId(), UserRole.USER);
135 String result = newRequest();
138 .withStrictArrayOrder()
139 .ignoreFields("permissions")
140 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/search_templates-example.json"));
144 public void empty_result() {
145 String result = newRequest();
148 .withStrictArrayOrder()
149 .ignoreFields("permissions")
150 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/empty.json"));
154 public void search_by_name() {
155 insertProjectTemplate();
156 insertViewsTemplate();
157 insertDeveloperTemplate();
160 String result = ws.newRequest()
161 .setParam(TEXT_QUERY, "views")
162 .execute().getInput();
164 assertThat(result).contains("Default template for Views")
165 .doesNotContain("projects")
166 .doesNotContain("developers");
170 public void fail_if_not_logged_in() {
171 expectedException.expect(UnauthorizedException.class);
172 userSession.anonymous();
174 ws.newRequest().execute();
178 public void fail_if_not_global_admin() {
179 expectedException.expect(ForbiddenException.class);
180 userSession.login().setGlobalPermissions(QUALITY_PROFILE_ADMIN);
182 ws.newRequest().execute();
186 public void display_all_project_permissions() {
187 String result = newRequest();
190 .withStrictArrayOrder()
191 .ignoreFields("defaultTemplates", "permissionTemplates")
192 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/display_all_project_permissions.json"));
195 private String newRequest() {
196 return ws.newRequest().execute().getInput();
199 private PermissionTemplateDto insertProjectTemplate() {
200 return insertTemplate(newPermissionTemplateDto()
201 .setUuid(UUID_EXAMPLE_01)
202 .setName("Default template for Projects")
203 .setDescription("Template for new projects")
205 .setCreatedAt(new Date(1_000_000_000_000L))
206 .setUpdatedAt(new Date(1_000_000_000_000L)));
209 private PermissionTemplateDto insertViewsTemplate() {
210 return insertTemplate(newPermissionTemplateDto()
211 .setUuid(UUID_EXAMPLE_02)
212 .setName("Default template for Views")
213 .setDescription("Template for new views")
214 .setKeyPattern(".*sonar.views.*")
215 .setCreatedAt(new Date(1_000_000_000_000L))
216 .setUpdatedAt(new Date(1_100_000_000_000L)));
219 private PermissionTemplateDto insertDeveloperTemplate() {
220 return insertTemplate(newPermissionTemplateDto()
221 .setUuid(UUID_EXAMPLE_03)
222 .setName("Default template for Developers")
223 .setKeyPattern(".*sonar.developer.*")
224 .setDescription(null)
225 .setCreatedAt(new Date(1_100_500_000_000L))
226 .setUpdatedAt(new Date(1_100_900_000_000L)));
229 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
230 return dbClient.permissionTemplateDao().insert(dbSession, template);
233 private GroupDto insertGroup(GroupDto groupDto) {
234 return dbClient.groupDao().insert(dbSession, groupDto);
237 private UserDto insertUser(UserDto userDto) {
238 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
241 private void addGroupToTemplate(long templateId, @Nullable Long groupId, String permission) {
242 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, templateId, groupId, permission);
245 private void addUserToTemplate(long templateId, long userId, String permission) {
246 dbClient.permissionTemplateDao().insertUserPermission(dbSession, templateId, userId, permission);
249 private void commit() {
253 private static List<ResourceType> rootResourceTypes() {
254 ResourceType project = ResourceType.builder(Qualifiers.PROJECT).build();
255 ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
256 ResourceType dev = ResourceType.builder("DEV").build();
258 return asList(project, view, dev);