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.usergroups.ws.UserGroupFinder;
52 import org.sonar.server.ws.TestRequest;
53 import org.sonar.server.ws.WsActionTester;
54 import org.sonar.test.DbTests;
56 import static java.util.Arrays.asList;
57 import static java.util.Collections.singletonList;
58 import static org.assertj.core.api.Assertions.assertThat;
59 import static org.mockito.Mockito.mock;
60 import static org.mockito.Mockito.when;
61 import static org.sonar.api.resources.Qualifiers.PROJECT;
62 import static org.sonar.api.resources.Qualifiers.VIEW;
63 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
64 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
65 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_QUALIFIER;
66 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_UUID;
67 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
69 @Category(DbTests.class)
70 public class SetDefaultTemplateActionTest {
72 public DbTester db = DbTester.create(System2.INSTANCE);
74 public UserSessionRule userSession = UserSessionRule.standalone();
76 public ExpectedException expectedException = ExpectedException.none();
77 I18nRule i18n = new I18nRule();
80 PersistentSettings persistentSettings;
81 ResourceTypes resourceTypes = mock(ResourceTypes.class);
83 PermissionTemplateDto template;
87 DbClient dbClient = db.getDbClient();
88 persistentSettings = new PersistentSettings(dbClient.propertiesDao(), new ServerSettings(new PropertyDefinitions(), new Properties()));
89 persistentSettings.saveProperty(DEFAULT_TEMPLATE_PROPERTY, "any-template-uuid");
90 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(PROJECT), "any-template-uuid");
91 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(VIEW), "any-view-template-uuid");
92 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty("DEV"), "any-dev-template-uuid");
93 when(resourceTypes.getRoots()).thenReturn(rootResourceTypes());
94 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
96 ws = new WsActionTester(new SetDefaultTemplateAction(
98 new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes),
103 template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto().setUuid("permission-template-uuid"));
107 public void update_settings_for_project_qualifier() {
108 // default value is project qualifier's value
109 String result = newRequest(template.getUuid(), null);
111 assertThat(result).isEmpty();
112 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
113 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
114 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
115 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
119 public void update_settings_for_project_qualifier_by_template_name() {
121 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
123 db.getSession().commit();
125 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
126 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
127 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
128 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
132 public void update_settings_of_views_property() {
133 newRequest(template.getUuid(), VIEW);
135 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
136 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo("any-template-uuid");
137 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo(template.getUuid());
138 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
142 public void fail_if_anonymous() {
143 expectedException.expect(UnauthorizedException.class);
144 userSession.anonymous();
146 newRequest(template.getUuid(), PROJECT);
150 public void fail_if_not_admin() {
151 expectedException.expect(ForbiddenException.class);
152 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
154 newRequest(template.getUuid(), PROJECT);
158 public void fail_if_template_not_provided() {
159 expectedException.expect(BadRequestException.class);
161 newRequest(null, PROJECT);
165 public void fail_if_template_does_not_exist() {
166 expectedException.expect(NotFoundException.class);
168 newRequest("unknown-template-uuid", PROJECT);
172 public void fail_if_qualifier_is_not_root() {
173 expectedException.expect(BadRequestException.class);
174 when(resourceTypes.getRoots()).thenReturn(singletonList(ResourceType.builder(PROJECT).build()));
176 newRequest(template.getUuid(), VIEW);
179 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) {
180 TestRequest request = ws.newRequest();
181 if (templateUuid != null) {
182 request.setParam(PARAM_TEMPLATE_UUID, templateUuid);
184 if (qualifier != null) {
185 request.setParam(PARAM_QUALIFIER, qualifier);
188 return request.execute().getInput();
191 private static List<ResourceType> rootResourceTypes() {
192 ResourceType project = ResourceType.builder(PROJECT).build();
193 ResourceType view = ResourceType.builder(Qualifiers.VIEW).build();
194 ResourceType dev = ResourceType.builder("DEV").build();
196 return asList(project, view, dev);