Skip to content

Commit 970a369

Browse files
committed
Added additional scopes in testCreateImpersonationToken() (#374).
1 parent a26690f commit 970a369

File tree

1 file changed

+42
-16
lines changed

1 file changed

+42
-16
lines changed

src/test/java/org/gitlab4j/api/TestUserApi.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package org.gitlab4j.api;
22

3+
import static org.hamcrest.Matchers.containsInAnyOrder;
34
import static org.junit.Assert.assertEquals;
45
import static org.junit.Assert.assertFalse;
56
import static org.junit.Assert.assertNotEquals;
67
import static org.junit.Assert.assertNotNull;
78
import static org.junit.Assert.assertNull;
9+
import static org.junit.Assert.assertThat;
810
import static org.junit.Assert.assertTrue;
911
import static org.junit.Assume.assumeNotNull;
1012
import static org.junit.Assume.assumeTrue;
@@ -214,15 +216,29 @@ public void testSudoAsUser() throws GitLabApiException {
214216
public void testCreateImpersonationToken() throws GitLabApiException, ParseException {
215217

216218
User user = gitLabApi.getUserApi().getCurrentUser();
217-
Scope[] scopes = {Scope.API, Scope.READ_USER, Scope.READ_REGISTRY, Scope.WRITE_REPOSITORY, Scope.SUDO};
219+
220+
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
221+
// registry configured and the test would thus fail.
222+
Scope[] scopes = {Scope.API, Scope.READ_USER, Scope.READ_REPOSITORY, Scope.WRITE_REPOSITORY, Scope.SUDO};
218223
Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z");
219-
ImpersonationToken token = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes);
220-
assertNotNull(token);
221-
assertNotNull(token.getId());
222-
assertEquals(TEST_IMPERSONATION_TOKEN_NAME, token.getName());
223-
assertEquals(5, token.getScopes().size());
224224

225-
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
225+
ImpersonationToken token = null;
226+
try {
227+
228+
token = gitLabApi.getUserApi().createImpersonationToken(user, TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes);
229+
230+
assertNotNull(token);
231+
assertNotNull(token.getId());
232+
assertEquals(TEST_IMPERSONATION_TOKEN_NAME, token.getName());
233+
assertEquals(scopes.length, token.getScopes().size());
234+
assertEquals(expiresAt.getTime(), token.getExpiresAt().getTime());
235+
assertThat(token.getScopes(), containsInAnyOrder(scopes));
236+
237+
} finally {
238+
if (user != null && token != null) {
239+
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
240+
}
241+
}
226242
}
227243

228244
@Test
@@ -231,17 +247,27 @@ public void testGetOptionalImpersonationToken() throws GitLabApiException, Parse
231247
User user = gitLabApi.getUserApi().getCurrentUser();
232248
Scope[] scopes = {Scope.API, Scope.READ_USER};
233249
Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z");
234-
ImpersonationToken token = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes);
235-
assertNotNull(token);
236250

237-
Optional<ImpersonationToken> optional = gitLabApi.getUserApi().getOptionalImpersonationToken(user.getId(), token.getId());
238-
assertTrue(optional.isPresent());
239-
assertEquals(token.getId(), optional.get().getId());
240-
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
251+
ImpersonationToken token = null;
252+
try {
241253

242-
optional = gitLabApi.getUserApi().getOptionalImpersonationToken(user.getId(), 123456);
243-
assertNotNull(optional);
244-
assertFalse(optional.isPresent());
254+
token = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes);
255+
assertNotNull(token);
256+
257+
Optional<ImpersonationToken> optional = gitLabApi.getUserApi().getOptionalImpersonationToken(user.getId(), token.getId());
258+
assertTrue(optional.isPresent());
259+
assertEquals(token.getId(), optional.get().getId());
260+
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
261+
262+
optional = gitLabApi.getUserApi().getOptionalImpersonationToken(user.getId(), 123456);
263+
assertNotNull(optional);
264+
assertFalse(optional.isPresent());
265+
266+
} finally {
267+
if (user != null && token != null) {
268+
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
269+
}
270+
}
245271
}
246272

247273
@Test

0 commit comments

Comments
 (0)