Skip to content

Commit 224d8c7

Browse files
committed
Add wiremocks for tests, move existing tests to test org
1 parent bde6ad9 commit 224d8c7

File tree

75 files changed

+3010
-1177
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+3010
-1177
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package org.kohsuke.github;
2+
3+
import io.jsonwebtoken.Jwts;
4+
import org.apache.commons.io.IOUtils;
5+
6+
import java.io.IOException;
7+
import java.security.KeyFactory;
8+
import java.security.PrivateKey;
9+
import java.security.spec.PKCS8EncodedKeySpec;
10+
import java.time.Instant;
11+
import java.time.temporal.ChronoUnit;
12+
import java.util.Base64;
13+
import java.util.Date;
14+
15+
public class AbstractGHAppInstallationTest extends AbstractGitHubWireMockTest {
16+
17+
private static String TEST_APP_ID_1 = "82994";
18+
private static String TEST_APP_ID_2 = "83009";
19+
private static String TEST_APP_ID_3 = "89368";
20+
private static String PRIVATE_KEY_FILE_APP_1 = "/ghapi-test-app-1.private-key.pem";
21+
private static String PRIVATE_KEY_FILE_APP_2 = "/ghapi-test-app-2.private-key.pem";
22+
private static String PRIVATE_KEY_FILE_APP_3 = "/ghapi-test-app-3.private-key.pem";
23+
24+
private String createJwtToken(String keyFileResouceName, String appId) {
25+
try {
26+
String keyPEM = IOUtils.toString(this.getClass().getResource(keyFileResouceName), "US-ASCII")
27+
.replaceAll("(?m)^--.*", "") // remove comments from PEM to allow decoding
28+
.replaceAll("\\s", "");
29+
30+
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(keyPEM));
31+
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpecPKCS8);
32+
33+
return Jwts.builder()
34+
.setIssuedAt(Date.from(Instant.now()))
35+
.setExpiration(Date.from(Instant.now().plus(5, ChronoUnit.MINUTES)))
36+
.setIssuer(appId)
37+
.signWith(privateKey)
38+
.compact();
39+
} catch (Exception e) {
40+
throw new RuntimeException("Error creating JWT token.", e);
41+
}
42+
}
43+
44+
private GHAppInstallation getAppInstallationWithToken(String jwtToken) throws IOException {
45+
GitHub gitHub = getGitHubBuilder().withJwtToken(jwtToken)
46+
.withEndpoint(mockGitHub.apiServer().baseUrl())
47+
.build();
48+
49+
GHAppInstallation appInstallation = gitHub.getApp()
50+
.listInstallations()
51+
.toList()
52+
.stream()
53+
.filter(it -> it.getAccount().login.equals("hub4j-test-org"))
54+
.findFirst()
55+
.get();
56+
57+
appInstallation
58+
.setRoot(getGitHubBuilder().withAppInstallationToken(appInstallation.createToken().create().getToken())
59+
.withEndpoint(mockGitHub.apiServer().baseUrl())
60+
.build());
61+
62+
return appInstallation;
63+
}
64+
65+
protected GHAppInstallation getAppInstallationWithTokenApp1() throws IOException {
66+
return getAppInstallationWithToken(createJwtToken(PRIVATE_KEY_FILE_APP_1, TEST_APP_ID_1));
67+
}
68+
69+
protected GHAppInstallation getAppInstallationWithTokenApp2() throws IOException {
70+
return getAppInstallationWithToken(createJwtToken(PRIVATE_KEY_FILE_APP_2, TEST_APP_ID_2));
71+
}
72+
73+
protected GHAppInstallation getAppInstallationWithTokenApp3() throws IOException {
74+
return getAppInstallationWithToken(createJwtToken(PRIVATE_KEY_FILE_APP_3, TEST_APP_ID_3));
75+
}
76+
77+
}

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

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,11 @@
11
package org.kohsuke.github;
22

3-
import io.jsonwebtoken.Jwts;
4-
import org.apache.commons.io.IOUtils;
53
import org.junit.Test;
64

75
import java.io.IOException;
8-
import java.security.KeyFactory;
9-
import java.security.PrivateKey;
10-
import java.security.spec.PKCS8EncodedKeySpec;
11-
import java.time.Instant;
12-
import java.time.temporal.ChronoUnit;
13-
import java.util.*;
6+
import java.util.List;
147

15-
public class GHAppInstallationTest extends AbstractGitHubWireMockTest {
16-
17-
private static String TEST_APP_ID_1 = "82994";
18-
private static String TEST_APP_ID_2 = "83009";
19-
private static String PRIVATE_KEY_FILE_APP_1 = "/ghapi-test-app-1.private-key.pem";
20-
private static String PRIVATE_KEY_FILE_APP_2 = "/ghapi-test-app-2.private-key.pem";
21-
22-
private String createJwtToken(String keyFileResouceName, String appId) {
23-
try {
24-
String keyPEM = IOUtils.toString(this.getClass().getResource(keyFileResouceName), "US-ASCII")
25-
.replaceAll("(?m)^--.*", "") // remove comments from PEM to allow decoding
26-
.replaceAll("\\s", "");
27-
28-
PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(keyPEM));
29-
PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpecPKCS8);
30-
31-
return Jwts.builder()
32-
.setIssuedAt(Date.from(Instant.now()))
33-
.setExpiration(Date.from(Instant.now().plus(5, ChronoUnit.MINUTES)))
34-
.setIssuer(appId)
35-
.signWith(privateKey)
36-
.compact();
37-
} catch (Exception e) {
38-
throw new RuntimeException("Error creating JWT token.", e);
39-
}
40-
}
41-
42-
private GHAppInstallation getAppInstallationWithToken(String jwtToken) throws IOException {
43-
GitHub gitHub = getGitHubBuilder().withJwtToken(jwtToken)
44-
.withEndpoint(mockGitHub.apiServer().baseUrl())
45-
.build();
46-
47-
GHAppInstallation appInstallation = gitHub.getApp()
48-
.listInstallations()
49-
.toList()
50-
.stream()
51-
.filter(it -> it.getAccount().login.equals("hub4j-test-org"))
52-
.findFirst()
53-
.get();
54-
55-
appInstallation
56-
.setRoot(getGitHubBuilder().withAppInstallationToken(appInstallation.createToken().create().getToken())
57-
.withEndpoint(mockGitHub.apiServer().baseUrl())
58-
.build());
59-
60-
return appInstallation;
61-
}
62-
63-
private GHAppInstallation getAppInstallationWithTokenApp1() throws IOException {
64-
return getAppInstallationWithToken(createJwtToken(PRIVATE_KEY_FILE_APP_1, TEST_APP_ID_1));
65-
}
66-
67-
private GHAppInstallation getAppInstallationWithTokenApp2() throws IOException {
68-
return getAppInstallationWithToken(createJwtToken(PRIVATE_KEY_FILE_APP_2, TEST_APP_ID_2));
69-
}
8+
public class GHAppInstallationTest extends AbstractGHAppInstallationTest {
709

7110
@Test
7211
public void testListRepositoriesTwoRepos() throws IOException {

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

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,22 @@
2626

2727
import org.junit.Test;
2828

29+
import java.io.IOException;
2930
import java.util.Date;
3031

3132
import static org.hamcrest.Matchers.containsString;
3233

3334
@SuppressWarnings("deprecation") // preview
34-
public class GHCheckRunBuilderTest extends AbstractGitHubWireMockTest {
35+
public class GHCheckRunBuilderTest extends AbstractGHAppInstallationTest {
36+
37+
protected GitHub getInstallationGithub() throws IOException {
38+
return getAppInstallationWithTokenApp3().getRoot();
39+
}
3540

3641
@Test
3742
public void createCheckRun() throws Exception {
38-
GHCheckRun checkRun = gitHub.getRepository("jglick/github-api-test")
39-
.createCheckRun("foo", "4a929d464a2fae7ee899ce603250f7dab304bc4b")
43+
GHCheckRun checkRun = getInstallationGithub().getRepository("hub4j-test-org/test-checks")
44+
.createCheckRun("foo", "89a9ae301e35e667756034fdc933b1fc94f63fc1")
4045
.withStatus(GHCheckRun.Status.COMPLETED)
4146
.withConclusion(GHCheckRun.Conclusion.SUCCESS)
4247
.withDetailsURL("http://nowhere.net/stuff")
@@ -55,7 +60,7 @@ public void createCheckRun() throws Exception {
5560
.create();
5661
assertEquals("completed", checkRun.getStatus());
5762
assertEquals(1, checkRun.getOutput().getAnnotationsCount());
58-
assertEquals(546384586, checkRun.getId());
63+
assertEquals(1424883286, checkRun.getId());
5964
}
6065

6166
@Test
@@ -65,46 +70,46 @@ public void createCheckRunManyAnnotations() throws Exception {
6570
output.add(
6671
new GHCheckRunBuilder.Annotation("stuff.txt", 1, GHCheckRun.AnnotationLevel.NOTICE, "hello #" + i));
6772
}
68-
GHCheckRun checkRun = gitHub.getRepository("jglick/github-api-test")
69-
.createCheckRun("big", "4a929d464a2fae7ee899ce603250f7dab304bc4b")
73+
GHCheckRun checkRun = getInstallationGithub().getRepository("hub4j-test-org/test-checks")
74+
.createCheckRun("big", "89a9ae301e35e667756034fdc933b1fc94f63fc1")
7075
.withConclusion(GHCheckRun.Conclusion.SUCCESS)
7176
.add(output)
7277
.create();
7378
assertEquals("completed", checkRun.getStatus());
7479
assertEquals("Big Run", checkRun.getOutput().getTitle());
7580
assertEquals("Lots of stuff here »", checkRun.getOutput().getSummary());
7681
assertEquals(101, checkRun.getOutput().getAnnotationsCount());
77-
assertEquals(546384622, checkRun.getId());
82+
assertEquals(1424883599, checkRun.getId());
7883
}
7984

8085
@Test
8186
public void createCheckRunNoAnnotations() throws Exception {
82-
GHCheckRun checkRun = gitHub.getRepository("jglick/github-api-test")
83-
.createCheckRun("quick", "4a929d464a2fae7ee899ce603250f7dab304bc4b")
87+
GHCheckRun checkRun = getInstallationGithub().getRepository("hub4j-test-org/test-checks")
88+
.createCheckRun("quick", "89a9ae301e35e667756034fdc933b1fc94f63fc1")
8489
.withConclusion(GHCheckRun.Conclusion.NEUTRAL)
8590
.add(new GHCheckRunBuilder.Output("Quick note", "nothing more to see here"))
8691
.create();
8792
assertEquals("completed", checkRun.getStatus());
8893
assertEquals(0, checkRun.getOutput().getAnnotationsCount());
89-
assertEquals(546384705, checkRun.getId());
94+
assertEquals(1424883957, checkRun.getId());
9095
}
9196

9297
@Test
9398
public void createPendingCheckRun() throws Exception {
94-
GHCheckRun checkRun = gitHub.getRepository("jglick/github-api-test")
95-
.createCheckRun("outstanding", "4a929d464a2fae7ee899ce603250f7dab304bc4b")
99+
GHCheckRun checkRun = getInstallationGithub().getRepository("hub4j-test-org/test-checks")
100+
.createCheckRun("outstanding", "89a9ae301e35e667756034fdc933b1fc94f63fc1")
96101
.withStatus(GHCheckRun.Status.IN_PROGRESS)
97102
.create();
98103
assertEquals("in_progress", checkRun.getStatus());
99104
assertNull(checkRun.getConclusion());
100-
assertEquals(546469053, checkRun.getId());
105+
assertEquals(1424883451, checkRun.getId());
101106
}
102107

103108
@Test
104109
public void createCheckRunErrMissingConclusion() throws Exception {
105110
try {
106-
gitHub.getRepository("jglick/github-api-test")
107-
.createCheckRun("outstanding", "4a929d464a2fae7ee899ce603250f7dab304bc4b")
111+
getInstallationGithub().getRepository("hub4j-test-org/test-checks")
112+
.createCheckRun("outstanding", "89a9ae301e35e667756034fdc933b1fc94f63fc1")
108113
.withStatus(GHCheckRun.Status.COMPLETED)
109114
.create();
110115
fail("should have been rejected");
@@ -116,8 +121,8 @@ public void createCheckRunErrMissingConclusion() throws Exception {
116121

117122
@Test
118123
public void updateCheckRun() throws Exception {
119-
GHCheckRun checkRun = gitHub.getRepository("jglick/github-api-test")
120-
.createCheckRun("foo", "4a929d464a2fae7ee899ce603250f7dab304bc4b")
124+
GHCheckRun checkRun = getInstallationGithub().getRepository("hub4j-test-org/test-checks")
125+
.createCheckRun("foo", "89a9ae301e35e667756034fdc933b1fc94f63fc1")
121126
.withStatus(GHCheckRun.Status.IN_PROGRESS)
122127
.withStartedAt(new Date(999_999_000))
123128
.add(new GHCheckRunBuilder.Output("Some Title", "what happened…")
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC4vG2q4zLRcQGe
3+
5Z4seo8LY2hROKkdy4LmA96TeCxwDd/bnFIIQGmWMZHVZkyruuLLArTt50/mJ4yC
4+
ggedRiw8CmH8s6iQmZQyRu+6gKVqm5KozQ2omniGAJU6KqZTbUUpvytpmmcWcoLU
5+
eMoLCJTScRK2rofbiSCFy1WwlyVe1b+nBNf2b9VG9bE461K47kgk1cI1/TqZ3QeI
6+
jc/0jV+w7qdTdCqD5A2FyFZa5mU6jj83bVyC0O0AlZlEnwHN58vvN3j7uljF6Mh0
7+
sx+0xtRjmTncl7pu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Qr2U0MbbY6h8POQVHFF/dfBveX25ugIrOZNNetYv
24+
WxH4h/cCUZLG1EUoHGMaH8GoCcj/J/kdDXRtxWInAoGAQbJ/6Ry5+cLGYgt6e/7Z
25+
33wvwVjRmeZxAiE+msdKqXljGmxHKvat3l4eURh1CSqJ5tw0+J/LzXF2WrK8ncQj
26+
d3S4tPQW7yMBxXhJuFNczowmB8QiXFQCAdFe9sMRYyRe7EHJSNYEFjPuPDEVIfwO
27+
nqEDUjQ6neGvMei09uX1eVk=
28+
-----END PRIVATE KEY-----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"id": 89368,
3+
"slug": "ghapi-test-app-3",
4+
"node_id": "MDM6QXBwODkzNjg=",
5+
"owner": {
6+
"login": "hub4j-test-org",
7+
"id": 7544739,
8+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
9+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
10+
"gravatar_id": "",
11+
"url": "https://api.github.com/users/hub4j-test-org",
12+
"html_url": "https://github.com/hub4j-test-org",
13+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
14+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
15+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
16+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
17+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
18+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
19+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
20+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
21+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
22+
"type": "Organization",
23+
"site_admin": false
24+
},
25+
"name": "GHApi Test app 3",
26+
"description": "Test app for checks api testing",
27+
"external_url": "http://localhost",
28+
"html_url": "https://github.com/apps/ghapi-test-app-3",
29+
"created_at": "2020-11-19T14:30:34Z",
30+
"updated_at": "2020-11-19T14:30:34Z",
31+
"permissions": {
32+
"checks": "write",
33+
"metadata": "read"
34+
},
35+
"events": [],
36+
"installations_count": 1
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
[
2+
{
3+
"id": 13064215,
4+
"account": {
5+
"login": "hub4j-test-org",
6+
"id": 7544739,
7+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjc1NDQ3Mzk=",
8+
"avatar_url": "https://avatars3.githubusercontent.com/u/7544739?v=4",
9+
"gravatar_id": "",
10+
"url": "https://api.github.com/users/hub4j-test-org",
11+
"html_url": "https://github.com/hub4j-test-org",
12+
"followers_url": "https://api.github.com/users/hub4j-test-org/followers",
13+
"following_url": "https://api.github.com/users/hub4j-test-org/following{/other_user}",
14+
"gists_url": "https://api.github.com/users/hub4j-test-org/gists{/gist_id}",
15+
"starred_url": "https://api.github.com/users/hub4j-test-org/starred{/owner}{/repo}",
16+
"subscriptions_url": "https://api.github.com/users/hub4j-test-org/subscriptions",
17+
"organizations_url": "https://api.github.com/users/hub4j-test-org/orgs",
18+
"repos_url": "https://api.github.com/users/hub4j-test-org/repos",
19+
"events_url": "https://api.github.com/users/hub4j-test-org/events{/privacy}",
20+
"received_events_url": "https://api.github.com/users/hub4j-test-org/received_events",
21+
"type": "Organization",
22+
"site_admin": false
23+
},
24+
"repository_selection": "selected",
25+
"access_tokens_url": "https://api.github.com/app/installations/13064215/access_tokens",
26+
"repositories_url": "https://api.github.com/installation/repositories",
27+
"html_url": "https://github.com/organizations/hub4j-test-org/settings/installations/13064215",
28+
"app_id": 89368,
29+
"app_slug": "ghapi-test-app-3",
30+
"target_id": 7544739,
31+
"target_type": "Organization",
32+
"permissions": {
33+
"checks": "write",
34+
"metadata": "read"
35+
},
36+
"events": [],
37+
"created_at": "2020-11-19T14:33:27.000Z",
38+
"updated_at": "2020-11-19T14:33:27.000Z",
39+
"single_file_name": null,
40+
"has_multiple_single_files": false,
41+
"single_file_paths": [],
42+
"suspended_by": null,
43+
"suspended_at": null
44+
}
45+
]

0 commit comments

Comments
 (0)