|
| 1 | +// Unit tests for requireIngestAuth. api-key-store is mocked so the cak_ |
| 2 | +// validation path is exercised without Mongo. |
| 3 | + |
| 4 | +import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; |
| 5 | + |
| 6 | +const h = vi.hoisted(() => ({ verify: vi.fn() })); |
| 7 | +vi.mock("./stores/api-key-store.js", () => ({ |
| 8 | + apiKeyStore: { verify: h.verify }, |
| 9 | + KEY_PREFIX: "cak_", |
| 10 | +})); |
| 11 | + |
| 12 | +import { requireIngestAuth } from "./ingest-auth.js"; |
| 13 | + |
| 14 | +function ctx(authHeader?: string) { |
| 15 | + const req = { |
| 16 | + header: (n: string) => (n.toLowerCase() === "authorization" ? authHeader : undefined), |
| 17 | + } as unknown as import("express").Request; |
| 18 | + let statusCode = 0; |
| 19 | + let jsonBody: unknown; |
| 20 | + const res = { |
| 21 | + status(c: number) { |
| 22 | + statusCode = c; |
| 23 | + return this; |
| 24 | + }, |
| 25 | + json(b: unknown) { |
| 26 | + jsonBody = b; |
| 27 | + return this; |
| 28 | + }, |
| 29 | + } as unknown as import("express").Response; |
| 30 | + let passed = false; |
| 31 | + const next = () => { |
| 32 | + passed = true; |
| 33 | + }; |
| 34 | + return { |
| 35 | + req, |
| 36 | + res, |
| 37 | + next, |
| 38 | + passed: () => passed, |
| 39 | + status: () => statusCode, |
| 40 | + body: () => jsonBody as { error?: { code?: string } }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +const ORIGINAL = process.env["AGENTOS_INGEST_TOKEN"]; |
| 45 | + |
| 46 | +beforeEach(() => { |
| 47 | + h.verify.mockReset(); |
| 48 | + delete process.env["AGENTOS_INGEST_TOKEN"]; |
| 49 | +}); |
| 50 | + |
| 51 | +afterEach(() => { |
| 52 | + if (ORIGINAL === undefined) delete process.env["AGENTOS_INGEST_TOKEN"]; |
| 53 | + else process.env["AGENTOS_INGEST_TOKEN"] = ORIGINAL; |
| 54 | +}); |
| 55 | + |
| 56 | +describe("requireIngestAuth — cak_ API key", () => { |
| 57 | + it("accepts a valid cak_ key (verified via apiKeyStore)", async () => { |
| 58 | + h.verify.mockResolvedValue({ active: true, principal: "key_abc", roleIds: [], scopes: ["*"] }); |
| 59 | + const c = ctx("Bearer cak_validkey"); |
| 60 | + await requireIngestAuth(c.req, c.res, c.next); |
| 61 | + expect(c.passed()).toBe(true); |
| 62 | + expect(h.verify).toHaveBeenCalledWith("cak_validkey"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("rejects an inactive/unknown cak_ key with 401", async () => { |
| 66 | + h.verify.mockResolvedValue(null); |
| 67 | + const c = ctx("Bearer cak_revoked"); |
| 68 | + await requireIngestAuth(c.req, c.res, c.next); |
| 69 | + expect(c.passed()).toBe(false); |
| 70 | + expect(c.status()).toBe(401); |
| 71 | + expect(c.body().error?.code).toBe("UNAUTHENTICATED"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("fails closed with 503 when key verification throws (Mongo down)", async () => { |
| 75 | + h.verify.mockRejectedValue(new Error("connection refused")); |
| 76 | + const c = ctx("Bearer cak_whatever"); |
| 77 | + await requireIngestAuth(c.req, c.res, c.next); |
| 78 | + expect(c.passed()).toBe(false); |
| 79 | + expect(c.status()).toBe(503); |
| 80 | + expect(c.body().error?.code).toBe("KEY_VERIFICATION_UNAVAILABLE"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("never consults the legacy token for a cak_ key", async () => { |
| 84 | + process.env["AGENTOS_INGEST_TOKEN"] = "cak_validkey"; // even if it matches verbatim |
| 85 | + h.verify.mockResolvedValue(null); |
| 86 | + const c = ctx("Bearer cak_validkey"); |
| 87 | + await requireIngestAuth(c.req, c.res, c.next); |
| 88 | + expect(c.status()).toBe(401); // a cak_ must be a real key, not string-equal to the legacy token |
| 89 | + }); |
| 90 | +}); |
| 91 | + |
| 92 | +describe("requireIngestAuth — legacy static token (back-compat)", () => { |
| 93 | + it("accepts a matching AGENTOS_INGEST_TOKEN", async () => { |
| 94 | + process.env["AGENTOS_INGEST_TOKEN"] = "s3cr3t"; |
| 95 | + const c = ctx("Bearer s3cr3t"); |
| 96 | + await requireIngestAuth(c.req, c.res, c.next); |
| 97 | + expect(c.passed()).toBe(true); |
| 98 | + expect(h.verify).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("rejects a wrong token with 401", async () => { |
| 102 | + process.env["AGENTOS_INGEST_TOKEN"] = "s3cr3t"; |
| 103 | + const c = ctx("Bearer nope"); |
| 104 | + await requireIngestAuth(c.req, c.res, c.next); |
| 105 | + expect(c.status()).toBe(401); |
| 106 | + }); |
| 107 | + |
| 108 | + it("rejects when token is configured but none is presented", async () => { |
| 109 | + process.env["AGENTOS_INGEST_TOKEN"] = "s3cr3t"; |
| 110 | + const c = ctx(undefined); |
| 111 | + await requireIngestAuth(c.req, c.res, c.next); |
| 112 | + expect(c.status()).toBe(401); |
| 113 | + }); |
| 114 | +}); |
| 115 | + |
| 116 | +describe("requireIngestAuth — open mode", () => { |
| 117 | + it("passes when no token is configured and none is presented (network policy)", async () => { |
| 118 | + const c = ctx(undefined); |
| 119 | + await requireIngestAuth(c.req, c.res, c.next); |
| 120 | + expect(c.passed()).toBe(true); |
| 121 | + }); |
| 122 | + |
| 123 | + it("passes a non-cak bearer when no token is configured", async () => { |
| 124 | + const c = ctx("Bearer some-random-thing"); |
| 125 | + await requireIngestAuth(c.req, c.res, c.next); |
| 126 | + expect(c.passed()).toBe(true); |
| 127 | + }); |
| 128 | +}); |
0 commit comments