]> source.dussan.org Git - sonarqube.git/blob
8051c3c425cf942964b870d225047a6935a33764
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
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 package org.sonar.db.permission.template;
21
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;
33
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;
43
44 public class GroupWithPermissionTemplateDaoTest {
45
46   @Rule
47   public DbTester db = DbTester.create(System2.INSTANCE);
48
49   private DbSession session = db.getSession();
50   private PermissionTemplateDbTester permissionTemplateDbTester = db.permissionTemplates();
51   private PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
52
53   @Test
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");
58
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());
63
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());
67
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");
82
83     assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), "123"))
84       .isEmpty();
85     assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), template))
86       .isEmpty();
87   }
88
89   @Test
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");
95
96     permissionTemplateDbTester.addGroupToTemplate(template, group3, UserRole.USER);
97
98     PermissionQuery query = PermissionQuery.builder().build();
99     assertThat(underTest.selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getUuid()))
100       .containsExactly("Anyone", group3.getName(), group1.getName(), group2.getName());
101   }
102
103   @Test
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));
107
108     String lastGroupName = "Group-" + (DEFAULT_PAGE_SIZE + 1);
109     permissionTemplateDbTester.addGroupToTemplate(template, db.users().selectGroup(lastGroupName).get(), UserRole.USER);
110
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");
115   }
116
117   @Test
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);
124     });
125
126     String lastGroupName = "Group-" + (DEFAULT_PAGE_SIZE + 1);
127     permissionTemplateDbTester.addGroupToTemplate(template, db.users().selectGroup(lastGroupName).get(), UserRole.USER);
128
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");
133   }
134
135   @Test
136   public void select_group_names_by_query_and_template_is_paginated() {
137     IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(i + "-name"));
138
139     PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
140
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");
145   }
146
147   @Test
148   public void select_group_names_by_query_and_template_returns_anyone() {
149     PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
150
151     GroupDto group = db.users().insertGroup("Group");
152     PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate();
153     permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getUuid(), group.getUuid(), USER, otherTemplate.getName(), group.getName());
154
155     assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo"), template))
156       .containsExactly("Anyone");
157   }
158
159   @Test
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");
164
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());
169
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());
173
174     assertThat(countGroupNamesByQueryAndTemplate(builder(), template))
175       .isEqualTo(4);
176     assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission(), template))
177       .isEqualTo(2);
178     assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER), template)).isOne();
179     assertThat(countGroupNamesByQueryAndTemplate(builder().setPermission(USER), anotherTemplate))
180       .isOne();
181     assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU"), template))
182       .isEqualTo(3);
183     assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo"), template))
184       .isOne();
185     assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2"), template))
186       .isOne();
187
188     assertThat(countGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission().build(), "123"))
189       .isZero();
190     assertThat(countGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), template))
191       .isZero();
192   }
193
194   @Test
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"));
199
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());
204
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());
208
209     assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getUuid(), asList("Group-1")))
210       .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
211       .containsOnly(
212         tuple(group1.getUuid(), "Group-1", USER),
213         tuple(group1.getUuid(), "Group-1", ADMIN));
214
215     assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getUuid(), asList("Group-1")))
216       .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
217       .containsOnly(
218         tuple(group1.getUuid(), "Group-1", PROVISIONING));
219
220     assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getUuid(), asList("Anyone")))
221       .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
222       .containsOnly(
223         tuple("Anyone", "Anyone", USER));
224
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();
228   }
229
230   @Test
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"));
235
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());
240
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());
244
245     assertThat(underTest.selectGroupPermissionsByTemplateUuid(session, template.getUuid()))
246       .extracting(PermissionTemplateGroupDto::getGroupUuid, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
247       .containsOnly(
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)
253       .containsOnly(
254         tuple(group1.getUuid(), "Group-1", PROVISIONING),
255         tuple("Anyone", "Anyone", USER));
256
257     assertThat(underTest.selectGroupPermissionsByTemplateUuid(session, "321")).isEmpty();
258   }
259
260   private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, PermissionTemplateDto permissionTemplateDto) {
261     return selectGroupNamesByQueryAndTemplate(queryBuilder.build(), permissionTemplateDto.getUuid());
262   }
263
264   private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery query, String templateUuid) {
265     return underTest.selectGroupNamesByQueryAndTemplate(session, query, templateUuid);
266   }
267
268   private int countGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, PermissionTemplateDto permissionTemplateDto) {
269     return countGroupNamesByQueryAndTemplate(queryBuilder.build(), permissionTemplateDto.getUuid());
270   }
271
272   private int countGroupNamesByQueryAndTemplate(PermissionQuery query, String templateUuid) {
273     return underTest.countGroupNamesByQueryAndTemplate(session, query, templateUuid);
274   }
275
276 }