Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/main/java/org/gitlab4j/api/UserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,48 @@ public Optional<User> getOptionalUserByEmail(String email) {
}
}

/**
* Lookup a user by external UID. Returns null if not found.
*
* <p>NOTE: This is for admin users only.</p>
*
* <pre><code>GitLab Endpoint: GET /users?extern_uid=:externalUid&provider=:provider</code></pre>
*
* @param provider the provider of the external uid
* @param externalUid the external UID of the user
* @return the User instance for the specified external UID, or null if not found
* @throws GitLabApiException if any exception occurs
*/
public User getUserByExternalUid(String provider, String externalUid) throws GitLabApiException {
GitLabApiForm formData = createGitLabApiForm()
.withParam("provider", provider, true)
.withParam("extern_uid", externalUid, true)
.withParam(PAGE_PARAM, 1)
.withParam(PER_PAGE_PARAM, 1);
Response response = get(Response.Status.OK, formData.asMap(), "users");
List<User> users = response.readEntity(new GenericType<List<User>>() {});
return (users.isEmpty() ? null : users.get(0));
}

/**
* Lookup a user by external UID and return an Optional instance.
*
* <p>NOTE: This is for admin users only.</p>
*
* <pre><code>GitLab Endpoint: GET /users?extern_uid=:externUid&provider=:provider</code></pre>
*
* @param provider the provider of the external uid
* @param externalUid the external UID of the user
* @return the User for the specified external UID as an Optional instance
*/
public Optional<User> getOptionalUserByExternalUid(String provider, String externalUid) {
try {
return (Optional.ofNullable(getUserByExternalUid(provider, externalUid)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}

/**
* Search users by Email or username
*
Expand Down
22 changes: 12 additions & 10 deletions src/test/java/org/gitlab4j/api/IntegrationTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

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 java.util.*;

import org.gitlab4j.api.models.*;
import org.gitlab4j.api.utils.AccessTokenUtils;
import org.gitlab4j.api.utils.AccessTokenUtils.Scope;
import org.junit.AfterClass;
Expand Down Expand Up @@ -44,6 +38,8 @@ 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);
Expand Down Expand Up @@ -168,12 +164,18 @@ private static void seedData() throws GitLabApiException {
// 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);
.withIsAdmin(true)
.withIdentities(Collections.singletonList(identity));

gitLabApi.getUserApi().createUser(userSettings, TEST_LOGIN_PASSWORD, false);
System.out.format("Created %s user (%s)%n", userSettings.getName(), userSettings.getUsername());
}
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/org/gitlab4j/api/PropertyConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface PropertyConstants {
public static final String TEST_REQUEST_ACCESS_USERNAME_KEY = "TEST_REQUEST_ACCESS_USERNAME";
public static final String USERNAME_KEY = "TEST_USERNAME";
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";
}
16 changes: 15 additions & 1 deletion src/test/java/org/gitlab4j/api/TestUserApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ public class TestUserApi extends AbstractIntegrationTest {
"IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" +
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com";
private static final String TEST_USER_EMAIL = "test-user-email123@gitlab4j.org";


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;
Expand Down Expand Up @@ -180,6 +182,18 @@ public void testGetOptionalUser() throws GitLabApiException {
assertFalse(optional.isPresent());
}

@Test
public void testGetOptionalUserByExternalUid() throws GitLabApiException {

Optional<User> optional = gitLabApi.getUserApi().getOptionalUserByExternalUid(TEST_EXTERNAL_PROVIDER, TEST_EXTERNAL_UID);
assertNotNull(optional);
assertTrue(optional.isPresent());

optional = gitLabApi.getUserApi().getOptionalUserByExternalUid("unknown-provider", "unknown-uid");
assertNotNull(optional);
assertFalse(optional.isPresent());
}

@Test
public void testSudoAsUser() throws GitLabApiException {

Expand Down
6 changes: 6 additions & 0 deletions src/test/resources/test-gitlab4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ 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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tranqyll SInce this is dummy data, what would the reason to change it be? Also, we don't change/override any settings in this file, they are done in ~/test-gitlab4j.properties

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the tests, I need to register a Gitlab Identity (https://github.com/gitlab4j/gitlab4j-api/pull/428/files/fc99cc4a809c8e701e7465171cc7a7e453e726b4#diff-179c0c9282e28646cdd416dd74590443R168). I thought test-gitlab4j.properties was an example of all possible properties and needed to be updated.

What should I do, remove it ?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Tranqyll
It remains here but commented out, and then you create a ~/test-gitlab4j.properties file and put it in there for testing.


# This is the user to test sudo, block, and project transfer. If the user does not exist
# it will be created during integration testing
TEST_SUDO_AS_USERNAME=user1
Expand Down