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.Test;
24 import org.sonar.api.utils.System2;
25 import org.sonar.api.utils.internal.TestSystem2;
26 import org.sonar.db.permission.template.PermissionTemplateDto;
27 import org.sonar.server.exceptions.BadRequestException;
28 import org.sonar.server.exceptions.ForbiddenException;
29 import org.sonar.server.permission.ws.BasePermissionWsTest;
30 import org.sonar.server.ws.TestRequest;
31 import org.sonar.server.ws.TestResponse;
33 import static org.assertj.core.api.Assertions.assertThat;
34 import static org.sonar.core.permission.GlobalPermissions.QUALITY_PROFILE_ADMIN;
35 import static org.sonar.test.JsonAssert.assertJson;
36 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
37 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
38 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN;
40 public class CreateTemplateActionTest extends BasePermissionWsTest<CreateTemplateAction> {
42 private static final long NOW = 1_440_512_328_743L;
43 private System2 system = new TestSystem2().setNow(NOW);
46 protected CreateTemplateAction buildWsAction() {
47 return new CreateTemplateAction(db.getDbClient(), userSession, system, newPermissionWsSupport());
51 public void create_full_permission_template() throws Exception {
52 loginAsAdminOnDefaultOrganization();
54 TestResponse result = newRequest("Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
56 assertJson(result.getInput())
58 .isSimilarTo(getClass().getResource("create_template-example.json"));
59 PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
60 assertThat(finance.getName()).isEqualTo("Finance");
61 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
62 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
63 assertThat(finance.getUuid()).isNotEmpty();
64 assertThat(finance.getCreatedAt().getTime()).isEqualTo(NOW);
65 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(NOW);
69 public void create_minimalist_permission_template() throws Exception {
70 loginAsAdminOnDefaultOrganization();
72 newRequest("Finance", null, null);
74 PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
75 assertThat(finance.getName()).isEqualTo("Finance");
76 assertThat(finance.getDescription()).isNullOrEmpty();
77 assertThat(finance.getKeyPattern()).isNullOrEmpty();
78 assertThat(finance.getUuid()).isNotEmpty();
79 assertThat(finance.getCreatedAt().getTime()).isEqualTo(NOW);
80 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(NOW);
84 public void fail_if_name_not_provided() throws Exception {
85 loginAsAdminOnDefaultOrganization();
87 expectedException.expect(IllegalArgumentException.class);
89 newRequest(null, null, null);
93 public void fail_if_name_empty() throws Exception {
94 loginAsAdminOnDefaultOrganization();
96 expectedException.expect(BadRequestException.class);
97 expectedException.expectMessage("The template name must not be blank");
99 newRequest("", null, null);
103 public void fail_if_regexp_if_not_valid() throws Exception {
104 loginAsAdminOnDefaultOrganization();
106 expectedException.expect(BadRequestException.class);
107 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
109 newRequest("Finance", null, "[azerty");
113 public void fail_if_name_already_exists_in_database_case_insensitive() throws Exception {
114 loginAsAdminOnDefaultOrganization();
115 PermissionTemplateDto template = insertTemplate();
117 expectedException.expect(BadRequestException.class);
118 expectedException.expectMessage("A template with the name '" + template.getName() + "' already exists (case insensitive).");
120 newRequest(template.getName(), null, null);
124 public void fail_if_not_admin() throws Exception {
125 userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), QUALITY_PROFILE_ADMIN);
127 expectedException.expect(ForbiddenException.class);
129 newRequest("Finance", null, null);
132 private TestResponse newRequest(@Nullable String name, @Nullable String description, @Nullable String projectPattern) throws Exception {
133 TestRequest request = newRequest();
135 request.setParam(PARAM_NAME, name);
137 if (description != null) {
138 request.setParam(PARAM_DESCRIPTION, description);
140 if (projectPattern != null) {
141 request.setParam(PARAM_PROJECT_KEY_PATTERN, projectPattern);
144 return request.execute();