Skip to content

Commit a851e3a

Browse files
committed
Mods related to blockUser() and unblockUser() fixes (#169).
1 parent 94b37c5 commit a851e3a

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

example-test-gitlab4j.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,12 @@ TEST_GROUP_MEMBER_USERNAME=
2424
TEST_LOGIN_USERNAME=
2525
TEST_LOGIN_PASSWORD=
2626

27-
# OPTIONAL: To test sudo capability provide a username to sudo as
27+
# OPTIONAL: To test sudo capability, provide a username to sudo as
2828
TEST_SUDO_AS_USERNAME=
2929

30+
# OPTIONAL: To test block/unblock capability, provide a username to block/unblock
31+
TEST_BLOCK_USERNAME=
32+
3033
# OPTIONAL: To test using GitLab4J-API with a proxy, set the following properties
3134
TEST_PROXY_URI=
3235
TEST_PROXY_USERNAME=

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

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,15 @@ public Pager<User> getActiveUsers(int itemsPerPage) throws GitLabApiException{
119119
* POST /users/:id/block
120120
*
121121
* @param userId the ID of the user to block
122-
* @return the User instance for the blocked user
123122
* @throws GitLabApiException if any exception occurs
124123
*/
125-
public User blockUser(Integer userId) throws GitLabApiException {
124+
public void blockUser(Integer userId) throws GitLabApiException {
126125

127126
if (userId == null) {
128127
throw new RuntimeException("userId cannot be null");
129128
}
130129

131-
Response response = post(Response.Status.CREATED, (Form) null, "users", userId, "block");
132-
return (response.readEntity(User.class));
130+
post(Response.Status.CREATED, (Form) null, "users", userId, "block");
133131
}
134132

135133
/**
@@ -138,17 +136,15 @@ public User blockUser(Integer userId) throws GitLabApiException {
138136
* POST /users/:id/unblock
139137
*
140138
* @param userId the ID of the user to unblock
141-
* @return the User instance for the unblocked user
142139
* @throws GitLabApiException if any exception occurs
143140
*/
144-
public User unblockUser(Integer userId) throws GitLabApiException {
141+
public void unblockUser(Integer userId) throws GitLabApiException {
145142

146143
if (userId == null) {
147144
throw new RuntimeException("userId cannot be null");
148145
}
149146

150-
Response response = post(Response.Status.CREATED, (Form) null, "users", userId, "unblock");
151-
return (response.readEntity(User.class));
147+
post(Response.Status.CREATED, (Form) null, "users", userId, "unblock");
152148
}
153149

154150
/**

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5+
import static org.junit.Assert.assertNotEquals;
56
import static org.junit.Assert.assertNotNull;
67
import static org.junit.Assert.assertNull;
78
import static org.junit.Assert.assertTrue;
9+
import static org.junit.Assume.assumeNotNull;
810
import static org.junit.Assume.assumeTrue;
911

1012
import java.text.ParseException;
@@ -49,12 +51,14 @@ public class TestUserApi {
4951
private static final String TEST_HOST_URL;
5052
private static final String TEST_PRIVATE_TOKEN;
5153
private static final String TEST_USERNAME;
54+
private static final String TEST_BLOCK_USERNAME;
5255
private static final String TEST_SUDO_AS_USERNAME;
5356
private static final String TEST_SSH_KEY;
5457
static {
5558
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
5659
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
5760
TEST_USERNAME = TestUtils.getProperty("TEST_USERNAME");
61+
TEST_BLOCK_USERNAME = TestUtils.getProperty("TEST_BLOCK_USERNAME");
5862
TEST_SUDO_AS_USERNAME = TestUtils.getProperty("TEST_SUDO_AS_USERNAME");
5963
TEST_SSH_KEY = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCvbkmGRaANy2nmLrfYa9LkjMqjs9twYZXQKUPK18j" +
6064
"BWmNgnAm818IikxjfFit3Gqnnh9zdNzlzUYs2osmfdHwRLeFY3hKVR6WckGYVroQuV5ArUA4+oME+IIQ2soCv/" +
@@ -66,6 +70,7 @@ public class TestUserApi {
6670
private static final String TEST_IMPERSONATION_TOKEN_NAME = "token1";
6771

6872
private static GitLabApi gitLabApi;
73+
private static User blockUser;
6974

7075
public TestUserApi() {
7176
super();
@@ -90,6 +95,15 @@ public static void setup() {
9095
if (problems.isEmpty()) {
9196
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
9297

98+
if (TEST_BLOCK_USERNAME != null) {
99+
try {
100+
blockUser = gitLabApi.getUserApi().getUser(TEST_BLOCK_USERNAME);
101+
if (blockUser != null) {
102+
gitLabApi.getUserApi().unblockUser(blockUser.getId());
103+
}
104+
} catch (Exception ignore) {}
105+
}
106+
93107
if (TEST_SSH_KEY != null) {
94108
try {
95109
List<SshKey> sshKeys = gitLabApi.getUserApi().getSshKeys();
@@ -136,6 +150,20 @@ public void testLookupUser() throws GitLabApiException {
136150
assertEquals(TEST_USERNAME, user.getUsername());
137151
}
138152

153+
@Test
154+
public void testBlockUnblockUser() throws GitLabApiException {
155+
assumeNotNull(blockUser);
156+
157+
assertNotEquals("blocked", blockUser.getState());
158+
gitLabApi.getUserApi().blockUser(blockUser.getId());
159+
User user = gitLabApi.getUserApi().getUser(blockUser.getId());
160+
assertEquals("blocked", user.getState());
161+
162+
gitLabApi.getUserApi().unblockUser(blockUser.getId());
163+
user = gitLabApi.getUserApi().getUser(blockUser.getId());
164+
assertNotEquals("blocked", user.getState());
165+
}
166+
139167
@Test
140168
public void testGetOptionalUser() throws GitLabApiException {
141169

0 commit comments

Comments
 (0)