3 * Copyright (C) 2009-2019 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.organization.OrganizationDto;
32 import org.sonar.db.permission.PermissionQuery;
33 import org.sonar.db.user.GroupDto;
35 import static java.util.Arrays.asList;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.assertj.core.api.Assertions.tuple;
38 import static org.sonar.api.web.UserRole.ADMIN;
39 import static org.sonar.api.web.UserRole.USER;
40 import static org.sonar.core.permission.GlobalPermissions.PROVISIONING;
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 OrganizationDto organization = db.organizations().insert();
56 GroupDto group1 = db.users().insertGroup(organization, "Group-1");
57 GroupDto group2 = db.users().insertGroup(organization, "Group-2");
58 GroupDto group3 = db.users().insertGroup(organization, "Group-3");
60 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
61 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), USER);
62 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), ADMIN);
63 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), PROVISIONING);
65 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(organization);
66 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
67 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
69 assertThat(selectGroupNamesByQueryAndTemplate(builder(), organization, template))
70 .containsOnly("Group-1", "Group-2", "Group-3", "Anyone");
71 assertThat(selectGroupNamesByQueryAndTemplate(builder().withAtLeastOnePermission(), organization, template))
72 .containsOnly("Group-1", "Group-2");
73 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER), organization, template))
74 .containsOnly("Group-1");
75 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPermission(USER), organization, anotherTemplate))
76 .containsOnly("Anyone");
77 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("groU"), organization, template))
78 .containsOnly("Group-1", "Group-2", "Group-3");
79 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nYo"), organization, template))
80 .containsOnly("Anyone");
81 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("p-2"), organization, template))
82 .containsOnly("Group-2");
84 assertThat(selectGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(), organization, 123L))
86 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), organization, template))
91 public void selectGroupNamesByQueryAndTemplate_is_ordering_results_by_groups_with_permission_then_by_name() {
92 OrganizationDto organization = db.organizations().insert();
93 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
95 GroupDto group1 = db.users().insertGroup(organization, "A");
96 GroupDto group2 = db.users().insertGroup(organization, "B");
97 GroupDto group3 = db.users().insertGroup(organization, "C");
99 permissionTemplateDbTester.addGroupToTemplate(template, group3, UserRole.USER);
101 PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).build();
102 assertThat(underTest.selectGroupNamesByQueryAndTemplate(db.getSession(), query, template.getId()))
103 .containsExactly("Anyone", group3.getName(), group1.getName(), group2.getName());
107 public void select_group_names_by_query_and_template_is_paginated() {
108 OrganizationDto organization = db.organizations().insert();
109 IntStream.rangeClosed(0, 9).forEach(i -> db.users().insertGroup(organization, i + "-name"));
111 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
113 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(1).setPageSize(1), organization, template))
114 .containsExactly("0-name");
115 assertThat(selectGroupNamesByQueryAndTemplate(builder().setPageIndex(2).setPageSize(3), organization, template))
116 .containsExactly("3-name", "4-name", "5-name");
120 public void select_group_names_by_query_and_template_returns_anyone() {
121 OrganizationDto organization = db.organizations().insert();
122 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
124 GroupDto group = db.users().insertGroup(newGroupDto().setName("Group"));
125 PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate(organization);
126 permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getId(), group.getId(), USER);
128 assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo"), organization, template))
129 .containsExactly("Anyone");
133 public void count_group_names_by_query_and_template() {
134 OrganizationDto organization = db.organizations().insert();
135 GroupDto group1 = db.users().insertGroup(organization, "Group-1");
136 GroupDto group2 = db.users().insertGroup(organization, "Group-2");
137 GroupDto group3 = db.users().insertGroup(organization, "Group-3");
139 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
140 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), USER);
141 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), ADMIN);
142 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), PROVISIONING);
144 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(organization);
145 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
146 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
148 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()), organization, template))
150 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission(), organization, template))
152 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setPermission(USER), organization, template)).isEqualTo(1);
153 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setPermission(USER), organization, anotherTemplate))
155 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("groU"), organization, template))
157 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("nYo"), organization, template))
159 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("p-2"), organization, template))
162 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(), organization, 123L))
164 assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("unknown"), organization, template))
169 public void select_group_permissions_by_template_id_and_group_names() {
170 GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
171 GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
172 GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
174 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
175 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), USER);
176 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), ADMIN);
177 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), PROVISIONING);
179 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
180 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
181 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
183 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getId(), asList("Group-1")))
184 .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
186 tuple(group1.getId(), "Group-1", USER),
187 tuple(group1.getId(), "Group-1", ADMIN));
189 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getId(), asList("Group-1")))
190 .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
192 tuple(group1.getId(), "Group-1", PROVISIONING));
194 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getId(), asList("Anyone")))
195 .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
197 tuple(0, "Anyone", USER));
199 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getId(), asList("Group-1", "Group-2", "Anyone"))).hasSize(3);
200 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getId(), asList("Unknown"))).isEmpty();
201 assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getId(), Collections.emptyList())).isEmpty();
205 public void select_group_permissions_by_template_id() {
206 GroupDto group1 = db.users().insertGroup(newGroupDto().setName("Group-1"));
207 GroupDto group2 = db.users().insertGroup(newGroupDto().setName("Group-2"));
208 GroupDto group3 = db.users().insertGroup(newGroupDto().setName("Group-3"));
210 PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate();
211 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), USER);
212 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group1.getId(), ADMIN);
213 permissionTemplateDbTester.addGroupToTemplate(template.getId(), group2.getId(), PROVISIONING);
215 PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
216 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
217 permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
219 assertThat(underTest.selectGroupPermissionsByTemplateId(session, template.getId()))
220 .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
222 tuple(group1.getId(), "Group-1", USER),
223 tuple(group1.getId(), "Group-1", ADMIN),
224 tuple(group2.getId(), "Group-2", PROVISIONING));
225 assertThat(underTest.selectGroupPermissionsByTemplateId(session, anotherTemplate.getId()))
226 .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
228 tuple(group1.getId(), "Group-1", PROVISIONING),
229 tuple(0, "Anyone", USER));
231 assertThat(underTest.selectGroupPermissionsByTemplateId(session, 321L)).isEmpty();
234 private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, OrganizationDto organization, PermissionTemplateDto permissionTemplateDto) {
235 return selectGroupNamesByQueryAndTemplate(queryBuilder.setOrganizationUuid(organization.getUuid()).build(), organization, permissionTemplateDto.getId());
238 private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery query, OrganizationDto organization, long templateId) {
239 return underTest.selectGroupNamesByQueryAndTemplate(session, query, templateId);
242 private int countGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, OrganizationDto organization, PermissionTemplateDto permissionTemplateDto) {
243 return countGroupNamesByQueryAndTemplate(queryBuilder.build(), organization, permissionTemplateDto.getId());
246 private int countGroupNamesByQueryAndTemplate(PermissionQuery query, OrganizationDto organization, long templateId) {
247 return underTest.countGroupNamesByQueryAndTemplate(session, query, organization.getUuid(), templateId);