|
22 | 22 |
|
23 | 23 | import static com.google.common.io.Resources.getResource;
|
24 | 24 | import static java.nio.charset.Charset.defaultCharset;
|
| 25 | +import static java.util.concurrent.CompletableFuture.completedFuture; |
| 26 | +import static java.util.concurrent.CompletableFuture.failedFuture; |
25 | 27 | import static org.hamcrest.CoreMatchers.containsString;
|
26 | 28 | import static org.hamcrest.MatcherAssert.assertThat;
|
27 | 29 | import static org.hamcrest.collection.IsMapContaining.hasEntry;
|
|
33 | 35 | import com.google.common.io.Resources;
|
34 | 36 | import com.spotify.github.Tracer;
|
35 | 37 | import com.spotify.github.v3.checks.CheckSuiteResponseList;
|
| 38 | +import com.spotify.github.v3.checks.Installation; |
36 | 39 | import com.spotify.github.v3.exceptions.ReadOnlyRepositoryException;
|
37 | 40 | import com.spotify.github.v3.exceptions.RequestNotOkException;
|
38 | 41 | import com.spotify.github.v3.repos.CommitItem;
|
|
42 | 45 | import java.io.IOException;
|
43 | 46 | import java.net.URI;
|
44 | 47 | import java.net.URISyntaxException;
|
| 48 | +import java.util.HashMap; |
45 | 49 | import java.util.List;
|
46 | 50 | import java.util.Optional;
|
47 | 51 | import java.util.concurrent.CompletableFuture;
|
@@ -228,4 +232,59 @@ public void testGetCheckSuites() throws Throwable {
|
228 | 232 | assertThat(result.checkSuites().get(0).app().get().slug().get(), is("octoapp"));
|
229 | 233 |
|
230 | 234 | }
|
| 235 | + |
| 236 | + @Test |
| 237 | + void asAppScopedClientGetsUserClientIfOrgClientNotFound() { |
| 238 | + var appGithub = GitHubClient.create(client, URI.create("http://bogus"), new byte[] {}, 1); |
| 239 | + var githubSpy = spy(appGithub); |
| 240 | + |
| 241 | + var orgClientMock = mock(OrganisationClient.class); |
| 242 | + when(githubSpy.createOrganisationClient("owner")).thenReturn(orgClientMock); |
| 243 | + |
| 244 | + var appClientMock = mock(GithubAppClient.class); |
| 245 | + when(orgClientMock.createGithubAppClient()).thenReturn(appClientMock); |
| 246 | + when(appClientMock.getInstallation()).thenReturn(failedFuture(new RequestNotOkException("", "", 404, "", new HashMap<>()))); |
| 247 | + |
| 248 | + var userClientMock = mock(UserClient.class); |
| 249 | + when(githubSpy.createUserClient("owner")).thenReturn(userClientMock); |
| 250 | + |
| 251 | + var appClientMock2 = mock(GithubAppClient.class); |
| 252 | + when(userClientMock.createGithubAppClient()).thenReturn(appClientMock2); |
| 253 | + |
| 254 | + var installationMock = mock(Installation.class); |
| 255 | + when(appClientMock2.getUserInstallation()).thenReturn(completedFuture(installationMock)); |
| 256 | + when(installationMock.id()).thenReturn(1); |
| 257 | + |
| 258 | + var maybeScopedClient = githubSpy.asAppScopedClient("owner").toCompletableFuture().join(); |
| 259 | + |
| 260 | + Assertions.assertTrue(maybeScopedClient.isPresent()); |
| 261 | + verify(githubSpy, times(1)).createOrganisationClient("owner"); |
| 262 | + verify(githubSpy, times(1)).createUserClient("owner"); |
| 263 | + } |
| 264 | + |
| 265 | + @Test |
| 266 | + void asAppScopedClientReturnsEmptyIfNoInstallation() { |
| 267 | + var appGithub = GitHubClient.create(client, URI.create("http://bogus"), new byte[] {}, 1); |
| 268 | + var githubSpy = spy(appGithub); |
| 269 | + |
| 270 | + var orgClientMock = mock(OrganisationClient.class); |
| 271 | + when(githubSpy.createOrganisationClient("owner")).thenReturn(orgClientMock); |
| 272 | + |
| 273 | + var appClientMock = mock(GithubAppClient.class); |
| 274 | + when(orgClientMock.createGithubAppClient()).thenReturn(appClientMock); |
| 275 | + when(appClientMock.getInstallation()).thenReturn(failedFuture(new RequestNotOkException("", "", 404, "", new HashMap<>()))); |
| 276 | + |
| 277 | + var userClientMock = mock(UserClient.class); |
| 278 | + when(githubSpy.createUserClient("owner")).thenReturn(userClientMock); |
| 279 | + |
| 280 | + var appClientMock2 = mock(GithubAppClient.class); |
| 281 | + when(userClientMock.createGithubAppClient()).thenReturn(appClientMock2); |
| 282 | + |
| 283 | + var installationMock = mock(Installation.class); |
| 284 | + when(appClientMock2.getUserInstallation()).thenReturn(failedFuture(new RequestNotOkException("", "", 404, "", new HashMap<>()))); |
| 285 | + when(installationMock.id()).thenReturn(1); |
| 286 | + |
| 287 | + var maybeScopedClient = githubSpy.asAppScopedClient("owner").toCompletableFuture().join(); |
| 288 | + Assertions.assertTrue(maybeScopedClient.isEmpty()); |
| 289 | + } |
231 | 290 | }
|
0 commit comments