import java.time.Instant;
import java.time.temporal.ChronoUnit;
+import java.util.Collection;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
verify(userDeactivator, never()).deactivateUser(any(), eq(user.getLogin()));
}
+ @Test
+ public void fetchUser_whenUserDoesntExist_shouldThrowNotFoundException() {
+ assertThatThrownBy(() -> userService.fetchUser("login"))
+ .isInstanceOf(NotFoundException.class)
+ .hasMessage("User 'login' not found");
+ }
+
+ @Test
+ public void fetchUser_whenUserExists_shouldReturnUser() {
+ UserDto user = db.users().insertUser();
+ GroupDto group1 = db.users().insertGroup("group1");
+ GroupDto group2 = db.users().insertGroup("group2");
+ db.users().insertMember(group1, user);
+ db.users().insertMember(group2, user);
+
+ db.users().insertToken(user);
+ db.users().insertToken(user);
+
+ when(managedInstanceService.isUserManaged(any(), eq(user.getUuid()))).thenReturn(false);
+
+ UserSearchResult result = userService.fetchUser(user.getLogin());
+ UserDto resultUser = result.userDto();
+ Collection<String> resultGroups = result.groups();
+
+ assertThat(resultUser).usingRecursiveComparison().isEqualTo(user);
+ assertThat(resultGroups).containsExactlyInAnyOrder(group1.getName(), group2.getName());
+ assertThat(result.managed()).isFalse();
+ }
+
private void assertUserWithFilter(Function<UsersSearchRequest.Builder, UsersSearchRequest.Builder> query, String userLogin, boolean isExpectedToBeThere) {
UsersSearchRequest.Builder builder = getBuilderWithDefaultsPageSize();
.toList();
}
- private UserSearchResult toUserSearchResult(Collection<String> groups, int tokenCount, boolean managed, UserDto userDto) {
- return new UserSearchResult(
- userDto,
- managed,
- findAvatar(userDto),
- groups,
- tokenCount);
- }
-
private List<UserDto> findUsersAndSortByLogin(DbSession dbSession, UserQuery userQuery, int page, int pageSize) {
return dbClient.userDao().selectUsers(dbSession, userQuery, page, pageSize)
.stream()
return deactivatedUser;
}
}
+
+ public UserSearchResult fetchUser(String login) {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+ UserDto userDto = checkFound(dbClient.userDao().selectByLogin(dbSession, login), "User '%s' not found", login);
+ Collection<String> groups = dbClient.groupMembershipDao().selectGroupsByLogins(dbSession, Set.of(login)).get(login);
+ int tokenCount = dbClient.userTokenDao().selectByUser(dbSession, userDto).size();
+ boolean isManaged = managedInstanceService.isUserManaged(dbSession, userDto.getUuid());
+ return toUserSearchResult(groups, tokenCount, isManaged, userDto);
+ }
+ }
+
+ private UserSearchResult toUserSearchResult(Collection<String> groups, int tokenCount, boolean managed, UserDto userDto) {
+ return new UserSearchResult(
+ userDto,
+ managed,
+ findAvatar(userDto),
+ groups,
+ tokenCount);
+ }
}
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.v2.api.ControllerTester;
+import org.sonar.server.v2.api.response.PageRestResponse;
import org.sonar.server.v2.api.user.converter.UsersSearchRestResponseGenerator;
import org.sonar.server.v2.api.user.model.RestUser;
import org.sonar.server.v2.api.user.response.UsersSearchRestResponse;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
private final UserService userService = mock(UserService.class);
- private final MockMvc mockMvc = ControllerTester.getMockMvc(new DefaultUserController(userSession, userService, new UsersSearchRestResponseGenerator(userSession)));
+ private final UsersSearchRestResponseGenerator responseGenerator = mock(UsersSearchRestResponseGenerator.class);
+ private final MockMvc mockMvc = ControllerTester.getMockMvc(new DefaultUserController(userSession, userService, responseGenerator));
private static final Gson gson = new Gson();
when(userService.findUsers(any())).thenReturn(new SearchResults<>(List.of(), 0));
mockMvc.perform(get(USER_ENDPOINT))
- .andExpect(status().isOk())
- .andReturn();
+ .andExpect(status().isOk());
ArgumentCaptor<UsersSearchRequest> requestCaptor = ArgumentCaptor.forClass(UsersSearchRequest.class);
verify(userService).findUsers(requestCaptor.capture());
.param("sonarLintLastConnectionDateTo", "2020-01-01T00:00:00+0100")
.param("pageSize", "100")
.param("pageIndex", "2"))
- .andExpect(status().isOk())
- .andReturn();
+ .andExpect(status().isOk());
ArgumentCaptor<UsersSearchRequest> requestCaptor = ArgumentCaptor.forClass(UsersSearchRequest.class);
verify(userService).findUsers(requestCaptor.capture());
@Test
public void search_whenAdminParametersUsedButNotAdmin_shouldFail() throws Exception {
mockMvc.perform(get(USER_ENDPOINT)
- .param("sonarQubeLastConnectionDateFrom", "2020-01-01T00:00:00+0100"))
+ .param("sonarQubeLastConnectionDateFrom", "2020-01-01T00:00:00+0100"))
.andExpectAll(
status().isForbidden(),
content().string("{\"message\":\"parameter sonarQubeLastConnectionDateFrom requires Administer System permission.\"}"));
mockMvc.perform(get(USER_ENDPOINT)
- .param("sonarQubeLastConnectionDateTo", "2020-01-01T00:00:00+0100"))
+ .param("sonarQubeLastConnectionDateTo", "2020-01-01T00:00:00+0100"))
.andExpectAll(
status().isForbidden(),
content().string("{\"message\":\"parameter sonarQubeLastConnectionDateTo requires Administer System permission.\"}"));
mockMvc.perform(get(USER_ENDPOINT)
- .param("sonarLintLastConnectionDateFrom", "2020-01-01T00:00:00+0100"))
+ .param("sonarLintLastConnectionDateFrom", "2020-01-01T00:00:00+0100"))
.andExpectAll(
status().isForbidden(),
content().string("{\"message\":\"parameter sonarLintLastConnectionDateFrom requires Administer System permission.\"}"));
mockMvc.perform(get(USER_ENDPOINT)
- .param("sonarLintLastConnectionDateTo", "2020-01-01T00:00:00+0100"))
+ .param("sonarLintLastConnectionDateTo", "2020-01-01T00:00:00+0100"))
.andExpectAll(
status().isForbidden(),
content().string("{\"message\":\"parameter sonarLintLastConnectionDateTo requires Administer System permission.\"}"));
UserSearchResult user3 = generateUserSearchResult("user3", true, false, true, 1, 1);
UserSearchResult user4 = generateUserSearchResult("user4", false, true, false, 0, 0);
List<UserSearchResult> users = List.of(user1, user2, user3, user4);
- when(userService.findUsers(any())).thenReturn(new SearchResults<>(users, users.size()));
+ SearchResults<UserSearchResult> searchResult = new SearchResults<>(users, users.size());
+ when(userService.findUsers(any())).thenReturn(searchResult);
+ List<RestUser> restUsers = List.of(toRestUser(user1), toRestUser(user2), toRestUser(user3), toRestUser(user4));
+ when(responseGenerator.toUsersForResponse(eq(searchResult.searchResults()), any())).thenReturn(new UsersSearchRestResponse(restUsers, new PageRestResponse(1, 50, 4)));
userSession.logIn().setSystemAdministrator();
MvcResult mvcResult = mockMvc.perform(get(USER_ENDPOINT))
UsersSearchRestResponse actualUsersSearchRestResponse = gson.fromJson(mvcResult.getResponse().getContentAsString(), UsersSearchRestResponse.class);
assertThat(actualUsersSearchRestResponse.users())
- .containsExactlyInAnyOrder(toRestUser(user1), toRestUser(user2), toRestUser(user3), toRestUser(user4));
+ .containsExactlyElementsOf(restUsers);
assertThat(actualUsersSearchRestResponse.pageRestResponse().total()).isEqualTo(users.size());
}
formatDateTime(userSearchResult.userDto().getLastConnectionDate()),
formatDateTime(userSearchResult.userDto().getLastSonarlintConnectionDate()),
userSearchResult.groups().size(),
- userSearchResult.tokensCount()
- );
+ userSearchResult.tokensCount(),
+ userSearchResult.userDto().getSortedScmAccounts());
}
@Test
verify(userService).deactivate("userToDelete", true);
}
+
+ @Test
+ public void fetchUser_whenUserServiceThrowsNotFoundException_returnsNotFound() throws Exception {
+ when(userService.fetchUser("userLogin")).thenThrow(new NotFoundException("Not found"));
+ mockMvc.perform(get(USER_ENDPOINT + "/userLogin"))
+ .andExpectAll(
+ status().isNotFound(),
+ content().json("{\"message\":\"Not found\"}")
+ );
+
+ }
+
+ @Test
+ public void fetchUser_whenUserExists_shouldReturnUser() throws Exception {
+ UserSearchResult user = generateUserSearchResult("user1", true, true, false, 2, 3);
+ RestUser restUser = toRestUser(user);
+ when(userService.fetchUser("userLogin")).thenReturn(user);
+ when(responseGenerator.toRestUser(user)).thenReturn(restUser);
+ MvcResult mvcResult = mockMvc.perform(get(USER_ENDPOINT + "/userLogin"))
+ .andExpect(status().isOk())
+ .andReturn();
+ RestUser responseUser = gson.fromJson(mvcResult.getResponse().getContentAsString(), RestUser.class);
+ assertThat(responseUser).isEqualTo(restUser);
+
+ }
}