3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info 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 java.util.Date;
23 import javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.permission.template.PermissionTemplateDto;
28 import org.sonar.server.exceptions.BadRequestException;
29 import org.sonar.server.exceptions.ForbiddenException;
30 import org.sonar.server.exceptions.NotFoundException;
31 import org.sonar.server.exceptions.UnauthorizedException;
32 import org.sonar.server.permission.ws.BasePermissionWsTest;
33 import org.sonar.server.ws.TestRequest;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.mockito.Mockito.spy;
37 import static org.mockito.Mockito.when;
38 import static org.sonar.db.permission.GlobalPermission.SCAN;
39 import static org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto;
40 import static org.sonar.test.JsonAssert.assertJson;
41 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN;
46 public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplateAction> {
48 private final System2 system = spy(System2.INSTANCE);
49 private PermissionTemplateDto template;
52 protected UpdateTemplateAction buildWsAction() {
53 return new UpdateTemplateAction(db.getDbClient(), userSession, system, newPermissionWsSupport());
58 when(system.now()).thenReturn(1_440_512_328_743L);
59 template = db.getDbClient().permissionTemplateDao().insert(db.getSession(), newPermissionTemplateDto()
60 .setName("Permission Template Name")
61 .setDescription("Permission Template Description")
62 .setKeyPattern(".*\\.pattern\\..*")
63 .setCreatedAt(new Date(1_000_000_000_000L))
64 .setUpdatedAt(new Date(1_000_000_000_000L)));
69 public void update_all_permission_template_fields() {
72 String result = call(template.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
76 .isSimilarTo(getClass().getResource("update_template-example.json"));
77 PermissionTemplateDto finance = selectPermissionTemplate("Finance");
78 assertThat(finance.getName()).isEqualTo("Finance");
79 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
80 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
81 assertThat(finance.getUuid()).isEqualTo(template.getUuid());
82 assertThat(finance.getCreatedAt()).isEqualTo(template.getCreatedAt());
83 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
87 public void update_with_the_same_values() {
90 call(template.getUuid(), template.getName(), template.getDescription(), template.getKeyPattern());
92 PermissionTemplateDto reloaded = db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid());
93 assertThat(reloaded.getName()).isEqualTo(template.getName());
94 assertThat(reloaded.getDescription()).isEqualTo(template.getDescription());
95 assertThat(reloaded.getKeyPattern()).isEqualTo(template.getKeyPattern());
99 public void update_name_only() {
102 call(template.getUuid(), "Finance", null, null);
104 PermissionTemplateDto finance = selectPermissionTemplate("Finance");
105 assertThat(finance.getName()).isEqualTo("Finance");
106 assertThat(finance.getDescription()).isEqualTo(template.getDescription());
107 assertThat(finance.getKeyPattern()).isEqualTo(template.getKeyPattern());
111 public void fail_if_key_is_not_found() {
114 expectedException.expect(NotFoundException.class);
115 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
117 call("unknown-key", null, null, null);
121 public void fail_if_name_already_exists_in_another_template() {
123 PermissionTemplateDto anotherTemplate = addTemplate();
125 expectedException.expect(BadRequestException.class);
126 expectedException.expectMessage("A template with the name '" + anotherTemplate.getName() + "' already exists (case insensitive).");
128 call(this.template.getUuid(), anotherTemplate.getName(), null, null);
132 public void fail_if_key_is_not_provided() {
135 expectedException.expect(IllegalArgumentException.class);
137 call(null, "Finance", null, null);
141 public void fail_if_name_empty() {
144 expectedException.expect(BadRequestException.class);
145 expectedException.expectMessage("The template name must not be blank");
147 call(template.getUuid(), "", null, null);
151 public void fail_if_name_has_just_whitespaces() {
154 expectedException.expect(BadRequestException.class);
155 expectedException.expectMessage("The template name must not be blank");
157 call(template.getUuid(), " \r\n", null, null);
161 public void fail_if_regexp_if_not_valid() {
164 expectedException.expect(BadRequestException.class);
165 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
167 call(template.getUuid(), "Finance", null, "[azerty");
171 public void fail_if_name_already_exists_in_database_case_insensitive() {
173 PermissionTemplateDto anotherTemplate = addTemplate();
175 String nameCaseInsensitive = anotherTemplate.getName().toUpperCase();
176 expectedException.expect(BadRequestException.class);
177 expectedException.expectMessage("A template with the name '" + nameCaseInsensitive + "' already exists (case insensitive).");
179 call(this.template.getUuid(), nameCaseInsensitive, null, null);
183 public void fail_if_not_logged_in() {
184 expectedException.expect(UnauthorizedException.class);
185 userSession.anonymous();
187 call(template.getUuid(), "Finance", null, null);
191 public void fail_if_not_admin() {
192 userSession.logIn().addPermission(SCAN);
194 expectedException.expect(ForbiddenException.class);
196 call(template.getUuid(), "Finance", null, null);
199 private String call(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) {
200 TestRequest request = newRequest();
202 request.setParam(PARAM_ID, key);
205 request.setParam(PARAM_NAME, name);
207 if (description != null) {
208 request.setParam(PARAM_DESCRIPTION, description);
210 if (projectPattern != null) {
211 request.setParam(PARAM_PROJECT_KEY_PATTERN, projectPattern);
214 return request.execute().getInput();