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 org.junit.Rule;
24 import org.junit.Test;
25 import org.sonar.api.utils.System2;
26 import org.sonar.api.web.UserRole;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.organization.OrganizationDto;
30 import org.sonar.db.permission.PermissionQuery;
31 import org.sonar.db.user.UserDto;
33 import static java.util.Arrays.asList;
34 import static java.util.Collections.singletonList;
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.CODEVIEWER;
39 import static org.sonar.api.web.UserRole.USER;
40 import static org.sonar.db.permission.PermissionQuery.builder;
42 public class UserWithPermissionTemplateDaoTest {
45 public DbTester db = DbTester.create(System2.INSTANCE);
47 private DbSession dbSession = db.getSession();
49 private PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
52 public void select_logins() {
53 OrganizationDto organization = db.organizations().insert();
54 UserDto user1 = db.users().insertUser();
55 UserDto user2 = db.users().insertUser();
56 UserDto user3 = db.users().insertUser();
57 db.organizations().addMember(organization, user1, user2, user3);
58 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
59 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, USER);
60 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, ADMIN);
61 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, CODEVIEWER);
62 db.permissionTemplates().addUserToTemplate(permissionTemplate, user2, USER);
63 PermissionTemplateDto anotherPermissionTemplate = db.permissionTemplates().insertTemplate();
64 db.permissionTemplates().addUserToTemplate(anotherPermissionTemplate, user1, USER);
66 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
67 builder().setOrganizationUuid(organization.getUuid()).build(),
68 permissionTemplate.getId()))
69 .containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin(), user3.getLogin());
70 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
71 builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setPermission(USER).build(),
72 permissionTemplate.getId()))
73 .containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin());
77 public void return_no_logins_on_unknown_template_key() {
78 OrganizationDto organization = db.organizations().insert();
79 UserDto user = db.users().insertUser();
80 db.organizations().addMember(organization, user);
81 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
82 db.permissionTemplates().addUserToTemplate(permissionTemplate, user, USER);
84 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
85 builder().setOrganizationUuid(organization.getUuid()).setPermission(USER).withAtLeastOnePermission().build(), 999L))
90 public void select_only_logins_with_permission() {
91 OrganizationDto organization = db.organizations().insert();
92 UserDto user1 = db.users().insertUser();
93 UserDto user2 = db.users().insertUser();
94 UserDto user3 = db.users().insertUser();
95 db.organizations().addMember(organization, user1, user2, user3);
96 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
97 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, USER);
98 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, ADMIN);
99 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, CODEVIEWER);
100 db.permissionTemplates().addUserToTemplate(permissionTemplate, user2, USER);
101 PermissionTemplateDto anotherPermissionTemplate = db.permissionTemplates().insertTemplate();
102 db.permissionTemplates().addUserToTemplate(anotherPermissionTemplate, user1, USER);
104 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
105 builder().setOrganizationUuid(organization.getUuid()).setPermission(USER).withAtLeastOnePermission().build(),
106 permissionTemplate.getId()))
107 .containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin());
111 public void select_only_enable_users() {
112 OrganizationDto organization = db.organizations().insert();
113 UserDto user = db.users().insertUser();
114 UserDto disabledUser = db.users().insertUser(u -> u.setActive(false));
115 db.organizations().addMember(organization, user, disabledUser);
116 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
117 db.permissionTemplates().addUserToTemplate(permissionTemplate, user, USER);
118 db.permissionTemplates().addUserToTemplate(permissionTemplate, disabledUser, USER);
120 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
121 builder().setOrganizationUuid(organization.getUuid()).setPermission(USER).build(), permissionTemplate.getId()))
122 .containsExactlyInAnyOrder(user.getLogin());
126 public void search_by_user_name() {
127 OrganizationDto organization = db.organizations().insert();
128 UserDto user1 = db.users().insertUser(u -> u.setName("User1"));
129 UserDto user2 = db.users().insertUser(u -> u.setName("User2"));
130 UserDto user3 = db.users().insertUser(u -> u.setName("User3"));
131 db.organizations().addMember(organization, user1, user2, user3);
132 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
133 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, USER);
134 db.permissionTemplates().addUserToTemplate(permissionTemplate, user2, USER);
136 assertThat(underTest.selectUserLoginsByQueryAndTemplate(
137 dbSession, builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setPermission(USER).setSearchQuery("SEr1").build(),
138 permissionTemplate.getId()))
139 .containsExactlyInAnyOrder(user1.getLogin());
141 assertThat(underTest.selectUserLoginsByQueryAndTemplate(
142 dbSession, builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setPermission(USER).setSearchQuery("user").build(),
143 permissionTemplate.getId()))
144 .containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin());
148 public void selectUserLoginsByQueryAndTemplate_is_ordering_result_by_users_with_permissions_then_by_name() {
149 OrganizationDto organization = db.organizations().insert();
150 PermissionTemplateDto template = db.permissionTemplates().insertTemplate(organization);
151 UserDto user1 = db.users().insertUser(u -> u.setName("A"));
152 UserDto user2 = db.users().insertUser(u -> u.setName("B"));
153 UserDto user3 = db.users().insertUser(u -> u.setName("C"));
154 db.organizations().addMember(organization, user1, user2, user3);
155 db.permissionTemplates().addUserToTemplate(template.getId(), user3.getId(), UserRole.USER);
157 PermissionQuery query = PermissionQuery.builder().setOrganizationUuid(organization.getUuid()).build();
158 assertThat(underTest.selectUserLoginsByQueryAndTemplate(db.getSession(), query, template.getId()))
159 .containsExactly(user3.getLogin(), user1.getLogin(), user2.getLogin());
163 public void should_be_paginated() {
164 OrganizationDto organization = db.organizations().insert();
165 UserDto user1 = db.users().insertUser(u -> u.setName("User1"));
166 UserDto user2 = db.users().insertUser(u -> u.setName("User2"));
167 UserDto user3 = db.users().insertUser(u -> u.setName("User3"));
168 db.organizations().addMember(organization, user1, user2, user3);
169 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
170 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, USER);
171 db.permissionTemplates().addUserToTemplate(permissionTemplate, user2, USER);
173 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
174 builder().setOrganizationUuid(organization.getUuid()).setPageIndex(1).setPageSize(2).build(), permissionTemplate.getId()))
175 .containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin());
176 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
177 builder().setOrganizationUuid(organization.getUuid()).setPageIndex(2).setPageSize(2).build(), permissionTemplate.getId()))
178 .containsExactlyInAnyOrder(user3.getLogin());
179 assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
180 builder().setOrganizationUuid(organization.getUuid()).setPageIndex(3).setPageSize(1).build(), permissionTemplate.getId()))
181 .containsExactlyInAnyOrder(user3.getLogin());
185 public void count_users() {
186 OrganizationDto organization = db.organizations().insert();
187 UserDto user1 = db.users().insertUser();
188 UserDto user2 = db.users().insertUser();
189 UserDto user3 = db.users().insertUser();
190 db.organizations().addMember(organization, user1, user2, user3);
191 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
192 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, USER);
193 db.permissionTemplates().addUserToTemplate(permissionTemplate, user2, USER);
195 assertThat(underTest.countUserLoginsByQueryAndTemplate(dbSession,
196 builder().setOrganizationUuid(organization.getUuid()).build(), permissionTemplate.getId()))
198 assertThat(underTest.countUserLoginsByQueryAndTemplate(dbSession,
199 builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setPermission("user").build(), permissionTemplate.getId()))
204 public void select_user_permission_templates_by_template_and_logins() {
205 OrganizationDto organization = db.organizations().insert();
206 UserDto user1 = db.users().insertUser();
207 UserDto user2 = db.users().insertUser();
208 UserDto user3 = db.users().insertUser();
209 db.organizations().addMember(organization, user1, user2, user3);
210 PermissionTemplateDto permissionTemplate = db.permissionTemplates().insertTemplate();
211 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, USER);
212 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, ADMIN);
213 db.permissionTemplates().addUserToTemplate(permissionTemplate, user1, CODEVIEWER);
214 db.permissionTemplates().addUserToTemplate(permissionTemplate, user2, USER);
215 PermissionTemplateDto anotherPermissionTemplate = db.permissionTemplates().insertTemplate();
216 db.permissionTemplates().addUserToTemplate(anotherPermissionTemplate, user1, USER);
218 assertThat(underTest.selectUserPermissionsByTemplateIdAndUserLogins(dbSession, permissionTemplate.getId(), singletonList(user1.getLogin())))
219 .extracting(PermissionTemplateUserDto::getUserLogin, PermissionTemplateUserDto::getPermission)
220 .containsExactlyInAnyOrder(
221 tuple(user1.getLogin(), USER),
222 tuple(user1.getLogin(), ADMIN),
223 tuple(user1.getLogin(), CODEVIEWER));
225 assertThat(underTest.selectUserPermissionsByTemplateIdAndUserLogins(dbSession, permissionTemplate.getId(), asList(user1.getLogin(), user2.getLogin(), user2.getLogin())))
226 .extracting(PermissionTemplateUserDto::getUserLogin, PermissionTemplateUserDto::getPermission)
227 .containsExactlyInAnyOrder(
228 tuple(user1.getLogin(), USER),
229 tuple(user1.getLogin(), ADMIN),
230 tuple(user1.getLogin(), CODEVIEWER),
231 tuple(user2.getLogin(), USER));
233 assertThat(underTest.selectUserPermissionsByTemplateIdAndUserLogins(dbSession, permissionTemplate.getId(), singletonList("unknown"))).isEmpty();
234 assertThat(underTest.selectUserPermissionsByTemplateIdAndUserLogins(dbSession, permissionTemplate.getId(), Collections.emptyList())).isEmpty();
235 assertThat(underTest.selectUserPermissionsByTemplateIdAndUserLogins(dbSession, 123L, singletonList(user1.getLogin()))).isEmpty();