|
| 1 | +import { beforeEach, describe, expect, vi } from "vitest"; |
| 2 | + |
| 3 | +const SECRET = "billing-service-secret-0123456789abcdef"; |
| 4 | + |
| 5 | +const db = vi.hoisted(() => ({ client: null as any })); |
| 6 | +const env = vi.hoisted(() => ({ |
| 7 | + SESSION_SECRET: "test-session-secret-for-billing-service-auth", |
| 8 | + ENCRYPTION_KEY: "0123456789abcdef0123456789abcdef", |
| 9 | + BILLING_SERVICE_SECRET: undefined as string | undefined, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("~/db.server", () => ({ |
| 13 | + get prisma() { |
| 14 | + return db.client; |
| 15 | + }, |
| 16 | + get $replica() { |
| 17 | + return db.client; |
| 18 | + }, |
| 19 | +})); |
| 20 | +vi.mock("~/env.server", () => ({ env })); |
| 21 | +vi.mock("~/services/logger.server", () => ({ |
| 22 | + logger: { debug: vi.fn(), error: vi.fn(), warn: vi.fn(), info: vi.fn() }, |
| 23 | +})); |
| 24 | +vi.mock("~/services/rbac.server", () => ({ |
| 25 | + rbac: { isUsingPlugin: async () => false, setTokenRole: async () => ({ ok: true }) }, |
| 26 | +})); |
| 27 | + |
| 28 | +import { postgresTest } from "@internal/testcontainers"; |
| 29 | +import { requireBillingServiceOrAdminApiRequest } from "~/services/billingServiceAuth.server"; |
| 30 | +import { |
| 31 | + createPersonalAccessToken, |
| 32 | + requireAdminApiRequest, |
| 33 | +} from "~/services/personalAccessToken.server"; |
| 34 | + |
| 35 | +vi.setConfig({ testTimeout: 60_000 }); |
| 36 | + |
| 37 | +let userCounter = 0; |
| 38 | + |
| 39 | +async function patFor(prisma: any, admin: boolean) { |
| 40 | + userCounter += 1; |
| 41 | + const user = await prisma.user.create({ |
| 42 | + data: { |
| 43 | + email: `billing-service-auth-${userCounter}-${Date.now()}@example.com`, |
| 44 | + authenticationMethod: "MAGIC_LINK", |
| 45 | + admin, |
| 46 | + }, |
| 47 | + }); |
| 48 | + const pat = await createPersonalAccessToken({ name: "test", userId: user.id }); |
| 49 | + return (pat as { token: string }).token; |
| 50 | +} |
| 51 | + |
| 52 | +function bearer(token: string) { |
| 53 | + return new Request("http://localhost/admin/api/v1/orgs/org_1/runs/enable", { |
| 54 | + method: "POST", |
| 55 | + headers: { Authorization: `Bearer ${token}` }, |
| 56 | + }); |
| 57 | +} |
| 58 | + |
| 59 | +async function statusOf(guard: Promise<unknown>) { |
| 60 | + try { |
| 61 | + await guard; |
| 62 | + return 200; |
| 63 | + } catch (error) { |
| 64 | + if (error instanceof Response) return error.status; |
| 65 | + throw error; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +describe("billing service admin API auth", () => { |
| 70 | + beforeEach(() => { |
| 71 | + env.BILLING_SERVICE_SECRET = SECRET; |
| 72 | + }); |
| 73 | + |
| 74 | + /** Catches: billing's calls failing once the secret is configured. */ |
| 75 | + postgresTest("billing routes accept the billing service secret", async ({ prisma }) => { |
| 76 | + db.client = prisma; |
| 77 | + expect(await statusOf(requireBillingServiceOrAdminApiRequest(bearer(SECRET)))).toBe(200); |
| 78 | + }); |
| 79 | + |
| 80 | + /** Catches: admins losing access to the billing routes, or the fallback accepting any user's PAT. */ |
| 81 | + postgresTest("billing routes still take admin PATs and refuse others", async ({ prisma }) => { |
| 82 | + db.client = prisma; |
| 83 | + const adminPat = await patFor(prisma, true); |
| 84 | + const userPat = await patFor(prisma, false); |
| 85 | + |
| 86 | + expect(await statusOf(requireBillingServiceOrAdminApiRequest(bearer(adminPat)))).toBe(200); |
| 87 | + expect(await statusOf(requireBillingServiceOrAdminApiRequest(bearer(userPat)))).toBe(403); |
| 88 | + }); |
| 89 | + |
| 90 | + /** Catches: a near-match of the secret being accepted. */ |
| 91 | + postgresTest("billing routes refuse a wrong secret", async ({ prisma }) => { |
| 92 | + db.client = prisma; |
| 93 | + expect(await statusOf(requireBillingServiceOrAdminApiRequest(bearer(`${SECRET}x`)))).toBe(401); |
| 94 | + }); |
| 95 | + |
| 96 | + /** Catches: installs without billing accepting a bearer value because no secret is configured. */ |
| 97 | + postgresTest("the secret does nothing when it isn't configured", async ({ prisma }) => { |
| 98 | + db.client = prisma; |
| 99 | + env.BILLING_SERVICE_SECRET = undefined; |
| 100 | + expect(await statusOf(requireBillingServiceOrAdminApiRequest(bearer(SECRET)))).toBe(401); |
| 101 | + }); |
| 102 | + |
| 103 | + /** Catches: the billing secret working as an admin credential on the other admin routes. */ |
| 104 | + postgresTest("other admin routes refuse the secret", async ({ prisma }) => { |
| 105 | + db.client = prisma; |
| 106 | + expect(await statusOf(requireAdminApiRequest(bearer(SECRET)))).toBe(401); |
| 107 | + }); |
| 108 | +}); |
0 commit comments