Skip to content

Commit 65ae41c

Browse files
committed
Fix tests for isCredentialsValid
1 parent 796c644 commit 65ae41c

File tree

9 files changed

+251
-20
lines changed

9 files changed

+251
-20
lines changed

src/test/java/org/kohsuke/github/AppTest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,34 @@ private void cleanupUserRepository(final String name) throws IOException {
7777
@Test
7878
public void testCredentialValid() throws IOException {
7979
assertTrue(gitHub.isCredentialValid());
80+
assertThat(gitHub.lastRateLimit().getCore(), not(instanceOf(GHRateLimit.UnknownLimitRecord.class)));
81+
assertThat(gitHub.lastRateLimit().getCore().getLimit(), equalTo(5000));
8082

81-
// TODO: Doh, this doesn't go through the proxy. Needs rewriting.
82-
GitHub connect = GitHub.connect("totally", "bogus");
83-
assertFalse(connect.isCredentialValid());
83+
gitHub = getGitHubBuilder().withOAuthToken("bogus", "user")
84+
.withEndpoint(mockGitHub.apiServer().baseUrl())
85+
.build();
86+
assertThat(gitHub.lastRateLimit(), nullValue());
87+
assertFalse(gitHub.isCredentialValid());
88+
// For invalid credentials, we get a 401 but it includes anonymous rate limit headers
89+
assertThat(gitHub.lastRateLimit().getCore(), not(instanceOf(GHRateLimit.UnknownLimitRecord.class)));
90+
assertThat(gitHub.lastRateLimit().getCore().getLimit(), equalTo(60));
91+
}
8492

85-
// TODO: Add GHE test - where RateLimit returns 404.
93+
@Test
94+
public void testCredentialValidEnterprise() throws IOException {
95+
// Simulated GHE: getRateLimit returns 404
96+
assertThat(gitHub.lastRateLimit(), nullValue());
97+
assertTrue(gitHub.isCredentialValid());
98+
// lastRateLimit stays null when 404 is encountered
99+
assertThat(gitHub.lastRateLimit(), nullValue());
100+
101+
gitHub = getGitHubBuilder().withOAuthToken("bogus", "user")
102+
.withEndpoint(mockGitHub.apiServer().baseUrl())
103+
.build();
104+
assertThat(gitHub.lastRateLimit(), nullValue());
105+
assertFalse(gitHub.isCredentialValid());
106+
// Simulated GHE: For invalid credentials, we get a 401 that does not include ratelimit info
107+
assertThat(gitHub.lastRateLimit(), nullValue());
86108
}
87109

88110
@Test

src/test/resources/org/kohsuke/github/AppTest/wiremock/testCredentialValid/__files/rate_limit-2.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,33 @@
22
"resources": {
33
"core": {
44
"limit": 5000,
5-
"remaining": 4998,
5+
"remaining": 4994,
66
"reset": 1585671841
77
},
88
"search": {
99
"limit": 30,
1010
"remaining": 30,
11-
"reset": 1585668302
11+
"reset": 1585671895
1212
},
1313
"graphql": {
1414
"limit": 5000,
1515
"remaining": 5000,
16-
"reset": 1585671842
16+
"reset": 1585675435
1717
},
1818
"integration_manifest": {
1919
"limit": 5000,
2020
"remaining": 5000,
21-
"reset": 1585671842
21+
"reset": 1585675435
2222
},
2323
"source_import": {
2424
"limit": 100,
2525
"remaining": 100,
26-
"reset": 1585668302
26+
"reset": 1585671895
2727
}
2828
},
2929
"rate": {
3030
"limit": 5000,
31-
"remaining": 4998,
31+
"remaining": 4994,
3232
"reset": 1585671841
3333
}
3434
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testCredentialValid/mappings/rate_limit-2.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "776ae4cc-bd8c-4c7d-b299-4ba1b2c11ad0",
2+
"id": "c875f18e-a0af-4114-8d1f-123b5377a52c",
33
"name": "rate_limit",
44
"request": {
55
"url": "/rate_limit",
@@ -14,12 +14,12 @@
1414
"status": 200,
1515
"bodyFileName": "rate_limit-2.json",
1616
"headers": {
17-
"Date": "Tue, 31 Mar 2020 15:24:02 GMT",
17+
"Date": "Tue, 31 Mar 2020 16:23:55 GMT",
1818
"Content-Type": "application/json; charset=utf-8",
1919
"Server": "GitHub.com",
2020
"Status": "200 OK",
2121
"X-RateLimit-Limit": "5000",
22-
"X-RateLimit-Remaining": "4998",
22+
"X-RateLimit-Remaining": "4994",
2323
"X-RateLimit-Reset": "1585671841",
2424
"Cache-Control": "no-cache",
2525
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
@@ -32,10 +32,13 @@
3232
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
3333
"Content-Security-Policy": "default-src 'none'",
3434
"Vary": "Accept-Encoding, Accept, X-Requested-With",
35-
"X-GitHub-Request-Id": "FEBE:52B1:FCCEE:123132:5E836091"
35+
"X-GitHub-Request-Id": "C14E:8628:147DC:18E26:5E836E9A"
3636
}
3737
},
38-
"uuid": "776ae4cc-bd8c-4c7d-b299-4ba1b2c11ad0",
38+
"uuid": "c875f18e-a0af-4114-8d1f-123b5377a52c",
3939
"persistent": true,
40+
"scenarioName": "scenario-1-rate_limit",
41+
"requiredScenarioState": "Started",
42+
"newScenarioState": "scenario-1-rate_limit-2",
4043
"insertionIndex": 2
4144
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"id": "5ba21199-9f1d-420c-87e9-ab30d3e73884",
3+
"name": "rate_limit",
4+
"request": {
5+
"url": "/rate_limit",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 401,
15+
"body": "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://developer.github.com/v3\"}",
16+
"headers": {
17+
"Date": "Tue, 31 Mar 2020 16:23:55 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "401 Unauthorized",
21+
"X-GitHub-Media-Type": "unknown, github.v3",
22+
"X-RateLimit-Limit": "60",
23+
"X-RateLimit-Remaining": "59",
24+
"X-RateLimit-Reset": "1585675435",
25+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
26+
"X-Frame-Options": "deny",
27+
"X-Content-Type-Options": "nosniff",
28+
"X-XSS-Protection": "1; mode=block",
29+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
30+
"Content-Security-Policy": "default-src 'none'",
31+
"Vary": "Accept-Encoding, Accept, X-Requested-With",
32+
"X-GitHub-Request-Id": "C14E:8628:147E6:18E5B:5E836E9B"
33+
}
34+
},
35+
"uuid": "5ba21199-9f1d-420c-87e9-ab30d3e73884",
36+
"persistent": true,
37+
"scenarioName": "scenario-1-rate_limit",
38+
"requiredScenarioState": "scenario-1-rate_limit-2",
39+
"insertionIndex": 3
40+
}

src/test/resources/org/kohsuke/github/AppTest/wiremock/testCredentialValid/mappings/user-1.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "218a25c5-b10d-4371-a446-bd4b220c2402",
2+
"id": "27891e8f-aae7-459b-9860-a4f505cec688",
33
"name": "user",
44
"request": {
55
"url": "/user",
@@ -14,12 +14,12 @@
1414
"status": 200,
1515
"bodyFileName": "user-1.json",
1616
"headers": {
17-
"Date": "Tue, 31 Mar 2020 15:24:01 GMT",
17+
"Date": "Tue, 31 Mar 2020 16:23:54 GMT",
1818
"Content-Type": "application/json; charset=utf-8",
1919
"Server": "GitHub.com",
2020
"Status": "200 OK",
2121
"X-RateLimit-Limit": "5000",
22-
"X-RateLimit-Remaining": "4999",
22+
"X-RateLimit-Remaining": "4995",
2323
"X-RateLimit-Reset": "1585671841",
2424
"Cache-Control": "private, max-age=60, s-maxage=60",
2525
"Vary": [
@@ -37,10 +37,10 @@
3737
"X-XSS-Protection": "1; mode=block",
3838
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
3939
"Content-Security-Policy": "default-src 'none'",
40-
"X-GitHub-Request-Id": "FEBE:52B1:FCCA5:12311D:5E836091"
40+
"X-GitHub-Request-Id": "C14E:8628:147B0:18E1A:5E836E9A"
4141
}
4242
},
43-
"uuid": "218a25c5-b10d-4371-a446-bd4b220c2402",
43+
"uuid": "27891e8f-aae7-459b-9860-a4f505cec688",
4444
"persistent": true,
4545
"insertionIndex": 1
4646
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"login": "bitwiseman",
3+
"id": 1958953,
4+
"node_id": "MDQ6VXNlcjE5NTg5NTM=",
5+
"avatar_url": "https://avatars3.githubusercontent.com/u/1958953?v=4",
6+
"gravatar_id": "",
7+
"url": "https://api.github.com/users/bitwiseman",
8+
"html_url": "https://github.com/bitwiseman",
9+
"followers_url": "https://api.github.com/users/bitwiseman/followers",
10+
"following_url": "https://api.github.com/users/bitwiseman/following{/other_user}",
11+
"gists_url": "https://api.github.com/users/bitwiseman/gists{/gist_id}",
12+
"starred_url": "https://api.github.com/users/bitwiseman/starred{/owner}{/repo}",
13+
"subscriptions_url": "https://api.github.com/users/bitwiseman/subscriptions",
14+
"organizations_url": "https://api.github.com/users/bitwiseman/orgs",
15+
"repos_url": "https://api.github.com/users/bitwiseman/repos",
16+
"events_url": "https://api.github.com/users/bitwiseman/events{/privacy}",
17+
"received_events_url": "https://api.github.com/users/bitwiseman/received_events",
18+
"type": "User",
19+
"site_admin": false,
20+
"name": "Liam Newman",
21+
"company": "Cloudbees, Inc.",
22+
"blog": "",
23+
"location": "Seattle, WA, USA",
24+
"email": "[email protected]",
25+
"hireable": null,
26+
"bio": "https://twitter.com/bitwiseman",
27+
"public_repos": 181,
28+
"public_gists": 7,
29+
"followers": 151,
30+
"following": 9,
31+
"created_at": "2012-07-11T20:38:33Z",
32+
"updated_at": "2020-03-27T19:14:56Z",
33+
"private_gists": 8,
34+
"total_private_repos": 10,
35+
"owned_private_repos": 0,
36+
"disk_usage": 33697,
37+
"collaborators": 0,
38+
"two_factor_authentication": true,
39+
"plan": {
40+
"name": "free",
41+
"space": 976562499,
42+
"collaborators": 0,
43+
"private_repos": 10000
44+
}
45+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"id": "c875f18e-a0af-4114-8d1f-123b5377a52c",
3+
"name": "rate_limit",
4+
"request": {
5+
"url": "/rate_limit",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 404,
15+
"body": "{\"message\":\"Not Found\",\"documentation_url\":\"https://developer.github.com/v3\"}",
16+
"headers": {
17+
"Date": "Tue, 31 Mar 2020 16:23:55 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "404 Not Found",
21+
"Cache-Control": "no-cache",
22+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
23+
"X-Accepted-OAuth-Scopes": "",
24+
"X-GitHub-Media-Type": "unknown, github.v3",
25+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
26+
"X-Frame-Options": "deny",
27+
"X-Content-Type-Options": "nosniff",
28+
"X-XSS-Protection": "1; mode=block",
29+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
30+
"Content-Security-Policy": "default-src 'none'",
31+
"Vary": "Accept-Encoding, Accept, X-Requested-With",
32+
"X-GitHub-Request-Id": "C14E:8628:147DC:18E26:5E836E9A"
33+
}
34+
},
35+
"uuid": "c875f18e-a0af-4114-8d1f-123b5377a52c",
36+
"persistent": true,
37+
"scenarioName": "scenario-1-rate_limit",
38+
"requiredScenarioState": "Started",
39+
"newScenarioState": "scenario-1-rate_limit-2",
40+
"insertionIndex": 2
41+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"id": "5ba21199-9f1d-420c-87e9-ab30d3e73884",
3+
"name": "rate_limit",
4+
"request": {
5+
"url": "/rate_limit",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 401,
15+
"body": "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://developer.github.com/v3\"}",
16+
"headers": {
17+
"Date": "Tue, 31 Mar 2020 16:23:55 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "401 Unauthorized",
21+
"X-GitHub-Media-Type": "unknown, github.v3",
22+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
23+
"X-Frame-Options": "deny",
24+
"X-Content-Type-Options": "nosniff",
25+
"X-XSS-Protection": "1; mode=block",
26+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
27+
"Content-Security-Policy": "default-src 'none'",
28+
"Vary": "Accept-Encoding, Accept, X-Requested-With",
29+
"X-GitHub-Request-Id": "C14E:8628:147E6:18E5B:5E836E9B"
30+
}
31+
},
32+
"uuid": "5ba21199-9f1d-420c-87e9-ab30d3e73884",
33+
"persistent": true,
34+
"scenarioName": "scenario-1-rate_limit",
35+
"requiredScenarioState": "scenario-1-rate_limit-2",
36+
"insertionIndex": 3
37+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"id": "27891e8f-aae7-459b-9860-a4f505cec688",
3+
"name": "user",
4+
"request": {
5+
"url": "/user",
6+
"method": "GET",
7+
"headers": {
8+
"Accept": {
9+
"equalTo": "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2"
10+
}
11+
}
12+
},
13+
"response": {
14+
"status": 200,
15+
"bodyFileName": "user-1.json",
16+
"headers": {
17+
"Date": "Tue, 31 Mar 2020 16:23:54 GMT",
18+
"Content-Type": "application/json; charset=utf-8",
19+
"Server": "GitHub.com",
20+
"Status": "200 OK",
21+
"Cache-Control": "private, max-age=60, s-maxage=60",
22+
"Vary": [
23+
"Accept, Authorization, Cookie, X-GitHub-OTP",
24+
"Accept-Encoding, Accept, X-Requested-With"
25+
],
26+
"ETag": "W/\"740bb7db37d5437d08ea1aa5e652cf37\"",
27+
"Last-Modified": "Fri, 27 Mar 2020 19:14:56 GMT",
28+
"X-OAuth-Scopes": "admin:org, admin:org_hook, admin:public_key, admin:repo_hook, delete_repo, gist, notifications, repo, user, write:discussion",
29+
"X-Accepted-OAuth-Scopes": "",
30+
"X-GitHub-Media-Type": "unknown, github.v3",
31+
"Strict-Transport-Security": "max-age=31536000; includeSubdomains; preload",
32+
"X-Frame-Options": "deny",
33+
"X-Content-Type-Options": "nosniff",
34+
"X-XSS-Protection": "1; mode=block",
35+
"Referrer-Policy": "origin-when-cross-origin, strict-origin-when-cross-origin",
36+
"Content-Security-Policy": "default-src 'none'",
37+
"X-GitHub-Request-Id": "C14E:8628:147B0:18E1A:5E836E9A"
38+
}
39+
},
40+
"uuid": "27891e8f-aae7-459b-9860-a4f505cec688",
41+
"persistent": true,
42+
"insertionIndex": 1
43+
}

0 commit comments

Comments
 (0)