Skip to content

Commit e8be18d

Browse files
committed
Add tests for re-preparing request on refreshed tokens
1 parent 53cb4d8 commit e8be18d

File tree

10 files changed

+481
-0
lines changed

10 files changed

+481
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.kohsuke.github.extras.authorization;
2+
3+
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
4+
import org.junit.Test;
5+
import org.kohsuke.github.AbstractGitHubWireMockTest;
6+
import org.kohsuke.github.GHUser;
7+
import org.kohsuke.github.RateLimitHandler;
8+
import org.kohsuke.github.authorization.AuthorizationProvider;
9+
10+
import java.io.IOException;
11+
12+
public class AuthorizationTokenRefreshTest extends AbstractGitHubWireMockTest {
13+
14+
public AuthorizationTokenRefreshTest() throws IOException {
15+
useDefaultGitHub = false;
16+
}
17+
18+
@Override
19+
protected WireMockConfiguration getWireMockOptions() {
20+
return super.getWireMockOptions().extensions(templating.newResponseTransformer());
21+
}
22+
23+
@Test
24+
public void testRetriedRequestGetsNewAuthorizationTokenWhenOldOneExpires() throws IOException {
25+
snapshotNotAllowed();
26+
gitHub = getGitHubBuilder().withAuthorizationProvider(new RefreshingAuthorizationProvider())
27+
.withEndpoint(mockGitHub.apiServer().baseUrl())
28+
.withRateLimitHandler(RateLimitHandler.WAIT).build();
29+
final GHUser kohsuke = gitHub.getUser("kohsuke");
30+
assertThat("Usernames match", "kohsuke".equals(kohsuke.getLogin()));
31+
}
32+
33+
@Test
34+
public void testRetriedRequestDoesNotGetNewAuthorizationTokenWhenOldOneIsStillValid() throws IOException {
35+
gitHub = getGitHubBuilder().withAuthorizationProvider(() -> "original token")
36+
.withEndpoint(mockGitHub.apiServer().baseUrl())
37+
.withRateLimitHandler(RateLimitHandler.WAIT).build();
38+
final GHUser kohsuke = gitHub.getUser("kohsuke");
39+
assertThat("Usernames match", "kohsuke".equals(kohsuke.getLogin()));
40+
}
41+
42+
static class RefreshingAuthorizationProvider implements AuthorizationProvider {
43+
private boolean used = false;
44+
45+
@Override
46+
public String getEncodedAuthorization() {
47+
if (used) {
48+
return "refreshed token";
49+
}
50+
used = true;
51+
return "original token";
52+
}
53+
}
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "kohsuke",
3+
"id": 50003,
4+
"node_id": "MDQ6VXNlcjUwMDAz",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/50003?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/kohsuke",
8+
"html_url": "https://github.com/kohsuke",
9+
"followers_url": "https://api.github.com/users/kohsuke/followers",
10+
"following_url": "https://api.github.com/users/kohsuke/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/kohsuke/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/kohsuke/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/kohsuke/subscriptions",
14+
"organizations_url": "https://api.github.com/users/kohsuke/orgs",
15+
"repos_url": "https://api.github.com/users/kohsuke/repos",
16+
"events_url": "https://api.github.com/users/kohsuke/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/kohsuke/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Kohsuke Kawaguchi",
21+
"company": "@launchableinc ",
22+
"blog": "https://www.kohsuke.org/",
23+
"location": "San Jose, California",
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 263,
29+
"public_gists": 113,
30+
"followers": 2110,
31+
"following": 3,
32+
"created_at": "2009-01-28T18:53:21Z",
33+
"updated_at": "2023-08-09T13:37:19Z"
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
{
2+
"id": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
3+
"name": "users_kohsuke",
4+
"scenarioName": "Retry with valid credentials",
5+
"requiredScenarioState": "Started",
6+
"newScenarioState": "Retry with the same credentials",
7+
"request": {
8+
"url": "/users/kohsuke",
9+
"method": "GET",
10+
"headers": {
11+
"Accept": {
12+
"equalTo": "application/vnd.github.v3+json"
13+
},
14+
"Authorization": {
15+
"equalTo": "original token"
16+
}
17+
}
18+
},
19+
"response": {
20+
"status": 403,
21+
"headers": {
22+
"Server": "GitHub.com",
23+
"Date": "Thu, 10 Aug 2023 09:12:37 GMT",
24+
"Content-Type": "application/json; charset=utf-8",
25+
"Cache-Control": "public, max-age=60, s-maxage=60",
26+
"Vary": [
27+
"Accept",
28+
"Accept-Encoding, Accept, X-Requested-With"
29+
],
30+
"ETag": "W/\"e2b462e6fe85486beb06033fe196d7c7b7669a96ddbb8f411b91f56288b31b3b\"",
31+
"Last-Modified": "Wed, 09 Aug 2023 13:37:19 GMT",
32+
"X-GitHub-Media-Type": "github.v3; format=json",
33+
"x-github-api-version-selected": "2022-11-28",
34+
"X-RateLimit-Limit": "60",
35+
"X-RateLimit-Remaining": "0",
36+
"X-RateLimit-Reset": "{{testStartDate offset='3 seconds' format='unix'}}",
37+
"X-RateLimit-Used": "1",
38+
"X-RateLimit-Resource": "core",
39+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
40+
"Access-Control-Allow-Origin": "*",
41+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
42+
"X-Frame-Options": "deny",
43+
"X-Content-Type-Options": "nosniff",
44+
"X-XSS-Protection": "0",
45+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
46+
"Content-Security-Policy": "default-src 'none'",
47+
"X-GitHub-Request-Id": "BDE4:73C9:BF78362:C13B808:64D4AA05"
48+
}
49+
},
50+
"uuid": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
51+
"persistent": true,
52+
"insertionIndex": 1
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"id": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
3+
"name": "users_kohsuke",
4+
"requiredScenarioState": "Retry with the same credentials",
5+
"request": {
6+
"url": "/users/kohsuke",
7+
"method": "GET",
8+
"headers": {
9+
"Accept": {
10+
"equalTo": "application/vnd.github.v3+json"
11+
},
12+
"Authorization": {
13+
"equalTo": "original token"
14+
}
15+
}
16+
},
17+
"response": {
18+
"status": 200,
19+
"bodyFileName": "users_kohsuke-1.json",
20+
"headers": {
21+
"Server": "GitHub.com",
22+
"Date": "Thu, 10 Aug 2023 09:12:37 GMT",
23+
"Content-Type": "application/json; charset=utf-8",
24+
"Cache-Control": "public, max-age=60, s-maxage=60",
25+
"Vary": [
26+
"Accept",
27+
"Accept-Encoding, Accept, X-Requested-With"
28+
],
29+
"ETag": "W/\"e2b462e6fe85486beb06033fe196d7c7b7669a96ddbb8f411b91f56288b31b3b\"",
30+
"Last-Modified": "Wed, 09 Aug 2023 13:37:19 GMT",
31+
"X-GitHub-Media-Type": "github.v3; format=json",
32+
"x-github-api-version-selected": "2022-11-28",
33+
"X-RateLimit-Limit": "60",
34+
"X-RateLimit-Remaining": "59",
35+
"X-RateLimit-Reset": "{{testStartDate offset='1 hours' format='unix'}}",
36+
"X-RateLimit-Used": "1",
37+
"X-RateLimit-Resource": "core",
38+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
39+
"Access-Control-Allow-Origin": "*",
40+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
41+
"X-Frame-Options": "deny",
42+
"X-Content-Type-Options": "nosniff",
43+
"X-XSS-Protection": "0",
44+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
45+
"Content-Security-Policy": "default-src 'none'",
46+
"X-GitHub-Request-Id": "BDE4:73C9:BF78362:C13B808:64D4AA05"
47+
}
48+
},
49+
"uuid": "d37116f2-558c-4543-9d1d-e2241cfcaf54",
50+
"persistent": true,
51+
"insertionIndex": 1
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "emrebasar",
3+
"id": 3469498,
4+
"node_id": "MDQ6VXNlcjM0Njk0OTg=",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/3469498?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/emrebasar",
8+
"html_url": "https://github.com/emrebasar",
9+
"followers_url": "https://api.github.com/users/emrebasar/followers",
10+
"following_url": "https://api.github.com/users/emrebasar/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/emrebasar/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/emrebasar/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/emrebasar/subscriptions",
14+
"organizations_url": "https://api.github.com/users/emrebasar/orgs",
15+
"repos_url": "https://api.github.com/users/emrebasar/repos",
16+
"events_url": "https://api.github.com/users/emrebasar/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/emrebasar/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "R. Emre Basar",
21+
"company": null,
22+
"blog": "",
23+
"location": null,
24+
"email": null,
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 3,
29+
"public_gists": 0,
30+
"followers": 2,
31+
"following": 0,
32+
"created_at": "2013-02-04T08:26:33Z",
33+
"updated_at": "2023-06-29T21:22:45Z"
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"login": "kohsuke",
3+
"id": 50003,
4+
"node_id": "MDQ6VXNlcjUwMDAz",
5+
"avatar_url": "https://avatars.githubusercontent.com/u/50003?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/kohsuke",
8+
"html_url": "https://github.com/kohsuke",
9+
"followers_url": "https://api.github.com/users/kohsuke/followers",
10+
"following_url": "https://api.github.com/users/kohsuke/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/kohsuke/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/kohsuke/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/kohsuke/subscriptions",
14+
"organizations_url": "https://api.github.com/users/kohsuke/orgs",
15+
"repos_url": "https://api.github.com/users/kohsuke/repos",
16+
"events_url": "https://api.github.com/users/kohsuke/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/kohsuke/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Kohsuke Kawaguchi",
21+
"company": "@launchableinc ",
22+
"blog": "https://www.kohsuke.org/",
23+
"location": "San Jose, California",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": null,
27+
"twitter_username": null,
28+
"public_repos": 263,
29+
"public_gists": 113,
30+
"followers": 2098,
31+
"following": 3,
32+
"created_at": "2009-01-28T18:53:21Z",
33+
"updated_at": "2023-05-16T02:35:24Z"
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"id": "34cadfa1-f969-4d8d-a855-d4073878d0d8",
3+
"name": "user",
4+
"request": {
5+
"url": "/user",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "application/vnd.github.v3+json"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "user-1.json",
16+
"headers": {
17+
"Server": "GitHub.com",
18+
"Date": "Tue, 04 Jul 2023 09:27:51 GMT",
19+
"Content-Type": "application/json; charset=utf-8",
20+
"Cache-Control": "private, max-age=60, s-maxage=60",
21+
"Vary": [
22+
"Accept, Authorization, Cookie, X-GitHub-OTP",
23+
"Accept-Encoding, Accept, X-Requested-With"
24+
],
25+
"ETag": "W/\"1ccca51d2d215d0aa63c9c0e7912237f2e1f42bd17ac8e9ab30f539d9673fd4e\"",
26+
"Last-Modified": "Thu, 29 Jun 2023 21:22:45 GMT",
27+
"X-OAuth-Scopes": "gist, read:org, repo",
28+
"X-Accepted-OAuth-Scopes": "",
29+
"x-oauth-client-id": "178c6fc778ccc68e1d6a",
30+
"X-GitHub-Media-Type": "github.v3; format=json",
31+
"x-github-api-version-selected": "2022-11-28",
32+
"X-RateLimit-Limit": "5000",
33+
"X-RateLimit-Remaining": "4989",
34+
"X-RateLimit-Reset": "1688466174",
35+
"X-RateLimit-Used": "11",
36+
"X-RateLimit-Resource": "core",
37+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
38+
"Access-Control-Allow-Origin": "*",
39+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
40+
"X-Frame-Options": "deny",
41+
"X-Content-Type-Options": "nosniff",
42+
"X-XSS-Protection": "0",
43+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
44+
"Content-Security-Policy": "default-src 'none'",
45+
"X-GitHub-Request-Id": "BC5B:03C1:15BF2AD:1603E7D:64A3E617"
46+
}
47+
},
48+
"uuid": "34cadfa1-f969-4d8d-a855-d4073878d0d8",
49+
"persistent": true,
50+
"insertionIndex": 1
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"id": "c00fbe9b-6c8a-4f61-b0f1-ddfd442bd4f7",
3+
"name": "users_kohsuke",
4+
"scenarioName": "Retry with expired credentials",
5+
"requiredScenarioState": "Started",
6+
"newScenarioState": "Unauthorized",
7+
"request": {
8+
"url": "/users/kohsuke",
9+
"method": "GET",
10+
"headers": {
11+
"Accept": {
12+
"equalTo": "application/vnd.github.v3+json"
13+
},
14+
"Authorization": {
15+
"equalTo": "original token"
16+
}
17+
}
18+
},
19+
"response": {
20+
"status": 403,
21+
"headers": {
22+
"Server": "GitHub.com",
23+
"Date": "Tue, 04 Jul 2023 09:27:52 GMT",
24+
"Content-Type": "application/json; charset=utf-8",
25+
"Cache-Control": "private, max-age=60, s-maxage=60",
26+
"Vary": [
27+
"Accept, Authorization, Cookie, X-GitHub-OTP",
28+
"Accept-Encoding, Accept, X-Requested-With"
29+
],
30+
"ETag": "W/\"f41fda58171c5e0eca7018225db5009319a5b17852fc44db7ad6e4d0f7ac823a\"",
31+
"Last-Modified": "Tue, 16 May 2023 02:35:24 GMT",
32+
"X-OAuth-Scopes": "gist, read:org, repo",
33+
"X-Accepted-OAuth-Scopes": "",
34+
"x-oauth-client-id": "178c6fc778ccc68e1d6a",
35+
"X-GitHub-Media-Type": "github.v3; format=json",
36+
"x-github-api-version-selected": "2022-11-28",
37+
"X-RateLimit-Limit": "5000",
38+
"X-RateLimit-Remaining": "0",
39+
"X-RateLimit-Reset": "{{testStartDate offset='1 seconds' format='unix'}}",
40+
"X-RateLimit-Used": "13",
41+
"X-RateLimit-Resource": "core",
42+
"Access-Control-Expose-Headers": "ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset",
43+
"Access-Control-Allow-Origin": "*",
44+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
45+
"X-Frame-Options": "deny",
46+
"X-Content-Type-Options": "nosniff",
47+
"X-XSS-Protection": "0",
48+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
49+
"Content-Security-Policy": "default-src 'none'",
50+
"X-GitHub-Request-Id": "BC76:39D5:E12F87A:E3E5583:64A3E618"
51+
}
52+
},
53+
"uuid": "c00fbe9b-6c8a-4f61-b0f1-ddfd442bd4f7",
54+
"persistent": true,
55+
"insertionIndex": 2
56+
}

0 commit comments

Comments
 (0)