aboutsummaryrefslogtreecommitdiffstats
path: root/it/it-tests/src/test/java/util/user/UserRule.java
blob: 92443c96e275cf0c0f85546aeac2b3edfb1ea338 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
 * SonarQube
 * Copyright (C) 2009-2017 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 util.user;

import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.sonar.orchestrator.Orchestrator;
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.junit.rules.ExternalResource;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsResponse;

import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
import static util.ItUtils.newAdminWsClient;

public class UserRule extends ExternalResource implements GroupManagement {

  public static final String ADMIN_LOGIN = "admin";
  private final Orchestrator orchestrator;

  private WsClient adminWsClient;
  private final GroupManagement defaultOrganizationGroupManagement;

  private UserRule(Orchestrator orchestrator) {
    this.orchestrator = orchestrator;
    this.defaultOrganizationGroupManagement = new GroupManagementImpl(null);
  }

  public static UserRule from(Orchestrator orchestrator) {
    return new UserRule(requireNonNull(orchestrator, "Orchestrator instance cannot be null"));
  }

  // *****************
  // Users
  // *****************

  public void resetUsers() {
    for (Users.User user : getUsers().getUsers()) {
      String userLogin = user.getLogin();
      if (!userLogin.equals(ADMIN_LOGIN)) {
        deactivateUsers(userLogin);
      }
    }
  }

  public Users.User verifyUserExists(String login, String name, @Nullable String email) {
    Optional<Users.User> user = getUserByLogin(login);
    assertThat(user).as("User with login '%s' hasn't been found", login).isPresent();
    assertThat(user.get().getLogin()).isEqualTo(login);
    assertThat(user.get().getName()).isEqualTo(name);
    assertThat(user.get().getEmail()).isEqualTo(email);
    return user.get();
  }

  public void verifyUserExists(String login, String name, @Nullable String email, boolean local) {
    Users.User user = verifyUserExists(login, name, email);
    assertThat(user.isLocal()).isEqualTo(local);
  }

  public void verifyUserDoesNotExist(String login) {
    assertThat(getUserByLogin(login)).as("Unexpected user with login '%s' has been found", login).isAbsent();
  }

  public void createUser(String login, String name, @Nullable String email, String password) {
    adminWsClient().wsConnector().call(
      new PostRequest("api/users/create")
        .setParam("login", login)
        .setParam("name", name)
        .setParam("email", email)
        .setParam("password", password));
  }

  public void createUser(String login, String password) {
    createUser(login, login, null, password);
  }

  public void setRoot(String login) {
    adminWsClient().rootService().setRoot(login);
  }

  public void unsetRoot(String login) {
    adminWsClient().rootService().unsetRoot(login);
  }

  public Optional<Users.User> getUserByLogin(String login) {
    return FluentIterable.from(getUsers().getUsers()).firstMatch(new MatchUserLogin(login));
  }

  public Users getUsers() {
    WsResponse response = adminWsClient().wsConnector().call(
      new GetRequest("api/users/search"));
    assertThat(response.code()).isEqualTo(200);
    return Users.parse(response.content());
  }

  public void deactivateUsers(List<String> userLogins) {
    for (String userLogin : userLogins) {
      if (getUserByLogin(userLogin).isPresent()) {
        adminWsClient().wsConnector().call(
          new PostRequest("api/users/deactivate")
            .setParam("login", userLogin));
      }
    }
  }

  public void deactivateUsers(String... userLogins) {
    deactivateUsers(asList(userLogins));
  }

  // *****************
  // User groups
  // *****************

  public GroupManagement forOrganization(String organizationKey) {
    return new GroupManagementImpl(organizationKey);
  }

  private final class GroupManagementImpl implements GroupManagement {
    @CheckForNull
    private final String organizationKey;

    private GroupManagementImpl(@Nullable String organizationKey) {
      this.organizationKey = organizationKey;
    }

    @Override
    public void createGroup(String name) {
      createGroup(name, null);
    }

    @Override
    public void createGroup(String name, @Nullable String description) {
      PostRequest request = new PostRequest("api/user_groups/create")
          .setParam("name", name)
          .setParam("description", description);
      addOrganizationParam(request);
      adminWsClient().wsConnector().call(request);
    }

    private void addOrganizationParam(PostRequest request) {
      if (organizationKey != null) {
        request.setParam("organization", organizationKey);
      }
    }

    private void addOrganizationParam(GetRequest request) {
      if (organizationKey != null) {
        request.setParam("organization", organizationKey);
      }
    }

    @Override
    public void removeGroups(List<String> groupNames) {
      for (String groupName : groupNames) {
        if (getGroupByName(groupName).isPresent()) {
          PostRequest request = new PostRequest("api/user_groups/delete")
              .setParam("name", groupName);
          addOrganizationParam(request);
          adminWsClient().wsConnector().call(request);
        }
      }
    }

    @Override
    public void removeGroups(String... groupNames) {
      removeGroups(asList(groupNames));
    }

    @Override
    public java.util.Optional<Groups.Group> getGroupByName(String name) {
      return getGroups().getGroups().stream().filter(new MatchGroupName(name)::apply).findFirst();
    }

    @Override
    public Groups getGroups() {
      GetRequest request = new GetRequest("api/user_groups/search");
      addOrganizationParam(request);
      WsResponse response = adminWsClient().wsConnector().call(request);
      assertThat(response.code()).isEqualTo(200);
      return Groups.parse(response.content());
    }

    @Override
    public void verifyUserGroupMembership(String userLogin, String... groups) {
      Groups userGroup = getUserGroups(userLogin);
      List<String> userGroupName = FluentIterable.from(userGroup.getGroups()).transform(ToGroupName.INSTANCE).toList();
      assertThat(userGroupName).containsOnly(groups);
    }

    @Override
    public Groups getUserGroups(String userLogin) {
      GetRequest request = new GetRequest("api/users/groups")
          .setParam("login", userLogin)
          .setParam("selected", "selected");
      addOrganizationParam(request);
      WsResponse response = adminWsClient().wsConnector().call(request);
      assertThat(response.code()).isEqualTo(200);
      return Groups.parse(response.content());
    }

    @Override
    public void associateGroupsToUser(String userLogin, String... groups) {
      for (String group : groups) {
        PostRequest request = new PostRequest("api/user_groups/add_user")
            .setParam("login", userLogin)
            .setParam("name", group);
        addOrganizationParam(request);
        WsResponse response = adminWsClient().wsConnector().call(request);
        assertThat(response.code()).isEqualTo(204);
      }
    }
  }

  @Override
  public void createGroup(String name) {
    defaultOrganizationGroupManagement.createGroup(name);
  }

  @Override
  public void createGroup(String name, @Nullable String description) {
    defaultOrganizationGroupManagement.createGroup(name, description);
  }

  @Override
  public void removeGroups(List<String> groupNames) {
    defaultOrganizationGroupManagement.removeGroups(groupNames);
  }

  @Override
  public void removeGroups(String... groupNames) {
    defaultOrganizationGroupManagement.removeGroups(groupNames);
  }

  @Override
  public java.util.Optional<Groups.Group> getGroupByName(String name) {
    return defaultOrganizationGroupManagement.getGroupByName(name);
  }

  @Override
  public Groups getGroups() {
    return defaultOrganizationGroupManagement.getGroups();
  }

  @Override
  public void verifyUserGroupMembership(String userLogin, String... groups) {
    defaultOrganizationGroupManagement.verifyUserGroupMembership(userLogin, groups);
  }

  @Override
  public Groups getUserGroups(String userLogin) {
    return defaultOrganizationGroupManagement.getUserGroups(userLogin);
  }

  @Override
  public void associateGroupsToUser(String userLogin, String... groups) {
    defaultOrganizationGroupManagement.associateGroupsToUser(userLogin, groups);
  }

  private WsClient adminWsClient() {
    if (adminWsClient == null) {
      adminWsClient = newAdminWsClient(orchestrator);
    }
    return adminWsClient;
  }

  private class MatchUserLogin implements Predicate<Users.User> {
    private final String login;

    private MatchUserLogin(String login) {
      this.login = login;
    }

    @Override
    public boolean apply(@Nonnull Users.User user) {
      String login = user.getLogin();
      return login != null && login.equals(this.login) && user.isActive();
    }
  }

  private class MatchGroupName implements Predicate<Groups.Group> {
    private final String groupName;

    private MatchGroupName(String groupName) {
      this.groupName = groupName;
    }

    @Override
    public boolean apply(@Nonnull Groups.Group group) {
      String groupName = group.getName();
      return groupName != null && groupName.equals(this.groupName);
    }
  }

  private enum ToGroupName implements Function<Groups.Group, String> {
    INSTANCE;

    @Override
    public String apply(@Nonnull Groups.Group group) {
      return group.getName();
    }
  }
}