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.resources.Qualifiers;
30 import org.sonar.api.utils.System2;
31 import org.sonar.core.permission.GlobalPermissions;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.component.ResourceTypesRule;
36 import org.sonar.db.permission.PermissionTemplateDto;
37 import org.sonar.db.user.GroupDto;
38 import org.sonar.server.component.ComponentFinder;
39 import org.sonar.server.exceptions.BadRequestException;
40 import org.sonar.server.exceptions.ForbiddenException;
41 import org.sonar.server.exceptions.NotFoundException;
42 import org.sonar.server.exceptions.UnauthorizedException;
43 import org.sonar.server.permission.ws.PermissionDependenciesFinder;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.usergroups.ws.UserGroupFinder;
46 import org.sonar.server.ws.TestRequest;
47 import org.sonar.server.ws.TestResponse;
48 import org.sonar.server.ws.WsActionTester;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.when;
53 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
54 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_DESCRIPTION;
55 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_ID;
56 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_NAME;
57 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PATTERN;
58 import static org.sonar.test.JsonAssert.assertJson;
60 public class UpdateTemplateActionTest {
63 public DbTester db = DbTester.create(System2.INSTANCE);
65 public UserSessionRule userSession = UserSessionRule.standalone();
67 public ExpectedException expectedException = ExpectedException.none();
72 ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
74 PermissionTemplateDto templateDto;
78 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
79 System2 system = mock(System2.class);
80 when(system.now()).thenReturn(1_440_512_328_743L);
82 dbClient = db.getDbClient();
83 dbSession = db.getSession();
84 PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
86 ws = new WsActionTester(new UpdateTemplateAction(dbClient, userSession, system, finder));
88 templateDto = insertTemplate(newPermissionTemplateDto()
89 .setName("Permission Template Name")
90 .setDescription("Permission Template Description")
91 .setKeyPattern(".*\\.pattern\\..*")
92 .setCreatedAt(new Date(1_000_000_000_000L))
93 .setUpdatedAt(new Date(1_000_000_000_000L)));
98 public void update_all_permission_template_fields() {
99 TestResponse result = newRequest(templateDto.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
101 assertJson(result.getInput())
103 .isSimilarTo(getClass().getResource("update_template-example.json"));
104 PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
105 assertThat(finance.getName()).isEqualTo("Finance");
106 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
107 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
108 assertThat(finance.getUuid()).isEqualTo(templateDto.getUuid());
109 assertThat(finance.getCreatedAt()).isEqualTo(templateDto.getCreatedAt());
110 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
114 public void update_with_the_same_values() {
115 newRequest(templateDto.getUuid(), templateDto.getName(), templateDto.getDescription(), templateDto.getKeyPattern());
117 PermissionTemplateDto updatedTemplate = dbClient.permissionTemplateDao().selectByUuid(dbSession, templateDto.getUuid());
118 assertThat(updatedTemplate.getName()).isEqualTo(templateDto.getName());
119 assertThat(updatedTemplate.getDescription()).isEqualTo(templateDto.getDescription());
120 assertThat(updatedTemplate.getKeyPattern()).isEqualTo(templateDto.getKeyPattern());
124 public void update_name_only() {
125 newRequest(templateDto.getUuid(), "Finance", null, null);
127 PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
128 assertThat(finance.getName()).isEqualTo("Finance");
129 assertThat(finance.getDescription()).isEqualTo(templateDto.getDescription());
130 assertThat(finance.getKeyPattern()).isEqualTo(templateDto.getKeyPattern());
134 public void fail_if_key_is_not_found() {
135 expectedException.expect(NotFoundException.class);
136 expectedException.expectMessage("Permission template with id 'unknown-key' is not found");
138 newRequest("unknown-key", null, null, null);
142 public void fail_if_name_already_exists_in_another_template() {
143 expectedException.expect(BadRequestException.class);
144 expectedException.expectMessage("A template with the name 'My Template' already exists (case insensitive).");
146 insertTemplate(newPermissionTemplateDto()
147 .setName("My Template")
149 .setCreatedAt(new Date(12345789L))
150 .setUpdatedAt(new Date(12345789L)));
153 newRequest(templateDto.getUuid(), "My Template", null, null);
157 public void fail_if_key_is_not_provided() {
158 expectedException.expect(IllegalArgumentException.class);
160 newRequest(null, "Finance", null, null);
164 public void fail_if_name_empty() {
165 expectedException.expect(BadRequestException.class);
166 expectedException.expectMessage("The template name must not be blank");
168 newRequest(templateDto.getUuid(), "", null, null);
172 public void fail_if_name_has_just_whitespaces() {
173 expectedException.expect(BadRequestException.class);
174 expectedException.expectMessage("The template name must not be blank");
176 newRequest(templateDto.getUuid(), " \r\n", null, null);
180 public void fail_if_regexp_if_not_valid() {
181 expectedException.expect(BadRequestException.class);
182 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
184 newRequest(templateDto.getUuid(), "Finance", null, "[azerty");
188 public void fail_if_name_already_exists_in_database_case_insensitive() {
189 expectedException.expect(BadRequestException.class);
190 expectedException.expectMessage("A template with the name 'Finance' already exists (case insensitive).");
191 insertTemplate(newPermissionTemplateDto().setName("finance"));
194 newRequest(templateDto.getUuid(), "Finance", null, null);
198 public void fail_if_not_logged_in() {
199 expectedException.expect(UnauthorizedException.class);
200 userSession.anonymous();
202 newRequest(templateDto.getUuid(), "Finance", null, null);
206 public void fail_if_not_admin() {
207 expectedException.expect(ForbiddenException.class);
208 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
210 newRequest(templateDto.getUuid(), "Finance", null, null);
213 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
214 return dbClient.permissionTemplateDao().insert(dbSession, template);
217 private GroupDto insertGroup(GroupDto group) {
218 return dbClient.groupDao().insert(db.getSession(), group);
221 private void commit() {
225 private TestResponse newRequest(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) {
226 TestRequest request = ws.newRequest();
228 request.setParam(PARAM_ID, key);
231 request.setParam(PARAM_NAME, name);
233 if (description != null) {
234 request.setParam(PARAM_DESCRIPTION, description);
236 if (projectPattern != null) {
237 request.setParam(PARAM_PATTERN, projectPattern);
240 return request.execute();