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.List;
23 import java.util.Properties;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.config.PropertyDefinitions;
30 import org.sonar.api.resources.Qualifiers;
31 import org.sonar.api.resources.ResourceType;
32 import org.sonar.api.resources.ResourceTypes;
33 import org.sonar.api.utils.System2;
34 import org.sonar.core.permission.GlobalPermissions;
35 import org.sonar.db.DbClient;
36 import org.sonar.db.DbTester;
37 import org.sonar.db.permission.PermissionTemplateDto;
38 import org.sonar.db.permission.PermissionTemplateTesting;
39 import org.sonar.server.component.ComponentFinder;
40 import org.sonar.server.exceptions.BadRequestException;
41 import org.sonar.server.exceptions.ForbiddenException;
42 import org.sonar.server.exceptions.NotFoundException;
43 import org.sonar.server.exceptions.UnauthorizedException;
44 import org.sonar.server.i18n.I18nRule;
45 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
46 import org.sonar.server.platform.PersistentSettings;
47 import org.sonar.server.platform.WebServerSettings;
48 import org.sonar.server.tester.UserSessionRule;
49 import org.sonar.server.usergroups.ws.UserGroupFinder;
50 import org.sonar.server.ws.TestRequest;
51 import org.sonar.server.ws.WsActionTester;
53 import static java.util.Arrays.asList;
54 import static java.util.Collections.singletonList;
55 import static org.assertj.core.api.Assertions.assertThat;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.when;
58 import static org.sonar.api.resources.Qualifiers.PROJECT;
59 import static org.sonar.api.resources.Qualifiers.VIEW;
60 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
61 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
62 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
63 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
64 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
67 public class SetDefaultTemplateActionTest {
69 public DbTester db = DbTester.create(System2.INSTANCE);
71 public UserSessionRule userSession = UserSessionRule.standalone();
73 public ExpectedException expectedException = ExpectedException.none();
74 I18nRule i18n = new I18nRule();
77 PersistentSettings persistentSettings;
78 ResourceTypes resourceTypes = mock(ResourceTypes.class);
80 PermissionTemplateDto template;
84 DbClient dbClient = db.getDbClient();
85 persistentSettings = new PersistentSettings(dbClient, new WebServerSettings(new PropertyDefinitions(), new Properties()));
86 persistentSettings.saveProperty(DEFAULT_TEMPLATE_PROPERTY, "any-template-uuid");
87 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(PROJECT), "any-template-uuid");
88 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(VIEW), "any-view-template-uuid");
89 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty("DEV"), "any-dev-template-uuid");
90 when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
91 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
93 ws = new WsActionTester(new SetDefaultTemplateAction(
95 new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes),
100 template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto().setUuid("permission-template-uuid"));
104 public void update_settings_for_project_qualifier() {
105 // default value is project qualifier's value
106 String result = newRequest(template.getUuid(), null);
108 assertThat(result).isEmpty();
109 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
110 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
111 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
112 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
116 public void update_settings_for_project_qualifier_by_template_name() {
118 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
120 db.getSession().commit();
122 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
123 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
124 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
125 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
129 public void update_settings_of_views_property() {
130 newRequest(template.getUuid(), VIEW);
132 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
133 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo("any-template-uuid");
134 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo(template.getUuid());
135 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
139 public void fail_if_anonymous() {
140 expectedException.expect(UnauthorizedException.class);
141 userSession.anonymous();
143 newRequest(template.getUuid(), PROJECT);
147 public void fail_if_not_admin() {
148 expectedException.expect(ForbiddenException.class);
149 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
151 newRequest(template.getUuid(), PROJECT);
155 public void fail_if_template_not_provided() {
156 expectedException.expect(BadRequestException.class);
158 newRequest(null, PROJECT);
162 public void fail_if_template_does_not_exist() {
163 expectedException.expect(NotFoundException.class);
165 newRequest("unknown-template-uuid", PROJECT);
169 public void fail_if_qualifier_is_not_root() {
170 expectedException.expect(BadRequestException.class);
171 when(resourceTypes.getRoots()).thenReturn(singletonList(ResourceType.builder(PROJECT).build()));
173 newRequest(template.getUuid(), VIEW);
176 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) {
177 TestRequest request = ws.newRequest();
178 if (templateUuid != null) {
179 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
181 if (qualifier != null) {
182 request.setParam(PARAM_QUALIFIER, qualifier);
185 return request.execute().getInput();
188 private static List<ResourceType> rootResourceTypes() {
189 ResourceType project = ResourceType.builder(PROJECT).build();
190 ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
191 ResourceType dev = ResourceType.builder("DEV").build();
193 return asList(project, view, dev);