|
1 | 1 | package org.gitlab4j.api; |
2 | 2 |
|
3 | 3 | import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
4 | 5 | import static org.junit.Assert.assertNotNull; |
5 | 6 | import static org.junit.Assert.assertNull; |
| 7 | +import static org.junit.Assert.assertTrue; |
6 | 8 | import static org.junit.Assume.assumeTrue; |
7 | 9 |
|
| 10 | +import java.text.ParseException; |
| 11 | +import java.util.Date; |
| 12 | +import java.util.List; |
| 13 | + |
8 | 14 | import org.gitlab4j.api.GitLabApi.ApiVersion; |
| 15 | +import org.gitlab4j.api.models.ImpersonationToken; |
| 16 | +import org.gitlab4j.api.models.ImpersonationToken.Scope; |
9 | 17 | import org.gitlab4j.api.models.User; |
10 | 18 | import org.gitlab4j.api.models.Version; |
| 19 | +import org.gitlab4j.api.utils.ISO8601; |
11 | 20 | import org.junit.Before; |
12 | 21 | import org.junit.BeforeClass; |
13 | 22 | import org.junit.Test; |
@@ -40,6 +49,8 @@ public class TestUserApi { |
40 | 49 | TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME"); |
41 | 50 | } |
42 | 51 |
|
| 52 | + private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1"; |
| 53 | + |
43 | 54 | private static GitLabApi gitLabApi; |
44 | 55 |
|
45 | 56 | public TestUserApi() { |
@@ -128,4 +139,60 @@ public void testSudoAsUser() throws GitLabApiException { |
128 | 139 | gitLabApi.unsudo(); |
129 | 140 | } |
130 | 141 | } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void testCreateImpersonationToken() throws GitLabApiException, ParseException { |
| 145 | + |
| 146 | + User user = gitLabApi.getUserApi().getCurrentUser(); |
| 147 | + Scope[] scopes = {Scope.API, Scope.READ_USER}; |
| 148 | + Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z"); |
| 149 | + ImpersonationToken token = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes); |
| 150 | + assertNotNull(token); |
| 151 | + assertNotNull(token.getId()); |
| 152 | + assertEquals(TEST_IMPERSONATION_TOKEN_NAME, token.getName()); |
| 153 | + assertEquals(2, token.getScopes().size()); |
| 154 | + |
| 155 | + gitLabApi.getUserApi().deleteImpersonationToken(user.getId(), token.getId()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testGetImpersonationTokens() throws GitLabApiException, ParseException { |
| 160 | + |
| 161 | + User user = gitLabApi.getUserApi().getCurrentUser(); |
| 162 | + Scope[] scopes = {Scope.API, Scope.READ_USER}; |
| 163 | + Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z"); |
| 164 | + ImpersonationToken createdToken = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes); |
| 165 | + assertNotNull(createdToken); |
| 166 | + |
| 167 | + ImpersonationToken token = gitLabApi.getUserApi().getImpersonationToken(user.getId(), createdToken.getId()); |
| 168 | + assertNotNull(token); |
| 169 | + assertEquals(createdToken.getId(), token.getId()); |
| 170 | + assertEquals(TEST_IMPERSONATION_TOKEN_NAME, token.getName()); |
| 171 | + assertEquals(createdToken.getExpiresAt(), token.getExpiresAt()); |
| 172 | + |
| 173 | + List<ImpersonationToken> tokens = gitLabApi.getUserApi().getImpersonationTokens(user.getId()); |
| 174 | + assertNotNull(tokens); |
| 175 | + assertTrue(tokens.size() > 0); |
| 176 | + |
| 177 | + gitLabApi.getUserApi().deleteImpersonationToken(user.getId(), createdToken.getId()); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testDeleteImpersonationTokens() throws GitLabApiException, ParseException { |
| 182 | + |
| 183 | + User user = gitLabApi.getUserApi().getCurrentUser(); |
| 184 | + Scope[] scopes = {Scope.API, Scope.READ_USER}; |
| 185 | + Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z"); |
| 186 | + ImpersonationToken createdToken = gitLabApi.getUserApi().createImpersonationToken(user.getId(), TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes); |
| 187 | + assertNotNull(createdToken); |
| 188 | + |
| 189 | + ImpersonationToken token = gitLabApi.getUserApi().getImpersonationToken(user.getId(), createdToken.getId()); |
| 190 | + assertNotNull(token); |
| 191 | + assertEquals(createdToken.getId(), token.getId()); |
| 192 | + assertTrue(token.getActive()); |
| 193 | + |
| 194 | + gitLabApi.getUserApi().deleteImpersonationToken(user.getId(), createdToken.getId()); |
| 195 | + token = gitLabApi.getUserApi().getImpersonationToken(user.getId(), createdToken.getId()); |
| 196 | + assertFalse(token.getActive()); |
| 197 | + } |
131 | 198 | } |
0 commit comments