|
| 1 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 2 | +import { AuthClient, apiBaseUrl, buildAuthHeaders } from "./client.js"; |
| 3 | +import { isAuthError } from "./errors.js"; |
| 4 | +import type { ResolvedCredential } from "./resolver.js"; |
| 5 | + |
| 6 | +function jsonFetch(body: unknown, status = 200): typeof fetch { |
| 7 | + return (async () => |
| 8 | + new Response(JSON.stringify(body), { |
| 9 | + status, |
| 10 | + headers: { "content-type": "application/json" }, |
| 11 | + })) as unknown as typeof fetch; |
| 12 | +} |
| 13 | + |
| 14 | +function textFetch(body: string, status: number): typeof fetch { |
| 15 | + return (async () => new Response(body, { status })) as unknown as typeof fetch; |
| 16 | +} |
| 17 | + |
| 18 | +function apiKeyCred(): ResolvedCredential { |
| 19 | + return { type: "api_key", key: "hg_x", source: "env" }; |
| 20 | +} |
| 21 | + |
| 22 | +function makeClient(fetchImpl: typeof fetch): AuthClient { |
| 23 | + return new AuthClient({ baseUrl: "https://api.test.example", fetchImpl }); |
| 24 | +} |
| 25 | + |
| 26 | +describe("auth/client", () => { |
| 27 | + const original = process.env["HEYGEN_API_URL"]; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + delete process.env["HEYGEN_API_URL"]; |
| 31 | + }); |
| 32 | + |
| 33 | + afterEach(() => { |
| 34 | + if (original !== undefined) process.env["HEYGEN_API_URL"] = original; |
| 35 | + else delete process.env["HEYGEN_API_URL"]; |
| 36 | + }); |
| 37 | + |
| 38 | + it("apiBaseUrl defaults to https://api.heygen.com", () => { |
| 39 | + expect(apiBaseUrl()).toBe("https://api.heygen.com"); |
| 40 | + }); |
| 41 | + |
| 42 | + it("apiBaseUrl honors HEYGEN_API_URL and strips trailing slash", () => { |
| 43 | + process.env["HEYGEN_API_URL"] = "https://api.dev.heygen.com/"; |
| 44 | + expect(apiBaseUrl()).toBe("https://api.dev.heygen.com"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("buildAuthHeaders uses Bearer for oauth", () => { |
| 48 | + const cred: ResolvedCredential = { |
| 49 | + type: "oauth", |
| 50 | + access_token: "at_123", |
| 51 | + source: "file_json", |
| 52 | + refreshable: false, |
| 53 | + }; |
| 54 | + expect(buildAuthHeaders(cred)).toEqual({ authorization: "Bearer at_123" }); |
| 55 | + }); |
| 56 | + |
| 57 | + it("buildAuthHeaders uses x-api-key for api_key", () => { |
| 58 | + expect(buildAuthHeaders(apiKeyCred())).toEqual({ "x-api-key": "hg_x" }); |
| 59 | + }); |
| 60 | + |
| 61 | + it("getCurrentUser parses a wrapped {data: {...}} payload", async () => { |
| 62 | + const client = makeClient( |
| 63 | + jsonFetch({ |
| 64 | + code: 100, |
| 65 | + message: "ok", |
| 66 | + data: { |
| 67 | + username: "alice", |
| 68 | + |
| 69 | + billing_type: "subscription", |
| 70 | + subscription: { plan: "team", credits: { premium_credits: 4200 } }, |
| 71 | + }, |
| 72 | + }), |
| 73 | + ); |
| 74 | + const user = await client.getCurrentUser(apiKeyCred()); |
| 75 | + expect(user.username).toBe("alice"); |
| 76 | + expect(user.email).toBe("[email protected]"); |
| 77 | + expect(user.subscription?.plan).toBe("team"); |
| 78 | + expect(user.subscription?.credits?.premium_credits).toBe(4200); |
| 79 | + }); |
| 80 | + |
| 81 | + it("getCurrentUser parses an unwrapped payload", async () => { |
| 82 | + const client = makeClient(jsonFetch({ email: "[email protected]" })); |
| 83 | + const user = await client.getCurrentUser(apiKeyCred()); |
| 84 | + expect(user.email).toBe("[email protected]"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("getCurrentUser throws ErrUnauthenticated on 401", async () => { |
| 88 | + const client = makeClient(textFetch("invalid token", 401)); |
| 89 | + await expect(client.getCurrentUser(apiKeyCred())).rejects.toSatisfy((err) => { |
| 90 | + return isAuthError(err) && (err as { code: string }).code === "UNAUTHENTICATED"; |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + it("getCurrentUser throws ErrApi on 5xx", async () => { |
| 95 | + const client = makeClient(textFetch("upstream", 503)); |
| 96 | + await expect(client.getCurrentUser(apiKeyCred())).rejects.toSatisfy((err) => { |
| 97 | + return isAuthError(err) && (err as { code: string }).code === "API_ERROR"; |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + it("getCurrentUser throws ErrApi when 2xx body is not valid JSON", async () => { |
| 102 | + const fetchImpl = (async () => |
| 103 | + new Response("<html>not json</html>", { |
| 104 | + status: 200, |
| 105 | + headers: { "content-type": "text/html" }, |
| 106 | + })) as unknown as typeof fetch; |
| 107 | + const client = makeClient(fetchImpl); |
| 108 | + await expect(client.getCurrentUser(apiKeyCred())).rejects.toSatisfy((err) => { |
| 109 | + return isAuthError(err) && (err as { code: string }).code === "API_ERROR"; |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + it("getCurrentUser returns empty UserInfo when payload.data is an array", async () => { |
| 114 | + const client = makeClient(jsonFetch({ code: 0, data: [{ email: "x@y" }] })); |
| 115 | + const user = await client.getCurrentUser(apiKeyCred()); |
| 116 | + expect(user).toEqual({}); |
| 117 | + }); |
| 118 | + |
| 119 | + it("getCurrentUser scrubs hg_ keys and JWTs from 401 detail", async () => { |
| 120 | + const fetchImpl = textFetch( |
| 121 | + 'invalid request — got header "x-api-key: hg_supersecret_abc123"', |
| 122 | + 401, |
| 123 | + ); |
| 124 | + const client = makeClient(fetchImpl); |
| 125 | + try { |
| 126 | + await client.getCurrentUser(apiKeyCred()); |
| 127 | + } catch (err) { |
| 128 | + const msg = (err as Error).message; |
| 129 | + expect(msg).not.toContain("hg_supersecret_abc123"); |
| 130 | + expect(msg).toContain("<redacted>"); |
| 131 | + return; |
| 132 | + } |
| 133 | + throw new Error("expected rejection"); |
| 134 | + }); |
| 135 | + |
| 136 | + it("getCurrentUser redacts the full Authorization: Bearer value (not just the scheme)", async () => { |
| 137 | + const fetchImpl = textFetch( |
| 138 | + "rejected — echoed Authorization: Bearer at_opaque_secret_999", |
| 139 | + 401, |
| 140 | + ); |
| 141 | + const client = makeClient(fetchImpl); |
| 142 | + try { |
| 143 | + await client.getCurrentUser(apiKeyCred()); |
| 144 | + } catch (err) { |
| 145 | + const msg = (err as Error).message; |
| 146 | + expect(msg).not.toContain("at_opaque_secret_999"); |
| 147 | + expect(msg).not.toContain("Bearer at_opaque_secret_999"); |
| 148 | + expect(msg).toContain("<redacted>"); |
| 149 | + return; |
| 150 | + } |
| 151 | + throw new Error("expected rejection"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("getCurrentUser sends the right header for oauth credentials", async () => { |
| 155 | + let captured: Record<string, string> = {}; |
| 156 | + const fetchImpl = (async (_url: string, init?: RequestInit) => { |
| 157 | + captured = (init?.headers as Record<string, string>) ?? {}; |
| 158 | + return new Response(JSON.stringify({ email: "[email protected]" }), { |
| 159 | + status: 200, |
| 160 | + headers: { "content-type": "application/json" }, |
| 161 | + }); |
| 162 | + }) as unknown as typeof fetch; |
| 163 | + |
| 164 | + const client = makeClient(fetchImpl); |
| 165 | + await client.getCurrentUser({ |
| 166 | + type: "oauth", |
| 167 | + access_token: "at_xyz", |
| 168 | + source: "file_json", |
| 169 | + refreshable: false, |
| 170 | + }); |
| 171 | + expect(captured["authorization"]).toBe("Bearer at_xyz"); |
| 172 | + }); |
| 173 | +}); |
0 commit comments