3 * Copyright (C) 2009-2023 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program 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 * This program 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.
20 package org.sonar.db.permission.template;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.stream.IntStream;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.sonar.api.utils.System2;
28 import org.sonar.api.web.UserRole;
29 import org.sonar.db.DbSession;
30 import org.sonar.db.DbTester;
31 import org.sonar.db.permission.PermissionQuery;
32 import org.sonar.db.user.GroupDto;
34 import static java.util.Arrays.asList;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.assertj.core.api.Assertions.tuple;
37 import static org.sonar.api.web.UserRole.ADMIN;
38 import static org.sonar.api.web.UserRole.USER;
39 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
40 import static org.sonar.db.permission.PermissionQuery.DEFAULT_PAGE_SIZE;
41 import static org.sonar.db.permission.PermissionQuery.builder;
42 import static org.sonar.db.user.GroupTesting.newGroupDto;
44 public class GroupWithPermissionTemplateDaoTest {
47 public DbTester db = DbTester.create(System2.INSTANCE);
49 private DbSession session = db.getSession();
50 private PermissionTemplateDbTester permissionTemplateDbTester = db.permissionTemplates();
51 private PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
54 public void select_group_names_by_query_and_template() {
55 GroupDto group1 = db.users().insertGroup("Group-1");
56 GroupDto group2 = db.users().insertGroup("Group-2");
57 GroupDto group3 = db.users().insertGroup("Group-3");
59 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
60 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName());
61 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName());
62 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName());
64 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
65 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null);
66 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName());
68 assertThat(selectGroupNamesByQueryAndTemplate(builder(), template))
69 .containsOnly("Group-1", "Group-2", "Group-3", "Anyone");
70 assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission(), template))
71 .containsOnly("Group-1", "Group-2");
72 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER), template))
73 .containsOnly("Group-1");
74 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER), anotherTemplate))
75 .containsOnly("Anyone");
76 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU"), template))
77 .containsOnly("Group-1", "Group-2", "Group-3");
78 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo"), template))
79 .containsOnly("Anyone");
80 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2"), template))
81 .containsOnly("Group-2");
83 assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), "123"))
85 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), template))
90 public void selectGroupNamesByQueryAndTemplate_is_ordering_results_by_groups_with_permission_then_by_name() {
91 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
92 GroupDto group1 = db.users().insertGroup("A");
93 GroupDto group2 = db.users().insertGroup("B");
94 GroupDto group3 = db.users().insertGroup("C");
96 permissionTemplateDbTester.addGroupToTemplate(template, group3, UserRole.USER);
98 PermissionQuery query = PermissionQuery.builder().build();
99 assertThat(underTest.selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getUuid()))
100 .containsExactly("Anyone", group3.getName(), group1.getName(), group2.getName());
104 public void selectGroupNamesByQueryAndTemplate_is_order_by_groups_with_permission_then_by_name_when_many_groups() {
105 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
106 IntStream.rangeClosed(1, DEFAULT_PAGE_SIZE + 1).forEach(i -> db.users().insertGroup("Group-" + i));
108 String lastGroupName = "Group-" + (DEFAULT_PAGE_SIZE + 1);
109 permissionTemplateDbTester.addGroupToTemplate(template, db.users().selectGroup(lastGroupName).get(), UserRole.USER);
111 PermissionQuery query = PermissionQuery.builder().build();
112 assertThat(underTest.selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getUuid()))
113 .hasSize(DEFAULT_PAGE_SIZE)
114 .startsWith("Anyone", lastGroupName, "Group-1");
118 public void selectGroupNamesByQueryAndTemplate_ignores_other_template_and_is_ordered_by_groups_with_permission_then_by_name_when_many_groups() {
119 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
120 PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate();
121 IntStream.rangeClosed(1, DEFAULT_PAGE_SIZE + 1).forEach(i -> {
122 GroupDto group = db.users().insertGroup("Group-" + i);
123 permissionTemplateDbTester.addGroupToTemplate(otherTemplate, group, UserRole.USER);
126 String lastGroupName = "Group-" + (DEFAULT_PAGE_SIZE + 1);
127 permissionTemplateDbTester.addGroupToTemplate(template, db.users().selectGroup(lastGroupName).get(), UserRole.USER);
129 PermissionQuery query = PermissionQuery.builder().build();
130 assertThat(underTest.selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getUuid()))
131 .hasSize(DEFAULT_PAGE_SIZE)
132 .startsWith("Anyone", lastGroupName, "Group-1");
136 public void select_group_names_by_query_and_template_is_paginated() {
137 IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(i + "-name"));
139 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
141 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(1).setPageSize(1), template))
142 .containsExactly("0-name");
143 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(2).setPageSize(3), template))
144 .containsExactly("3-name", "4-name", "5-name");
148 public void select_group_names_by_query_and_template_returns_anyone() {
149 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
151 GroupDto group = db.users().insertGroup("Group");
152 PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate();
153 permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getUuid(), group.getUuid(), USER, otherTemplate.getName(), group.getName());
155 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo"), template))
156 .containsExactly("Anyone");
160 public void count_group_names_by_query_and_template() {
161 GroupDto group1 = db.users().insertGroup("Group-1");
162 GroupDto group2 = db.users().insertGroup("Group-2");
163 GroupDto group3 = db.users().insertGroup("Group-3");
165 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
166 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName());
167 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName());
168 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName());
170 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
171 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null);
172 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName());
174 assertThat(countGroupNamesByQueryAndTemplate(builder(), template))
176 assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission(), template))
178 assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER), template)).isOne();
179 assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER), anotherTemplate))
181 assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU"), template))
183 assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo"), template))
185 assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2"), template))
188 assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), "123"))
190 assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), template))
195 public void select_group_permissions_by_template_id_and_group_names() {
196 GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
197 GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
198 GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
200 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
201 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName());
202 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName());
203 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName());
205 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
206 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null);
207 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName());
209 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getUuid(), asList("Group-1")))
210 .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
212 tuple(group1.getUuid(), "Group-1", USER),
213 tuple(group1.getUuid(), "Group-1", ADMIN));
215 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getUuid(), asList("Group-1")))
216 .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
218 tuple(group1.getUuid(), "Group-1", PROVISIONING));
220 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getUuid(), asList("Anyone")))
221 .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
223 tuple("Anyone", "Anyone", USER));
225 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getUuid(), asList("Group-1", "Group-2", "Anyone"))).hasSize(3);
226 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getUuid(), asList("Unknown"))).isEmpty();
227 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getUuid(), Collections.emptyList())).isEmpty();
231 public void select_group_permissions_by_template_id() {
232 GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
233 GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
234 GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
236 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
237 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), USER, template.getName(), group1.getName());
238 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group1.getUuid(), ADMIN, template.getName(), group1.getName());
239 permissionTemplateDbTester.addGroupToTemplate(template.getUuid(), group2.getUuid(), PROVISIONING, template.getName(), group2.getName());
241 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
242 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), null, USER, anotherTemplate.getName(), null);
243 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getUuid(), group1.getUuid(), PROVISIONING, anotherTemplate.getName(), group1.getName());
245 assertThat(underTest.selectGroupPermissionsByTemplateUuid(session, template.getUuid()))
246 .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
248 tuple(group1.getUuid(), "Group-1", USER),
249 tuple(group1.getUuid(), "Group-1", ADMIN),
250 tuple(group2.getUuid(), "Group-2", PROVISIONING));
251 assertThat(underTest.selectGroupPermissionsByTemplateUuid(session, anotherTemplate.getUuid()))
252 .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
254 tuple(group1.getUuid(), "Group-1", PROVISIONING),
255 tuple("Anyone", "Anyone", USER));
257 assertThat(underTest.selectGroupPermissionsByTemplateUuid(session, "321")).isEmpty();
260 private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, PermissionTemplateDto permissionTemplateDto) {
261 return selectGroupNamesByQueryAndTemplate(queryBuilder.build(), permissionTemplateDto.getUuid());
264 private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery query, String templateUuid) {
265 return underTest.selectGroupNamesByQueryAndTemplate(session, query, templateUuid);
268 private int countGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, PermissionTemplateDto permissionTemplateDto) {
269 return countGroupNamesByQueryAndTemplate(queryBuilder.build(), permissionTemplateDto.getUuid());
272 private int countGroupNamesByQueryAndTemplate(PermissionQuery query, String templateUuid) {
273 return underTest.countGroupNamesByQueryAndTemplate(session, query, templateUuid);