An error occurred while loading the file. Please try again.
An error occurred while loading the file. Please try again.
An error occurred while loading the file. Please try again.
-
Gautier de Saint Martin Lacaze authoredUnverified48804220
package org.gitlab4j.api;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Email;
import org.gitlab4j.api.models.GpgKey;
import org.gitlab4j.api.models.ImpersonationToken;
import org.gitlab4j.api.models.ImpersonationToken.Scope;
import org.gitlab4j.api.models.Membership;
import org.gitlab4j.api.models.MembershipSourceType;
import org.gitlab4j.api.models.SshKey;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Version;
import org.gitlab4j.api.utils.ISO8601;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
/**
* In order for these tests to run you must set the following properties in test-gitlab4j.properties
*
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* TEST_USERNAME
*
* If any of the above are NULL, all tests in this class will be skipped.
*
* TEST_SUDO_AS_USERNAME
*
* If this is null the sudo() tests will be skipped.
*
* TEST_SSH_KEY
*
* If this is null the SSH key tests will be skipped.
*
*/
@Category(IntegrationTest.class)
public class TestUserApi extends AbstractIntegrationTest {
// The following needs to be set to your test repository
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
private static final String TEST_BLOCK_USERNAME = HelperUtils.getProperty(BLOCK_USERNAME_KEY);
private static final String TEST_SUDO_AS_USERNAME = HelperUtils.getProperty(SUDO_AS_USERNAME_KEY);
private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1";
private static final String TEST_SSH_KEY =
"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC3rWzl/oPAD+Em2iGTmR81HcYZsopvnKp7jelI4XS91fT1NjCRrGsxf5Mw/" +
"KnmtBjhk+kQjkhIrnsBDcs6DZWtNcHJtyWJZrYsfxMTqWCaQv+OTRwVboqS2pmPcbK3gizUd5GCLFTKbg4OMpdywTwi6NAPwQ" +
"rtn3xwiVnGGCfBSyRFppcYP81otALctrlAW57V5+bQwFIJteJ+NWe1UmPxrqQ0N/a+dEEoJHzwX8RtVSkULafrRw8avn6Zp2x" +
"1OlD2aIEMQWvepNTRW6UDMSmWFc61ycy1pF5sCT5rij+b/fN4qCEvQs6R7GmCzaaZzbWuAqaxLRdITm/WUxdG6rjh";
private static final String TEST_USER_EMAIL = "test-user-email123@gitlab4j.org";
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
// Key for fake@fake.com - set to never expire
private static final String TEST_GPG_KEY = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n\n" +
"mQINBGBHltIBEADW8zSGAs/XEkwWI36xOusuOqSINhTDVuqq3n50Oazb+a9Ai/MM\n" +
"8GTm900ZZghGBVUXGm8PkWuSmabcpbDbjYmJx8aIY71UipXxXrfBzr8S2yOx7IjU\n" +
"m6AEw0HwNUF6ayBwsklUFFWMyCeCVSCeZNldFwKRQApP6YOlTlwmZFESv32J6AHz\n" +
"goeEsIcoea484nVJKOl7unneb8TyuF6kmViyZtiDkjeiH5vNy8XjSWH9xl5tcBxy\n" +
"70NxkZt9EKnMq8izy51OBdzA+oWByGIGRjRPrW+5niMCGltV0w12M0uMDa2pJU2B\n" +
"Z0U7uL/Lj3srMnD54OjbjK++wtYbshhGKXhAzshk9RgZq5fEN7Jjn1CTvue5EcHz\n" +
"D27RD4yy35MledJ0hrvcTVVxvFmTg3TfDFdQBVLHjRATdXo7xT1Wg35M3z3aVSRt\n" +
"PoynOxGN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UjkUKI/wi4rl0t7ELQKhlSlUC11f\n" +
"Nz0C6Q+9cwRQCCT4sX/ZkzVQbGWx9fkAYVHzezDqh43xorXJ2ix5y63pr1NGuUgx\n" +
"EujMlUbXFzXpUrCmdUVWujlp4gSEfd6tLW0WMw0tYJe6UY7xx4EmWsT+kAGj7QLH\n" +
"L06yFigDQ0eUkGQ1T7Z0AjG4EXGETbX6lSLwzIBnmaZXQxdx4LiRy9TcaNtcowH4\n" +
"U2yxCoG0o0kS7sS/rI77TV6WZ46DPCJmlNJ+MP8lt0j/nsDA3AECB1AA+8SNepbA\n" +
"LSZY7MJmh4nsqJ+iy/XMosipluZx2u6ZwlXAHxAzHhs7FBsvdMtq/gLNAlZzVyeH\n" +
"UqzRaMJps7xIbap5d5jZT5jaZwFeGi+63YVRx3Jm6dkiBCPFffLyWdrzkFTZdWqZ\n" +
"PkiRbJ64wYPIWQgAN/RhmCcRBhxJE8f7kgo/nBkZ5dwmfXgnXpheEaaCSzvJ4nMh\n" +
"TUdg6ZLna12QndjI5gy5aenrr5H/HmDKKSNkuWZv0+NS4GhwnL8NFs+MRk6znpLN\n" +
"aEjPdfYxINCMz+uotKJV9NieDWIbEJLlfZUf2hJwuwwjQGAyVf7b\n" +
"=ryCD\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
private static final String TEST_EXTERNAL_USERNAME = HelperUtils.getProperty(EXTERNAL_USERNAME_KEY);
private static final String TEST_EXTERNAL_PROVIDER = HelperUtils.getProperty(EXTERNAL_PROVIDER_KEY);
private static final String TEST_EXTERNAL_UID = HelperUtils.getProperty(EXTERNAL_UID_KEY);
private static GitLabApi gitLabApi;
private static User blockUser;
public TestUserApi() {
super();
}
@BeforeClass
public static void setup() {
String problems = "";
if (TEST_USERNAME == null || TEST_USERNAME.trim().isEmpty()) {
problems += "TEST_USER_NAME cannot be empty\n";
141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
}
if (problems.isEmpty()) {
// Must setup the connection to the GitLab test server
gitLabApi = baseTestSetup();
if (gitLabApi != null) {
if (TEST_EXTERNAL_USERNAME != null) {
Optional<User> optionalUser = gitLabApi.getUserApi().getOptionalUser(TEST_EXTERNAL_USERNAME);
if (optionalUser.isPresent()) {
try {
gitLabApi.getUserApi().deleteUser(optionalUser.get());
} catch (Exception ignore) {}
}
}
if (TEST_BLOCK_USERNAME != null) {
try {
blockUser = gitLabApi.getUserApi().getUser(TEST_BLOCK_USERNAME);
if (blockUser != null) {
gitLabApi.getUserApi().unblockUser(blockUser.getId());
}
} catch (Exception ignore) {}
}
if (TEST_SSH_KEY != null) {
try {
List<SshKey> sshKeys = gitLabApi.getUserApi().getSshKeys();
if (sshKeys != null) {
for (SshKey key : sshKeys) {
if (key.getKey().startsWith(TEST_SSH_KEY)) {
gitLabApi.getUserApi().deleteSshKey(key.getId());
}
}
}
} catch (Exception ignore) {}
}
try {
List<Email> emails = gitLabApi.getUserApi().getEmails();
for (Email email : emails) {
if (TEST_USER_EMAIL.equals(email.getEmail())) {
gitLabApi.getUserApi().deleteEmail(email.getId());
}
}
} catch (Exception ignore) {}
}
} else {
System.err.print(problems);
}
}
@Before
public void beforeMethod() {
assumeNotNull(gitLabApi);
}
@Test
public void testGetVersion() throws GitLabApiException {
Version version = gitLabApi.getVersion();
assertNotNull(version);
System.out.format("version=%s, revision=%s%n", version.getVersion(), version.getRevision());
assertNotNull(version.getVersion());
assertNotNull(version.getRevision());
}
@Test
211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
public void testGetCurrentUser() throws GitLabApiException {
User currentUser = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(currentUser);
assertEquals(TEST_USERNAME, currentUser.getUsername());
}
@Test
public void testLookupUser() throws GitLabApiException {
User user = gitLabApi.getUserApi().getUser(TEST_USERNAME);
assertNotNull(user);
assertEquals(TEST_USERNAME, user.getUsername());
}
@Test
public void testBlockUnblockUser() throws GitLabApiException {
assumeNotNull(blockUser);
assertNotEquals("blocked", blockUser.getState());
gitLabApi.getUserApi().blockUser(blockUser.getId());
User user = gitLabApi.getUserApi().getUser(blockUser.getId());
assertEquals("blocked", user.getState());
gitLabApi.getUserApi().unblockUser(blockUser.getId());
user = gitLabApi.getUserApi().getUser(blockUser.getId());
assertNotEquals("blocked", user.getState());
}
@Test
public void testGetOptionalUser() throws GitLabApiException {
Optional<User> optional = gitLabApi.getUserApi().getOptionalUser(TEST_USERNAME);
assertNotNull(optional);
assertTrue(optional.isPresent());
assertEquals(TEST_USERNAME, optional.get().getUsername());
optional = gitLabApi.getUserApi().getOptionalUser("this-username-does-not-exist");
assertNotNull(optional);
assertFalse(optional.isPresent());
}
@Test
public void testExternalUid() throws GitLabApiException {
assumeNotNull(TEST_EXTERNAL_USERNAME);
assumeNotNull(TEST_EXTERNAL_PROVIDER);
assumeNotNull(TEST_EXTERNAL_UID);
User externalUser = null;
try {
User userSettings = new User()
.withUsername(TEST_EXTERNAL_USERNAME)
.withEmail(TEST_EXTERNAL_USERNAME + "@gitlab4j.org")
.withName("GitLab4J External User")
.withSkipConfirmation(true)
.withIsAdmin(false)
.withExternUid(TEST_EXTERNAL_UID)
.withProvider(TEST_EXTERNAL_PROVIDER);
externalUser = gitLabApi.getUserApi().createUser(userSettings, TEST_LOGIN_PASSWORD, false);
assertNotNull(externalUser);
Optional<User> optionalUser = gitLabApi.getUserApi().getOptionalUserByExternalUid(TEST_EXTERNAL_PROVIDER, TEST_EXTERNAL_UID);
assertNotNull(optionalUser);
assertTrue(optionalUser.isPresent());
assertEquals(externalUser.getId(), optionalUser.get().getId());
optionalUser = gitLabApi.getUserApi().getOptionalUserByExternalUid("unknown-provider", "unknown-uid");
assertNotNull(optionalUser);
assertFalse(optionalUser.isPresent());
281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
} finally {
if (externalUser != null) {
try {
gitLabApi.getUserApi().deleteUser(externalUser);
} catch (Exception ignore) {}
}
}
}
@Test
public void testSudoAsUser() throws GitLabApiException {
assumeTrue(TEST_SUDO_AS_USERNAME != null && TEST_SUDO_AS_USERNAME.length() > 0);
try {
gitLabApi.sudo(TEST_SUDO_AS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
assertEquals(TEST_SUDO_AS_USERNAME, user.getUsername());
Integer sudoAsId = user.getId();
gitLabApi.sudo(null);
user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
assertEquals(TEST_USERNAME, user.getUsername());
gitLabApi.unsudo();
assertNull(gitLabApi.getSudoAsId());
gitLabApi.setSudoAsId(sudoAsId);
user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
assertEquals(sudoAsId, user.getId());
assertEquals(TEST_SUDO_AS_USERNAME, user.getUsername());
} finally {
gitLabApi.unsudo();
}
}
@Test
public void testCreateImpersonationToken() throws GitLabApiException, ParseException {
User user = gitLabApi.getUserApi().getCurrentUser();
// NOTE: READ_REGISTRY scope is left out because the GitLab server docker instance does not have the
// registry configured and the test would thus fail.
Scope[] scopes = {Scope.API, Scope.READ_USER, Scope.READ_REPOSITORY, Scope.WRITE_REPOSITORY, Scope.SUDO};
Date expiresAt = ISO8601.toDate("2018-01-01T00:00:00Z");
ImpersonationToken token = null;
try {
token = gitLabApi.getUserApi().createImpersonationToken(user, TEST_IMPERSONATION_TOKEN_NAME, expiresAt, scopes);
assertNotNull(token);
assertNotNull(token.getId());
assertEquals(TEST_IMPERSONATION_TOKEN_NAME, token.getName());
assertEquals(expiresAt.getTime(), token.getExpiresAt().getTime());
assertEquals(scopes.length, token.getScopes().size());
assertThat(token.getScopes(), contains(scopes));
} finally {
if (user != null && token != null) {
gitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
}
}
}