|
20 | 20 |
|
21 | 21 | package com.spotify.github.v3.clients;
|
22 | 22 |
|
23 |
| -import static java.nio.charset.StandardCharsets.UTF_8; |
24 | 23 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 24 | +import static org.hamcrest.Matchers.containsInAnyOrder; |
25 | 25 | import static org.hamcrest.core.Is.is;
|
26 |
| -import static org.mockito.Mockito.mock; |
27 |
| -import static org.mockito.Mockito.when; |
28 |
| - |
29 |
| -import com.google.common.io.Resources; |
30 |
| -import com.spotify.github.jackson.Json; |
31 |
| -import com.spotify.github.v3.checks.AccessToken; |
32 |
| -import com.spotify.github.v3.checks.InstallationList; |
33 |
| -import java.io.IOException; |
34 |
| -import java.io.UncheckedIOException; |
| 26 | + |
| 27 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 28 | +import com.spotify.github.FixtureHelper; |
| 29 | +import com.spotify.github.v3.apps.InstallationRepositoriesResponse; |
| 30 | +import com.spotify.github.v3.checks.Installation; |
| 31 | +import java.io.File; |
| 32 | +import java.net.URI; |
35 | 33 | import java.time.ZonedDateTime;
|
| 34 | +import java.util.List; |
| 35 | +import java.util.concurrent.TimeUnit; |
| 36 | +import okhttp3.mockwebserver.MockResponse; |
| 37 | +import okhttp3.mockwebserver.MockWebServer; |
| 38 | +import okhttp3.mockwebserver.RecordedRequest; |
36 | 39 | import org.junit.Before;
|
| 40 | +import org.junit.Rule; |
37 | 41 | import org.junit.Test;
|
38 | 42 |
|
39 | 43 | public class GithubAppClientTest {
|
40 | 44 |
|
41 |
| - private static final String FIXTURES_PATH = "com/spotify/github/v3/githubapp/"; |
42 |
| - private Json json; |
| 45 | + @Rule |
| 46 | + public final MockWebServer mockServer = new MockWebServer(); |
43 | 47 |
|
44 |
| - public static String loadResource(final String path) { |
45 |
| - try { |
46 |
| - return Resources.toString(Resources.getResource(path), UTF_8); |
47 |
| - } catch (IOException e) { |
48 |
| - throw new UncheckedIOException(e); |
49 |
| - } |
50 |
| - } |
| 48 | + private final ObjectMapper objectMapper = new ObjectMapper(); |
| 49 | + private final int appId = 42; |
| 50 | + private GithubAppClient client; |
51 | 51 |
|
52 | 52 | @Before
|
53 |
| - public void setUp() { |
54 |
| - final GitHubClient github = mock(GitHubClient.class); |
55 |
| - final GithubAppClient client = new GithubAppClient(github, "org", "repo"); |
56 |
| - json = Json.create(); |
57 |
| - when(github.json()).thenReturn(json); |
| 53 | + public void setUp() throws Exception { |
| 54 | + URI uri = mockServer.url("").uri(); |
| 55 | + File key = FixtureHelper.loadFile("githubapp/key.pem"); |
| 56 | + |
| 57 | + GitHubClient rootclient = GitHubClient.create(uri, key, appId); |
| 58 | + client = rootclient.createRepositoryClient("", "").createGithubAppClient(); |
58 | 59 | }
|
59 | 60 |
|
60 | 61 | @Test
|
61 | 62 | public void getInstallationsList() throws Exception {
|
62 |
| - final InstallationList installations = |
63 |
| - json.fromJson( |
64 |
| - loadResource(FIXTURES_PATH + "installations-list.json"), InstallationList.class); |
65 |
| - |
66 |
| - assertThat(installations.totalCount(), is(2)); |
67 |
| - assertThat(installations.installations().get(0).account().login(), is("github")); |
68 |
| - assertThat(installations.installations().get(0).id(), is(1)); |
69 |
| - assertThat(installations.installations().get(1).account().login(), is("octocat")); |
70 |
| - assertThat(installations.installations().get(1).id(), is(3)); |
| 63 | + mockServer.enqueue( |
| 64 | + new MockResponse() |
| 65 | + .setResponseCode(200) |
| 66 | + .setBody(FixtureHelper.loadFixture("githubapp/installations-list.json"))); |
| 67 | + |
| 68 | + List<Installation> installations = client.getInstallations().join(); |
| 69 | + |
| 70 | + assertThat(installations.size(), is(2)); |
| 71 | + assertThat(installations.get(0).account().login(), is("github")); |
| 72 | + assertThat(installations.get(0).id(), is(1)); |
| 73 | + assertThat(installations.get(1).account().login(), is("octocat")); |
| 74 | + assertThat(installations.get(1).id(), is(3)); |
| 75 | + |
| 76 | + RecordedRequest recordedRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 77 | + assertThat(recordedRequest.getRequestUrl().encodedPath(), is("/app/installations")); |
| 78 | + assertThat(recordedRequest.getRequestUrl().queryParameter("per_page"), is("100")); |
| 79 | + |
| 80 | + assertThat( |
| 81 | + recordedRequest.getHeaders().values("Accept"), |
| 82 | + containsInAnyOrder("application/json", "application/vnd.github.machine-man-preview+json")); |
71 | 83 | }
|
72 | 84 |
|
73 | 85 | @Test
|
74 |
| - public void canDeserializeToken() throws IOException { |
75 |
| - final AccessToken accessToken = |
76 |
| - json.fromJson(loadResource(FIXTURES_PATH + "access-token.json"), AccessToken.class); |
77 |
| - assertThat(accessToken.token(), is("v1.1f699f1069f60xxx")); |
78 |
| - assertThat(accessToken.expiresAt(), is(ZonedDateTime.parse("2016-07-11T22:14:10Z"))); |
| 86 | + public void listAccessibleRepositories() throws Exception { |
| 87 | + // response for POST /app/installations/:id/access_tokens |
| 88 | + final String installationAccessToken = "abc123-secret"; |
| 89 | + mockServer.enqueue( |
| 90 | + new MockResponse() |
| 91 | + .setResponseCode(201) |
| 92 | + // this might not serialize 100% the same as the Json class's ObjectMapper but should be |
| 93 | + // fine for this test |
| 94 | + .setBody( |
| 95 | + objectMapper |
| 96 | + .createObjectNode() |
| 97 | + .put("token", installationAccessToken) |
| 98 | + .put("expires_at", ZonedDateTime.now().plusHours(1).toString()) |
| 99 | + .toString())); |
| 100 | + |
| 101 | + // response for GET /installation/repositories |
| 102 | + mockServer.enqueue( |
| 103 | + new MockResponse() |
| 104 | + .setResponseCode(200) |
| 105 | + .setBody(FixtureHelper.loadFixture("githubapp/accessible-repositories.json"))); |
| 106 | + |
| 107 | + InstallationRepositoriesResponse response = |
| 108 | + client.listAccessibleRepositories(1234).join(); |
| 109 | + |
| 110 | + assertThat(response.totalCount(), is(2)); |
| 111 | + assertThat(response.repositories().size(), is(2)); |
| 112 | + assertThat(response.repositories().get(0).id(), is(1)); |
| 113 | + assertThat(response.repositories().get(1).id(), is(2)); |
| 114 | + |
| 115 | + RecordedRequest accessTokenRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 116 | + assertThat(accessTokenRequest.getMethod(), is("POST")); |
| 117 | + assertThat( |
| 118 | + accessTokenRequest.getRequestUrl().encodedPath(), |
| 119 | + is("/app/installations/1234/access_tokens")); |
| 120 | + |
| 121 | + RecordedRequest listReposRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 122 | + assertThat(listReposRequest.getMethod(), is("GET")); |
| 123 | + assertThat(listReposRequest.getRequestUrl().encodedPath(), is("/installation/repositories")); |
79 | 124 | }
|
80 | 125 | }
|
0 commit comments