]> source.dussan.org Git - sonarqube.git/blob
fbbaa1d11f9bab21241b6ea6711af29500a0a006
[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.server.v2.api.user.converter;
21
22 import java.util.List;
23 import java.util.Optional;
24 import org.jetbrains.annotations.Nullable;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.sonar.api.utils.DateUtils;
31 import org.sonar.db.user.UserDto;
32 import org.sonar.server.common.PaginationInformation;
33 import org.sonar.server.common.user.service.UserInformation;
34 import org.sonar.server.user.UserSession;
35 import org.sonar.server.v2.api.response.PageRestResponse;
36 import org.sonar.server.v2.api.user.model.RestUserForAdmins;
37 import org.sonar.server.v2.api.user.model.RestUserForAnonymousUsers;
38 import org.sonar.server.v2.api.user.model.RestUserForLoggedInUsers;
39 import org.sonar.server.v2.api.user.response.UsersSearchRestResponse;
40
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45 import static org.sonar.server.common.PaginationInformation.forPageIndex;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class UsersSearchRestResponseGeneratorTest {
49
50   @Mock
51   private UserSession userSession;
52
53   @InjectMocks
54   private UsersSearchRestResponseGenerator usersSearchRestResponseGenerator;
55
56   @Test
57   public void toUsersForResponse_whenNoResults_mapsCorrectly() {
58     PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
59
60     UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(), paging);
61
62     assertThat(usersForResponse.users()).isEmpty();
63     assertPaginationInformationAreCorrect(paging, usersForResponse.page());
64   }
65
66   @Test
67   public void toUsersForResponse_whenAdmin_mapsAllFields() {
68     when(userSession.isLoggedIn()).thenReturn(true);
69     when(userSession.isSystemAdministrator()).thenReturn(true);
70
71     PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
72
73     UserInformation userInformation1 = mockSearchResult(1, true);
74     UserInformation userInformation2 = mockSearchResult(2, false);
75
76     UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(userInformation1, userInformation2), paging);
77
78     RestUserForAdmins expectUser1 = buildExpectedResponseForAdmin(userInformation1);
79     RestUserForAdmins expectUser2 = buildExpectedResponseForAdmin(userInformation2);
80     assertThat(usersForResponse.users()).containsExactly(expectUser1, expectUser2);
81     assertPaginationInformationAreCorrect(paging, usersForResponse.page());
82   }
83
84   private static RestUserForAdmins buildExpectedResponseForAdmin(UserInformation userInformation) {
85     UserDto userDto = userInformation.userDto();
86     return new RestUserForAdmins(
87       userDto.getLogin(),
88       userDto.getLogin(),
89       userDto.getName(),
90       userDto.getEmail(),
91       userDto.isActive(),
92       userDto.isLocal(),
93       userInformation.managed(),
94       userDto.getExternalLogin(),
95       userDto.getExternalIdentityProvider(),
96       userInformation.avatar().orElse(null),
97       toDateTime(userDto.getLastConnectionDate()),
98       toDateTime(userDto.getLastSonarlintConnectionDate()),
99       userInformation.userDto().getSortedScmAccounts()
100     );
101   }
102
103   @Test
104   public void toUsersForResponse_whenNonAdmin_mapsNonAdminFields() {
105     when(userSession.isLoggedIn()).thenReturn(true);
106
107     PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
108
109     UserInformation userInformation1 = mockSearchResult(1, true);
110     UserInformation userInformation2 = mockSearchResult(2, false);
111
112     UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(userInformation1, userInformation2), paging);
113
114     RestUserForLoggedInUsers expectUser1 = buildExpectedResponseForUser(userInformation1);
115     RestUserForLoggedInUsers expectUser2 = buildExpectedResponseForUser(userInformation2);
116     assertThat(usersForResponse.users()).containsExactly(expectUser1, expectUser2);
117     assertPaginationInformationAreCorrect(paging, usersForResponse.page());
118   }
119
120   private static RestUserForLoggedInUsers buildExpectedResponseForUser(UserInformation userInformation) {
121     UserDto userDto = userInformation.userDto();
122     return new RestUserForLoggedInUsers(
123       userDto.getLogin(),
124       userDto.getLogin(),
125       userDto.getName(),
126       userDto.getEmail(),
127       userDto.isActive(),
128       userDto.isLocal(),
129       userDto.getExternalIdentityProvider(),
130       userInformation.avatar().orElse(null)
131     );
132   }
133
134   @Test
135   public void toUsersForResponse_whenAnonymous_returnsOnlyNameAndLogin() {
136     PaginationInformation paging = forPageIndex(1).withPageSize(2).andTotal(3);
137
138     UserInformation userInformation1 = mockSearchResult(1, true);
139     UserInformation userInformation2 = mockSearchResult(2, false);
140
141     UsersSearchRestResponse usersForResponse = usersSearchRestResponseGenerator.toUsersForResponse(List.of(userInformation1, userInformation2), paging);
142
143     RestUserForAnonymousUsers expectUser1 = buildExpectedResponseForAnonymous(userInformation1);
144     RestUserForAnonymousUsers expectUser2 = buildExpectedResponseForAnonymous(userInformation2);
145     assertThat(usersForResponse.users()).containsExactly(expectUser1, expectUser2);
146     assertPaginationInformationAreCorrect(paging, usersForResponse.page());
147   }
148
149   private static RestUserForAnonymousUsers buildExpectedResponseForAnonymous(UserInformation userInformation) {
150     UserDto userDto = userInformation.userDto();
151     return new RestUserForAnonymousUsers(
152       userDto.getLogin(),
153       userDto.getLogin(),
154       userDto.getName()
155     );
156   }
157
158   private static String toDateTime(@Nullable Long dateTimeMs) {
159     return Optional.ofNullable(dateTimeMs).map(DateUtils::formatDateTime).orElse(null);
160   }
161
162   private static UserInformation mockSearchResult(int i, boolean booleanFlagsValue) {
163     UserInformation userInformation = mock(UserInformation.class, RETURNS_DEEP_STUBS);
164     UserDto user1 = new UserDto()
165       .setUuid("uuid_" + i)
166       .setLogin("login_" + i)
167       .setName("name_" + i)
168       .setEmail("email@" + i)
169       .setExternalId("externalId" + i)
170       .setExternalLogin("externalLogin" + 1)
171       .setExternalIdentityProvider("exernalIdp_" + i)
172       .setLastConnectionDate(100L + i)
173       .setLastSonarlintConnectionDate(200L + i)
174       .setLocal(booleanFlagsValue)
175       .setActive(booleanFlagsValue);
176
177     when(userInformation.userDto()).thenReturn(user1);
178     when(userInformation.managed()).thenReturn(booleanFlagsValue);
179     return userInformation;
180   }
181
182   private static void assertPaginationInformationAreCorrect(PaginationInformation paginationInformation, PageRestResponse pageRestResponse) {
183     assertThat(pageRestResponse.pageIndex()).isEqualTo(paginationInformation.pageIndex());
184     assertThat(pageRestResponse.pageSize()).isEqualTo(paginationInformation.pageSize());
185     assertThat(pageRestResponse.total()).isEqualTo(paginationInformation.total());
186   }
187
188 }