-
Notifications
You must be signed in to change notification settings - Fork 775
Expand file tree
/
Copy pathGHAppTest.java
More file actions
311 lines (276 loc) · 12.6 KB
/
GHAppTest.java
File metadata and controls
311 lines (276 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package org.kohsuke.github;
import org.junit.Test;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import static org.hamcrest.Matchers.*;
// TODO: Auto-generated Javadoc
/**
* Tests for the GitHub App API methods.
*
* @author Paulo Miguel Almeida
*/
public class GHAppTest extends AbstractGHAppInstallationTest {
/**
* Create default GHAppTest instance
*/
public GHAppTest() {
}
/**
* Creates the token.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void createToken() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationByUser("bogus");
Map<String, GHPermissionType> permissions = new HashMap<String, GHPermissionType>();
permissions.put("checks", GHPermissionType.WRITE);
permissions.put("pull_requests", GHPermissionType.WRITE);
permissions.put("contents", GHPermissionType.READ);
permissions.put("metadata", GHPermissionType.READ);
// Create token specifying both permissions and repository ids
GHAppInstallationToken installationToken = installation.createToken(permissions)
.repositoryIds(Collections.singletonList((long) 111111111))
.create();
assertThat(installationToken.getToken(), is("bogus"));
assertThat(installation.getPermissions(), is(permissions));
assertThat(installationToken.getRepositorySelection(), is(GHRepositorySelection.SELECTED));
assertThat(installationToken.getExpiresAt(), is(GitHubClient.parseInstant("2019-08-10T05:54:58Z")));
GHRepository repository = installationToken.getRepositories().get(0);
assertThat(installationToken.getRepositories().size(), is(1));
assertThat(repository.getId(), is((long) 111111111));
assertThat(repository.getName(), is("bogus"));
// Create token with no payload
GHAppInstallationToken installationToken2 = installation.createToken().create();
assertThat(installationToken2.getToken(), is("bogus"));
assertThat(installationToken2.getPermissions().size(), is(4));
assertThat(installationToken2.getRepositorySelection(), is(GHRepositorySelection.ALL));
assertThat(installationToken2.getExpiresAt(), is(GitHubClient.parseInstant("2019-12-19T12:27:59Z")));
assertThat(installationToken2.getRepositories(), nullValue());;
}
/**
* Creates the token with repositories.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void createTokenWithRepositories() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationByUser("bogus");
// Create token specifying repositories (not repository_ids!)
GHAppInstallationToken installationToken = installation.createToken()
.repositories(Collections.singletonList("bogus"))
.create();
assertThat(installationToken.getToken(), is("bogus"));
assertThat(installationToken.getPermissions().entrySet(), hasSize(4));
assertThat(installationToken.getRepositorySelection(), is(GHRepositorySelection.SELECTED));
assertThat(installationToken.getExpiresAt(), is(GitHubClient.parseInstant("2022-07-27T21:38:33Z")));
GHRepository repository = installationToken.getRepositories().get(0);
assertThat(installationToken.getRepositories().size(), is(1));
assertThat(repository.getId(), is((long) 11111111));
assertThat(repository.getName(), is("bogus"));
}
/**
* Delete installation.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void deleteInstallation() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationByUser("bogus");
try {
installation.deleteInstallation();
} catch (IOException e) {
fail("deleteInstallation wasn't suppose to fail in this test");
}
}
/**
* Gets the git hub app.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getGitHubApp() throws IOException {
GHApp app = gitHub.getApp();
assertThat(app.getId(), is((long) 82994));
assertThat(app.getOwner().getId(), is((long) 7544739));
assertThat(app.getOwner().getLogin(), is("hub4j-test-org"));
assertThat(app.getOwner().getType(), is("Organization"));
assertThat(app.getName(), is("GHApi Test app 1"));
assertThat(app.getSlug(), is("ghapi-test-app-1"));
assertThat(app.getDescription(), is(""));
assertThat(app.getExternalUrl(), is("http://localhost"));
assertThat(app.getHtmlUrl().toString(), is("https://github.com/apps/ghapi-test-app-1"));
assertThat(app.getCreatedAt(), is(GitHubClient.parseInstant("2020-09-30T13:40:56Z")));
assertThat(app.getUpdatedAt(), is(GitHubClient.parseInstant("2020-09-30T13:40:56Z")));
assertThat(app.getPermissions().size(), is(2));
assertThat(app.getEvents().size(), is(0));
assertThat(app.getInstallationsCount(), is((long) 1));
}
/**
* Gets the installation by id.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getInstallationById() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationById(1111111);
testAppInstallation(installation);
}
/**
* Gets the installation by organization.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getInstallationByOrganization() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationByOrganization("bogus");
testAppInstallation(installation);
}
/**
* Gets the installation by repository.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getInstallationByRepository() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationByRepository("bogus", "bogus");
testAppInstallation(installation);
}
/**
* Gets the installation by user.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void getInstallationByUser() throws IOException {
GHApp app = gitHub.getApp();
GHAppInstallation installation = app.getInstallationByUser("bogus");
testAppInstallation(installation);
}
/**
* List installation requests.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void listInstallationRequests() throws IOException {
GHApp app = gitHub.getApp();
List<GHAppInstallationRequest> installations = app.listInstallationRequests().toList();
assertThat(installations.size(), is(1));
GHAppInstallationRequest appInstallation = installations.get(0);
assertThat(appInstallation.getId(), is((long) 1037204));
assertThat(appInstallation.getAccount().getId(), is((long) 195438329));
assertThat(appInstallation.getAccount().getLogin(), is("approval-test"));
assertThat(appInstallation.getAccount().getType(), is("Organization"));
assertThat(appInstallation.getRequester().getId(), is((long) 195437694));
assertThat(appInstallation.getRequester().getLogin(), is("kaladinstormblessed2"));
assertThat(appInstallation.getRequester().getType(), is("User"));
assertThat(appInstallation.getCreatedAt(), is(GitHubClient.parseInstant("2025-01-17T15:50:51Z")));
assertThat(appInstallation.getNodeId(), is("MDMwOkludGVncmF0aW9uSW5zdGFsbGF0aW9uUmVxdWVzdDEwMzcyMDQ="));
}
/**
* List installations.
*
* @throws IOException
* Signals that an I/O exception has occurred.
*/
@Test
public void listInstallations() throws IOException {
GHApp app = gitHub.getApp();
List<GHAppInstallation> installations = app.listInstallations().toList();
assertThat(installations.size(), is(1));
GHAppInstallation appInstallation = installations.get(0);
testAppInstallation(appInstallation);
}
/**
* List installations that have been updated since a given date.
*
* @throws IOException
* Signals that an I/O exception has occurred.
* @throws ParseException
* Signals that a ParseException has occurred.
*
*/
@Test
public void listInstallationsSince() throws IOException, ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Date localDate = simpleDateFormat.parse("2023-11-01");
Instant localInstant = LocalDate.parse("2023-11-01", DateTimeFormatter.ISO_LOCAL_DATE)
.atStartOfDay()
.toInstant(ZoneOffset.UTC);
GHApp app = gitHub.getApp();
List<GHAppInstallation> installations = app.listInstallations(localDate).toList();
assertThat(installations.size(), is(1));
GHAppInstallation appInstallation = installations.get(0);
testAppInstallation(appInstallation);
}
private void testAppInstallation(GHAppInstallation appInstallation) throws IOException {
Map<String, GHPermissionType> appPermissions = appInstallation.getPermissions();
GHPerson appAccount = appInstallation.getAccount();
assertThat(appInstallation.getId(), is((long) 11111111));
assertThat(appAccount.getId(), is((long) 111111111));
assertThat(appAccount.login, is("bogus"));
assertThat(appAccount, instanceOf(GHOrganization.class));
assertThat(appAccount.getType(), is("Organization"));
assertThat(appInstallation.getRepositorySelection(), is(GHRepositorySelection.SELECTED));
assertThat(appInstallation.getAccessTokenUrl(), endsWith("/app/installations/11111111/access_tokens"));
assertThat(appInstallation.getRepositoriesUrl(), endsWith("/installation/repositories"));
assertThat(appInstallation.getAppId(), is((long) 11111));
assertThat(appInstallation.getTargetId(), is((long) 111111111));
assertThat(appInstallation.getTargetType(), is(GHTargetType.ORGANIZATION));
Map<String, GHPermissionType> permissionsMap = new HashMap<String, GHPermissionType>();
permissionsMap.put("checks", GHPermissionType.WRITE);
permissionsMap.put("pull_requests", GHPermissionType.WRITE);
permissionsMap.put("contents", GHPermissionType.READ);
permissionsMap.put("metadata", GHPermissionType.READ);
assertThat(appPermissions, is(permissionsMap));
List<GHEvent> events = Arrays.asList(GHEvent.PULL_REQUEST, GHEvent.PUSH);
assertThat(appInstallation.getEvents(), containsInAnyOrder(events.toArray(new GHEvent[0])));
assertThat(appInstallation.getCreatedAt(), is(GitHubClient.parseInstant("2019-07-04T01:19:36.000Z")));
assertThat(appInstallation.getUpdatedAt(), is(GitHubClient.parseInstant("2019-07-30T22:48:09.000Z")));
assertThat(appInstallation.getSingleFileName(), nullValue());
}
/**
* Gets the git hub builder.
*
* @return the git hub builder
*/
protected GitHubBuilder getGitHubBuilder() {
return super.getGitHubBuilder()
// ensure that only JWT will be used against the tests below
.withOAuthToken(null, null)
// Note that we used to provide a bogus token here and to rely on (apparently) manually crafted/edited
// Wiremock recordings, so most of the tests cannot actually be executed against GitHub without
// relying on the Wiremock recordings.
// Some tests have been updated, though (getGitHubApp in particular).
.withAuthorizationProvider(jwtProvider1);
}
}