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.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.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.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 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 .setOrganizationUuid(db.getDefaultOrganization().getUuid())
61 .setName("Permission Template Name")
62 .setDescription("Permission Template Description")
63 .setKeyPattern(".*\\.pattern\\..*")
64 .setCreatedAt(new Date(1_000_000_000_000L))
65 .setUpdatedAt(new Date(1_000_000_000_000L)));
70 public void update_all_permission_template_fields() throws Exception {
71 loginAsAdminOnDefaultOrganization();
73 String result = call(template.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
77 .isSimilarTo(getClass().getResource("update_template-example.json"));
78 PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
79 assertThat(finance.getName()).isEqualTo("Finance");
80 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
81 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
82 assertThat(finance.getUuid()).isEqualTo(template.getUuid());
83 assertThat(finance.getCreatedAt()).isEqualTo(template.getCreatedAt());
84 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
88 public void update_with_the_same_values() throws Exception {
89 loginAsAdminOnDefaultOrganization();
91 call(template.getUuid(), template.getName(), template.getDescription(), template.getKeyPattern());
93 PermissionTemplateDto reloaded = db.getDbClient().permissionTemplateDao().selectByUuid(db.getSession(), template.getUuid());
94 assertThat(reloaded.getName()).isEqualTo(template.getName());
95 assertThat(reloaded.getDescription()).isEqualTo(template.getDescription());
96 assertThat(reloaded.getKeyPattern()).isEqualTo(template.getKeyPattern());
100 public void update_name_only() throws Exception {
101 loginAsAdminOnDefaultOrganization();
103 call(template.getUuid(), "Finance", null, null);
105 PermissionTemplateDto finance = selectTemplateInDefaultOrganization("Finance");
106 assertThat(finance.getName()).isEqualTo("Finance");
107 assertThat(finance.getDescription()).isEqualTo(template.getDescription());
108 assertThat(finance.getKeyPattern()).isEqualTo(template.getKeyPattern());
112 public void fail_if_key_is_not_found() throws Exception {
113 loginAsAdminOnDefaultOrganization();
115 expectedException.expect(NotFoundException.class);
116 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
118 call("unknown-key", null, null, null);
122 public void fail_if_name_already_exists_in_another_template() throws Exception {
123 loginAsAdminOnDefaultOrganization();
124 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
126 expectedException.expect(BadRequestException.class);
127 expectedException.expectMessage("A template with the name '" + anotherTemplate.getName() + "' already exists (case insensitive).");
129 call(this.template.getUuid(), anotherTemplate.getName(), null, null);
133 public void fail_if_key_is_not_provided() throws Exception {
134 loginAsAdminOnDefaultOrganization();
136 expectedException.expect(IllegalArgumentException.class);
138 call(null, "Finance", null, null);
142 public void fail_if_name_empty() throws Exception {
143 loginAsAdminOnDefaultOrganization();
145 expectedException.expect(BadRequestException.class);
146 expectedException.expectMessage("The template name must not be blank");
148 call(template.getUuid(), "", null, null);
152 public void fail_if_name_has_just_whitespaces() throws Exception {
153 loginAsAdminOnDefaultOrganization();
155 expectedException.expect(BadRequestException.class);
156 expectedException.expectMessage("The template name must not be blank");
158 call(template.getUuid(), " \r\n", null, null);
162 public void fail_if_regexp_if_not_valid() throws Exception {
163 loginAsAdminOnDefaultOrganization();
165 expectedException.expect(BadRequestException.class);
166 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
168 call(template.getUuid(), "Finance", null, "[azerty");
172 public void fail_if_name_already_exists_in_database_case_insensitive() throws Exception {
173 loginAsAdminOnDefaultOrganization();
174 PermissionTemplateDto anotherTemplate = addTemplateToDefaultOrganization();
176 String nameCaseInsensitive = anotherTemplate.getName().toUpperCase();
177 expectedException.expect(BadRequestException.class);
178 expectedException.expectMessage("A template with the name '" + nameCaseInsensitive + "' already exists (case insensitive).");
180 call(this.template.getUuid(), nameCaseInsensitive, null, null);
184 public void fail_if_not_logged_in() throws Exception {
185 expectedException.expect(UnauthorizedException.class);
186 userSession.anonymous();
188 call(template.getUuid(), "Finance", null, null);
192 public void fail_if_not_admin() throws Exception {
193 userSession.logIn().addOrganizationPermission(db.getDefaultOrganization().getUuid(), SCAN_EXECUTION);
195 expectedException.expect(ForbiddenException.class);
197 call(template.getUuid(), "Finance", null, null);
200 private String call(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) throws Exception {
201 TestRequest request = newRequest();
203 request.setParam(PARAM_ID, key);
206 request.setParam(PARAM_NAME, name);
208 if (description != null) {
209 request.setParam(PARAM_DESCRIPTION, description);
211 if (projectPattern != null) {
212 request.setParam(PARAM_PROJECT_KEY_PATTERN, projectPattern);
215 return request.execute().getInput();