1414import org .gitlab4j .api .GitLabApi .ApiVersion ;
1515import org .gitlab4j .api .models .ImpersonationToken ;
1616import org .gitlab4j .api .models .ImpersonationToken .Scope ;
17+ import org .gitlab4j .api .models .SshKey ;
1718import org .gitlab4j .api .models .User ;
1819import org .gitlab4j .api .models .Version ;
1920import org .gitlab4j .api .utils .ISO8601 ;
3233 *
3334 * TEST_SUDO_AS_USERNAME
3435 *
35- * If this is null the sudo() tyests will be skipped.
36+ * If this is null the sudo() tests will be skipped.
37+ *
38+ * TEST_SSH_KEY
39+ *
40+ * If this is null the SSH key tests will be skipped.
3641 *
3742 */
3843public class TestUserApi {
@@ -42,11 +47,13 @@ public class TestUserApi {
4247 private static final String TEST_PRIVATE_TOKEN ;
4348 private static final String TEST_USERNAME ;
4449 private static final String TEST_SUDO_AS_USERNAME ;
50+ private static final String TEST_SSH_KEY ;
4551 static {
4652 TEST_HOST_URL = TestUtils .getProperty ("TEST_HOST_URL" );
4753 TEST_PRIVATE_TOKEN = TestUtils .getProperty ("TEST_PRIVATE_TOKEN" );
4854 TEST_USERNAME = TestUtils .getProperty ("TEST_USERNAME" );
4955 TEST_SUDO_AS_USERNAME = TestUtils .getProperty ("TEST_SUDO_AS_USERNAME" );
56+ TEST_SSH_KEY = TestUtils .getProperty ("TEST_SSH_KEY" );
5057 }
5158
5259 private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1" ;
@@ -75,6 +82,20 @@ public static void setup() {
7582
7683 if (problems .isEmpty ()) {
7784 gitLabApi = new GitLabApi (ApiVersion .V4 , TEST_HOST_URL , TEST_PRIVATE_TOKEN );
85+
86+ if (TEST_SSH_KEY != null ) {
87+ try {
88+ List <SshKey > sshKeys = gitLabApi .getUserApi ().getSshKeys ();
89+ if (sshKeys != null ) {
90+ for (SshKey key : sshKeys ) {
91+ if (TEST_SSH_KEY .equals (key .getKey ())) {
92+ gitLabApi .getUserApi ().deleteSshKey (key .getId ());
93+ }
94+ }
95+ }
96+ } catch (Exception ignore ) {}
97+ }
98+
7899 } else {
79100 System .err .print (problems );
80101 }
@@ -183,16 +204,32 @@ public void testDeleteImpersonationTokens() throws GitLabApiException, ParseExce
183204 User user = gitLabApi .getUserApi ().getCurrentUser ();
184205 Scope [] scopes = {Scope .API , Scope .READ_USER };
185206 Date expiresAt = ISO8601 .toDate ("2018-01-01T00:00:00Z" );
186- ImpersonationToken createdToken = gitLabApi .getUserApi ().createImpersonationToken (user .getId (), TEST_IMPERSONATION_TOKEN_NAME , expiresAt , scopes );
207+ ImpersonationToken createdToken = gitLabApi .getUserApi ().createImpersonationToken (user .getId (), TEST_IMPERSONATION_TOKEN_NAME + "a" , expiresAt , scopes );
187208 assertNotNull (createdToken );
188209
189210 ImpersonationToken token = gitLabApi .getUserApi ().getImpersonationToken (user .getId (), createdToken .getId ());
190211 assertNotNull (token );
191212 assertEquals (createdToken .getId (), token .getId ());
192- assertTrue (token .getActive ());
193213
194214 gitLabApi .getUserApi ().revokeImpersonationToken (user .getId (), createdToken .getId ());
195215 token = gitLabApi .getUserApi ().getImpersonationToken (user .getId (), createdToken .getId ());
196216 assertFalse (token .getActive ());
197217 }
218+
219+ @ Test
220+ public void testGetSshKeys () throws GitLabApiException {
221+
222+ assumeTrue (TEST_SSH_KEY != null );
223+ SshKey sshKey = gitLabApi .getUserApi ().addSshKey ("Test-Key" , TEST_SSH_KEY );
224+ assertNotNull (sshKey );
225+ assertEquals (TEST_SSH_KEY , sshKey .getKey ());
226+ gitLabApi .getUserApi ().deleteSshKey (sshKey .getId ());
227+
228+ User user = gitLabApi .getUserApi ().getCurrentUser ();
229+ sshKey = gitLabApi .getUserApi ().addSshKey (user .getId (), "Test-Key1" , TEST_SSH_KEY );
230+ assertNotNull (sshKey );
231+ assertEquals (TEST_SSH_KEY , sshKey .getKey ());
232+ assertEquals (user .getId (), sshKey .getUserId ());
233+ gitLabApi .getUserApi ().deleteSshKey (sshKey .getId ());
234+ }
198235}
0 commit comments