11package org .gitlab4j .api ;
22
3+ import static org .hamcrest .Matchers .containsInAnyOrder ;
34import static org .junit .Assert .assertEquals ;
45import static org .junit .Assert .assertFalse ;
56import static org .junit .Assert .assertNotEquals ;
67import static org .junit .Assert .assertNotNull ;
78import static org .junit .Assert .assertNull ;
9+ import static org .junit .Assert .assertThat ;
810import static org .junit .Assert .assertTrue ;
911import static org .junit .Assume .assumeNotNull ;
1012import 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