]> source.dussan.org Git - sonarqube.git/blob
13d2f2b947df50071fec3627a0d4240df39e8c46
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.organization.OrganizationDto;
32 import org.sonar.db.permission.PermissionQuery;
33 import org.sonar.db.user.GroupDto;
34
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;
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     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");
59
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);
64
65     PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(organization);
66     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
67     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
68
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");
83
84     assertThat(selectGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(), organization, 123L))
85       .isEmpty();
86     assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("unknown"), organization, template))
87       .isEmpty();
88   }
89
90   @Test
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);
94
95     GroupDto group1 = db.users().insertGroup(organization, "A");
96     GroupDto group2 = db.users().insertGroup(organization, "B");
97     GroupDto group3 = db.users().insertGroup(organization, "C");
98
99     permissionTemplateDbTester.addGroupToTemplate(template, group3, UserRole.USER);
100
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());
104   }
105
106   @Test
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"));
110
111     PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
112
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");
117   }
118
119   @Test
120   public void select_group_names_by_query_and_template_returns_anyone() {
121     OrganizationDto organization = db.organizations().insert();
122     PermissionTemplateDto template = permissionTemplateDbTester.insertTemplate(organization);
123
124     GroupDto group = db.users().insertGroup(newGroupDto().setName("Group"));
125     PermissionTemplateDto otherTemplate = permissionTemplateDbTester.insertTemplate(organization);
126     permissionTemplateDbTester.addGroupToTemplate(otherTemplate.getId(), group.getId(), USER);
127
128     assertThat(selectGroupNamesByQueryAndTemplate(builder().setSearchQuery("nyo"), organization, template))
129       .containsExactly("Anyone");
130   }
131
132   @Test
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");
138
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);
143
144     PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate(organization);
145     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
146     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
147
148     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()), organization, template))
149       .isEqualTo(4);
150     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission(), organization, template))
151       .isEqualTo(2);
152     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setPermission(USER), organization, template)).isEqualTo(1);
153     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setPermission(USER), organization, anotherTemplate))
154       .isEqualTo(1);
155     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("groU"), organization, template))
156       .isEqualTo(3);
157     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("nYo"), organization, template))
158       .isEqualTo(1);
159     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("p-2"), organization, template))
160       .isEqualTo(1);
161
162     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().build(), organization, 123L))
163       .isZero();
164     assertThat(countGroupNamesByQueryAndTemplate(builder().setOrganizationUuid(organization.getUuid()).setSearchQuery("unknown"), organization, template))
165       .isZero();
166   }
167
168   @Test
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"));
173
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);
178
179     PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
180     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
181     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
182
183     assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, template.getId(), asList("Group-1")))
184       .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
185       .containsOnly(
186         tuple(group1.getId(), "Group-1", USER),
187         tuple(group1.getId(), "Group-1", ADMIN));
188
189     assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getId(), asList("Group-1")))
190       .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
191       .containsOnly(
192         tuple(group1.getId(), "Group-1", PROVISIONING));
193
194     assertThat(underTest.selectGroupPermissionsByTemplateIdAndGroupNames(session, anotherTemplate.getId(), asList("Anyone")))
195       .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
196       .containsOnly(
197         tuple(0, "Anyone", USER));
198
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();
202   }
203
204   @Test
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"));
209
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);
214
215     PermissionTemplateDto anotherTemplate = permissionTemplateDbTester.insertTemplate();
216     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), null, USER);
217     permissionTemplateDbTester.addGroupToTemplate(anotherTemplate.getId(), group1.getId(), PROVISIONING);
218
219     assertThat(underTest.selectGroupPermissionsByTemplateId(session, template.getId()))
220       .extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getGroupName, PermissionTemplateGroupDto::getPermission)
221       .containsOnly(
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)
227       .containsOnly(
228         tuple(group1.getId(), "Group-1", PROVISIONING),
229         tuple(0, "Anyone", USER));
230
231     assertThat(underTest.selectGroupPermissionsByTemplateId(session, 321L)).isEmpty();
232   }
233
234   private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, OrganizationDto organization, PermissionTemplateDto permissionTemplateDto) {
235     return selectGroupNamesByQueryAndTemplate(queryBuilder.setOrganizationUuid(organization.getUuid()).build(), organization, permissionTemplateDto.getId());
236   }
237
238   private List<String> selectGroupNamesByQueryAndTemplate(PermissionQuery query, OrganizationDto organization, long templateId) {
239     return underTest.selectGroupNamesByQueryAndTemplate(session, query, templateId);
240   }
241
242   private int countGroupNamesByQueryAndTemplate(PermissionQuery.Builder queryBuilder, OrganizationDto organization, PermissionTemplateDto permissionTemplateDto) {
243     return countGroupNamesByQueryAndTemplate(queryBuilder.build(), organization, permissionTemplateDto.getId());
244   }
245
246   private int countGroupNamesByQueryAndTemplate(PermissionQuery query, OrganizationDto organization, long templateId) {
247     return underTest.countGroupNamesByQueryAndTemplate(session, query, organization.getUuid(), templateId);
248   }
249
250 }