]> source.dussan.org Git - sonarqube.git/blob
3c73d60f7ea835d1f8cc5565ad4e3a1cba8a9b4c
[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 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;
32
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;
41
42 public class UserWithPermissionTemplateDaoTest {
43
44   @Rule
45   public DbTester db = DbTester.create(System2.INSTANCE);
46
47   private DbSession dbSession = db.getSession();
48
49   private PermissionTemplateDao underTest = db.getDbClient().permissionTemplateDao();
50
51   @Test
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);
65
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());
74   }
75
76   @Test
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);
83
84     assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
85       builder().setOrganizationUuid(organization.getUuid()).setPermission(USER).withAtLeastOnePermission().build(), 999L))
86         .isEmpty();
87   }
88
89   @Test
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);
103
104     assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
105       builder().setOrganizationUuid(organization.getUuid()).setPermission(USER).withAtLeastOnePermission().build(),
106       permissionTemplate.getId()))
107         .containsExactlyInAnyOrder(user1.getLogin(), user2.getLogin());
108   }
109
110   @Test
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);
119
120     assertThat(underTest.selectUserLoginsByQueryAndTemplate(dbSession,
121       builder().setOrganizationUuid(organization.getUuid()).setPermission(USER).build(), permissionTemplate.getId()))
122         .containsExactlyInAnyOrder(user.getLogin());
123   }
124
125   @Test
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);
135
136     assertThat(underTest.selectUserLoginsByQueryAndTemplate(
137       dbSession, builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setPermission(USER).setSearchQuery("SEr1").build(),
138       permissionTemplate.getId()))
139         .containsExactlyInAnyOrder(user1.getLogin());
140
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());
145   }
146
147   @Test
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);
156
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());
160   }
161
162   @Test
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);
172
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());
182   }
183
184   @Test
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);
194
195     assertThat(underTest.countUserLoginsByQueryAndTemplate(dbSession,
196       builder().setOrganizationUuid(organization.getUuid()).build(), permissionTemplate.getId()))
197         .isEqualTo(3);
198     assertThat(underTest.countUserLoginsByQueryAndTemplate(dbSession,
199       builder().setOrganizationUuid(organization.getUuid()).withAtLeastOnePermission().setPermission("user").build(), permissionTemplate.getId()))
200         .isEqualTo(2);
201   }
202
203   @Test
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);
217
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));
224
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));
232
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();
236   }
237 }