14
14
import org .gitlab4j .api .GitLabApi .ApiVersion ;
15
15
import org .gitlab4j .api .models .ImpersonationToken ;
16
16
import org .gitlab4j .api .models .ImpersonationToken .Scope ;
17
+ import org .gitlab4j .api .models .SshKey ;
17
18
import org .gitlab4j .api .models .User ;
18
19
import org .gitlab4j .api .models .Version ;
19
20
import org .gitlab4j .api .utils .ISO8601 ;
32
33
*
33
34
* TEST_SUDO_AS_USERNAME
34
35
*
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.
36
41
*
37
42
*/
38
43
public class TestUserApi {
@@ -42,11 +47,13 @@ public class TestUserApi {
42
47
private static final String TEST_PRIVATE_TOKEN ;
43
48
private static final String TEST_USERNAME ;
44
49
private static final String TEST_SUDO_AS_USERNAME ;
50
+ private static final String TEST_SSH_KEY ;
45
51
static {
46
52
TEST_HOST_URL = TestUtils .getProperty ("TEST_HOST_URL" );
47
53
TEST_PRIVATE_TOKEN = TestUtils .getProperty ("TEST_PRIVATE_TOKEN" );
48
54
TEST_USERNAME = TestUtils .getProperty ("TEST_USERNAME" );
49
55
TEST_SUDO_AS_USERNAME = TestUtils .getProperty ("TEST_SUDO_AS_USERNAME" );
56
+ TEST_SSH_KEY = TestUtils .getProperty ("TEST_SSH_KEY" );
50
57
}
51
58
52
59
private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1" ;
@@ -75,6 +82,20 @@ public static void setup() {
75
82
76
83
if (problems .isEmpty ()) {
77
84
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
+
78
99
} else {
79
100
System .err .print (problems );
80
101
}
@@ -183,16 +204,32 @@ public void testDeleteImpersonationTokens() throws GitLabApiException, ParseExce
183
204
User user = gitLabApi .getUserApi ().getCurrentUser ();
184
205
Scope [] scopes = {Scope .API , Scope .READ_USER };
185
206
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 );
187
208
assertNotNull (createdToken );
188
209
189
210
ImpersonationToken token = gitLabApi .getUserApi ().getImpersonationToken (user .getId (), createdToken .getId ());
190
211
assertNotNull (token );
191
212
assertEquals (createdToken .getId (), token .getId ());
192
- assertTrue (token .getActive ());
193
213
194
214
gitLabApi .getUserApi ().revokeImpersonationToken (user .getId (), createdToken .getId ());
195
215
token = gitLabApi .getUserApi ().getImpersonationToken (user .getId (), createdToken .getId ());
196
216
assertFalse (token .getActive ());
197
217
}
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
+ }
198
235
}
0 commit comments