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 javax.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.utils.System2;
29 import org.sonar.core.permission.GlobalPermissions;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.DbTester;
33 import org.sonar.db.permission.PermissionTemplateDto;
34 import org.sonar.server.exceptions.BadRequestException;
35 import org.sonar.server.exceptions.ForbiddenException;
36 import org.sonar.server.exceptions.UnauthorizedException;
37 import org.sonar.server.tester.UserSessionRule;
38 import org.sonar.server.ws.TestRequest;
39 import org.sonar.server.ws.TestResponse;
40 import org.sonar.server.ws.WsActionTester;
42 import static org.assertj.core.api.Assertions.assertThat;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.db.permission.PermissionTemplateTesting.newPermissionTemplateDto;
46 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_DESCRIPTION;
47 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_NAME;
48 import static org.sonar.server.permission.ws.PermissionsWsParameters.PARAM_PATTERN;
49 import static org.sonar.test.JsonAssert.assertJson;
51 public class CreateTemplateActionTest {
54 public DbTester db = DbTester.create(System2.INSTANCE);
56 public UserSessionRule userSession = UserSessionRule.standalone();
58 public ExpectedException expectedException = ExpectedException.none();
63 System2 system = mock(System2.class);
67 userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
68 when(system.now()).thenReturn(1440512328743L);
70 dbClient = db.getDbClient();
71 dbSession = db.getSession();
72 ws = new WsActionTester(new CreateTemplateAction(dbClient, userSession, system));
76 public void create_full_permission_template() {
77 TestResponse result = newRequest("Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
79 assertJson(result.getInput())
81 .isSimilarTo(getClass().getResource("create_template-example.json"));
82 PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
83 assertThat(finance.getName()).isEqualTo("Finance");
84 assertThat(finance.getDescription()).isEqualTo("Permissions for financially related projects");
85 assertThat(finance.getKeyPattern()).isEqualTo(".*\\.finance\\..*");
86 assertThat(finance.getUuid()).isNotEmpty();
87 assertThat(finance.getCreatedAt().getTime()).isEqualTo(1440512328743L);
88 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
92 public void create_minimalist_permission_template() {
93 newRequest("Finance", null, null);
95 PermissionTemplateDto finance = dbClient.permissionTemplateDao().selectByName(dbSession, "Finance");
96 assertThat(finance.getName()).isEqualTo("Finance");
97 assertThat(finance.getDescription()).isNullOrEmpty();
98 assertThat(finance.getKeyPattern()).isNullOrEmpty();
99 assertThat(finance.getUuid()).isNotEmpty();
100 assertThat(finance.getCreatedAt().getTime()).isEqualTo(1440512328743L);
101 assertThat(finance.getUpdatedAt().getTime()).isEqualTo(1440512328743L);
105 public void fail_if_name_not_provided() {
106 expectedException.expect(IllegalArgumentException.class);
108 newRequest(null, null, null);
112 public void fail_if_name_empty() {
113 expectedException.expect(BadRequestException.class);
114 expectedException.expectMessage("The template name must not be blank");
116 newRequest("", null, null);
120 public void fail_if_regexp_if_not_valid() {
121 expectedException.expect(BadRequestException.class);
122 expectedException.expectMessage("The 'projectKeyPattern' parameter must be a valid Java regular expression. '[azerty' was passed");
124 newRequest("Finance", null, "[azerty");
128 public void fail_if_name_already_exists_in_database_case_insensitive() {
129 expectedException.expect(BadRequestException.class);
130 expectedException.expectMessage("A template with the name 'Finance' already exists (case insensitive).");
131 insertTemplate(newPermissionTemplateDto().setName("finance"));
134 newRequest("Finance", null, null);
138 public void fail_if_not_logged_in() {
139 expectedException.expect(UnauthorizedException.class);
140 userSession.anonymous();
142 newRequest("Finance", null, null);
146 public void fail_if_not_admin() {
147 expectedException.expect(ForbiddenException.class);
148 userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
150 newRequest("Finance", null, null);
153 private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
154 return dbClient.permissionTemplateDao().insert(dbSession, template);
157 private void commit() {
161 private TestResponse newRequest(@Nullable String name, @Nullable String description, @Nullable String projectPattern) {
162 TestRequest request = ws.newRequest();
164 request.setParam(PARAM_NAME, name);
166 if (description != null) {
167 request.setParam(PARAM_DESCRIPTION, description);
169 if (projectPattern != null) {
170 request.setParam(PARAM_PATTERN, projectPattern);
173 return request.execute();