-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.test.ts
197 lines (175 loc) · 6.45 KB
/
index.test.ts
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
import { install } from "lolex";
import nock from "nock";
import { stub } from "simple-mock";
import { App } from "../src";
const APP_ID = 1;
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA1c7+9z5Pad7OejecsQ0bu3aozN3tihPmljnnudb9G3HECdnH
lWu2/a1gB9JW5TBQ+AVpum9Okx7KfqkfBKL9mcHgSL0yWMdjMfNOqNtrQqKlN4kE
p6RD++7sGbzbfZ9arwrlD/HSDAWGdGGJTSOBM6pHehyLmSC3DJoR/CTu0vTGTWXQ
rO64Z8tyXQPtVPb/YXrcUhbBp8i72b9Xky0fD6PkEebOy0Ip58XVAn2UPNlNOSPS
ye+Qjtius0Md4Nie4+X8kwVI2Qjk3dSm0sw/720KJkdVDmrayeljtKBx6AtNQsSX
gzQbeMmiqFFkwrG1+zx6E7H7jqIQ9B6bvWKXGwIDAQABAoIBAD8kBBPL6PPhAqUB
K1r1/gycfDkUCQRP4DbZHt+458JlFHm8QL6VstKzkrp8mYDRhffY0WJnYJL98tr4
4tohsDbqFGwmw2mIaHjl24LuWXyyP4xpAGDpl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UzLOw4N72/Hu+UG6MNt5iDZFCtapRaKt6OvSBwy8w==
-----END RSA PRIVATE KEY-----`;
// see https://runkit.com/gr2m/reproducable-jwt
const BEARER =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjAsImV4cCI6NjAwLCJpc3MiOjF9.UYfZtE742hkMV5cKMwp6-gVUvsWnUGoCQkl2UZZEkN8-lgvqzq5V8e5KtTrJxAAgcK7Yn1ViAlDUpwc9hZxrZ-gLaR10GR2hubte3OgkRDH-m_lCQ1Sgb9VQpZnagh_PMyRwphOw3uDXU3D7h2jL86UP3Ora8i9SRgXLq8X_2R9jtr2FDT1wtmcOLdyIc0Q7c_4X1uIPNjZS2UY04QBT7VWePk81EGdJAVQ_nEygXIuWOpMwZvtD0K1hzqQQM9GyV2QOwFSvFLtdbMVyld6Qvs8eEA5VS6Y4vTrGuyUF_lH5XlPdfAFAyrzsGP4inLq3tq6y4mjsx3YIF0P8DcMNPw";
// simulate the beginning of unix time so that Date.now() returns 0
// that way the signed token is always the same
// Documentation: https://git.io/fASyr
const clock = install({ now: 0, toFake: ["Date", "setTimeout"] });
describe("app.js", () => {
let app: App;
beforeEach(function() {
// set up stuff
app = new App({
id: APP_ID,
privateKey: PRIVATE_KEY
});
});
// see https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app
it("gets bearer token", function() {
const bearer = app.getSignedJsonWebToken();
expect(bearer).toEqual(BEARER);
});
// see https://developer.github.com/v3/apps/#create-a-new-installation-token
it("gets installation token", () => {
nock("https://api.github.com", {
reqheaders: {
authorization: `bearer ${BEARER}` // installation access token
}
})
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "foo"
});
return app
.getInstallationAccessToken({ installationId: 123 })
.then(token => {
expect(token).toEqual("foo");
});
});
it("gets installation token from cache", () => {
nock("https://api.github.com")
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "foo"
});
return app
.getInstallationAccessToken({ installationId: 123 })
.then(token => {
expect(token).toEqual("foo");
return app.getInstallationAccessToken({ installationId: 123 });
})
.then(token => {
expect(token).toEqual("foo");
});
});
it("caches based on installation id", () => {
nock("https://api.github.com")
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "foo"
})
.post("/app/installations/456/access_tokens")
.reply(201, {
token: "bar"
});
return app
.getInstallationAccessToken({ installationId: 123 })
.then(token => {
expect(token).toEqual("foo");
return app.getInstallationAccessToken({ installationId: 456 });
})
.then(token => {
expect(token).toEqual("bar");
});
});
const oneHourInMs = 1000 * 60 * 60;
it("request installation again after timeout", () => {
const mock = nock("https://api.github.com")
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "foo"
})
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "bar"
});
return app
.getInstallationAccessToken({ installationId: 123 })
.then(token => {
expect(token).toEqual("foo");
return new Promise(resolve => {
setTimeout(resolve, oneHourInMs);
clock.tick(oneHourInMs);
});
})
.then(() => {
return app.getInstallationAccessToken({ installationId: 123 });
})
.then(token => {
expect(token).toEqual("bar");
expect(mock.pendingMocks()).toStrictEqual([]);
});
});
it("supports custom cache", () => {
nock("https://api.github.com")
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "foo"
});
const options = {
id: APP_ID,
privateKey: PRIVATE_KEY,
cache: {
get: stub(),
set: stub()
}
};
const appWithCustomCache = new App(options);
return appWithCustomCache
.getInstallationAccessToken({ installationId: 123 })
.then(() => {
expect(options.cache.get.callCount).toEqual(1);
expect(options.cache.set.callCount).toEqual(1);
});
});
it("supports custom base url", () => {
nock("https://github-enterprise.com/api/v3")
.post("/app/installations/123/access_tokens")
.reply(201, {
token: "foo"
});
const options = {
id: APP_ID,
privateKey: PRIVATE_KEY,
baseUrl: "https://github-enterprise.com/api/v3"
};
const appWithCustomEndpointDefaults = new App(options);
return appWithCustomEndpointDefaults
.getInstallationAccessToken({ installationId: 123 })
.then(token => {
expect(token).toEqual("foo");
});
});
});