|
| 1 | +/// <reference types="vite/client" /> |
| 2 | +import { describe, test, expect, vi, beforeEach, afterEach } from "vitest"; |
| 3 | +import { WorkOS } from "@workos-inc/node"; |
| 4 | +import { AuthKit } from "./index.js"; |
| 5 | +import type { ComponentApi } from "../component/_generated/component.js"; |
| 6 | + |
| 7 | +vi.mock("@workos-inc/node", () => { |
| 8 | + return { |
| 9 | + WorkOS: vi.fn().mockImplementation(function () { |
| 10 | + return {}; |
| 11 | + }), |
| 12 | + }; |
| 13 | +}); |
| 14 | + |
| 15 | +const requiredEnv = { |
| 16 | + WORKOS_CLIENT_ID: "client_test", |
| 17 | + WORKOS_API_KEY: "sk_test", |
| 18 | + WORKOS_WEBHOOK_SECRET: "whsec_test", |
| 19 | +}; |
| 20 | + |
| 21 | +const fakeComponent = {} as ComponentApi; |
| 22 | + |
| 23 | +describe("AuthKit constructor", () => { |
| 24 | + beforeEach(() => { |
| 25 | + for (const [k, v] of Object.entries(requiredEnv)) { |
| 26 | + process.env[k] = v; |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + for (const k of Object.keys(requiredEnv)) { |
| 32 | + delete process.env[k]; |
| 33 | + } |
| 34 | + vi.clearAllMocks(); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("apiHostname", () => { |
| 38 | + test("forwards apiHostname option to the WorkOS SDK", () => { |
| 39 | + new AuthKit(fakeComponent, { apiHostname: "auth.example.com" }); |
| 40 | + expect(vi.mocked(WorkOS)).toHaveBeenCalledWith( |
| 41 | + "sk_test", |
| 42 | + expect.objectContaining({ apiHostname: "auth.example.com" }) |
| 43 | + ); |
| 44 | + }); |
| 45 | + |
| 46 | + test("forwards undefined when option is not set", () => { |
| 47 | + new AuthKit(fakeComponent); |
| 48 | + expect(vi.mocked(WorkOS)).toHaveBeenCalledWith( |
| 49 | + "sk_test", |
| 50 | + expect.objectContaining({ apiHostname: undefined }) |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + test("clientId is forwarded to the WorkOS SDK", () => { |
| 56 | + new AuthKit(fakeComponent); |
| 57 | + expect(vi.mocked(WorkOS)).toHaveBeenCalledWith( |
| 58 | + "sk_test", |
| 59 | + expect.objectContaining({ clientId: "client_test" }) |
| 60 | + ); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("AuthKit.getAuthConfigProviders", () => { |
| 65 | + beforeEach(() => { |
| 66 | + for (const [k, v] of Object.entries(requiredEnv)) { |
| 67 | + process.env[k] = v; |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + afterEach(() => { |
| 72 | + for (const k of Object.keys(requiredEnv)) { |
| 73 | + delete process.env[k]; |
| 74 | + } |
| 75 | + vi.clearAllMocks(); |
| 76 | + }); |
| 77 | + |
| 78 | + test("falls back to api.workos.com when no custom hostname is set", () => { |
| 79 | + const authKit = new AuthKit(fakeComponent); |
| 80 | + const providers = authKit.getAuthConfigProviders(); |
| 81 | + expect(providers[0].issuer).toBe("https://api.workos.com/"); |
| 82 | + expect(providers[0].jwks).toBe( |
| 83 | + "https://api.workos.com/sso/jwks/client_test" |
| 84 | + ); |
| 85 | + expect(providers[1].issuer).toBe( |
| 86 | + "https://api.workos.com/user_management/client_test" |
| 87 | + ); |
| 88 | + expect(providers[1].jwks).toBe( |
| 89 | + "https://api.workos.com/sso/jwks/client_test" |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + test("custom hostname rewrites issuer but not jwks", () => { |
| 94 | + const authKit = new AuthKit(fakeComponent, { |
| 95 | + apiHostname: "auth.example.com", |
| 96 | + }); |
| 97 | + const providers = authKit.getAuthConfigProviders(); |
| 98 | + expect(providers[0].issuer).toBe("https://auth.example.com/"); |
| 99 | + expect(providers[0].jwks).toBe( |
| 100 | + "https://api.workos.com/sso/jwks/client_test" |
| 101 | + ); |
| 102 | + expect(providers[1].issuer).toBe( |
| 103 | + "https://auth.example.com/user_management/client_test" |
| 104 | + ); |
| 105 | + expect(providers[1].jwks).toBe( |
| 106 | + "https://api.workos.com/sso/jwks/client_test" |
| 107 | + ); |
| 108 | + }); |
| 109 | +}); |
0 commit comments