Commit 5934fd4d authored by Greg Messner's avatar Greg Messner
Browse files

Moved creation of external user to TestUserApi (#427).

parent 8367683b
......@@ -4,9 +4,15 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import java.util.*;
import org.gitlab4j.api.models.*;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.RepositoryFile;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Visibility;
import org.gitlab4j.api.utils.AccessTokenUtils;
import org.gitlab4j.api.utils.AccessTokenUtils.Scope;
import org.junit.AfterClass;
......@@ -38,8 +44,6 @@ public class IntegrationTestSuite implements PropertyConstants {
private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY);
private static final String TEST_GROUP_PROJECT_NAME = HelperUtils.getProperty(GROUP_PROJECT_KEY);
private static final String TEST_SUB_GROUP = HelperUtils.getProperty(SUB_GROUP_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);
protected static final String TEST_PRIVATE_TOKEN_NAME = "GitLab4J Test Private Token - " + HelperUtils.getRandomInt(1000);
protected static String TEST_PRIVATE_TOKEN = HelperUtils.getProperty(PRIVATE_TOKEN_KEY);
......@@ -164,18 +168,12 @@ public class IntegrationTestSuite implements PropertyConstants {
// If the tester user doen't exists, create it
Optional<User> optionalUser = gitLabApi.getUserApi().getOptionalUser(TEST_LOGIN_USERNAME);
if (!optionalUser.isPresent()) {
Identity identity = new Identity();
identity.setExternUid(TEST_EXTERNAL_UID);
identity.setProvider(TEST_EXTERNAL_PROVIDER);
User userSettings = new User()
.withUsername(TEST_LOGIN_USERNAME)
.withEmail(TEST_LOGIN_USERNAME + "@gitlab4j.org")
.withName("GitLab4J Tester")
.withSkipConfirmation(true)
.withIsAdmin(true)
.withIdentities(Collections.singletonList(identity));
.withIsAdmin(true);
gitLabApi.getUserApi().createUser(userSettings, TEST_LOGIN_PASSWORD, false);
System.out.format("Created %s user (%s)%n", userSettings.getName(), userSettings.getUsername());
}
......
......@@ -30,4 +30,5 @@ public interface PropertyConstants {
public static final String XFER_NAMESPACE_KEY = "TEST_XFER_NAMESPACE";
public static final String EXTERNAL_PROVIDER_KEY = "TEST_EXTERNAL_PROVIDER";
public static final String EXTERNAL_UID_KEY = "TEST_EXTERNAL_UID";
public static final String EXTERNAL_USERNAME_KEY = "TEST_EXTERNAL_USERNAME";
}
......@@ -65,6 +65,7 @@ public class TestUserApi extends AbstractIntegrationTest {
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com";
private static final String TEST_USER_EMAIL = "test-user-email123@gitlab4j.org";
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);
......@@ -90,6 +91,15 @@ public class TestUserApi extends AbstractIntegrationTest {
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);
......@@ -183,15 +193,42 @@ public class TestUserApi extends AbstractIntegrationTest {
}
@Test
public void testGetOptionalUserByExternalUid() throws GitLabApiException {
public void testExternalUid() throws GitLabApiException {
Optional<User> optional = gitLabApi.getUserApi().getOptionalUserByExternalUid(TEST_EXTERNAL_PROVIDER, TEST_EXTERNAL_UID);
assertNotNull(optional);
assertTrue(optional.isPresent());
assumeNotNull(TEST_EXTERNAL_USERNAME);
assumeNotNull(TEST_EXTERNAL_PROVIDER);
assumeNotNull(TEST_EXTERNAL_UID);
optional = gitLabApi.getUserApi().getOptionalUserByExternalUid("unknown-provider", "unknown-uid");
assertNotNull(optional);
assertFalse(optional.isPresent());
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());
} finally {
if (externalUser != null) {
try {
gitLabApi.getUserApi().deleteUser(externalUser);
} catch (Exception ignore) {}
}
}
}
@Test
......
......@@ -29,11 +29,6 @@ TEST_LOGIN_PASSWORD=ChangeMeNow
TEST_PROJECT_NAME=test-project
TEST_USERNAME=gitlab4j
# This specifies the default external provider to test against and the user served by this provider, change
# this if you'd like to test against a different project
TEST_EXTERNAL_PROVIDER=github
TEST_EXTERNAL_UID=2435223452345
# This is the user to test sudo, block, and project transfer. If the user does not exist
# it will be created during integration testing
......@@ -54,3 +49,9 @@ TEST_PROXY_URI=
TEST_PROXY_USERNAME=
TEST_PROXY_PASSWORD=
# OPTIONAL: This specifies an external provider to test against and the user served by this provider.
# Copy to ~/test-gitlab4j.properties and change this if you'd like to test against a different project or user.
# TEST_EXTERNAL_USERNAME=externaluser
# TEST_EXTERNAL_PROVIDER=github
# TEST_EXTERNAL_UID=2435223452345
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment