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 .isSimilarTo(getClass().getResource("search_templates-example.json"));
143 public void empty_result() {
144 String result = newRequest();
147 .withStrictArrayOrder()
148 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/empty.json"));
152 public void search_by_name() {
153 insertProjectTemplate();
154 insertViewsTemplate();
155 insertDeveloperTemplate();
158 String result = ws.newRequest()
159 .setParam(TEXT_QUERY, "views")
160 .execute().getInput();
162 assertThat(result).contains("Default template for Views")
163 .doesNotContain("projects")
164 .doesNotContain("developers");
168 public void fail_if_not_logged_in() {
169 expectedException.expect(UnauthorizedException.class);
170 userSession.anonymous();
172 ws.newRequest().execute();
176 public void fail_if_not_global_admin() {
177 expectedException.expect(ForbiddenException.class);
178 userSession.login().setGlobalPermissions(QUALITY_PROFILE_ADMIN);
180 ws.newRequest().execute();
183 private String newRequest() {
184 return ws.newRequest().execute().getInput();
187 private PermissionTemplateDto insertProjectTemplate() {
188 return insertTemplate(newPermissionTemplateDto()
189 .setUuid(UUID_EXAMPLE_01)
190 .setName("Default template for Projects")
191 .setDescription("Template for new projects")
193 .setCreatedAt(new Date(1_000_000_000_000L))
194 .setUpdatedAt(new Date(1_000_000_000_000L)));
197 private PermissionTemplateDto insertViewsTemplate() {
198 return insertTemplate(newPermissionTemplateDto()
199 .setUuid(UUID_EXAMPLE_02)
200 .setName("Default template for Views")
201 .setDescription("Template for new views")
202 .setKeyPattern(".*sonar.views.*")
203 .setCreatedAt(new Date(1_000_000_000_000L))
204 .setUpdatedAt(new Date(1_100_000_000_000L)));
207 private PermissionTemplateDto insertDeveloperTemplate() {
208 return insertTemplate(newPermissionTemplateDto()
209 .setUuid(UUID_EXAMPLE_03)
210 .setName("Default template for Developers")
211 .setKeyPattern(".*sonar.developer.*")
212 .setDescription(null)
213 .setCreatedAt(new Date(1_100_500_000_000L))
214 .setUpdatedAt(new Date(1_100_900_000_000L)));
217 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
218 return dbClient.permissionTemplateDao().insert(dbSession, template);
221 private GroupDto insertGroup(GroupDto groupDto) {
222 return dbClient.groupDao().insert(dbSession, groupDto);
225 private UserDto insertUser(UserDto userDto) {
226 return dbClient.userDao().insert(dbSession, userDto.setActive(true));
229 private void addGroupToTemplate(long templateId, @Nullable Long groupId, String permission) {
230 dbClient.permissionTemplateDao().insertGroupPermission(dbSession, templateId, groupId, permission);
233 private void addUserToTemplate(long templateId, long userId, String permission) {
234 dbClient.permissionTemplateDao().insertUserPermission(dbSession, templateId, userId, permission);
237 private void commit() {
241 private static List<ResourceType> rootResourceTypes() {
242 ResourceType project = ResourceType.builder(Qualifiers.PROJECT).build();
243 ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
244 ResourceType dev = ResourceType.builder("DEV").build();
246 return asList(project, view, dev);