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 loginAsAdminOnDefaultOrganization();
73 template = dbClient.permissionTemplateDao().insert(db.getSession(), PermissionTemplateTesting.newPermissionTemplateDto()
74 .setOrganizationUuid(db.getDefaultOrganization().getUuid())
75 .setUuid("permission-template-uuid"));
80 public void update_settings_for_project_qualifier() throws Exception {
81 // default value is project qualifier's value
82 String result = newRequest(template.getUuid(), null);
84 assertThat(result).isEmpty();
85 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
86 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
87 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
88 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
92 public void update_settings_for_project_qualifier_by_template_name() throws Exception {
93 wsTester.newPostRequest(CONTROLLER, ACTION)
94 .setParam(PARAM_TEMPLATE_NAME, template.getName().toUpperCase())
96 db.getSession().commit();
98 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
99 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo(template.getUuid());
100 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo("any-view-template-uuid");
101 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
105 public void update_settings_of_views_property() throws Exception {
106 newRequest(template.getUuid(), VIEW);
108 assertThat(persistentSettings.getString(DEFAULT_TEMPLATE_PROPERTY)).isEqualTo("any-template-uuid");
109 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(PROJECT))).isEqualTo("any-template-uuid");
110 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty(VIEW))).isEqualTo(template.getUuid());
111 assertThat(persistentSettings.getString(defaultRootQualifierTemplateProperty("DEV"))).isEqualTo("any-dev-template-uuid");
115 public void fail_if_anonymous() throws Exception {
116 expectedException.expect(UnauthorizedException.class);
117 userSession.anonymous();
119 newRequest(template.getUuid(), PROJECT);
123 public void fail_if_not_admin() throws Exception {
124 expectedException.expect(ForbiddenException.class);
125 userSession.login().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
127 newRequest(template.getUuid(), PROJECT);
131 public void fail_if_template_not_provided() throws Exception {
132 expectedException.expect(BadRequestException.class);
134 newRequest(null, PROJECT);
138 public void fail_if_template_does_not_exist() throws Exception {
139 expectedException.expect(NotFoundException.class);
141 newRequest("unknown-template-uuid", PROJECT);
145 public void fail_if_qualifier_is_not_root() throws Exception {
146 expectedException.expect(IllegalArgumentException.class);
147 expectedException.expectMessage("Value of parameter 'qualifier' (FIL) must be one of: [DEV, TRK, VW]");
149 newRequest(template.getUuid(), Qualifiers.FILE);
152 private String newRequest(@Nullable String templateUuid, @Nullable String qualifier) throws Exception {
153 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
154 if (templateUuid != null) {
155 request.setParam(PARAM_TEMPLATE_ID, templateUuid);
157 if (qualifier != null) {
158 request.setParam(PARAM_QUALIFIER, qualifier);
161 return request.execute().outputAsString();