open-agents/apps/web/lib/github/token.test.ts
Will Sather cee13abfaa
feat: replace manual oauth with better-auth (#820)
* feat: replace manual oauth with better-auth

* fix: lint

* chore: refactor accounts to unify github + vercel

* feat: enhance auth config with host allowlisting

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* fix: add newline at end of manifest.json

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* fix: update betterAuth database schema mappings

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* fix: sanitize redirects and preserve destination in github flow

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* feat: enhance GitHub app callback with installation sync tracking

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* build: add BETTER_AUTH_SECRET to turbo env

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

* refactor: update sign-in callback to skip onboarding

Co-Authored-By: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>

---------

Co-authored-by: nicoalbanese <49612682+nicoalbanese@users.noreply.github.com>
Co-authored-by: open-agents-bot[bot] <260704009+open-agents-bot[bot]@users.noreply.github.com>
2026-04-20 15:40:13 +01:00

70 lines
1.7 KiB
TypeScript

import { beforeEach, describe, expect, mock, test } from "bun:test";
let getAccessTokenResult: { accessToken?: string | null } | null;
let getAccessTokenError: Error | null;
const getAccessTokenSpy = mock(
async (_input: { body: { providerId: string; userId: string } }) => {
if (getAccessTokenError) {
throw getAccessTokenError;
}
return getAccessTokenResult;
},
);
mock.module("server-only", () => ({}));
mock.module("next/headers", () => ({
headers: async () => {
throw new Error("headers should not be called");
},
}));
mock.module("@/lib/auth/config", () => ({
auth: {
api: {
getAccessToken: getAccessTokenSpy,
},
},
}));
mock.module("@/lib/db/client", () => ({
db: {},
}));
mock.module("@/lib/db/schema", () => ({
accounts: {},
}));
const tokenModulePromise = import("./token");
describe("getUserGitHubToken", () => {
beforeEach(() => {
getAccessTokenSpy.mockClear();
getAccessTokenResult = { accessToken: "ghu_test" };
getAccessTokenError = null;
});
test("looks up access tokens by user id without request headers", async () => {
const { getUserGitHubToken } = await tokenModulePromise;
const token = await getUserGitHubToken("user-1");
expect(token).toBe("ghu_test");
expect(getAccessTokenSpy).toHaveBeenCalledTimes(1);
expect(getAccessTokenSpy.mock.calls[0]?.[0]).toEqual({
body: { providerId: "github", userId: "user-1" },
});
});
test("returns null when better-auth token lookup fails", async () => {
const { getUserGitHubToken } = await tokenModulePromise;
getAccessTokenError = new Error("boom");
const token = await getUserGitHubToken("user-1");
expect(token).toBeNull();
});
});