]> source.dussan.org Git - sonarqube.git/blob
8aa7f028c36be2b3a5f5d957163804c05221289f
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2014 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 package org.sonar.server.permission.ws.template;
22
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;
49
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;
59
60 public class UpdateTemplateActionTest {
61
62   @Rule
63   public DbTester db = DbTester.create(System2.INSTANCE);
64   @Rule
65   public UserSessionRule userSession = UserSessionRule.standalone();
66   @Rule
67   public ExpectedException expectedException = ExpectedException.none();
68
69   WsActionTester ws;
70   DbClient dbClient;
71   DbSession dbSession;
72   ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
73
74   PermissionTemplateDto templateDto;
75
76   @Before
77   public void setUp() {
78     userSession.login().setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
79     System2 system = mock(System2.class);
80     when(system.now()).thenReturn(1_440_512_328_743L);
81
82     dbClient = db.getDbClient();
83     dbSession = db.getSession();
84     PermissionDependenciesFinder finder = new PermissionDependenciesFinder(dbClient, new ComponentFinder(dbClient), new UserGroupFinder(dbClient), resourceTypes);
85
86     ws = new WsActionTester(new UpdateTemplateAction(dbClient, userSession, system, finder));
87
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)));
94     commit();
95   }
96
97   @Test
98   public void update_all_permission_template_fields() {
99     TestResponse result = newRequest(templateDto.getUuid(), "Finance", "Permissions for financially related projects", ".*\\.finance\\..*");
100
101     assertJson(result.getInput())
102       .ignoreFields("id")
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);
111   }
112
113   @Test
114   public void update_with_the_same_values() {
115     newRequest(templateDto.getUuid(), templateDto.getName(), templateDto.getDescription(), templateDto.getKeyPattern());
116
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());
121   }
122
123   @Test
124   public void update_name_only() {
125     newRequest(templateDto.getUuid(), "Finance", null, null);
126
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());
131   }
132
133   @Test
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");
137
138     newRequest("unknown-key", null, null, null);
139   }
140
141   @Test
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).");
145
146     insertTemplate(newPermissionTemplateDto()
147       .setName("My Template")
148       .setUuid("my-key")
149       .setCreatedAt(new Date(12345789L))
150       .setUpdatedAt(new Date(12345789L)));
151     commit();
152
153     newRequest(templateDto.getUuid(), "My Template", null, null);
154   }
155
156   @Test
157   public void fail_if_key_is_not_provided() {
158     expectedException.expect(IllegalArgumentException.class);
159
160     newRequest(null, "Finance", null, null);
161   }
162
163   @Test
164   public void fail_if_name_empty() {
165     expectedException.expect(BadRequestException.class);
166     expectedException.expectMessage("The template name must not be blank");
167
168     newRequest(templateDto.getUuid(), "", null, null);
169   }
170
171   @Test
172   public void fail_if_name_has_just_whitespaces() {
173     expectedException.expect(BadRequestException.class);
174     expectedException.expectMessage("The template name must not be blank");
175
176     newRequest(templateDto.getUuid(), "  \r\n", null, null);
177   }
178
179   @Test
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");
183
184     newRequest(templateDto.getUuid(), "Finance", null, "[azerty");
185   }
186
187   @Test
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"));
192     commit();
193
194     newRequest(templateDto.getUuid(), "Finance", null, null);
195   }
196
197   @Test
198   public void fail_if_not_logged_in() {
199     expectedException.expect(UnauthorizedException.class);
200     userSession.anonymous();
201
202     newRequest(templateDto.getUuid(), "Finance", null, null);
203   }
204
205   @Test
206   public void fail_if_not_admin() {
207     expectedException.expect(ForbiddenException.class);
208     userSession.setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN);
209
210     newRequest(templateDto.getUuid(), "Finance", null, null);
211   }
212
213   private PermissionTemplateDto insertTemplate(PermissionTemplateDto template) {
214     return dbClient.permissionTemplateDao().insert(dbSession, template);
215   }
216
217   private GroupDto insertGroup(GroupDto group) {
218     return dbClient.groupDao().insert(db.getSession(), group);
219   }
220
221   private void commit() {
222     dbSession.commit();
223   }
224
225   private TestResponse newRequest(@Nullable String key, @Nullable String name, @Nullable String description, @Nullable String projectPattern) {
226     TestRequest request = ws.newRequest();
227     if (key != null) {
228       request.setParam(PARAM_ID, key);
229     }
230     if (name != null) {
231       request.setParam(PARAM_NAME, name);
232     }
233     if (description != null) {
234       request.setParam(PARAM_DESCRIPTION, description);
235     }
236     if (projectPattern != null) {
237       request.setParam(PARAM_PATTERN, projectPattern);
238     }
239
240     return request.execute();
241   }
242 }