-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcheck-token.test.ts
114 lines (108 loc) · 2.97 KB
/
check-token.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
import { describe, expect, it } from "vitest";
import fetchMock from "fetch-mock";
import { request } from "@octokit/request";
import { checkToken } from "../src/index.js";
describe("checkToken()", () => {
it("README example", async () => {
const mock = fetchMock.sandbox().postOnce(
"https://api.github.com/applications/1234567890abcdef1234/token",
{
scopes: ["repo"],
},
{
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": "test",
authorization:
"basic MTIzNDU2Nzg5MGFiY2RlZjEyMzQ6MTIzNDU2Nzg5MGFiY2RlZjEyMzQ3ODkwYWJjZGVmMTIzNDU2Nzg=",
"content-type": "application/json; charset=utf-8",
},
body: {
access_token: "token123",
},
},
);
const { data, authentication } = await checkToken({
clientType: "oauth-app",
clientId: "1234567890abcdef1234",
clientSecret: "1234567890abcdef12347890abcdef12345678",
token: "token123",
request: request.defaults({
headers: {
"user-agent": "test",
},
request: {
fetch: mock,
},
}),
});
expect(data).toMatchInlineSnapshot(`
{
"scopes": [
"repo",
],
}
`);
expect(authentication).toMatchInlineSnapshot(`
{
"clientId": "1234567890abcdef1234",
"clientSecret": "1234567890abcdef12347890abcdef12345678",
"clientType": "oauth-app",
"scopes": [
"repo",
],
"token": "token123",
}
`);
});
it("GitHub Example", async () => {
const mock = fetchMock.sandbox().postOnce(
"https://api.github.com/applications/lv1.1234567890abcdef/token",
{
expires_at: "2021-10-06T17:26:27Z",
scopes: [],
},
{
headers: {
accept: "application/vnd.github.v3+json",
"user-agent": "test",
authorization:
"basic bHYxLjEyMzQ1Njc4OTBhYmNkZWY6MTIzNDU2Nzg5MGFiY2RlZjEyMzQ3ODkwYWJjZGVmMTIzNDU2Nzg=",
"content-type": "application/json; charset=utf-8",
},
body: {
access_token: "token123",
},
},
);
const { data, authentication } = await checkToken({
clientType: "github-app",
clientId: "lv1.1234567890abcdef",
clientSecret: "1234567890abcdef12347890abcdef12345678",
token: "token123",
request: request.defaults({
headers: {
"user-agent": "test",
},
request: {
fetch: mock,
},
}),
});
expect(data).toMatchInlineSnapshot(`
{
"expires_at": "2021-10-06T17:26:27Z",
"scopes": [],
}
`);
expect(authentication).toMatchInlineSnapshot(`
{
"clientId": "lv1.1234567890abcdef",
"clientSecret": "1234567890abcdef12347890abcdef12345678",
"clientType": "github-app",
"expiresAt": "2021-10-06T17:26:27Z",
"token": "token123",
}
`);
});
});