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 javax.annotation.Nullable;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.sonar.api.config.MapSettings;
26 import org.sonar.api.resources.Qualifiers;
27 import org.sonar.core.permission.GlobalPermissions;
28 import org.sonar.db.DbClient;
29 import org.sonar.db.permission.template.PermissionTemplateDto;
30 import org.sonar.db.permission.template.PermissionTemplateTesting;
31 import org.sonar.server.exceptions.BadRequestException;
32 import org.sonar.server.exceptions.ForbiddenException;
33 import org.sonar.server.exceptions.NotFoundException;
34 import org.sonar.server.exceptions.UnauthorizedException;
35 import org.sonar.server.i18n.I18nRule;
36 import org.sonar.server.permission.ws.BasePermissionWsTest;
37 import org.sonar.server.platform.PersistentSettings;
38 import org.sonar.server.platform.SettingsChangeNotifier;
39 import org.sonar.server.ws.WsTester;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.sonar.api.resources.Qualifiers.PROJECT;
43 import static org.sonar.api.resources.Qualifiers.VIEW;
44 import static org.sonar.server.permission.DefaultPermissionTemplates.DEFAULT_TEMPLATE_PROPERTY;
45 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
46 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.CONTROLLER;
47 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER;
48 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_ID;
49 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_TEMPLATE_NAME;
51 public class SetDefaultTemplateActionTest extends BasePermissionWsTest<SetDefaultTemplateAction> {
53 private static final String ACTION = "set_default_template";
55 private I18nRule i18n = new I18nRule();
56 private PersistentSettings persistentSettings = new PersistentSettings(new MapSettings(), db.getDbClient(), new SettingsChangeNotifier());
57 private PermissionTemplateDto template;
60 protected SetDefaultTemplateAction buildWsAction() {
61 return new SetDefaultTemplateAction(db.getDbClient(), newPermissionWsSupport(), newRootResourceTypes(), persistentSettings, userSession, i18n);
66 DbClient dbClient = db.getDbClient();
67 persistentSettings.saveProperty(DEFAULT_TEMPLATE_PROPERTY, "any-template-uuid");
68 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(PROJECT), "any-template-uuid");
69 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty(VIEW), "any-view-template-uuid");
70 persistentSettings.saveProperty(defaultRootQualifierTemplateProperty("DEV"), "any-dev-template-uuid");
71 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
73 template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto().setUuid("permission-template-uuid"));
78 public void update_settings_for_project_qualifier() throws Exception {
79 // default value is project qualifier's value
80 String result = newRequest(template.getUuid(), null);
82 assertThat(result).isEmpty();
83 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
84 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
85 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
86 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
90 public void update_settings_for_project_qualifier_by_template_name() throws Exception {
91 wsTester.newPostRequest(CONTROLLER, ACTION)
92 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
94 db.getSession().commit();
96 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
97 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
98 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
99 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
103 public void update_settings_of_views_property() throws Exception {
104 newRequest(template.getUuid(), VIEW);
106 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
107 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo("any-template-uuid");
108 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo(template.getUuid());
109 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
113 public void fail_if_anonymous() throws Exception {
114 expectedException.expect(UnauthorizedException.class);
115 userSession.anonymous();
117 newRequest(template.getUuid(), PROJECT);
121 public void fail_if_not_admin() throws Exception {
122 expectedException.expect(ForbiddenException.class);
123 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
125 newRequest(template.getUuid(), PROJECT);
129 public void fail_if_template_not_provided() throws Exception {
130 expectedException.expect(BadRequestException.class);
132 newRequest(null, PROJECT);
136 public void fail_if_template_does_not_exist() throws Exception {
137 expectedException.expect(NotFoundException.class);
139 newRequest("unknown-template-uuid", PROJECT);
143 public void fail_if_qualifier_is_not_root() throws Exception {
144 expectedException.expect(IllegalArgumentException.class);
145 expectedException.expectMessage("Value of parameter 'qualifier' (FIL) must be one of: [DEV, TRK, VW]");
147 newRequest(template.getUuid(), Qualifiers.FILE);
150 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) throws Exception {
151 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
152 if (templateUuid != null) {
153 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
155 if (qualifier != null) {
156 request.setParam(PARAM_QUALIFIER, qualifier);
159 return request.execute().outputAsString();