Commit e7570932 authored by Greg Messner's avatar Greg Messner
Browse files

Mods to support access requests (#364).

parent c19002fe
......@@ -11,6 +11,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupFilter;
import org.gitlab4j.api.models.GroupProjectsFilter;
......@@ -23,6 +24,7 @@ import org.gitlab4j.api.models.Visibility;
* This class implements the client side API for the GitLab groups calls.
* @see <a href="https://docs.gitlab.com/ce/api/groups.html">Groups API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ee/api/access_requests.html#group-and-project-access-requests-api">Group and project access requests API</a>
*/
public class GroupApi extends AbstractApi {
......@@ -1221,4 +1223,91 @@ public class GroupApi extends AbstractApi {
"projects", getProjectIdOrPath(projectIdOrPath));
return (response.readEntity(Project.class));
}
/**
* Get a List of the group access requests viewable by the authenticated user.
*
* <pre><code>GET /group/:id/access_requests</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @return a List of project AccessRequest instances accessible by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public List<AccessRequest> getAccessRequests(Object groupIdOrPath) throws GitLabApiException {
return (getAccessRequests(groupIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a Pager of the group access requests viewable by the authenticated user.
*
* <pre><code>GET /groups/:id/access_requests</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param itemsPerPage the number of AccessRequest instances that will be fetched per page
* @return a Pager of group AccessRequest instances accessible by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public Pager<AccessRequest> getAccessRequests(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<AccessRequest>(this, AccessRequest.class, itemsPerPage, null,
"groups", getGroupIdOrPath(groupIdOrPath), "access_requests"));
}
/**
* Get a Stream of the group access requests viewable by the authenticated user.
*
* <pre><code>GET /groups/:id/access_requests</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @return a Stream of group AccessRequest instances accessible by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public Stream<AccessRequest> getAccessRequestsStream(Object groupIdOrPath) throws GitLabApiException {
return (getAccessRequests(groupIdOrPath, getDefaultPerPage()).stream());
}
/**
* Requests access for the authenticated user to the specified group.
*
* <pre><code>POST /groups/:id/access_requests</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @return the created AccessRequest instance
* @throws GitLabApiException if any exception occurs
*/
public AccessRequest requestAccess(Object groupIdOrPath) throws GitLabApiException {
Response response = post(Response.Status.CREATED, (Form)null, "groups", getGroupIdOrPath(groupIdOrPath), "access_requests");
return (response.readEntity(AccessRequest.class));
}
/**
* Approve access for the specified user to the specified group.
*
* <pre><code>PUT /groups/:id/access_requests/:user_id/approve</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param userId the user ID to approve access for
* @param accessLevel the access level the user is approved for, if null will be developer (30)
* @return the approved AccessRequest instance
* @throws GitLabApiException if any exception occurs
*/
public AccessRequest approveAccessRequest(Object groupIdOrPath, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("access_level", accessLevel);
Response response = this.putWithFormData(Response.Status.CREATED, formData,
"groups", getGroupIdOrPath(groupIdOrPath), "access_requests", userId, "approve");
return (response.readEntity(AccessRequest.class));
}
/**
* Deny access for the specified user to the specified group.
*
* <pre><code>DELETE /groups/:id/access_requests/:user_id</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param userId the user ID to deny access for
* @throws GitLabApiException if any exception occurs
*/
public void denyAccessRequest(Object groupIdOrPath, Integer userId) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null,
"groups", getGroupIdOrPath(groupIdOrPath), "access_requests", userId);
}
}
......@@ -39,6 +39,7 @@ import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Event;
import org.gitlab4j.api.models.FileUpload;
import org.gitlab4j.api.models.Issue;
......@@ -56,6 +57,7 @@ import org.gitlab4j.api.models.Visibility;
* This class provides an entry point to all the GitLab API project calls.
* @see <a href="https://docs.gitlab.com/ce/api/projects.html">Projects API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ee/api/members.html">Group and project members API at GitLab</a>
* @see <a href="https://docs.gitlab.com/ee/api/access_requests.html#group-and-project-access-requests-api">Group and project access requests API</a>
*/
public class ProjectApi extends AbstractApi implements Constants {
......@@ -2681,4 +2683,90 @@ public class ProjectApi extends AbstractApi implements Constants {
public void deleteVariable(Object projectIdOrPath, String key) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
}
/**
* Get a List of the project access requests viewable by the authenticated user.
*
* <pre><code>GET /projects/:id/access_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a List of project AccessRequest instances accessible by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public List<AccessRequest> getAccessRequests(Object projectIdOrPath) throws GitLabApiException {
return (getAccessRequests(projectIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a Pager of the project access requests viewable by the authenticated user.
*
* <pre><code>GET /projects/:id/access_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param itemsPerPage the number of AccessRequest instances that will be fetched per page
* @return a Pager of project AccessRequest instances accessible by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public Pager<AccessRequest> getAccessRequests(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<AccessRequest>(this, AccessRequest.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "access_requests"));
}
/**
* Get a Stream of the project access requests viewable by the authenticated user.
*
* <pre><code>GET /projects/:id/access_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return a Stream of project AccessRequest instances accessible by the authenticated user
* @throws GitLabApiException if any exception occurs
*/
public Stream<AccessRequest> getAccessRequestsStream(Object projectIdOrPath) throws GitLabApiException {
return (getAccessRequests(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Requests access for the authenticated user to the specified project.
*
* <pre><code>POST /projects/:id/access_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @return the created AccessRequest instance
* @throws GitLabApiException if any exception occurs
*/
public AccessRequest requestAccess(Object projectIdOrPath) throws GitLabApiException {
Response response = post(Response.Status.CREATED, (Form)null, "projects", getProjectIdOrPath(projectIdOrPath), "access_requests");
return (response.readEntity(AccessRequest.class));
}
/**
* Approve access for the specified user to the specified project.
*
* <pre><code>PUT /projects/:id/access_requests/:user_id/approve</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param userId the user ID to approve access for
* @param accessLevel the access level the user is approved for, if null will be developer (30)
* @return the approved AccessRequest instance
* @throws GitLabApiException if any exception occurs
*/
public AccessRequest approveAccessRequest(Object projectIdOrPath, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm().withParam("access_level", accessLevel);
Response response = this.putWithFormData(Response.Status.CREATED, formData,
"projects", getProjectIdOrPath(projectIdOrPath), "access_requests", userId, "approve");
return (response.readEntity(AccessRequest.class));
}
/**
* Deny access for the specified user to the specified project.
*
* <pre><code>DELETE /projects/:id/access_requests/:user_id</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param userId the user ID to deny access for
* @throws GitLabApiException if any exception occurs
*/
public void denyAccessRequest(Object projectIdOrPath, Integer userId) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null,
"projects", getProjectIdOrPath(projectIdOrPath), "access_requests", userId);
}
}
......@@ -13,6 +13,7 @@ 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;
import org.junit.BeforeClass;
import org.junit.experimental.categories.Categories.IncludeCategory;
......@@ -96,7 +97,7 @@ public class IntegrationTestSuite implements PropertyConstants {
TEST_PRIVATE_TOKEN = AccessTokenUtils.createPersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
TEST_PRIVATE_TOKEN_NAME, Arrays.asList("api", "sudo"));
TEST_PRIVATE_TOKEN_NAME, Arrays.asList(Scope.API, Scope.SUDO));
System.out.println("Created private token: " + TEST_PRIVATE_TOKEN);
assertNotNull(TEST_PRIVATE_TOKEN);
assertFalse(TEST_PRIVATE_TOKEN.trim().isEmpty());
......@@ -109,7 +110,7 @@ public class IntegrationTestSuite implements PropertyConstants {
TEST_ACCESS_TOKEN = AccessTokenUtils.createPersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
TEST_ACCESS_TOKEN_NAME, Arrays.asList("api", "sudo"));
TEST_ACCESS_TOKEN_NAME, Arrays.asList(Scope.API, Scope.SUDO));
System.out.println("Created access token: " + TEST_ACCESS_TOKEN);
assertNotNull(TEST_ACCESS_TOKEN);
assertFalse(TEST_ACCESS_TOKEN.trim().isEmpty());
......@@ -124,7 +125,7 @@ public class IntegrationTestSuite implements PropertyConstants {
try {
AccessTokenUtils.revokePersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
TEST_PRIVATE_TOKEN_NAME, Arrays.asList("api", "sudo"));
TEST_PRIVATE_TOKEN_NAME, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Revoved '%s'%n", TEST_PRIVATE_TOKEN_NAME);
} catch (Exception ignore) {}
}
......@@ -133,7 +134,7 @@ public class IntegrationTestSuite implements PropertyConstants {
try {
AccessTokenUtils.revokePersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
TEST_ACCESS_TOKEN_NAME, Arrays.asList("api", "sudo"));
TEST_ACCESS_TOKEN_NAME, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Revoved '%s'%n", TEST_ACCESS_TOKEN_NAME);
} catch (Exception ignore) {}
}
......@@ -202,7 +203,8 @@ public class IntegrationTestSuite implements PropertyConstants {
.withName(TEST_PROJECT_NAME)
.withDefaultBranch("master")
.withPublic(true)
.withInitializeWithReadme(true);
.withInitializeWithReadme(true)
.withRequestAccessEnabled(true);
testProject = gitLabApi.getProjectApi().createProject(projectSettings);
System.out.format("Created %s project%n", projectSettings.getName());
......@@ -241,7 +243,8 @@ public class IntegrationTestSuite implements PropertyConstants {
.withName("Test Group")
.withPath(TEST_GROUP)
.withDescription("Test Group")
.withVisibility(Visibility.PUBLIC);
.withVisibility(Visibility.PUBLIC)
.withRequestAccessEnabled(true);
testGroup = gitLabApi.getGroupApi().addGroup(groupSettings);
System.out.format("Created %s group (%s)%n", groupSettings.getName(), groupSettings.getPath());
}
......
......@@ -23,6 +23,7 @@ public interface PropertyConstants {
public static final String PROXY_PASSWORD_KEY = "TEST_PROXY_PASSWORD";
public static final String PROXY_URI_KEY = "TEST_PROXY_URI";
public static final String PROXY_USERNAME_KEY = "TEST_PROXY_USERNAME";
public static final String TEST_REQUEST_ACCESS_USERNAME_KEY = "TEST_REQUEST_ACCESS_USERNAME";
public static final String SUDO_AS_USERNAME_KEY = "TEST_SUDO_AS_USERNAME";
public static final String USERNAME_KEY = "TEST_USERNAME";
public static final String XFER_NAMESPACE_KEY = "TEST_XFER_NAMESPACE";
......
......@@ -9,6 +9,7 @@ import static org.junit.Assume.assumeTrue;
import java.util.Arrays;
import org.gitlab4j.api.utils.AccessTokenUtils;
import org.gitlab4j.api.utils.AccessTokenUtils.Scope;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
......@@ -71,7 +72,7 @@ public class TestAccessTokenUtils {
String accessToken = AccessTokenUtils.createPersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
tokenName, Arrays.asList("api", "sudo"));
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken);
assertNotNull(accessToken);
......@@ -81,7 +82,7 @@ public class TestAccessTokenUtils {
try {
AccessTokenUtils.revokePersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
tokenName, Arrays.asList("api", "sudo"));
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken);
} catch (Exception ignore) {}
}
......@@ -92,7 +93,7 @@ public class TestAccessTokenUtils {
try {
AccessTokenUtils.createPersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, "INVALID PASSWORD",
"Testing Token Creation", Arrays.asList("api", "sudo"));
"Testing Token Creation", Arrays.asList(Scope.API, Scope.SUDO));
fail("Expected a failure, but personal access token was created.");
} catch (GitLabApiException glae) {
assertEquals(401, glae.getHttpStatus());
......@@ -106,14 +107,14 @@ public class TestAccessTokenUtils {
String accessToken = AccessTokenUtils.createPersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
tokenName, Arrays.asList("api", "sudo"));
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Created '%s' personal access token: %s%n", tokenName, accessToken);
assertNotNull(accessToken);
assertFalse(accessToken.trim().isEmpty());
AccessTokenUtils.revokePersonalAccessToken(
TEST_HOST_URL, TEST_LOGIN_USERNAME, TEST_LOGIN_PASSWORD,
tokenName, Arrays.asList("api", "sudo"));
tokenName, Arrays.asList(Scope.API, Scope.SUDO));
System.out.format("Revoked '%s' personal access token: %s%n", tokenName, accessToken);
}
......
......@@ -32,6 +32,7 @@ import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.Map;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Application;
import org.gitlab4j.api.models.ArtifactsFile;
import org.gitlab4j.api.models.AwardEmoji;
......@@ -90,6 +91,12 @@ import org.junit.Test;
public class TestGitLabApiBeans {
@Test
public void testAccessRequest() throws Exception {
AccessRequest accessRequest = unmarshalResource(AccessRequest.class, "access-request.json");
assertTrue(compareJson(accessRequest, "access-request.json"));
}
@Test
public void testApplications() throws Exception {
List<Application> applications = unmarshalResourceList(Application.class, "applications.json");
......
......@@ -5,14 +5,17 @@ import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeNotNull;
import static org.junit.Assume.assumeTrue;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.User;
......@@ -41,6 +44,7 @@ public class TestGroupApi extends AbstractIntegrationTest {
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY);
private static final String TEST_GROUP_MEMBER_USERNAME = HelperUtils.getProperty(GROUP_MEMBER_USERNAME_KEY);
private static final String TEST_REQUEST_ACCESS_USERNAME = HelperUtils.getProperty(TEST_REQUEST_ACCESS_USERNAME_KEY);
private static GitLabApi gitLabApi;
private static Group testGroup;
......@@ -84,15 +88,15 @@ public class TestGroupApi extends AbstractIntegrationTest {
System.err.print(problems);
}
removeGroupMember();
removeGroupMembers();
}
@AfterClass
public static void teardown() {
removeGroupMember();
removeGroupMembers();
}
private static void removeGroupMember() {
private static void removeGroupMembers() {
if (gitLabApi != null && testGroup != null && testUser != null) {
try {
......@@ -100,6 +104,20 @@ public class TestGroupApi extends AbstractIntegrationTest {
} catch (GitLabApiException ignore) {
}
}
if (TEST_REQUEST_ACCESS_USERNAME != null) {
Optional<User> user = gitLabApi.getUserApi().getOptionalUser(TEST_REQUEST_ACCESS_USERNAME);
if (user.isPresent()) {
Integer userId = user.get().getId();
try {
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
} catch (Exception e) {
try {
gitLabApi.getGroupApi().removeMember(testGroup, userId);
} catch (Exception ignore) {}
}
}
}
}
@Before
......@@ -183,4 +201,91 @@ public class TestGroupApi extends AbstractIntegrationTest {
assertFalse(optional.isPresent());
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), GitLabApi.getOptionalException(optional).getHttpStatus());
}
@Test
public void testRequestAccess() throws GitLabApiException {
assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);
gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
final Integer userId = user.getId();
try {
try {
AccessRequest accessRequest = gitLabApi.getGroupApi().requestAccess(testGroup);
assertNotNull(accessRequest);
assertEquals(userId, accessRequest.getId());
} finally {
gitLabApi.unsudo();
}
Stream<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequestsStream(testGroup);
assertTrue(requests.anyMatch(r -> r.getId() == userId));
AccessRequest accessRequest = gitLabApi.getGroupApi().approveAccessRequest(testGroup, user.getId(), AccessLevel.DEVELOPER);
assertNotNull(accessRequest);
assertEquals(user.getId(), accessRequest.getId());
assertEquals(AccessLevel.DEVELOPER, accessRequest.getAccessLevel());
user = null;
requests = gitLabApi.getGroupApi().getAccessRequestsStream(testGroup);
assertFalse(requests.anyMatch(r -> r.getId() == userId));
} finally {
try {
if (user == null) {
gitLabApi.getGroupApi().removeMember(testGroup, userId);
} else {
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
}
} catch (Exception ignore) {}
}
}
@Test
public void testDenyRequestAccess() throws GitLabApiException {
assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);
gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
final Integer userId = user.getId();
try {
try {
AccessRequest accessRequest = gitLabApi.getGroupApi().requestAccess(testGroup);
assertNotNull(accessRequest);
assertEquals(userId, accessRequest.getId());
} finally {
gitLabApi.unsudo();
}
List<AccessRequest> requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
requests = gitLabApi.getGroupApi().getAccessRequests(testGroup);
assertFalse(requests.stream().anyMatch(r -> r.getId() == userId));
user = null;
} finally {
try {
if (user != null) {
gitLabApi.getGroupApi().denyAccessRequest(testGroup, userId);
}
} catch (Exception ignore) {
}
}
}
}
......@@ -40,10 +40,12 @@ import java.util.stream.Stream;
import javax.ws.rs.core.Response;
import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.AccessRequest;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.ProjectFilter;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.models.Visibility;
import org.junit.AfterClass;
......@@ -76,6 +78,7 @@ public class TestProjectApi extends AbstractIntegrationTest {
private static final String TEST_GROUP_PROJECT = HelperUtils.getProperty(GROUP_PROJECT_KEY);
private static final String TEST_XFER_NAMESPACE = HelperUtils.getProperty(XFER_NAMESPACE_KEY);
private static final String TEST_SUDO_AS_USERNAME = HelperUtils.getProperty(SUDO_AS_USERNAME_KEY);
private static final String TEST_REQUEST_ACCESS_USERNAME = HelperUtils.getProperty(TEST_REQUEST_ACCESS_USERNAME_KEY);
private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project";
private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2";
......@@ -161,6 +164,20 @@ public class TestProjectApi extends AbstractIntegrationTest {
gitLabApi.getProjectApi().deleteProject(project);
} catch (GitLabApiException ignore) {}
}
if (TEST_REQUEST_ACCESS_USERNAME != null) {
Optional<User> user = gitLabApi.getUserApi().getOptionalUser(TEST_REQUEST_ACCESS_USERNAME);
if (user.isPresent()) {
Integer userId = user.get().getId();
try {
gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);
} catch (Exception e) {
try {
gitLabApi.getProjectApi().removeMember(testProject, userId);
} catch (Exception ignore) {}
}
}
}
}
@Before
......@@ -679,4 +696,90 @@ public class TestProjectApi extends AbstractIntegrationTest {
// Assert
assertNotNull(members);
}
@Test
public void testRequestAccess() throws GitLabApiException {
assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);
gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
final Integer userId = user.getId();
try {
try {
AccessRequest accessRequest = gitLabApi.getProjectApi().requestAccess(testProject);
assertNotNull(accessRequest);
assertEquals(userId, accessRequest.getId());
} finally {
gitLabApi.unsudo();
}
Stream<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequestsStream(testProject);
assertTrue(requests.anyMatch(r -> r.getId() == userId));
AccessRequest accessRequest = gitLabApi.getProjectApi().approveAccessRequest(testProject, user.getId(), AccessLevel.DEVELOPER);
assertNotNull(accessRequest);
assertEquals(user.getId(), accessRequest.getId());
assertEquals(AccessLevel.DEVELOPER, accessRequest.getAccessLevel());
user = null;
requests = gitLabApi.getProjectApi().getAccessRequestsStream(testProject);
assertFalse(requests.anyMatch(r -> r.getId() == userId));
} finally {
try {
if (user == null) {
gitLabApi.getProjectApi().removeMember(testProject, userId);
} else {
gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);
}
} catch (Exception ignore) {}
}
}
@Test
public void testDenyRequestAccess() throws GitLabApiException {
assumeTrue(TEST_REQUEST_ACCESS_USERNAME != null && TEST_REQUEST_ACCESS_USERNAME.length() > 0);
gitLabApi.sudo(TEST_REQUEST_ACCESS_USERNAME);
User user = gitLabApi.getUserApi().getCurrentUser();
assertNotNull(user);
final Integer userId = user.getId();
try {
try {
AccessRequest accessRequest = gitLabApi.getProjectApi().requestAccess(testProject);
assertNotNull(accessRequest);
assertEquals(userId, accessRequest.getId());
} finally {
gitLabApi.unsudo();
}
List<AccessRequest> requests = gitLabApi.getProjectApi().getAccessRequests(testProject);
assertTrue(requests.stream().anyMatch(r -> r.getId() == userId));
gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);
requests = gitLabApi.getProjectApi().getAccessRequests(testProject);
assertFalse(requests.stream().anyMatch(r -> r.getId() == userId));
user = null;
} finally {
try {
if (user != null) {
gitLabApi.getProjectApi().denyAccessRequest(testProject, userId);
}
} catch (Exception ignore) {
}
}
}
}
......@@ -34,6 +34,7 @@ TEST_USERNAME=gitlab4j
TEST_SUDO_AS_USERNAME=user1
TEST_BLOCK_USERNAME=user1
TEST_XFER_NAMESPACE=user1
TEST_REQUEST_ACCESS_USERNAME=user1
# This specifies the group, a project owned by the specified group, and a username to
# test group membership functionality
......
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