2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.permission.ws.template;
23 import java.util.Date;
24 import javax.annotation.Nullable;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.sonar.api.utils.System2;
30 import org.sonar.core.permission.GlobalPermissions;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.DbTester;
34 import org.sonar.db.permission.PermissionTemplateDto;
35 import org.sonar.db.user.GroupDto;
36 import org.sonar.server.component.ComponentFinder;
37 import org.sonar.server.exceptions.BadRequestException;
38 import org.sonar.server.exceptions.ForbiddenException;
39 import org.sonar.server.exceptions.NotFoundException;
40 import org.sonar.server.exceptions.UnauthorizedException;
41 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
42 import org.sonar.server.tester.UserSessionRule;
43 import org.sonar.server.ws.TestRequest;
44 import org.sonar.server.ws.TestResponse;
45 import org.sonar.server.ws.WsActionTester;
47 import static org.assertj.core.api.Assertions.assertThat;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.when;
50 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
51 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_DESCRIPTION;
52 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_ID;
53 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_NAME;
54 import static org.sonar.server.permission.ws.WsPermissionParameters.PARAM_PATTERN;
55 import static org.sonar.test.JsonAssert.assertJson;
57 public class UpdateTemplateActionTest {
60 public DbTester db = DbTester.create(System2.INSTANCE);
62 public UserSessionRule userSession = UserSessionRule.standalone();
64 public ExpectedException expectedException = ExpectedException.none();
70 PermissionTemplateDto templateDto;
74 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
75 System2 system = mock(System2.class);
76 when(system.now()).thenReturn(1_440_512_328_743L);
78 dbClient = db.getDbClient();
79 dbSession = db.getSession();
80 PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient));
82 ws = new WsActionTester(new UpdateTemplateAction(dbClient, userSession, system, finder));
84 templateDto = insertTemplate(newPermissionTemplateDto()
85 .setName("Permission Template Name")
86 .setDescription("Permission Template Description")
87 .setKeyPattern(".*\\.pattern\\..*")
88 .setCreatedAt(new Date(1_000_000_000_000L))
89 .setUpdatedAt(new Date(1_000_000_000_000L)));
94 public void update_all_permission_template_fields() {
95 TestResponse result = newRequest(templateDto.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
97 assertJson(result.getInput())
99 .isSimilarTo(getClass().getResource("update_template-example.json"));
100 PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
101 assertThat(finance.getName()).isEqualTo("Finance");
102 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
103 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
104 assertThat(finance.getUuid()).isEqualTo(templateDto.getUuid());
105 assertThat(finance.getCreatedAt()).isEqualTo(templateDto.getCreatedAt());
106 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
110 public void update_with_the_same_values() {
111 newRequest(templateDto.getUuid(), templateDto.getName(), templateDto.getDescription(), templateDto.getKeyPattern());
113 PermissionTemplateDto updatedTemplate = dbClient.permissionTemplateDao().selectByUuid(dbSession, templateDto.getUuid());
114 assertThat(updatedTemplate.getName()).isEqualTo(templateDto.getName());
115 assertThat(updatedTemplate.getDescription()).isEqualTo(templateDto.getDescription());
116 assertThat(updatedTemplate.getKeyPattern()).isEqualTo(templateDto.getKeyPattern());
120 public void update_name_only() {
121 newRequest(templateDto.getUuid(), "Finance", null, null);
123 PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
124 assertThat(finance.getName()).isEqualTo("Finance");
125 assertThat(finance.getDescription()).isEqualTo(templateDto.getDescription());
126 assertThat(finance.getKeyPattern()).isEqualTo(templateDto.getKeyPattern());
130 public void fail_if_key_is_not_found() {
131 expectedException.expect(NotFoundException.class);
132 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
134 newRequest("unknown-key", null, null, null);
138 public void fail_if_name_already_exists_in_another_template() {
139 expectedException.expect(BadRequestException.class);
140 expectedException.expectMessage("A template with the name 'My Template' already exists (case insensitive).");
142 insertTemplate(newPermissionTemplateDto()
143 .setName("My Template")
145 .setCreatedAt(new Date(12345789L))
146 .setUpdatedAt(new Date(12345789L)));
149 newRequest(templateDto.getUuid(), "My Template", null, null);
153 public void fail_if_key_is_not_provided() {
154 expectedException.expect(IllegalArgumentException.class);
156 newRequest(null, "Finance", null, null);
160 public void fail_if_name_empty() {
161 expectedException.expect(BadRequestException.class);
162 expectedException.expectMessage("The template name must not be blank");
164 newRequest(templateDto.getUuid(), "", null, null);
168 public void fail_if_name_has_just_whitespaces() {
169 expectedException.expect(BadRequestException.class);
170 expectedException.expectMessage("The template name must not be blank");
172 newRequest(templateDto.getUuid(), " \r\n", null, null);
176 public void fail_if_regexp_if_not_valid() {
177 expectedException.expect(BadRequestException.class);
178 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
180 newRequest(templateDto.getUuid(), "Finance", null, "[azerty");
184 public void fail_if_name_already_exists_in_database_case_insensitive() {
185 expectedException.expect(BadRequestException.class);
186 expectedException.expectMessage("A template with the name 'Finance' already exists (case insensitive).");
187 insertTemplate(newPermissionTemplateDto().setName("finance"));
190 newRequest(templateDto.getUuid(), "Finance", null, null);
194 public void fail_if_not_logged_in() {
195 expectedException.expect(UnauthorizedException.class);
196 userSession.anonymous();
198 newRequest(templateDto.getUuid(), "Finance", null, null);
202 public void fail_if_not_admin() {
203 expectedException.expect(ForbiddenException.class);
204 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
206 newRequest(templateDto.getUuid(), "Finance", null, null);
209 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
210 return dbClient.permissionTemplateDao().insert(dbSession, template);
213 private GroupDto insertGroup(GroupDto group) {
214 return dbClient.groupDao().insert(db.getSession(), group);
217 private void commit() {
221 private TestResponse newRequest(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) {
222 TestRequest request = ws.newRequest();
224 request.setParam(PARAM_ID, key);
227 request.setParam(PARAM_NAME, name);
229 if (description != null) {
230 request.setParam(PARAM_DESCRIPTION, description);
232 if (projectPattern != null) {
233 request.setParam(PARAM_PATTERN, projectPattern);
236 return request.execute();