gt:     UserDto user = db.users().insertUser();
context: /*
   * SonarQube
   * Copyright (C) 2009-2019 SonarSource SA
   * mailto:info AT sonarsource DOT com
   *
   * This program is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 3 of the License, or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
   */
  package org.sonar.server.user.ws;
  
  import java.util.stream.IntStream;
  import org.junit.Rule;
  import org.junit.Test;
  import org.sonar.api.server.ws.WebService;
  import org.sonar.api.server.ws.WebService.Param;
  import org.sonar.api.utils.System2;
  import org.sonar.db.DbTester;
  import org.sonar.db.user.GroupDto;
  import org.sonar.db.user.UserDto;
  import org.sonar.server.es.EsTester;
  import org.sonar.server.issue.ws.AvatarResolverImpl;
  import org.sonar.server.tester.UserSessionRule;
  import org.sonar.server.user.index.UserIndex;
  import org.sonar.server.user.index.UserIndexer;
  import org.sonar.server.ws.WsActionTester;
  import org.sonarqube.ws.Common.Paging;
  import org.sonarqube.ws.Users.SearchWsResponse;
  import org.sonarqube.ws.Users.SearchWsResponse.User;
  
  import static java.util.Arrays.asList;
  import static java.util.Collections.emptyList;
  import static java.util.Collections.singletonList;
  import static org.assertj.core.api.Assertions.assertThat;
  import static org.assertj.core.api.Assertions.tuple;
  import static org.sonar.api.utils.DateUtils.formatDateTime;
  import static org.sonar.test.JsonAssert.assertJson;
  
  public class SearchActionTest {
  
    @Rule
    public EsTester es = EsTester.create();
  
    @Rule
    public UserSessionRule userSession = UserSessionRule.standalone();
  
    @Rule
    public DbTester db = DbTester.create();
  
    private UserIndex index = new UserIndex(es.client(), System2.INSTANCE);
    private UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client());
    private WsActionTester ws = new WsActionTester(new SearchAction(userSession, index, db.getDbClient(), new AvatarResolverImpl()));
  
    @Test
    public void search_for_all_users() {
      UserDto user1 = db.users().insertUser();
      UserDto user2 = db.users().insertUser();
      userIndexer.indexOnStartup(null);
      userSession.logIn();
  
      SearchWsResponse response = ws.newRequest()
        .executeProtobuf(SearchWsResponse.class);
  
      assertThat(response.getUsersList())
        .extracting(User::getLogin, User::getName)
        .containsExactlyInAnyOrder(
          tuple(user1.getLogin(), user1.getName()),
          tuple(user2.getLogin(), user2.getName()));
    }
  
    @Test
    public void search_with_query() {
      userSession.logIn();
      UserDto user = db.users().insertUser(u -> u
        .setLogin("user-%_%-login")
        .setName("user-name")
        .setEmail("user@mail.com")
        .setLocal(true)
        .setScmAccounts(singletonList("user1")));
      userIndexer.indexOnStartup(null);
  
      assertThat(ws.newRequest()
        .setParam("q", "user-%_%-")
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin)
          .containsExactlyInAnyOrder(user.getLogin());
      assertThat(ws.newRequest()
        .setParam("q", "user@MAIL.com")
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin)
          .containsExactlyInAnyOrder(user.getLogin());
      assertThat(ws.newRequest()
        .setParam("q", "user-name")
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin)
          .containsExactlyInAnyOrder(user.getLogin());
    }
  
    @Test
    public void return_avatar() {
      UserDto user = db.users().insertUser(u -> u.setEmail("john@doe.com"));
      userIndexer.indexOnStartup(null);
      userSession.logIn();
  
      SearchWsResponse response = ws.newRequest()
        .executeProtobuf(SearchWsResponse.class);
  
      assertThat(response.getUsersList())
        .extracting(User::getLogin, User::getAvatar)
        .containsExactlyInAnyOrder(tuple(user.getLogin(), "6a6c19fea4a3676970167ce51f39e6ee"));
    }
  
    @Test
    public void return_scm_accounts() {
      UserDto user = db.users().insertUser(u -> u.setScmAccounts(asList("john1", "john2")));
      userIndexer.indexOnStartup(null);
      userSession.logIn();
  
      SearchWsResponse response = ws.newRequest()
        .executeProtobuf(SearchWsResponse.class);
  
      assertThat(response.getUsersList())
        .extracting(User::getLogin, u -> u.getScmAccounts().getScmAccountsList())
        .containsExactlyInAnyOrder(tuple(user.getLogin(), asList("john1", "john2")));
    }
  
    @Test
    public void return_tokens_count_when_system_administer() {
      UserDto user = db.users().insertUser();
      db.users().insertToken(user);
      db.users().insertToken(user);
      userIndexer.indexOnStartup(null);
  
      userSession.logIn().setSystemAdministrator();
      assertThat(ws.newRequest()
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin, User::getTokensCount)
          .containsExactlyInAnyOrder(tuple(user.getLogin(), 2));
  
      userSession.logIn();
      assertThat(ws.newRequest()
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin, User::hasTokensCount)
          .containsExactlyInAnyOrder(tuple(user.getLogin(), false));
    }
  
    @Test
    public void return_email_only_when_system_administer() {
      UserDto user = db.users().insertUser();
      userIndexer.indexOnStartup(null);
  
      userSession.logIn().setSystemAdministrator();
      assertThat(ws.newRequest()
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin, User::getEmail)
          .containsExactlyInAnyOrder(tuple(user.getLogin(), user.getEmail()));
  
      userSession.logIn();
      assertThat(ws.newRequest()
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin, User::hasEmail)
          .containsExactlyInAnyOrder(tuple(user.getLogin(), false));
    }
  
    @Test
    public void return_user_not_having_email() {
      UserDto user = db.users().insertUser(u -> u.setEmail(null));
      userIndexer.indexOnStartup(null);
      userSession.logIn().setSystemAdministrator();
  
      SearchWsResponse response = ws.newRequest()
        .executeProtobuf(SearchWsResponse.class);
  
      assertThat(response.getUsersList())
        .extracting(User::getLogin, User::hasEmail)
        .containsExactlyInAnyOrder(tuple(user.getLogin(), false));
    }
  
    @Test
    public void return_groups_only_when_system_administer() {
      UserDto user = db.users().insertUser();
      GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group1");
      GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group2");
      GroupDto group3 = db.users().insertGroup(db.getDefaultOrganization(), "group3");
      db.users().insertMember(group1, user);
      db.users().insertMember(group2, user);
      userIndexer.indexOnStartup(null);
  
      userSession.logIn().setSystemAdministrator();
      assertThat(ws.newRequest()
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin, u -> u.getGroups().getGroupsList())
          .containsExactlyInAnyOrder(tuple(user.getLogin(), asList(group1.getName(), group2.getName())));
  
      userSession.logIn();
      assertThat(ws.newRequest()
        .executeProtobuf(SearchWsResponse.class).getUsersList())
          .extracting(User::getLogin, User::hasGroups)
          .containsExactlyInAnyOrder(tuple(user.getLogin(), false));
    }
  
    @Test
    public void return_external_information() {
      UserDto user = db.users().insertUser();
      userIndexer.indexOnStartup(null);
      userSession.logIn().setSystemAdministrator();
  
      SearchWsResponse response = ws.newRequest()
        .executeProtobuf(SearchWsResponse.class);
  
      assertThat(response.getUsersList())
        .extracting(User::getLogin, User::getExternalIdentity, User::getExternalProvider)
        .containsExactlyInAnyOrder(tuple(user.getLogin(), user.getExternalLogin(), user.getExternalIdentityProvider()));
    }
  
    @Test
    public void return_external_identity_only_when_system_administer() {
  