};
handleGetUsers: typeof getUsers<RestUserDetailed> = (data) => {
- let pageRestResponse = {
+ let page = {
pageIndex: 1,
pageSize: 0,
total: 10,
};
- if (data.pageIndex !== undefined && data.pageIndex !== pageRestResponse.pageIndex) {
- pageRestResponse = { pageIndex: 2, pageSize: 2, total: 10 };
+ if (data.pageIndex !== undefined && data.pageIndex !== page.pageIndex) {
+ page = { pageIndex: 2, pageSize: 2, total: 10 };
const users = [
mockRestUser({
name: `Local User ${this.users.length + 4}`,
}),
];
- return this.reply({ pageRestResponse, users });
+ return this.reply({ page, users });
}
const users = this.getFilteredRestUsers({
});
return this.reply({
- pageRestResponse: {
+ page: {
pageIndex: 1,
pageSize: users.length,
total: 10,
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.OK)
@Operation(summary = "Users search", description = """
- Get a list of users. By default, only active users are returned.<br>
+ Get a list of users. By default, only active users are returned.
The following fields are only returned when user has Administer System permission or for logged-in in user :
- 'email'
- 'externalIdentity'
- 'externalProvider'
- 'groups'
- 'lastConnectionDate'
- 'sonarLintLastConnectionDate'
- 'tokensCount'<br>
+ 'email',
+ 'externalIdentity',
+ 'externalProvider',
+ 'groups',
+ 'lastConnectionDate',
+ 'sonarLintLastConnectionDate',
+ 'tokensCount'.
Field 'sonarqubeLastConnectionDate' is only updated every hour, so it may not be accurate, for instance when a user authenticates many times in less than one hour.
""")
UsersSearchRestResponse search(
package org.sonar.server.v2.api.user.controller;
import com.google.gson.Gson;
+import com.google.gson.GsonBuilder;
+import com.google.gson.TypeAdapter;
+import com.google.gson.stream.JsonReader;
+import com.google.gson.stream.JsonWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
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();
+ private static final Gson gson = new GsonBuilder().registerTypeAdapter(RestUser.class, new RestUserDeserializer()).create();
@Test
public void search_whenNoParameters_shouldUseDefaultAndForwardToUserService() throws Exception {
@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.\"}"));
}
+ static class RestUserDeserializer extends TypeAdapter<RestUser> {
+
+ @Override
+ public void write(JsonWriter out, RestUser value) {
+ throw new IllegalStateException("not implemented");
+ }
+
+ @Override
+ public RestUser read(JsonReader reader) {
+ return gson.fromJson(reader, RestUserForAdmins.class);
+ }
+ }
+
private UserSearchResult generateUserSearchResult(String id, boolean active, boolean local, boolean managed, int groupsCount, int tokensCount) {
UserDto userDto = new UserDto()
.setLogin("login_" + id)
userSession.logIn().setNonSystemAdministrator();
mockMvc.perform(
- post(USER_ENDPOINT)
- .contentType(MediaType.APPLICATION_JSON_VALUE)
- .content(gson.toJson(new UserCreateRestRequest(null, null, "login", "name", null, null))))
+ post(USER_ENDPOINT)
+ .contentType(MediaType.APPLICATION_JSON_VALUE)
+ .content(gson.toJson(new UserCreateRestRequest(null, null, "login", "name", null, null))))
.andExpectAll(
status().isForbidden(),
content().json("{\"message\":\"Insufficient privileges\"}"));