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;
23 import com.google.common.collect.ImmutableMap;
24 import com.google.common.collect.Lists;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.rules.ExpectedException;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.sonar.api.web.UserRole;
33 import org.sonar.core.permission.GlobalPermissions;
34 import org.sonar.core.permission.PermissionQuery;
35 import org.sonar.core.permission.PermissionTemplateDao;
36 import org.sonar.core.permission.PermissionTemplateDto;
37 import org.sonar.core.permission.PermissionTemplateGroupDto;
38 import org.sonar.core.permission.PermissionTemplateUserDto;
39 import org.sonar.core.persistence.DbSession;
40 import org.sonar.core.persistence.MyBatis;
41 import org.sonar.core.properties.PropertiesDao;
42 import org.sonar.core.user.GroupDto;
43 import org.sonar.core.user.UserDao;
44 import org.sonar.core.user.UserDto;
45 import org.sonar.server.exceptions.BadRequestException;
46 import org.sonar.server.user.MockUserSession;
48 import java.util.List;
50 import static org.assertj.core.api.Assertions.assertThat;
51 import static org.mockito.Mockito.*;
53 @RunWith(MockitoJUnitRunner.class)
54 public class InternalPermissionTemplateServiceTest {
56 private static final String DEFAULT_KEY = "my_template";
57 private static final String DEFAULT_DESC = "my description";
58 private static final String DEFAULT_PATTERN = "com.foo.(.*)";
59 private static final String DEFAULT_PERMISSION = UserRole.USER;
60 private static final PermissionTemplateDto DEFAULT_TEMPLATE =
61 new PermissionTemplateDto().setId(1L).setName(DEFAULT_KEY).setDescription(DEFAULT_DESC).setKeyPattern(DEFAULT_PATTERN);
64 PermissionTemplateDao permissionTemplateDao;
70 PermissionFinder finder;
73 PropertiesDao propertiesDao;
78 InternalPermissionTemplateService service;
81 public ExpectedException expected = ExpectedException.none();
85 MockUserSession.set().setLogin("admin").setGlobalPermissions(GlobalPermissions.SYSTEM_ADMIN);
87 MyBatis myBatis = mock(MyBatis.class);
88 when(myBatis.openSession(false)).thenReturn(session);
89 service = new InternalPermissionTemplateService(myBatis, permissionTemplateDao, userDao, finder);
93 public void find_users_with_permission_template() throws Exception {
94 service.findUsersWithPermissionTemplate(ImmutableMap.<String, Object>of(
96 "template", "my_template",
98 verify(finder).findUsersWithPermissionTemplate(any(PermissionQuery.class));
102 public void find_groups_with_permission_template() throws Exception {
103 service.findGroupsWithPermissionTemplate(ImmutableMap.<String, Object>of(
104 "permission", "user",
105 "template", "my_template",
108 verify(finder).findGroupsWithPermissionTemplate(any(PermissionQuery.class));
112 public void should_create_permission_template() throws Exception {
113 when(permissionTemplateDao.createPermissionTemplate(DEFAULT_KEY, DEFAULT_DESC, DEFAULT_PATTERN)).thenReturn(DEFAULT_TEMPLATE);
115 PermissionTemplate permissionTemplate = service.createPermissionTemplate(DEFAULT_KEY, DEFAULT_DESC, DEFAULT_PATTERN);
117 assertThat(permissionTemplate.getId()).isEqualTo(1L);
118 assertThat(permissionTemplate.getName()).isEqualTo(DEFAULT_KEY);
119 assertThat(permissionTemplate.getDescription()).isEqualTo(DEFAULT_DESC);
120 assertThat(permissionTemplate.getKeyPattern()).isEqualTo(DEFAULT_PATTERN);
124 public void should_enforce_unique_template_name() throws Exception {
125 expected.expect(BadRequestException.class);
126 expected.expectMessage("A template with that name already exists");
128 when(permissionTemplateDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(DEFAULT_TEMPLATE));
130 service.createPermissionTemplate(DEFAULT_KEY, DEFAULT_DESC, null);
134 public void should_reject_empty_name_on_creation() throws Exception {
135 expected.expect(BadRequestException.class);
136 expected.expectMessage("Name can't be blank");
138 service.createPermissionTemplate("", DEFAULT_DESC, null);
142 public void should_reject_invalid_key_pattern_on_creation() throws Exception {
143 expected.expect(BadRequestException.class);
144 expected.expectMessage("Invalid pattern: [azerty. Should be a valid Java regular expression.");
146 service.createPermissionTemplate(DEFAULT_KEY, DEFAULT_DESC, "[azerty");
150 public void should_delete_permission_template() throws Exception {
151 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
153 service.deletePermissionTemplate(1L);
155 verify(permissionTemplateDao, times(1)).deletePermissionTemplate(1L);
159 public void should_retrieve_permission_template() throws Exception {
161 List<PermissionTemplateUserDto> usersPermissions = Lists.newArrayList(
162 buildUserPermission("user_scan", GlobalPermissions.SCAN_EXECUTION),
163 buildUserPermission("user_dry_run", GlobalPermissions.DRY_RUN_EXECUTION),
164 buildUserPermission("user_scan_and_dry_run", GlobalPermissions.SCAN_EXECUTION),
165 buildUserPermission("user_scan_and_dry_run", GlobalPermissions.DRY_RUN_EXECUTION)
168 List<PermissionTemplateGroupDto> groupsPermissions = Lists.newArrayList(
169 buildGroupPermission("admin_group", GlobalPermissions.SYSTEM_ADMIN),
170 buildGroupPermission("scan_group", GlobalPermissions.SCAN_EXECUTION),
171 buildGroupPermission(null, GlobalPermissions.DRY_RUN_EXECUTION)
174 PermissionTemplateDto permissionTemplateDto = new PermissionTemplateDto()
176 .setName("my template")
177 .setDescription("my description")
178 .setUsersPermissions(usersPermissions)
179 .setGroupsByPermission(groupsPermissions);
181 when(permissionTemplateDao.selectPermissionTemplate("my template")).thenReturn(permissionTemplateDto);
183 PermissionTemplate permissionTemplate = service.selectPermissionTemplate("my template");
185 assertThat(permissionTemplate.getUsersForPermission(GlobalPermissions.DASHBOARD_SHARING)).isEmpty();
186 assertThat(permissionTemplate.getUsersForPermission(GlobalPermissions.SCAN_EXECUTION)).extracting("userName").containsOnly("user_scan", "user_scan_and_dry_run");
187 assertThat(permissionTemplate.getUsersForPermission(GlobalPermissions.DRY_RUN_EXECUTION)).extracting("userName").containsOnly("user_dry_run", "user_scan_and_dry_run");
188 assertThat(permissionTemplate.getGroupsForPermission(GlobalPermissions.DASHBOARD_SHARING)).isEmpty();
189 assertThat(permissionTemplate.getGroupsForPermission(GlobalPermissions.SCAN_EXECUTION)).extracting("groupName").containsOnly("scan_group");
190 assertThat(permissionTemplate.getGroupsForPermission(GlobalPermissions.SYSTEM_ADMIN)).extracting("groupName").containsOnly("admin_group");
194 public void should_retrieve_all_permission_templates() throws Exception {
195 PermissionTemplateDto template1 =
196 new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
197 PermissionTemplateDto template2 =
198 new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2");
199 when(permissionTemplateDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2));
201 List<PermissionTemplate> templates = service.selectAllPermissionTemplates();
203 assertThat(templates).hasSize(2);
204 assertThat(templates).extracting("id").containsOnly(1L, 2L);
205 assertThat(templates).extracting("name").containsOnly("template1", "template2");
206 assertThat(templates).extracting("description").containsOnly("template1", "template2");
210 public void should_retrieve_all_permission_templates_from_project() throws Exception {
211 MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, "org.sample.Sample");
213 PermissionTemplateDto template1 =
214 new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
215 PermissionTemplateDto template2 =
216 new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2");
217 when(permissionTemplateDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2));
219 List<PermissionTemplate> templates = service.selectAllPermissionTemplates("org.sample.Sample");
221 assertThat(templates).hasSize(2);
222 assertThat(templates).extracting("id").containsOnly(1L, 2L);
223 assertThat(templates).extracting("name").containsOnly("template1", "template2");
224 assertThat(templates).extracting("description").containsOnly("template1", "template2");
228 public void should_update_permission_template() throws Exception {
230 service.updatePermissionTemplate(1L, "new_name", "new_description", null);
232 verify(permissionTemplateDao).updatePermissionTemplate(1L, "new_name", "new_description", null);
236 public void should_validate_template_name_on_update_if_applicable() throws Exception {
237 expected.expect(BadRequestException.class);
238 expected.expectMessage("A template with that name already exists");
240 PermissionTemplateDto template1 =
241 new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
242 PermissionTemplateDto template2 =
243 new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2");
244 when(permissionTemplateDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2));
246 service.updatePermissionTemplate(1L, "template2", "template1", null);
250 public void should_validate_template_key_pattern_on_update_if_applicable() throws Exception {
251 expected.expect(BadRequestException.class);
252 expected.expectMessage("Invalid pattern: [azerty. Should be a valid Java regular expression.");
254 PermissionTemplateDto template1 = new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
255 when(permissionTemplateDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1));
257 service.updatePermissionTemplate(1L, "template1", "template1", "[azerty");
261 public void should_skip_name_validation_where_not_applicable() throws Exception {
262 PermissionTemplateDto template1 =
263 new PermissionTemplateDto().setId(1L).setName("template1").setDescription("template1");
264 PermissionTemplateDto template2 =
265 new PermissionTemplateDto().setId(2L).setName("template2").setDescription("template2");
266 when(permissionTemplateDao.selectAllPermissionTemplates()).thenReturn(Lists.newArrayList(template1, template2));
268 service.updatePermissionTemplate(1L, "template1", "new_description", null);
270 verify(permissionTemplateDao).updatePermissionTemplate(1L, "template1", "new_description", null);
274 public void should_add_user_permission() throws Exception {
275 UserDto userDto = new UserDto().setId(1L).setLogin("user").setName("user");
276 when(userDao.selectActiveUserByLogin("user")).thenReturn(userDto);
277 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
279 service.addUserPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "user");
281 verify(permissionTemplateDao, times(1)).addUserPermission(1L, 1L, DEFAULT_PERMISSION);
285 public void should_validate_provided_user_login() throws Exception {
286 expected.expect(BadRequestException.class);
287 expected.expectMessage("Unknown user:");
289 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
290 when(userDao.selectActiveUserByLogin("unknown")).thenReturn(null);
292 service.addUserPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "unknown");
296 public void should_remove_user_permission() throws Exception {
297 UserDto userDto = new UserDto().setId(1L).setLogin("user").setName("user");
298 when(userDao.selectActiveUserByLogin("user")).thenReturn(userDto);
299 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
301 service.removeUserPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "user");
303 verify(permissionTemplateDao, times(1)).removeUserPermission(1L, 1L, DEFAULT_PERMISSION);
307 public void should_add_group_permission() throws Exception {
308 GroupDto groupDto = new GroupDto().setId(1L).setName("group");
309 when(userDao.selectGroupByName("group")).thenReturn(groupDto);
310 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
312 service.addGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "group");
314 verify(permissionTemplateDao, times(1)).addGroupPermission(1L, 1L, DEFAULT_PERMISSION);
318 public void should_validate_provided_group_name() throws Exception {
319 expected.expect(BadRequestException.class);
320 expected.expectMessage("Unknown group:");
322 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
323 when(userDao.selectGroupByName("unknown")).thenReturn(null);
325 service.addGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "unknown");
329 public void should_remove_group_permission() throws Exception {
330 GroupDto groupDto = new GroupDto().setId(1L).setName("group");
331 when(userDao.selectGroupByName("group")).thenReturn(groupDto);
332 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
334 service.removeGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "group");
336 verify(permissionTemplateDao, times(1)).removeGroupPermission(1L, 1L, DEFAULT_PERMISSION);
340 public void should_add_permission_to_anyone_group() throws Exception {
341 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
343 service.addGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "Anyone");
345 verify(permissionTemplateDao).addGroupPermission(1L, null, DEFAULT_PERMISSION);
346 verifyZeroInteractions(userDao);
350 public void should_remove_permission_from_anyone_group() throws Exception {
351 when(permissionTemplateDao.selectTemplateByKey(DEFAULT_KEY)).thenReturn(DEFAULT_TEMPLATE);
353 service.removeGroupPermission(DEFAULT_KEY, DEFAULT_PERMISSION, "Anyone");
355 verify(permissionTemplateDao).removeGroupPermission(1L, null, DEFAULT_PERMISSION);
356 verifyZeroInteractions(userDao);
360 public void should_remove_group_from_templates() throws Exception {
361 GroupDto groupDto = new GroupDto().setId(1L).setName("group");
362 when(userDao.selectGroupByName("group", session)).thenReturn(groupDto);
364 service.removeGroupFromTemplates("group");
366 verify(permissionTemplateDao).removeByGroup(eq(1L), eq(session));
369 private PermissionTemplateUserDto buildUserPermission(String userName, String permission) {
370 return new PermissionTemplateUserDto().setUserName(userName).setPermission(permission);
373 private PermissionTemplateGroupDto buildGroupPermission(String groupName, String permission) {
374 return new PermissionTemplateGroupDto().setGroupName(groupName).setPermission(permission);