Skip to content

Commit

Permalink
feat: factory option for auth({ type: "oauth-user" })
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Mar 24, 2021
1 parent 9ff6ce2 commit cb9fe59
Showing 1 changed file with 59 additions and 1 deletion.
60 changes: 59 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fetchMock, { MockMatcherFunction } from "fetch-mock";
import { request } from "@octokit/request";
import { install } from "@sinonjs/fake-timers";

import { createAppAuth } from "../src/index";
import { createAppAuth, createOAuthUserAuth } from "../src/index";

const APP_ID = 1;
const PRIVATE_KEY = `-----BEGIN RSA PRIVATE KEY-----
Expand Down Expand Up @@ -890,6 +890,64 @@ test("oauth-user device flow", async () => {
});
});

test("oauth-user witth `factory` option", async () => {
const mock = fetchMock.sandbox().postOnce(
"https://github.com/login/oauth/access_token",
{
access_token: "secret123",
scope: "",
token_type: "bearer",
},
{
headers: {
accept: "application/json",
"user-agent": "test",
"content-type": "application/json; charset=utf-8",
},
body: {
client_id: "lv1.1234567890abcdef",
client_secret: "1234567890abcdef1234567890abcdef12345678",
code: "random123",
},
}
);

const appAuth = createAppAuth({
appId: APP_ID,
privateKey: PRIVATE_KEY,
clientType: "oauth-app",
clientId: "lv1.1234567890abcdef",
clientSecret: "1234567890abcdef1234567890abcdef12345678",
request: request.defaults({
headers: {
"user-agent": "test",
},
request: {
fetch: mock,
},
}),
});

const userAuth = await appAuth({
type: "oauth-user",
code: "random123",
// @ts-expect-error TBD: set `factory` options correctly
factory: (options) => createOAuthUserAuth(options),
});

// @ts-expect-error TBD: set appAuth() return types correctly when `factory` is set
const authentication = await userAuth();

expect(authentication).toEqual({
clientId: "lv1.1234567890abcdef",
clientSecret: "1234567890abcdef1234567890abcdef12345678",
clientType: "github-app",
type: "token",
tokenType: "oauth",
token: "secret123",
});
});

test("caches based on installation id", async () => {
const createInstallationAccessTokenResponseData = {
token: "secret123",
Expand Down

0 comments on commit cb9fe59

Please sign in to comment.