|
| 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