Files
HealthLog/src/lib/polar/__tests__/credentials.test.ts
T
Marc-André Bombeck b6f45c0d88 refactor(integrations): move every provider OAuth client onto one factory
`oauth-kit/client.ts` builds a provider's consent URL, code exchange,
refresh and deauthorization from a declaration of what differs: endpoints
and scope, client authentication (form or HTTP Basic, with or without the
client id beside it), the fields a grant adds, how an answer is classified
and the refusal named, the error type, and where the token sits in the
body. The seven hand-written copies of the redirect URI, authorization URL,
token post, Basic header and PKCE pair are gone; each provider keeps an
`oauth.ts` with its declaration.

Shared behaviour is now uniform: a 2xx without a token object (an empty or
non-JSON body) is a transient `empty_token_body` failure for every
provider. WHOOP, Strava and Google Health used to return null, and
Withings threw an unclassified SyntaxError; the OAuth wire golden records
exactly these lines changing and nothing else. Oura's live/legacy endpoint
fallback, Google's invalid_grant lift, Strava's Fault summary and Fitbit's
errorType are declared per provider.
2026-10-23 12:57:47 +02:00

84 lines
2.8 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const findUnique = vi.fn();
const getPolarCredentials = vi.fn();
vi.mock("@/lib/db", () => ({
prisma: { user: { findUnique: (...args: unknown[]) => findUnique(...args) } },
}));
// decrypt() is the fail-closed AES-256-GCM reader; the resolver only needs it
// to round-trip the stored ciphertext, so a `dec(<x>)` shim is sufficient here.
vi.mock("@/lib/crypto", () => ({
decrypt: (cipher: string) => `dec(${cipher})`,
encrypt: (plain: string) => `enc(${plain})`,
}));
// The env fallback lives in ./client; stub it so the precedence is observable.
vi.mock("../oauth", () => ({
getPolarCredentials: () => getPolarCredentials(),
}));
import { getPolarClientCredentials } from "../credentials";
describe("getPolarClientCredentials — DB-first then env", () => {
beforeEach(() => {
findUnique.mockReset();
getPolarCredentials.mockReset();
});
it("returns the per-user BYO client id/secret when both columns are set", async () => {
findUnique.mockResolvedValue({
polarClientIdEncrypted: "enc-id",
polarClientSecretEncrypted: "enc-secret",
});
const creds = await getPolarClientCredentials("user-1");
expect(creds).toEqual({
clientId: "dec(enc-id)",
clientSecret: "dec(enc-secret)",
});
// DB hit must not consult the env fallback.
expect(getPolarCredentials).not.toHaveBeenCalled();
expect(findUnique).toHaveBeenCalledWith({
where: { id: "user-1" },
select: {
polarClientIdEncrypted: true,
polarClientSecretEncrypted: true,
},
});
});
it("falls back to the shared env app when the user has no BYO pair", async () => {
findUnique.mockResolvedValue({
polarClientIdEncrypted: null,
polarClientSecretEncrypted: null,
});
getPolarCredentials.mockReturnValue({
clientId: "env-id",
clientSecret: "env-secret",
});
const creds = await getPolarClientCredentials("user-1");
expect(creds).toEqual({ clientId: "env-id", clientSecret: "env-secret" });
expect(getPolarCredentials).toHaveBeenCalledOnce();
});
it("falls back to env when only one half of the BYO pair is present", async () => {
findUnique.mockResolvedValue({
polarClientIdEncrypted: "enc-id",
polarClientSecretEncrypted: null,
});
getPolarCredentials.mockReturnValue({
clientId: "env-id",
clientSecret: "env-secret",
});
const creds = await getPolarClientCredentials("user-1");
expect(creds).toEqual({ clientId: "env-id", clientSecret: "env-secret" });
});
it("returns null when neither the user nor the env is configured", async () => {
findUnique.mockResolvedValue(null);
getPolarCredentials.mockReturnValue(null);
expect(await getPolarClientCredentials("ghost")).toBeNull();
});
});