forked from adamlaska/core.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.test.ts
More file actions
334 lines (299 loc) · 10.1 KB
/
auth.test.ts
File metadata and controls
334 lines (299 loc) · 10.1 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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { getUserAgent } from "universal-user-agent";
import fetchMock, { MockMatcherFunction } from "fetch-mock";
import { Response } from "node-fetch";
import {
createBasicAuth,
createAppAuth,
createActionAuth
} from "@octokit/auth";
import lolex from "lolex";
import { Octokit } from "../src";
const userAgent = `octokit-core.js/0.0.0-development ${getUserAgent()}`;
beforeAll(() => {
// Math.random is used to generate the token fingerprint,
// unless `token.fingerprint` option was passed. The fingerprint is
// calculated using `Math.random().toString(36).substr(2)`, so the
// default fingerprint is always `"4feornbt361"`.
Math.random = jest.fn().mockReturnValue(0.123);
// A timestamp is added to the default token note, e.g.
// "octokit 2019-07-04 4feornbt361". Lolex mocks the Date class so
// `new Date()` always returns `new Date(0)` by default.
const clock = lolex.install({
now: 0,
toFake: ["Date"]
});
beforeEach(() => {
clock.reset();
});
});
describe("Authentication", () => {
it("new Octokit({ auth: 'secret123' })", () => {
const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/",
{ ok: true },
{
headers: {
accept: "application/vnd.github.v3+json",
authorization: "token secret123",
"user-agent": userAgent
}
}
);
const octokit = new Octokit({
auth: "secret123",
request: {
fetch: mock
}
});
return octokit.request("/");
});
it("new Octokit({ auth: 'token secret123' })", () => {
const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/",
{ ok: true },
{
headers: {
accept: "application/vnd.github.v3+json",
authorization: "token secret123",
"user-agent": userAgent
}
}
);
const octokit = new Octokit({
auth: "token secret123",
request: {
fetch: mock
}
});
return octokit.request("/");
});
it("new Octokit({ auth: 'Token secret123' })", () => {
const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/",
{ ok: true },
{
headers: {
accept: "application/vnd.github.v3+json",
authorization: "token secret123",
"user-agent": userAgent
}
}
);
const octokit = new Octokit({
auth: "Token secret123",
request: {
fetch: mock
}
});
return octokit.request("/");
});
const BEARER_TOKEN =
"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE1NTM4MTkzMTIsImV4cCI6MTU1MzgxOTM3MiwiaXNzIjoxfQ.etiSZ4LFQZ8tiMGJVqKDoGn8hxMCgwL4iLvU5xBUqbAPr4pbk_jJZmMQjuxTlOnRxq4e7NouTizGCdfohRMb3R1mpLzGPzOH9_jqSA_BWYxolsRP_WDSjuNcw6nSxrPRueMVRBKFHrqcTOZJej0djRB5pI61hDZJ_-DGtiOIFexlK3iuVKaqBkvJS5-TbTekGuipJ652g06gXuz-l8i0nHiFJldcuIruwn28hTUrjgtPbjHdSBVn_QQLKc2Fhij8OrhcGqp_D_fvb_KovVmf1X6yWiwXV5VXqWARS-JGD9JTAr2495ZlLV_E4WPxdDpz1jl6XS9HUhMuwBpaCOuipw";
it("new Octokit({ auth: BEARER_TOKEN })", () => {
const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/",
{ ok: true },
{
headers: {
accept: "application/vnd.github.v3+json",
authorization: `bearer ${BEARER_TOKEN}`,
"user-agent": userAgent
}
}
);
const octokit = new Octokit({
auth: BEARER_TOKEN,
request: {
fetch: mock
}
});
return octokit.request("/");
});
it("auth = createBasicAuth()", async () => {
const expectedCreateTokenRequestHeaders = {
accept: "application/vnd.github.v3+json",
authorization: "basic b2N0b2NhdDpzZWNyZXQ=",
"content-type": "application/json; charset=utf-8",
"user-agent": userAgent
};
const matchCreateToken: MockMatcherFunction = (url, { body, headers }) => {
expect(url).toEqual("https://api.github.com/authorizations");
expect(JSON.parse(String(body))).toStrictEqual({
fingerprint: "4feornbt361",
note: "octokit 1970-01-01 4feornbt361",
note_url: "https://github.com/octokit/auth-basic.js#readme",
scopes: []
});
expect(headers).toStrictEqual(expectedCreateTokenRequestHeaders);
return true;
};
const matchCreateTokenWithOtp: MockMatcherFunction = (
url,
{ body, headers }
) => {
expect(url).toEqual("https://api.github.com/authorizations");
expect(JSON.parse(String(body))).toStrictEqual({
fingerprint: "4feornbt361",
note: "octokit 1970-01-01 4feornbt361",
note_url: "https://github.com/octokit/auth-basic.js#readme",
scopes: []
});
expect(headers).toStrictEqual({
...expectedCreateTokenRequestHeaders,
"x-github-otp": "123456"
});
return true;
};
const matchGetUserWithOtp: MockMatcherFunction = (
url,
{ body, headers }
) => {
expect(url).toEqual("https://api.github.com/user");
expect(headers).toStrictEqual({
accept: "application/vnd.github.v3+json",
authorization: "token 1234567890abcdef1234567890abcdef12345678",
"user-agent": userAgent
});
return true;
};
const responseGetUser = {
id: 1
};
const responseTokenCreated = {
id: 123,
token: "1234567890abcdef1234567890abcdef12345678"
};
const responseOtpRequired = new Response("Unauthorized", {
status: 401,
headers: {
"x-github-otp": "required; app"
}
});
const mock = fetchMock
.sandbox()
.postOnce(matchCreateToken, responseOtpRequired)
.postOnce(matchCreateTokenWithOtp, responseTokenCreated)
.getOnce(matchGetUserWithOtp, responseGetUser);
const octokit = new Octokit({
auth: createBasicAuth({
username: "octocat",
password: "secret",
on2Fa: () => `123456`
}),
request: {
fetch: mock
}
});
const { data } = await octokit.request("/user");
expect(data).toStrictEqual({ id: 1 });
expect(mock.done()).toBeTruthy();
});
it("auth = createAppAuth()", async () => {
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
4tohsDbqFGwmw2mIaHjl24LuWXyyP4xpAGDpl9IcusjXBxLQLp2m4AKXbWpzb0OL
Ulrfc1ZooPck2uz7xlMIZOtLlOPjLz2DuejVe24JcwwHzrQWKOfA11R/9e50DVse
hnSH/w46Q763y4I0E3BIoUMsolEKzh2ydAAyzkgabGQBUuamZotNfvJoDXeCi1LD
8yNCWyTlYpJZJDDXooBU5EAsCvhN1sSRoaXWrlMSDB7r/E+aQyKua4KONqvmoJuC
21vSKeECgYEA7yW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RENCIfBZT9ibDUWy82cI4g3F09mdTq3bD1xLavIfmTksIQCz5EymTWR5v6gL14LSmQdWY9lSqkgUG0XCFljWUglEP39H4yeHbFgdjvAYg3ifDS12z9oQz2ACdSpvxPiTuCC804HkPVw8Qoy0OSXvCkFU70l7VXCVUxnuhHnk8-oCGcKUspmeP6UdDnXk-Aus-eGwDfJbU2WritxxaXw6B4a3flTPojkYLSkPBr6Pi0H2-mBsW_Nvs0aLPVLKobQd4gqTkosX3967DoAG8luUMhrnxe8Q";
const mock = fetchMock
.sandbox()
.postOnce("https://api.github.com/app/installations/123/access_tokens", {
token: "secret123",
expires_at: "1970-01-01T01:00:00.000Z",
permissions: {
metadata: "read"
},
repository_selection: "all"
})
.get(
"https://api.github.com/repos/octocat/hello-world",
{ id: 123 },
{
headers: {
authorization: "token secret123"
},
repeat: 2
}
)
.getOnce(
"https://api.github.com/app",
{ id: 123 },
{
headers: {
accept: "application/vnd.github.machine-man-preview+json",
"user-agent": userAgent,
authorization: `bearer ${BEARER}`
}
}
);
const octokit = new Octokit({
auth: createAppAuth({
id: APP_ID,
privateKey: PRIVATE_KEY,
installationId: 123
}),
request: {
fetch: mock
}
});
await octokit.request("GET /repos/octocat/hello-world");
await octokit.request("GET /repos/octocat/hello-world");
await octokit.request("GET /app", {
mediaType: {
previews: ["machine-man"]
}
});
expect(mock.done()).toBe(true);
});
it("auth = createActionAuth()", async () => {
const mock = fetchMock.sandbox().getOnce(
"https://api.github.com/app",
{ id: 123 },
{
headers: {
accept: "application/vnd.github.v3+json",
authorization: `token githubtoken123`,
"user-agent": userAgent
}
}
);
const currentEnv = process.env;
process.env = {
GITHUB_ACTION: "1",
GITHUB_TOKEN: "githubtoken123"
};
const octokit = new Octokit({
auth: createActionAuth(),
request: {
fetch: mock
}
});
await octokit.request("/app");
process.env = currentEnv;
});
});