Skip to content

Commit 3df2658

Browse files
Add user client to allow for suspending/unsuspending users (#172)
* Add user client to allow for suspending/unsuspending users * Change signature of suspend/unsuspend method to boolean return value
1 parent 68d80c7 commit 3df2658

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed

src/main/java/com/spotify/github/v3/clients/GitHubClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,10 @@ public OrganisationClient createOrganisationClient(final String org) {
396396
return OrganisationClient.create(this, org);
397397
}
398398

399+
public UserClient createUserClient() {
400+
return UserClient.create(this);
401+
}
402+
399403
Json json() {
400404
return json;
401405
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2024 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
21+
package com.spotify.github.v3.clients;
22+
23+
import com.spotify.github.v3.user.requests.SuspensionReason;
24+
import java.util.concurrent.CompletableFuture;
25+
26+
public class UserClient {
27+
28+
public static final int NO_CONTENT = 204;
29+
private final GitHubClient github;
30+
31+
private static final String SUSPEND_USER_TEMPLATE = "/users/%s/suspended";
32+
33+
UserClient(final GitHubClient github) {
34+
this.github = github;
35+
}
36+
37+
static UserClient create(final GitHubClient github) {
38+
return new UserClient(github);
39+
}
40+
41+
/**
42+
* Suspend a user.
43+
*
44+
* @param username username of the user to suspend
45+
* @return a CompletableFuture that indicates success or failure
46+
*/
47+
public CompletableFuture<Boolean> suspendUser(
48+
final String username, final SuspensionReason reason) {
49+
final String path = String.format(SUSPEND_USER_TEMPLATE, username);
50+
return github
51+
.put(path, github.json().toJsonUnchecked(reason))
52+
.thenApply(resp -> resp.code() == NO_CONTENT);
53+
}
54+
55+
/**
56+
* Unsuspend a user.
57+
*
58+
* @param username username of the user to unsuspend
59+
* @return a CompletableFuture that indicates success or failure
60+
*/
61+
public CompletableFuture<Boolean> unSuspendUser(
62+
final String username, final SuspensionReason reason) {
63+
final String path = String.format(SUSPEND_USER_TEMPLATE, username);
64+
return github
65+
.delete(path, github.json().toJsonUnchecked(reason))
66+
.thenApply(resp -> resp.code() == NO_CONTENT);
67+
}
68+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2024 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.github.v3.user.requests;
21+
22+
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
23+
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
24+
import com.spotify.github.GithubStyle;
25+
import org.immutables.value.Value;
26+
27+
@Value.Immutable
28+
@GithubStyle
29+
@JsonSerialize(as = ImmutableSuspensionReason.class)
30+
@JsonDeserialize(as = ImmutableSuspensionReason.class)
31+
public interface SuspensionReason {
32+
33+
String reason();
34+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*-
2+
* -\-\-
3+
* github-api
4+
* --
5+
* Copyright (C) 2016 - 2024 Spotify AB
6+
* --
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* -/-/-
19+
*/
20+
package com.spotify.github.v3.clients;
21+
22+
import static java.util.concurrent.CompletableFuture.completedFuture;
23+
import static org.junit.jupiter.api.Assertions.*;
24+
import static org.mockito.ArgumentMatchers.any;
25+
import static org.mockito.ArgumentMatchers.eq;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
28+
29+
import com.spotify.github.jackson.Json;
30+
import com.spotify.github.v3.user.requests.ImmutableSuspensionReason;
31+
import java.util.concurrent.CompletableFuture;
32+
import okhttp3.Response;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
35+
36+
public class UserClientTest {
37+
38+
private GitHubClient github;
39+
private UserClient userClient;
40+
41+
@BeforeEach
42+
public void setUp() {
43+
github = mock(GitHubClient.class);
44+
userClient = new UserClient(github);
45+
Json json = Json.create();
46+
when(github.json()).thenReturn(json);
47+
}
48+
49+
@Test
50+
public void testSuspendUserSuccess() throws Exception {
51+
Response response = mock(Response.class);
52+
when(response.code()).thenReturn(204);
53+
when(github.put(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response));
54+
final CompletableFuture<Boolean> result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build());
55+
assertTrue(result.get());
56+
}
57+
58+
@Test
59+
public void testSuspendUserFailure() throws Exception {
60+
Response response = mock(Response.class);
61+
when(response.code()).thenReturn(403);
62+
when(github.put(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response));
63+
final CompletableFuture<Boolean> result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build());
64+
assertFalse(result.get());
65+
}
66+
67+
@Test
68+
public void testUnSuspendUserSuccess() throws Exception {
69+
Response response = mock(Response.class);
70+
when(response.code()).thenReturn(204);
71+
when(github.delete(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response));
72+
final CompletableFuture<Boolean> result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build());
73+
assertTrue(result.get());
74+
}
75+
76+
@Test
77+
public void testUnSuspendUserFailure() throws Exception {
78+
Response response = mock(Response.class);
79+
when(response.code()).thenReturn(403);
80+
when(github.delete(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response));
81+
final CompletableFuture<Boolean> result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build());
82+
assertFalse(result.get());
83+
}
84+
}

0 commit comments

Comments
 (0)