2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
23 import java.util.List;
24 import java.util.Properties;
25 import javax.annotation.Nullable;
26 import org.junit.Before;
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.PropertyDefinitions;
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.core.permission.GlobalPermissions;
37 import org.sonar.db.DbClient;
38 import org.sonar.db.DbTester;
39 import org.sonar.db.permission.PermissionTemplateDto;
40 import org.sonar.db.permission.PermissionTemplateTesting;
41 import org.sonar.server.component.ComponentFinder;
42 import org.sonar.server.exceptions.BadRequestException;
43 import org.sonar.server.exceptions.ForbiddenException;
44 import org.sonar.server.exceptions.NotFoundException;
45 import org.sonar.server.exceptions.UnauthorizedException;
46 import org.sonar.server.i18n.I18nRule;
47 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
48 import org.sonar.server.platform.PersistentSettings;
49 import org.sonar.server.platform.ServerSettings;
50 import org.sonar.server.tester.UserSessionRule;
51 import org.sonar.server.ws.TestRequest;
52 import org.sonar.server.ws.WsActionTester;
53 import org.sonar.test.DbTests;
55 import static java.util.Arrays.asList;
56 import static java.util.Collections.singletonList;
57 import static org.assertj.core.api.Assertions.assertThat;
58 import static org.mockito.Mockito.mock;
59 import static org.mockito.Mockito.when;
60 import static org.sonar.api.resources.Qualifiers.PROJECT;
61 import static org.sonar.api.resources.Qualifiers.VIEW;
62 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
63 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
64 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_QUALIFIER;
65 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_TEMPLATE_UUID;
66 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_TEMPLATE_NAME;
68 @Category(DbTests.class)
69 public class SetDefaultTemplateActionTest {
71 public DbTester db = DbTester.create(System2.INSTANCE);
73 public UserSessionRule userSession = UserSessionRule.standalone();
75 public ExpectedException expectedException = ExpectedException.none();
76 I18nRule i18n = new I18nRule();
79 PersistentSettings persistentSettings;
80 ResourceTypes resourceTypes = mock(ResourceTypes.class);
82 PermissionTemplateDto template;
86 DbClient dbClient = db.getDbClient();
87 persistentSettings = new PersistentSettings(dbClient.propertiesDao(), new ServerSettings(new PropertyDefinitions(), new Properties()));
88 persistentSettings.saveProperty(DEFAULT_TEMPLATE_PROPERTY, "any-template-uuid");
89 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(PROJECT), "any-template-uuid");
90 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(VIEW), "any-view-template-uuid");
91 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty("DEV"), "any-dev-template-uuid");
92 when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
93 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
95 ws = new WsActionTester(new SetDefaultTemplateAction(
97 new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient)),
102 template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto().setUuid("permission-template-uuid"));
106 public void update_settings_for_project_qualifier() {
107 // default value is project qualifier's value
108 String result = newRequest(template.getUuid(), null);
110 assertThat(result).isEmpty();
111 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
112 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
113 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
114 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
118 public void update_settings_for_project_qualifier_by_template_name() {
120 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
122 db.getSession().commit();
124 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
125 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
126 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
127 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
131 public void update_settings_of_views_property() {
132 newRequest(template.getUuid(), VIEW);
134 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
135 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo("any-template-uuid");
136 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo(template.getUuid());
137 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
141 public void fail_if_anonymous() {
142 expectedException.expect(UnauthorizedException.class);
143 userSession.anonymous();
145 newRequest(template.getUuid(), PROJECT);
149 public void fail_if_not_admin() {
150 expectedException.expect(ForbiddenException.class);
151 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
153 newRequest(template.getUuid(), PROJECT);
157 public void fail_if_template_not_provided() {
158 expectedException.expect(BadRequestException.class);
160 newRequest(null, PROJECT);
164 public void fail_if_template_does_not_exist() {
165 expectedException.expect(NotFoundException.class);
167 newRequest("unknown-template-uuid", PROJECT);
171 public void fail_if_qualifier_is_not_root() {
172 expectedException.expect(BadRequestException.class);
173 when(resourceTypes.getRoots()).thenReturn(singletonList(ResourceType.builder(PROJECT).build()));
175 newRequest(template.getUuid(), VIEW);
178 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) {
179 TestRequest request = ws.newRequest();
180 if (templateUuid != null) {
181 request.setParam(PARAM_TEMPLATE_UUID, templateUuid);
183 if (qualifier != null) {
184 request.setParam(PARAM_QUALIFIER, qualifier);
187 return request.execute().getInput();
190 private static List<ResourceType> rootResourceTypes() {
191 ResourceType project = ResourceType.builder(PROJECT).build();
192 ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
193 ResourceType dev = ResourceType.builder("DEV").build();
195 return asList(project, view, dev);