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.Settings;
29 import org.sonar.api.resources.Qualifiers;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.web.UserRole;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.ResourceTypesRule;
36 import org.sonar.db.permission.template.PermissionTemplateDto;
37 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
38 import org.sonar.db.user.GroupDto;
39 import org.sonar.db.user.UserDto;
40 import org.sonar.server.exceptions.UnauthorizedException;
41 import org.sonar.server.i18n.I18nRule;
42 import org.sonar.server.tester.UserSessionRule;
43 import org.sonar.server.ws.WsActionTester;
45 import static org.assertj.core.api.Assertions.assertThat;
46 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
47 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01;
48 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02;
49 import static org.sonar.core.util.Uuids.UUID_EXAMPLE_03;
50 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
51 import static org.sonar.db.user.GroupTesting.newGroupDto;
52 import static org.sonar.db.user.UserTesting.newUserDto;
53 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
54 import static org.sonar.test.JsonAssert.assertJson;
56 public class SearchTemplatesActionTest {
58 public DbTester db = DbTester.create(System2.INSTANCE);
61 public ExpectedException expectedException = ExpectedException.none();
64 public UserSessionRule userSession = UserSessionRule.standalone();
67 I18nRule i18n = new I18nRule();
68 DbClient dbClient = db.getDbClient();
69 DbSession dbSession = db.getSession();
70 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
71 SearchTemplatesDataLoader dataLoader;
72 SearchTemplatesAction underTest;
76 i18n.setProjectPermissions();
78 Settings settings = new Settings();
79 settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01);
80 settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.VIEW), UUID_EXAMPLE_02);
81 settings.setProperty(defaultRootQualifierTemplateProperty("DEV"), UUID_EXAMPLE_03);
83 DefaultPermissionTemplateFinder defaultPermissionTemplateFinder = new DefaultPermissionTemplateFinder(settings, resourceTypes);
85 dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder);
86 underTest = new SearchTemplatesAction(dbClient, userSession, i18n, dataLoader);
88 ws = new WsActionTester(underTest);
94 public void search_project_permissions() {
95 PermissionTemplateDto projectTemplate = insertProjectTemplate();
96 PermissionTemplateDto viewsTemplate = insertViewsTemplate();
97 PermissionTemplateDto developerTemplate = insertDeveloperTemplate();
99 UserDto user1 = insertUser(newUserDto());
100 UserDto user2 = insertUser(newUserDto());
101 UserDto user3 = insertUser(newUserDto());
103 GroupDto group1 = insertGroup(newGroupDto());
104 GroupDto group2 = insertGroup(newGroupDto());
105 GroupDto group3 = insertGroup(newGroupDto());
107 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.ISSUE_ADMIN);
108 addUserToTemplate(projectTemplate.getId(), user2.getId(), UserRole.ISSUE_ADMIN);
109 addUserToTemplate(projectTemplate.getId(), user3.getId(), UserRole.ISSUE_ADMIN);
110 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.CODEVIEWER);
111 addGroupToTemplate(projectTemplate.getId(), group1.getId(), UserRole.ADMIN);
112 addPermissionTemplateWithProjectCreator(projectTemplate.getId(), UserRole.ADMIN);
114 addUserToTemplate(viewsTemplate.getId(), user1.getId(), UserRole.USER);
115 addUserToTemplate(viewsTemplate.getId(), user2.getId(), UserRole.USER);
116 addGroupToTemplate(viewsTemplate.getId(), group1.getId(), UserRole.ISSUE_ADMIN);
117 addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
118 addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
120 addGroupToTemplate(developerTemplate.getId(), group1.getId(), UserRole.USER);
124 String result = newRequest();
127 .withStrictArrayOrder()
128 .isSimilarTo(getClass().getResource("search_templates-example.json"));
132 public void empty_result() {
133 String result = newRequest();
136 .withStrictArrayOrder()
137 .ignoreFields("permissions")
138 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/empty.json"));
142 public void search_by_name() {
143 insertProjectTemplate();
144 insertViewsTemplate();
145 insertDeveloperTemplate();
148 String result = ws.newRequest()
149 .setParam(TEXT_QUERY, "views")
150 .execute().getInput();
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 ws.newRequest().execute();
166 public void display_all_project_permissions() {
167 String result = newRequest();
170 .withStrictArrayOrder()
171 .ignoreFields("defaultTemplates", "permissionTemplates")
172 .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/display_all_project_permissions.json"));
175 private String newRequest() {
176 return ws.newRequest().execute().getInput();
179 private PermissionTemplateDto insertProjectTemplate() {
180 return insertTemplate(newPermissionTemplateDto()
181 .setUuid(UUID_EXAMPLE_01)
182 .setName("Default template for Projects")
183 .setDescription("Template for new projects")
185 .setCreatedAt(new Date(1_000_000_000_000L))
186 .setUpdatedAt(new Date(1_000_000_000_000L)));
189 private PermissionTemplateDto insertViewsTemplate() {
190 return insertTemplate(newPermissionTemplateDto()
191 .setUuid(UUID_EXAMPLE_02)
192 .setName("Default template for Views")
193 .setDescription("Template for new views")
194 .setKeyPattern(".*sonar.views.*")
195 .setCreatedAt(new Date(1_000_000_000_000L))
196 .setUpdatedAt(new Date(1_100_000_000_000L)));
199 private PermissionTemplateDto insertDeveloperTemplate() {
200 return insertTemplate(newPermissionTemplateDto()
201 .setUuid(UUID_EXAMPLE_03)
202 .setName("Default template for Developers")
203 .setKeyPattern(".*sonar.developer.*")
204 .setDescription(null)
205 .setCreatedAt(new Date(1_100_500_000_000L))
206 .setUpdatedAt(new Date(1_100_900_000_000L)));
209 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
210 return dbClient.permissionTemplateDao().insert(db.getSession(), template);
213 private GroupDto insertGroup(GroupDto groupDto) {
214 return dbClient.groupDao().insert(db.getSession(), groupDto);
217 private UserDto insertUser(UserDto userDto) {
218 return dbClient.userDao().insert(db.getSession(), userDto.setActive(true));
221 private void addGroupToTemplate(long templateId, @Nullable Long groupId, String permission) {
222 dbClient.permissionTemplateDao().insertGroupPermission(db.getSession(), templateId, groupId, permission);
225 private void addUserToTemplate(long templateId, long userId, String permission) {
226 dbClient.permissionTemplateDao().insertUserPermission(db.getSession(), templateId, userId, permission);
229 private void addPermissionTemplateWithProjectCreator(long templateId, String permission) {
230 dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
231 .setWithProjectCreator(true)
232 .setTemplateId(templateId)
233 .setPermission(permission)
234 .setCreatedAt(1_000_000_000L)
235 .setUpdatedAt(2_000_000_000L));