|
24 | 24 | import static java.nio.charset.Charset.defaultCharset; |
25 | 25 | import static org.hamcrest.MatcherAssert.assertThat; |
26 | 26 | import static org.hamcrest.Matchers.containsInAnyOrder; |
| 27 | +import static org.hamcrest.Matchers.containsString; |
27 | 28 | import static org.hamcrest.core.Is.is; |
28 | 29 |
|
29 | 30 | import com.fasterxml.jackson.databind.ObjectMapper; |
30 | 31 | import com.google.common.io.Resources; |
31 | 32 | import com.spotify.github.FixtureHelper; |
32 | 33 | import com.spotify.github.v3.apps.InstallationRepositoriesResponse; |
| 34 | +import com.spotify.github.v3.apps.requests.AccessTokenRequest; |
| 35 | +import com.spotify.github.v3.apps.requests.ImmutableAccessTokenRequest; |
| 36 | +import com.spotify.github.v3.checks.AccessToken; |
33 | 37 | import com.spotify.github.v3.checks.App; |
34 | 38 | import com.spotify.github.v3.checks.Installation; |
35 | 39 | import java.io.File; |
@@ -182,4 +186,68 @@ public void getAuthenticatedApp() throws Exception { |
182 | 186 | recordedRequest.getHeaders().values("Authorization").get(0).startsWith("Bearer "), |
183 | 187 | is(true)); |
184 | 188 | } |
| 189 | + |
| 190 | + @Test |
| 191 | + public void getAccessTokenWithoutScoping() throws Exception { |
| 192 | + mockServer.enqueue( |
| 193 | + new MockResponse() |
| 194 | + .setResponseCode(201) |
| 195 | + .setBody(FixtureHelper.loadFixture("githubapp/access-token.json"))); |
| 196 | + |
| 197 | + AccessToken token = client.getAccessToken(1234).join(); |
| 198 | + |
| 199 | + assertThat(token.token(), is("ghs_16C7e42F292c6912E7710c838347Ae178B4a")); |
| 200 | + |
| 201 | + RecordedRequest recordedRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 202 | + assertThat(recordedRequest.getMethod(), is("POST")); |
| 203 | + assertThat( |
| 204 | + recordedRequest.getRequestUrl().encodedPath(), |
| 205 | + is("/app/installations/1234/access_tokens")); |
| 206 | + assertThat(recordedRequest.getBody().readUtf8(), is("")); |
| 207 | + } |
| 208 | + |
| 209 | + @Test |
| 210 | + public void getAccessTokenWithRepositoryScoping() throws Exception { |
| 211 | + mockServer.enqueue( |
| 212 | + new MockResponse() |
| 213 | + .setResponseCode(201) |
| 214 | + .setBody(FixtureHelper.loadFixture("githubapp/access-token.json"))); |
| 215 | + |
| 216 | + AccessTokenRequest request = ImmutableAccessTokenRequest.builder() |
| 217 | + .repositories(List.of("Hello-World")) |
| 218 | + .repositoryIds(List.of(1)) |
| 219 | + .build(); |
| 220 | + |
| 221 | + AccessToken token = client.getAccessToken(1234, request).join(); |
| 222 | + |
| 223 | + assertThat(token.token(), is("ghs_16C7e42F292c6912E7710c838347Ae178B4a")); |
| 224 | + |
| 225 | + RecordedRequest recordedRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 226 | + assertThat(recordedRequest.getMethod(), is("POST")); |
| 227 | + assertThat( |
| 228 | + recordedRequest.getRequestUrl().encodedPath(), |
| 229 | + is("/app/installations/1234/access_tokens")); |
| 230 | + |
| 231 | + String requestBody = recordedRequest.getBody().readUtf8(); |
| 232 | + assertThat(requestBody, containsString("\"repositories\":[\"Hello-World\"]")); |
| 233 | + assertThat(requestBody, containsString("\"repository_ids\":[1]")); |
| 234 | + } |
| 235 | + |
| 236 | + @Test |
| 237 | + public void getAccessTokenWithEmptyRequest() throws Exception { |
| 238 | + mockServer.enqueue( |
| 239 | + new MockResponse() |
| 240 | + .setResponseCode(201) |
| 241 | + .setBody(FixtureHelper.loadFixture("githubapp/access-token.json"))); |
| 242 | + |
| 243 | + AccessTokenRequest emptyRequest = ImmutableAccessTokenRequest.builder().build(); |
| 244 | + |
| 245 | + AccessToken token = client.getAccessToken(1234, emptyRequest).join(); |
| 246 | + |
| 247 | + assertThat(token.token(), is("ghs_16C7e42F292c6912E7710c838347Ae178B4a")); |
| 248 | + |
| 249 | + RecordedRequest recordedRequest = mockServer.takeRequest(1, TimeUnit.MILLISECONDS); |
| 250 | + String requestBody = recordedRequest.getBody().readUtf8(); |
| 251 | + assertThat(requestBody, is("{}")); |
| 252 | + } |
185 | 253 | } |
0 commit comments