From 01a35c712dd2221ec05f6d29b55b283d44621f2d Mon Sep 17 00:00:00 2001 From: Dennis Granath Date: Mon, 8 Jan 2024 17:32:09 +0100 Subject: [PATCH 1/2] Add user client to allow for suspending/unsuspending users --- .../github/v3/clients/GitHubClient.java | 4 + .../spotify/github/v3/clients/UserClient.java | 68 +++++++++++++++ .../v3/user/requests/SuspensionReason.java | 34 ++++++++ .../github/v3/clients/UserClientTest.java | 84 +++++++++++++++++++ 4 files changed, 190 insertions(+) create mode 100644 src/main/java/com/spotify/github/v3/clients/UserClient.java create mode 100644 src/main/java/com/spotify/github/v3/user/requests/SuspensionReason.java create mode 100644 src/test/java/com/spotify/github/v3/clients/UserClientTest.java diff --git a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java index 8364e5d9..9cb02af3 100644 --- a/src/main/java/com/spotify/github/v3/clients/GitHubClient.java +++ b/src/main/java/com/spotify/github/v3/clients/GitHubClient.java @@ -396,6 +396,10 @@ public OrganisationClient createOrganisationClient(final String org) { return OrganisationClient.create(this, org); } + public UserClient createUserClient() { + return UserClient.create(this); + } + Json json() { return json; } diff --git a/src/main/java/com/spotify/github/v3/clients/UserClient.java b/src/main/java/com/spotify/github/v3/clients/UserClient.java new file mode 100644 index 00000000..454e8744 --- /dev/null +++ b/src/main/java/com/spotify/github/v3/clients/UserClient.java @@ -0,0 +1,68 @@ +/*- + * -\-\- + * github-api + * -- + * Copyright (C) 2016 - 2024 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ + +package com.spotify.github.v3.clients; + +import com.spotify.github.v3.user.requests.SuspensionReason; +import java.util.concurrent.CompletableFuture; + +public class UserClient { + + public static final int NO_CONTENT = 204; + private final GitHubClient github; + + private static final String SUSPEND_USER_TEMPLATE = "/users/%s/suspended"; + + UserClient(final GitHubClient github) { + this.github = github; + } + + static UserClient create(final GitHubClient github) { + return new UserClient(github); + } + + /** + * Suspend a user. + * + * @param username username of the user to suspend + * @return a CompletableFuture that indicates success or failure + */ + public CompletableFuture suspendUser( + final String username, final SuspensionReason reason) { + final String path = String.format(SUSPEND_USER_TEMPLATE, username); + return github + .put(path, github.json().toJsonUnchecked(reason)) + .thenApply(resp -> resp.code() == NO_CONTENT ? 1 : 0); + } + + /** + * Unsuspend a user. + * + * @param username username of the user to unsuspend + * @return a CompletableFuture that indicates success or failure + */ + public CompletableFuture unSuspendUser( + final String username, final SuspensionReason reason) { + final String path = String.format(SUSPEND_USER_TEMPLATE, username); + return github + .delete(path, github.json().toJsonUnchecked(reason)) + .thenApply(resp -> resp.code() == NO_CONTENT ? 1 : 0); + } +} diff --git a/src/main/java/com/spotify/github/v3/user/requests/SuspensionReason.java b/src/main/java/com/spotify/github/v3/user/requests/SuspensionReason.java new file mode 100644 index 00000000..22bb1f32 --- /dev/null +++ b/src/main/java/com/spotify/github/v3/user/requests/SuspensionReason.java @@ -0,0 +1,34 @@ +/*- + * -\-\- + * github-api + * -- + * Copyright (C) 2016 - 2024 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ +package com.spotify.github.v3.user.requests; + +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; +import com.spotify.github.GithubStyle; +import org.immutables.value.Value; + +@Value.Immutable +@GithubStyle +@JsonSerialize(as = ImmutableSuspensionReason.class) +@JsonDeserialize(as = ImmutableSuspensionReason.class) +public interface SuspensionReason { + + String reason(); +} diff --git a/src/test/java/com/spotify/github/v3/clients/UserClientTest.java b/src/test/java/com/spotify/github/v3/clients/UserClientTest.java new file mode 100644 index 00000000..33ff63b3 --- /dev/null +++ b/src/test/java/com/spotify/github/v3/clients/UserClientTest.java @@ -0,0 +1,84 @@ +/*- + * -\-\- + * github-api + * -- + * Copyright (C) 2016 - 2024 Spotify AB + * -- + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * -/-/- + */ +package com.spotify.github.v3.clients; + +import static java.util.concurrent.CompletableFuture.completedFuture; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import com.spotify.github.jackson.Json; +import com.spotify.github.v3.user.requests.ImmutableSuspensionReason; +import java.util.concurrent.CompletableFuture; +import okhttp3.Response; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class UserClientTest { + + private GitHubClient github; + private UserClient userClient; + + @BeforeEach + public void setUp() { + github = mock(GitHubClient.class); + userClient = new UserClient(github); + Json json = Json.create(); + when(github.json()).thenReturn(json); + } + + @Test + public void testSuspendUserSuccess() throws Exception { + Response response = mock(Response.class); + when(response.code()).thenReturn(204); + when(github.put(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); + final CompletableFuture result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertEquals(1, (int) result.get()); + } + + @Test + public void testSuspendUserFailure() throws Exception { + Response response = mock(Response.class); + when(response.code()).thenReturn(403); + when(github.put(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); + final CompletableFuture result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertEquals(0, (int) result.get()); + } + + @Test + public void testUnSuspendUserSuccess() throws Exception { + Response response = mock(Response.class); + when(response.code()).thenReturn(204); + when(github.delete(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); + final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertEquals(1, (int) result.get()); + } + + @Test + public void testUnSuspendUserFailure() throws Exception { + Response response = mock(Response.class); + when(response.code()).thenReturn(403); + when(github.delete(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); + final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertEquals(0, (int) result.get()); + } +} From 6c60d5b7e06e07d3f1c1c5658789ac504740d64a Mon Sep 17 00:00:00 2001 From: Dennis Granath Date: Tue, 9 Jan 2024 08:22:04 +0100 Subject: [PATCH 2/2] Change signature of suspend/unsuspend method to boolean return value --- .../spotify/github/v3/clients/UserClient.java | 8 ++++---- .../github/v3/clients/UserClientTest.java | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/spotify/github/v3/clients/UserClient.java b/src/main/java/com/spotify/github/v3/clients/UserClient.java index 454e8744..3ea24979 100644 --- a/src/main/java/com/spotify/github/v3/clients/UserClient.java +++ b/src/main/java/com/spotify/github/v3/clients/UserClient.java @@ -44,12 +44,12 @@ static UserClient create(final GitHubClient github) { * @param username username of the user to suspend * @return a CompletableFuture that indicates success or failure */ - public CompletableFuture suspendUser( + public CompletableFuture suspendUser( final String username, final SuspensionReason reason) { final String path = String.format(SUSPEND_USER_TEMPLATE, username); return github .put(path, github.json().toJsonUnchecked(reason)) - .thenApply(resp -> resp.code() == NO_CONTENT ? 1 : 0); + .thenApply(resp -> resp.code() == NO_CONTENT); } /** @@ -58,11 +58,11 @@ public CompletableFuture suspendUser( * @param username username of the user to unsuspend * @return a CompletableFuture that indicates success or failure */ - public CompletableFuture unSuspendUser( + public CompletableFuture unSuspendUser( final String username, final SuspensionReason reason) { final String path = String.format(SUSPEND_USER_TEMPLATE, username); return github .delete(path, github.json().toJsonUnchecked(reason)) - .thenApply(resp -> resp.code() == NO_CONTENT ? 1 : 0); + .thenApply(resp -> resp.code() == NO_CONTENT); } } diff --git a/src/test/java/com/spotify/github/v3/clients/UserClientTest.java b/src/test/java/com/spotify/github/v3/clients/UserClientTest.java index 33ff63b3..0211769b 100644 --- a/src/test/java/com/spotify/github/v3/clients/UserClientTest.java +++ b/src/test/java/com/spotify/github/v3/clients/UserClientTest.java @@ -20,7 +20,7 @@ package com.spotify.github.v3.clients; import static java.util.concurrent.CompletableFuture.completedFuture; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; @@ -51,8 +51,8 @@ public void testSuspendUserSuccess() throws Exception { Response response = mock(Response.class); when(response.code()).thenReturn(204); when(github.put(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); - final CompletableFuture result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); - assertEquals(1, (int) result.get()); + final CompletableFuture result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertTrue(result.get()); } @Test @@ -60,8 +60,8 @@ public void testSuspendUserFailure() throws Exception { Response response = mock(Response.class); when(response.code()).thenReturn(403); when(github.put(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); - final CompletableFuture result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); - assertEquals(0, (int) result.get()); + final CompletableFuture result = userClient.suspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertFalse(result.get()); } @Test @@ -69,8 +69,8 @@ public void testUnSuspendUserSuccess() throws Exception { Response response = mock(Response.class); when(response.code()).thenReturn(204); when(github.delete(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); - final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); - assertEquals(1, (int) result.get()); + final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertTrue(result.get()); } @Test @@ -78,7 +78,7 @@ public void testUnSuspendUserFailure() throws Exception { Response response = mock(Response.class); when(response.code()).thenReturn(403); when(github.delete(eq("/users/username/suspended"), any())).thenReturn(completedFuture(response)); - final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); - assertEquals(0, (int) result.get()); + final CompletableFuture result = userClient.unSuspendUser("username", ImmutableSuspensionReason.builder().reason("That's why").build()); + assertFalse(result.get()); } }