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 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.WsTester;
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.core.permission.GlobalPermissions.SCAN_EXECUTION;
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.CONTROLLER;
42 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_DESCRIPTION;
43 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ID;
44 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_NAME;
45 import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_PROJECT_KEY_PATTERN;
47 public class UpdateTemplateActionTest extends BasePermissionWsTest<UpdateTemplateAction> {
49 private static final String ACTION = "update_template";
51 private System2 system = spy(System2.INSTANCE);
52 private PermissionTemplateDto template;
55 protected UpdateTemplateAction buildWsAction() {
56 return new UpdateTemplateAction(db.getDbClient(), userSession, system, newPermissionWsSupport());
61 when(system.now()).thenReturn(1_440_512_328_743L);
62 template = db.getDbClient().permissionTemplateDao().insert(db.getSession(), newPermissionTemplateDto()
63 .setOrganizationUuid(db.getDefaultOrganization().getUuid())
64 .setName("Permission Template Name")
65 .setDescription("Permission Template Description")
66 .setKeyPattern(".*\\.pattern\\..*")
67 .setCreatedAt(new Date(1_000_000_000_000L))
68 .setUpdatedAt(new Date(1_000_000_000_000L)));
73 public void update_all_permission_template_fields() throws Exception {
74 loginAsAdminOnDefaultOrganization();
76 String result = call(template.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
80 .isSimilarTo(getClass().getResource("update_template-example.json"));
81 PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
82 assertThat(finance.getName()).isEqualTo("Finance");
83 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
84 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
85 assertThat(finance.getUuid()).isEqualTo(template.getUuid());
86 assertThat(finance.getCreatedAt()).isEqualTo(template.getCreatedAt());
87 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
91 public void update_with_the_same_values() throws Exception {
92 loginAsAdminOnDefaultOrganization();
94 call(template.getUuid(), template.getName(), template.getDescription(), template.getKeyPattern());
96 PermissionTemplateDto reloaded = db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid());
97 assertThat(reloaded.getName()).isEqualTo(template.getName());
98 assertThat(reloaded.getDescription()).isEqualTo(template.getDescription());
99 assertThat(reloaded.getKeyPattern()).isEqualTo(template.getKeyPattern());
103 public void update_name_only() throws Exception {
104 loginAsAdminOnDefaultOrganization();
106 call(template.getUuid(), "Finance", null, null);
108 PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
109 assertThat(finance.getName()).isEqualTo("Finance");
110 assertThat(finance.getDescription()).isEqualTo(template.getDescription());
111 assertThat(finance.getKeyPattern()).isEqualTo(template.getKeyPattern());
115 public void fail_if_key_is_not_found() throws Exception {
116 loginAsAdminOnDefaultOrganization();
118 expectedException.expect(NotFoundException.class);
119 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
121 call("unknown-key", null, null, null);
125 public void fail_if_name_already_exists_in_another_template() throws Exception {
126 loginAsAdminOnDefaultOrganization();
127 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
129 expectedException.expect(BadRequestException.class);
130 expectedException.expectMessage("A template with the name '" + anotherTemplate.getName() + "' already exists (case insensitive).");
132 call(this.template.getUuid(), anotherTemplate.getName(), null, null);
136 public void fail_if_key_is_not_provided() throws Exception {
137 loginAsAdminOnDefaultOrganization();
139 expectedException.expect(IllegalArgumentException.class);
141 call(null, "Finance", null, null);
145 public void fail_if_name_empty() throws Exception {
146 loginAsAdminOnDefaultOrganization();
148 expectedException.expect(BadRequestException.class);
149 expectedException.expectMessage("The template name must not be blank");
151 call(template.getUuid(), "", null, null);
155 public void fail_if_name_has_just_whitespaces() throws Exception {
156 loginAsAdminOnDefaultOrganization();
158 expectedException.expect(BadRequestException.class);
159 expectedException.expectMessage("The template name must not be blank");
161 call(template.getUuid(), " \r\n", null, null);
165 public void fail_if_regexp_if_not_valid() throws Exception {
166 loginAsAdminOnDefaultOrganization();
168 expectedException.expect(BadRequestException.class);
169 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
171 call(template.getUuid(), "Finance", null, "[azerty");
175 public void fail_if_name_already_exists_in_database_case_insensitive() throws Exception {
176 loginAsAdminOnDefaultOrganization();
177 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
179 String nameCaseInsensitive = anotherTemplate.getName().toUpperCase();
180 expectedException.expect(BadRequestException.class);
181 expectedException.expectMessage("A template with the name '" + nameCaseInsensitive + "' already exists (case insensitive).");
183 call(this.template.getUuid(), nameCaseInsensitive, null, null);
187 public void fail_if_not_logged_in() throws Exception {
188 expectedException.expect(UnauthorizedException.class);
189 userSession.anonymous();
191 call(template.getUuid(), "Finance", null, null);
195 public void fail_if_not_admin() throws Exception {
196 userSession.login().addOrganizationPermission(db.getDefaultOrganization().getUuid(), SCAN_EXECUTION);
198 expectedException.expect(ForbiddenException.class);
200 call(template.getUuid(), "Finance", null, null);
203 private String call(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) throws Exception {
204 WsTester.TestRequest request = wsTester.newPostRequest(CONTROLLER, ACTION);
206 request.setParam(PARAM_ID, key);
209 request.setParam(PARAM_NAME, name);
211 if (description != null) {
212 request.setParam(PARAM_DESCRIPTION, description);
214 if (projectPattern != null) {
215 request.setParam(PARAM_PROJECT_KEY_PATTERN, projectPattern);
218 return request.execute().outputAsString();