Skip to content

Commit da358ec

Browse files
committed
Merge remote-tracking branch 'origin/main' into 6.x
2 parents 8d3cdac + ce46111 commit da358ec

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

src/main/java/org/gitlab4j/api/UserApi.java

+32
Original file line numberDiff line numberDiff line change
@@ -1381,4 +1381,36 @@ public Pager<Membership> getMemberships(Long userId, int itemsPerPage) throws Gi
13811381
GitLabApiForm formData = new GitLabApiForm();
13821382
return (new Pager<>(this, Membership.class, itemsPerPage, formData.asMap(), "users", userId, "memberships"));
13831383
}
1384+
1385+
/**
1386+
* Activates the given user (admin only)
1387+
*
1388+
* <pre><code>GitLab Endpoint: POST /users/:id/activate</code></pre>
1389+
*
1390+
* @param userId the ID of the user to activate
1391+
* @throws GitLabApiException if any exception occurs.
1392+
* @since GitLab 12.4
1393+
*/
1394+
public void activateUser(Long userId) throws GitLabApiException {
1395+
if (userId == null) {
1396+
throw new RuntimeException("userId cannot be null");
1397+
}
1398+
post(Response.Status.CREATED, (Form) null, "users", userId, "activate");
1399+
}
1400+
1401+
/**
1402+
* Deactivates the given user (admin only)
1403+
*
1404+
* <pre><code>GitLab Endpoint: POST /users/:id/deactivate</code></pre>
1405+
*
1406+
* @param userId the ID of the user to deactivate
1407+
* @throws GitLabApiException if any exception occurs.
1408+
* @since GitLab 12.4
1409+
*/
1410+
public void deactivateUser(Long userId) throws GitLabApiException {
1411+
if (userId == null) {
1412+
throw new RuntimeException("userId cannot be null");
1413+
}
1414+
post(Response.Status.CREATED, (Form) null, "users", userId, "deactivate");
1415+
}
13841416
}

src/test/java/org/gitlab4j/api/PropertyConstants.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface PropertyConstants {
99
String ADMIN_USERNAME_KEY = "TEST_ADMIN_USERNAME";
1010
String ACCESS_TOKEN_KEY = "TEST_ACCESS_TOKEN";
1111
String BLOCK_USERNAME_KEY = "TEST_BLOCK_USERNAME";
12+
String DEACTIVATE_USERNAME_KEY = "TEST_DEACTIVATE_USERNAME";
1213
String GROUP_KEY = "TEST_GROUP";
1314
String GROUP_MEMBER_USERNAME_KEY = "TEST_GROUP_MEMBER_USERNAME";
1415
String GROUP_PROJECT_KEY = "TEST_GROUP_PROJECT";

src/test/java/org/gitlab4j/api/TestUserApi.java

+28
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class TestUserApi extends AbstractIntegrationTest {
5959
// The following needs to be set to your test repository
6060
private static final String TEST_USERNAME = HelperUtils.getProperty(USERNAME_KEY);
6161
private static final String TEST_BLOCK_USERNAME = HelperUtils.getProperty(BLOCK_USERNAME_KEY);
62+
63+
private static final String TEST_DEACTIVATE_USERNAME = HelperUtils.getProperty(DEACTIVATE_USERNAME_KEY);
6264
private static final String TEST_SUDO_AS_USERNAME = HelperUtils.getProperty(SUDO_AS_USERNAME_KEY);
6365

6466
private static final String TEST_IMPERSONATION_TOKEN_NAME = "ipt_1";
@@ -130,6 +132,8 @@ public class TestUserApi extends AbstractIntegrationTest {
130132
private static GitLabApi gitLabApi;
131133
private static User blockUser;
132134

135+
private static User deactivateUser;
136+
133137
public TestUserApi() {
134138
super();
135139
}
@@ -167,6 +171,16 @@ public static void setup() {
167171
} catch (Exception ignore) {}
168172
}
169173

174+
if (TEST_DEACTIVATE_USERNAME != null) {
175+
try {
176+
deactivateUser = gitLabApi.getUserApi().getUser(TEST_DEACTIVATE_USERNAME);
177+
if (deactivateUser != null) {
178+
gitLabApi.getUserApi().unblockUser(deactivateUser.getId());
179+
}
180+
} catch (Exception ignore) {}
181+
}
182+
183+
170184
if (TEST_SSH_KEY != null) {
171185
try {
172186
List<SshKey> sshKeys = gitLabApi.getUserApi().getSshKeys();
@@ -237,6 +251,20 @@ public void testBlockUnblockUser() throws GitLabApiException {
237251
assertNotEquals("blocked", user.getState());
238252
}
239253

254+
@Test
255+
public void testActivateDeactivateUser() throws GitLabApiException {
256+
assumeTrue(deactivateUser != null);
257+
258+
assertNotEquals("deactivated", deactivateUser.getState());
259+
gitLabApi.getUserApi().deactivateUser(deactivateUser.getId());
260+
User user = gitLabApi.getUserApi().getUser(deactivateUser.getId());
261+
assertEquals("deactivated", user.getState());
262+
263+
gitLabApi.getUserApi().activateUser(deactivateUser.getId());
264+
user = gitLabApi.getUserApi().getUser(deactivateUser.getId());
265+
assertNotEquals("deactivated", user.getState());
266+
}
267+
240268
@Test
241269
public void testGetOptionalUser() throws GitLabApiException {
242270

src/test/resources/test-gitlab4j.properties

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ TEST_USERNAME=gitlab4j
3434
# it will be created during integration testing
3535
TEST_SUDO_AS_USERNAME=user1
3636
TEST_BLOCK_USERNAME=user1
37+
TEST_DEACTIVATE_USERNAME=user1
3738
TEST_XFER_NAMESPACE=user1
3839
TEST_REQUEST_ACCESS_USERNAME=user1
3940

0 commit comments

Comments
 (0)