-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgithub-client.test.ts
More file actions
196 lines (167 loc) · 5.37 KB
/
Copy pathgithub-client.test.ts
File metadata and controls
196 lines (167 loc) · 5.37 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
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { OAuthInvalidGrantError } from "@decocms/runtime";
import {
refreshAccessToken,
scopeUserAccessTokenToRepository,
} from "./github-client.ts";
const realFetch = globalThis.fetch;
type FetchMock = (
input: Request | URL | string,
init?: RequestInit,
) => Promise<Response>;
function mockFetch(impl: FetchMock) {
globalThis.fetch = impl as typeof globalThis.fetch;
}
describe("refreshAccessToken", () => {
beforeEach(() => {
globalThis.fetch = realFetch;
});
afterEach(() => {
globalThis.fetch = realFetch;
});
test("throws OAuthInvalidGrantError when GitHub returns 200 with error=invalid_grant", async () => {
mockFetch(
async () =>
new Response(
JSON.stringify({
error: "invalid_grant",
error_description:
"The refresh token has expired or has been revoked",
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
),
);
let caught: unknown;
try {
await refreshAccessToken("rt", "client_id", "client_secret");
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(OAuthInvalidGrantError);
const typed = caught as OAuthInvalidGrantError;
expect(typed.error).toBe("invalid_grant");
expect(typed.errorDescription).toBe(
"The refresh token has expired or has been revoked",
);
});
test("throws OAuthInvalidGrantError when GitHub returns error=bad_refresh_token", async () => {
mockFetch(
async () =>
new Response(JSON.stringify({ error: "bad_refresh_token" }), {
status: 200,
headers: { "Content-Type": "application/json" },
}),
);
let caught: unknown;
try {
await refreshAccessToken("rt", "client_id", "client_secret");
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(OAuthInvalidGrantError);
expect((caught as OAuthInvalidGrantError).error).toBe("bad_refresh_token");
});
test("throws OAuthInvalidGrantError on 400 invalid_grant", async () => {
mockFetch(
async () =>
new Response(JSON.stringify({ error: "invalid_grant" }), {
status: 400,
headers: { "Content-Type": "application/json" },
}),
);
let caught: unknown;
try {
await refreshAccessToken("rt", "client_id", "client_secret");
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(OAuthInvalidGrantError);
});
test("throws plain Error (not OAuthInvalidGrantError) on 5xx", async () => {
mockFetch(
async () =>
new Response("Bad Gateway", {
status: 502,
headers: { "Content-Type": "text/plain" },
}),
);
let caught: unknown;
try {
await refreshAccessToken("rt", "client_id", "client_secret");
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(Error);
expect(caught).not.toBeInstanceOf(OAuthInvalidGrantError);
});
test("propagates plain Error on network failure", async () => {
mockFetch(async () => {
throw new Error("network down");
});
let caught: unknown;
try {
await refreshAccessToken("rt", "client_id", "client_secret");
} catch (err) {
caught = err;
}
expect(caught).toBeInstanceOf(Error);
expect(caught).not.toBeInstanceOf(OAuthInvalidGrantError);
expect((caught as Error).message).toBe("network down");
});
test("returns token response on success", async () => {
mockFetch(
async () =>
new Response(
JSON.stringify({
access_token: "new-access",
token_type: "Bearer",
expires_in: 28800,
refresh_token: "new-refresh",
refresh_token_expires_in: 15897600,
scope: "repo",
}),
{ status: 200, headers: { "Content-Type": "application/json" } },
),
);
const result = await refreshAccessToken("rt", "client_id", "client_secret");
expect(result.access_token).toBe("new-access");
expect(result.refresh_token).toBe("new-refresh");
expect(result.scope).toBe("repo");
});
});
describe("scopeUserAccessTokenToRepository", () => {
beforeEach(() => {
globalThis.fetch = realFetch;
});
afterEach(() => {
globalThis.fetch = realFetch;
});
test("uses GitHub App basic auth for /token/scoped", async () => {
let authHeader: string | null = null;
let requestBody: Record<string, unknown> | null = null;
mockFetch(async (_input, init) => {
const headers = new Headers(init?.headers);
authHeader = headers.get("Authorization");
requestBody = JSON.parse(String(init?.body)) as Record<string, unknown>;
return new Response(
JSON.stringify({ token: "scoped-token", expires_in: 28800 }),
{ status: 200, headers: { "Content-Type": "application/json" } },
);
});
const result = await scopeUserAccessTokenToRepository(
"ghu_user_token",
"Iv1.client",
"client_secret",
{ repositoryId: 1016852701, target: "deco" },
);
expect(result.token).toBe("scoped-token");
expect(authHeader).toBe(
`Basic ${Buffer.from("Iv1.client:client_secret").toString("base64")}`,
);
expect(requestBody).toEqual({
access_token: "ghu_user_token",
target: "deco",
repository_ids: [1016852701],
});
});
});