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.Test;
26 import org.sonar.api.resources.Qualifiers;
27 import org.sonar.api.web.UserRole;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.component.ResourceTypesRule;
31 import org.sonar.db.organization.OrganizationDto;
32 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
33 import org.sonar.db.permission.template.PermissionTemplateDto;
34 import org.sonar.db.user.GroupDto;
35 import org.sonar.db.user.UserDto;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.i18n.I18nRule;
38 import org.sonar.server.permission.ws.BasePermissionWsTest;
39 import org.sonar.server.ws.TestRequest;
40 import org.sonar.server.ws.WsActionTester;
41 import org.sonarqube.ws.MediaTypes;
42 import org.sonarqube.ws.WsPermissions;
44 import static org.assertj.core.api.Assertions.assertThat;
45 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
46 import static org.sonar.core.permission.GlobalPermissions.SYSTEM_ADMIN;
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.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
50 import static org.sonar.test.JsonAssert.assertJson;
52 public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTemplatesAction> {
54 private I18nRule i18n = new I18nRule();
55 private DbClient dbClient = db.getDbClient();
56 private DbSession dbSession = db.getSession();
57 private ResourceTypesRule resourceTypesWithViews = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW);
58 private ResourceTypesRule resourceTypesWithoutViews = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT);
60 private WsActionTester underTestWithoutViews;
63 protected SearchTemplatesAction buildWsAction() {
64 DefaultTemplatesResolver defaultTemplatesResolverWithViews = new DefaultTemplatesResolverImpl(resourceTypesWithViews);
65 SearchTemplatesDataLoader dataLoaderWithViews = new SearchTemplatesDataLoader(dbClient, defaultTemplatesResolverWithViews);
66 SearchTemplatesAction searchTemplatesAction = new SearchTemplatesAction(dbClient, userSession, i18n, newPermissionWsSupport(), dataLoaderWithViews);
67 return searchTemplatesAction;
72 DefaultTemplatesResolver defaultTemplatesResolverWithViews = new DefaultTemplatesResolverImpl(resourceTypesWithoutViews);
73 SearchTemplatesDataLoader dataLoaderWithViews = new SearchTemplatesDataLoader(dbClient, defaultTemplatesResolverWithViews);
74 underTestWithoutViews = new WsActionTester(new SearchTemplatesAction(dbClient, userSession, i18n, newPermissionWsSupport(), dataLoaderWithViews));
75 i18n.setProjectPermissions();
76 userSession.logIn().addOrganizationPermission(db.getDefaultOrganization().getUuid(), SYSTEM_ADMIN);
80 public void search_project_permissions() {
81 OrganizationDto organization = db.getDefaultOrganization();
82 PermissionTemplateDto projectTemplate = insertProjectTemplate(organization);
83 PermissionTemplateDto viewsTemplate = insertViewsTemplate(organization);
85 UserDto user1 = db.users().insertUser();
86 UserDto user2 = db.users().insertUser();
87 UserDto user3 = db.users().insertUser();
89 GroupDto group1 = db.users().insertGroup(organization);
90 GroupDto group2 = db.users().insertGroup(organization);
91 GroupDto group3 = db.users().insertGroup(organization);
93 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.ISSUE_ADMIN);
94 addUserToTemplate(projectTemplate.getId(), user2.getId(), UserRole.ISSUE_ADMIN);
95 addUserToTemplate(projectTemplate.getId(), user3.getId(), UserRole.ISSUE_ADMIN);
96 addUserToTemplate(projectTemplate.getId(), user1.getId(), UserRole.CODEVIEWER);
97 addGroupToTemplate(projectTemplate.getId(), group1.getId(), UserRole.ADMIN);
98 addPermissionTemplateWithProjectCreator(projectTemplate.getId(), UserRole.ADMIN);
100 addUserToTemplate(viewsTemplate.getId(), user1.getId(), UserRole.USER);
101 addUserToTemplate(viewsTemplate.getId(), user2.getId(), UserRole.USER);
102 addGroupToTemplate(viewsTemplate.getId(), group1.getId(), UserRole.ISSUE_ADMIN);
103 addGroupToTemplate(viewsTemplate.getId(), group2.getId(), UserRole.ISSUE_ADMIN);
104 addGroupToTemplate(viewsTemplate.getId(), group3.getId(), UserRole.ISSUE_ADMIN);
106 db.organizations().setDefaultTemplates(organization, projectTemplate.getUuid(), viewsTemplate.getUuid());
108 String result = newRequest().execute().getInput();
111 .withStrictArrayOrder()
112 .isSimilarTo(getClass().getResource("search_templates-example.json"));
116 public void empty_result_with_views() {
117 db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "AU-Tpxb--iU5OvuD2FLy", "AU-TpxcA-iU5OvuD2FLz");
118 String result = newRequest(wsTester).execute().getInput();
121 .withStrictArrayOrder()
122 .ignoreFields("permissions")
124 " \"permissionTemplates\": []," +
125 " \"defaultTemplates\": [" +
127 " \"templateId\": \"AU-Tpxb--iU5OvuD2FLy\"," +
128 " \"qualifier\": \"TRK\"" +
131 " \"templateId\": \"AU-TpxcA-iU5OvuD2FLz\"," +
132 " \"qualifier\": \"VW\"" +
139 public void empty_result_without_views() {
140 db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "AU-Tpxb--iU5OvuD2FLy", "AU-TpxcA-iU5OvuD2FLz");
141 String result = newRequest(underTestWithoutViews).execute().getInput();
144 .withStrictArrayOrder()
145 .ignoreFields("permissions")
147 " \"permissionTemplates\": []," +
148 " \"defaultTemplates\": [" +
150 " \"templateId\": \"AU-Tpxb--iU5OvuD2FLy\"," +
151 " \"qualifier\": \"TRK\"" +
158 public void search_by_name_in_default_organization() {
159 db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", null);
160 insertProjectTemplate(db.getDefaultOrganization());
161 insertViewsTemplate(db.getDefaultOrganization());
163 String result = newRequest(wsTester)
164 .setParam(TEXT_QUERY, "views")
168 assertThat(result).contains("Default template for Views")
169 .doesNotContain("projects")
170 .doesNotContain("developers");
174 public void search_in_organization() throws Exception {
175 OrganizationDto org = db.organizations().insert();
176 db.organizations().setDefaultTemplates(org, "foo", null);
177 PermissionTemplateDto templateInOrg = insertProjectTemplate(org);
178 insertProjectTemplate(db.getDefaultOrganization());
180 userSession.addOrganizationPermission(org.getUuid(), SYSTEM_ADMIN);
182 WsPermissions.SearchTemplatesWsResponse result = WsPermissions.SearchTemplatesWsResponse.parseFrom(
183 newRequest(underTestWithoutViews)
184 .setParam("organization", org.getKey())
185 .setMediaType(MediaTypes.PROTOBUF)
189 assertThat(result.getPermissionTemplatesCount()).isEqualTo(1);
190 assertThat(result.getPermissionTemplates(0).getId()).isEqualTo(templateInOrg.getUuid());
194 public void fail_if_not_logged_in() {
195 expectedException.expect(UnauthorizedException.class);
196 userSession.anonymous();
198 newRequest().execute();
202 public void display_all_project_permissions() {
203 db.organizations().setDefaultTemplates(db.getDefaultOrganization(), "foo", "bar");
205 String result = newRequest().execute().getInput();
208 .withStrictArrayOrder()
209 .ignoreFields("defaultTemplates", "permissionTemplates")
212 " \"permissions\": [" +
214 " \"key\": \"admin\"," +
215 " \"name\": \"Administer\"," +
216 " \"description\": \"Ability to access project settings and perform administration tasks. (Users will also need \\\"Browse\\\" permission)\"" +
219 " \"key\": \"codeviewer\"," +
220 " \"name\": \"See Source Code\"," +
221 " \"description\": \"Ability to view the project\\u0027s source code. (Users will also need \\\"Browse\\\" permission)\"" +
224 " \"key\": \"issueadmin\"," +
225 " \"name\": \"Administer Issues\"," +
226 " \"description\": \"Grants the permission to perform advanced editing on issues: marking an issue False Positive / Won\\u0027t Fix or changing an Issue\\u0027s severity. (Users will also need \\\"Browse\\\" permission)\""
230 " \"key\": \"scan\"," +
231 " \"name\": \"Execute Analysis\"," +
232 " \"description\": \"Ability to execute analyses, and to get all settings required to perform the analysis, even the secured ones like the scm account password, the jira account password, and so on.\""
236 " \"key\": \"user\"," +
237 " \"name\": \"Browse\"," +
238 " \"description\": \"Ability to access a project, browse its measures, and create/edit issues for it.\"" +
244 private PermissionTemplateDto insertProjectTemplate(OrganizationDto org) {
245 return insertTemplate(newPermissionTemplateDto()
246 .setOrganizationUuid(org.getUuid())
247 .setUuid(UUID_EXAMPLE_01)
248 .setName("Default template for Projects")
249 .setDescription("Template for new projects")
251 .setCreatedAt(new Date(1_000_000_000_000L))
252 .setUpdatedAt(new Date(1_000_000_000_000L)));
255 private PermissionTemplateDto insertViewsTemplate(OrganizationDto organization) {
256 return insertTemplate(newPermissionTemplateDto()
257 .setOrganizationUuid(organization.getUuid())
258 .setUuid(UUID_EXAMPLE_02)
259 .setName("Default template for Views")
260 .setDescription("Template for new views")
261 .setKeyPattern(".*sonar.views.*")
262 .setCreatedAt(new Date(1_000_000_000_000L))
263 .setUpdatedAt(new Date(1_100_000_000_000L)));
266 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
267 PermissionTemplateDto insert = dbClient.permissionTemplateDao().insert(db.getSession(), template);
268 db.getSession().commit();
272 private void addGroupToTemplate(long templateId, @Nullable Long groupId, String permission) {
273 dbClient.permissionTemplateDao().insertGroupPermission(db.getSession(), templateId, groupId, permission);
274 db.getSession().commit();
277 private void addUserToTemplate(long templateId, long userId, String permission) {
278 dbClient.permissionTemplateDao().insertUserPermission(db.getSession(), templateId, userId, permission);
279 db.getSession().commit();
282 private void addPermissionTemplateWithProjectCreator(long templateId, String permission) {
283 dbClient.permissionTemplateCharacteristicDao().insert(dbSession, new PermissionTemplateCharacteristicDto()
284 .setWithProjectCreator(true)
285 .setTemplateId(templateId)
286 .setPermission(permission)
287 .setCreatedAt(1_000_000_000L)
288 .setUpdatedAt(2_000_000_000L));
292 private TestRequest newRequest(WsActionTester underTest) {
293 return underTest.newRequest().setMethod("POST");