Skip to content

Commit 77bd37c

Browse files
authoredMar 12, 2020
fix(auth.hook): authenticate as app if code strategy option is not set (#45)
1 parent cf08c70 commit 77bd37c

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
 

‎src/hook.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export async function hook(
2828
return request(endpoint);
2929
}
3030

31-
if (requiresBasicAuth(endpoint.url)) {
31+
if (!state.code || requiresBasicAuth(endpoint.url)) {
3232
const credentials = btoa(`${state.clientId}:${state.clientSecret}`);
3333
endpoint.headers.authorization = `basic ${credentials}`;
3434

‎test/index.test.ts

+34-1
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ test("test with request instance that has custom baseUrl (GHE)", async () => {
223223
});
224224
});
225225

226-
test("auth.hook() creates token and uses it for succeeding requests", async () => {
226+
test("auth.hook() creates token and uses it for succeeding requests (if code option was set)", async () => {
227227
const mock = fetchMock
228228
.sandbox()
229229
.postOnce("https://github.com/login/oauth/access_token", {
@@ -268,6 +268,39 @@ test("auth.hook() creates token and uses it for succeeding requests", async () =
268268
expect(mock.done()).toBe(true);
269269
});
270270

271+
test("auth.hook() does not attempt to create token if code option was not set", async () => {
272+
const mock = fetchMock.sandbox().get(
273+
"https://api.github.com/repos/octocat/hello-world",
274+
{ id: 123 },
275+
{
276+
headers: {
277+
authorization: "basic MTIzOnNlY3JldA==" // btoa('123:secret')
278+
}
279+
}
280+
);
281+
282+
const auth = createOAuthAppAuth({
283+
clientId: "123",
284+
clientSecret: "secret"
285+
});
286+
287+
const requestWithAuth = request
288+
.defaults({
289+
request: {
290+
fetch: mock
291+
}
292+
})
293+
.defaults({
294+
request: {
295+
hook: auth.hook
296+
}
297+
});
298+
299+
await requestWithAuth("GET /repos/octocat/hello-world");
300+
301+
expect(mock.done()).toBe(true);
302+
});
303+
271304
test("auth.hook(request, 'POST https://github.com/login/oauth/access_token') does not send request twice (#35)", async () => {
272305
const mock = fetchMock
273306
.sandbox()

0 commit comments

Comments
 (0)
Please sign in to comment.