forked from gitlab4j/gitlab4j-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestGroupApi.java
328 lines (256 loc) · 11.1 KB
/
TestGroupApi.java
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
package org.gitlab4j.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupParams;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.User;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* TEST_USERNAME
* TEST_GROUP
* TEST_GROUP_MEMBER_USERNAME
*
* If any of the above are NULL, all tests in this class will be skipped.
*
*/
@Tag("integration")
@ExtendWith(SetupIntegrationTestExtension.class)
public class TestGroupApi extends AbstractIntegrationTest {
// The following needs to be set to your test repository
private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY);
private static final String TEST_GROUP_MEMBER_USERNAME = HelperUtils.getProperty(GROUP_MEMBER_USERNAME_KEY);
private static final String TEST_REQUEST_ACCESS_USERNAME = HelperUtils.getProperty(TEST_REQUEST_ACCESS_USERNAME_KEY);
private static final String AVATAR_FILENAME = "avatar.png";
private static GitLabApi gitLabApi;
private static Group testGroup;
private static User testUser;
public TestGroupApi() {
super();
}
@BeforeAll
public static void setup() {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
String problems = "";
if (gitLabApi != null) {
Optional<Group> group = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
if (group.isPresent()) {
testGroup = group.get();
} else {
problems += "Problem fetching test group\n";
}
if (TEST_GROUP_MEMBER_USERNAME != null && TEST_GROUP_MEMBER_USERNAME.length() > 0) {
try {
testUser = gitLabApi.getUserApi().getUser(TEST_GROUP_MEMBER_USERNAME);
} catch (GitLabApiException gle) {
problems += "Problem fetching test user, error=" + gle.getMessage() + "\n";
}
}
}
if (!problems.isEmpty()) {
System.err.print(problems);
}
removeGroupMembers();
}
@AfterAll
public static void teardown() {
removeGroupMembers();
}
private static void removeGroupMembers() {
if (gitLabApi != null && testGroup != null && testUser != null) {
try {
gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());
} catch (GitLabApiException ignore) {
}
}
if (TEST_REQUEST_ACCESS_USERNAME != null) {
Optional<User> user = gitLabApi.getUserApi().getOptionalUser(TEST_REQUEST_ACCESS_USERNAME);
if (user.isPresent()) {
Long userId = user.get().getId();
try {
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
} catch (Exception e) {
try {
gitLabApi.getGroupApi().removeMember(testGroup, userId);
} catch (Exception ignore) {}
}
}
}
}
@BeforeEach
public void beforeMethod() {
assumeTrue(gitLabApi != null);
assumeTrue(testGroup != null);
assumeTrue(testUser != null);
}
@Test
public void testMemberOperations() throws GitLabApiException {
// Arrange and Act
Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER);
// Assert
assertNotNull(member);
assertEquals(testUser.getId(), member.getId());
assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel());
// Act
Optional<Member> optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());
// Assert
assertTrue(optionalMember.isPresent());
// Act
List<Member> members = gitLabApi.getGroupApi().getMembers(testGroup);
// Assert
assertNotNull(members);
Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null);
assertTrue(found);
// Act
gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());
// Act
optionalMember = gitLabApi.getGroupApi().getOptionalMember(testGroup, testUser.getId());
// Assert
assertFalse(optionalMember.isPresent());
}
@Test
public void testAllMemberOperations() throws GitLabApiException {
// Arrange and Act
Member member = gitLabApi.getGroupApi().addMember(testGroup.getId(), testUser.getId(), AccessLevel.DEVELOPER);
// Assert
assertNotNull(member);
assertEquals(testUser.getId(), member.getId());
assertEquals(AccessLevel.DEVELOPER, member.getAccessLevel());
// Act
List<Member> members = gitLabApi.getGroupApi().getAllMembers(testGroup);
// Assert
assertNotNull(members);
Boolean found = (members.stream().filter(m -> m.getId().equals(member.getId())).findAny().orElse(null) != null);
assertTrue(found);
gitLabApi.getGroupApi().removeMember(testGroup.getId(), testUser.getId());
}
@Test
public void getGroup() throws GitLabApiException {
Group group = gitLabApi.getGroupApi().getGroup(TEST_GROUP);
assertNotNull(group);
}
@Test
public void getOptionalGroup() {
Optional<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
assertTrue(optional.isPresent());
assertEquals(testGroup.getId(), optional.get().getId());
optional = gitLabApi.getGroupApi().getOptionalGroup(12345L);
assertNotNull(optional);
assertFalse(optional.isPresent());
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
}
@Test
@Disabled("Required Gitlab version not less then 14.0")
public void testGetAvatar() throws GitLabApiException, IOException {
assumeTrue(testGroup != null);
File avatarFile = new File("src/test/resources/org/gitlab4j/api", AVATAR_FILENAME);
gitLabApi.getGroupApi().setGroupAvatar(testGroup.getId(), avatarFile);
// Get the avatar of the test Group
InputStream in = gitLabApi.getGroupApi().getAvatar(testGroup);
Path target = Files.createTempFile(TEST_PROJECT_NAME + "-avatar", "png");
Files.copy(in, target, StandardCopyOption.REPLACE_EXISTING);
assertTrue(target.toFile().length() > 0);
Files.delete(target);
}
@Test
public void testRequestAccess() throws GitLabApiException {
assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);
gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
final Long userId = user.getId();
try {
try {
AccessRequest accessRequest = gitLabApi.getGroupApi().requestAccess(testGroup);
assertNotNull(accessRequest);
assertEquals(userId, accessRequest.getId());
} finally {
gitLabApi.unsudo();
}
Stream<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequestsStream(testGroup);
assertTrue(requests.anyMatch(r -> r.getId() == userId));
AccessRequest accessRequest = gitLabApi.getGroupApi().approveAccessRequest(testGroup, user.getId(), AccessLevel.DEVELOPER);
assertNotNull(accessRequest);
assertEquals(user.getId(), accessRequest.getId());
assertEquals(AccessLevel.DEVELOPER, accessRequest.getAccessLevel());
user = null;
requests = gitLabApi.getGroupApi().getAccessRequestsStream(testGroup);
assertFalse(requests.anyMatch(r -> r.getId() == userId));
} finally {
try {
if (user == null) {
gitLabApi.getGroupApi().removeMember(testGroup, userId);
} else {
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
}
} catch (Exception ignore) {}
}
}
@Test
public void testDenyRequestAccess() throws GitLabApiException {
assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);
gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
final Long userId = user.getId();
try {
try {
AccessRequest accessRequest = gitLabApi.getGroupApi().requestAccess(testGroup);
assertNotNull(accessRequest);
assertEquals(userId, accessRequest.getId());
} finally {
gitLabApi.unsudo();
}
List<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
assertFalse(requests.stream().anyMatch(r -> r.getId() == userId));
user = null;
} finally {
try {
if (user != null) {
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
}
} catch (Exception ignore) {
}
}
}
@Test
public void updateGroup() throws GitLabApiException {
String description = "Test Group (" + HelperUtils.getRandomInt(1000) + ")";
GroupParams params = new GroupParams().withDescription(description);
Group updatedGroup = gitLabApi.getGroupApi().updateGroup(testGroup, params);
assertEquals(description, updatedGroup.getDescription());
Optional<Group> optional = gitLabApi.getGroupApi().getOptionalGroup(TEST_GROUP);
assertTrue(optional.isPresent());
assertEquals(testGroup.getId(), optional.get().getId());
assertEquals(description, optional.get().getDescription());
}
}