|
| 1 | +import assert from "node:assert/strict" |
| 2 | +import { randomBytes } from "node:crypto" |
| 3 | +import { createServer } from "node:http" |
| 4 | +import { test } from "node:test" |
| 5 | +import { LLMock } from "@copilotkit/aimock" |
| 6 | + |
| 7 | +import { authenticatedMockFetch } from "./mock-auth" |
| 8 | + |
| 9 | +void test("mock authentication rejects fixture tampering and permits replay", async () => { |
| 10 | + const key = randomBytes(32).toString("hex") |
| 11 | + const mock = new LLMock({ port: 0, auth: { apiKeys: [key] } }) |
| 12 | + mock.addFixture({ match: { userMessage: "smoke" }, response: { content: "trusted" } }) |
| 13 | + await mock.start() |
| 14 | + try { |
| 15 | + for (const path of ["/__aimock/fixtures", "/__aimock/reset"]) { |
| 16 | + for (const credential of [undefined, "mock-key"]) { |
| 17 | + const response = await fetch(`${mock.url}${path}`, { |
| 18 | + method: "POST", |
| 19 | + headers: credential ? { authorization: `Bearer ${credential}` } : {}, |
| 20 | + body: JSON.stringify({ fixtures: [] }), |
| 21 | + }) |
| 22 | + assert.equal(response.status, 401) |
| 23 | + } |
| 24 | + } |
| 25 | + const client = authenticatedMockFetch(fetch, mock.url, key) |
| 26 | + assert.equal( |
| 27 | + ( |
| 28 | + await client(`${mock.url}/__aimock/fixtures`, { |
| 29 | + method: "POST", |
| 30 | + headers: { "content-type": "application/json" }, |
| 31 | + body: JSON.stringify({ |
| 32 | + fixtures: [{ match: { userMessage: "other" }, response: { content: "safe" } }], |
| 33 | + }), |
| 34 | + }) |
| 35 | + ).status, |
| 36 | + 200, |
| 37 | + ) |
| 38 | + const providerHeaders: Record<string, string>[] = [ |
| 39 | + {}, |
| 40 | + { "x-api-key": "original" }, |
| 41 | + { "x-goog-api-key": "original" }, |
| 42 | + ] |
| 43 | + for (const headers of providerHeaders) { |
| 44 | + const response = await client( |
| 45 | + new Request(`${mock.url}/v1/chat/completions`, { |
| 46 | + method: "POST", |
| 47 | + headers: { ...headers, "content-type": "application/json" }, |
| 48 | + body: JSON.stringify({ model: "test", messages: [{ role: "user", content: "smoke" }] }), |
| 49 | + }), |
| 50 | + ) |
| 51 | + assert.equal(response.status, 200) |
| 52 | + assert.equal((await response.json()).choices[0].message.content, "trusted") |
| 53 | + } |
| 54 | + assert.equal((await client(`${mock.url}/__aimock/fixtures`)).status, 200) |
| 55 | + assert.equal((await client(`${mock.url}/__aimock/reset`, { method: "POST" })).status, 200) |
| 56 | + } finally { |
| 57 | + await mock.stop() |
| 58 | + } |
| 59 | +}) |
| 60 | + |
| 61 | +void test("recording uses the upstream key and never forwards the mock credential", async () => { |
| 62 | + let authorization: string | undefined |
| 63 | + const upstream = createServer((req, res) => { |
| 64 | + authorization = req.headers.authorization |
| 65 | + res.setHeader("content-type", "application/json") |
| 66 | + res.end( |
| 67 | + JSON.stringify({ |
| 68 | + choices: [{ message: { role: "assistant", content: "recorded" }, finish_reason: "stop" }], |
| 69 | + }), |
| 70 | + ) |
| 71 | + }) |
| 72 | + await new Promise<void>((resolve) => upstream.listen(0, "127.0.0.1", resolve)) |
| 73 | + const address = upstream.address() |
| 74 | + assert.ok(address && typeof address !== "string") |
| 75 | + const upstreamUrl = `http://127.0.0.1:${address.port}` |
| 76 | + const key = randomBytes(32).toString("hex") |
| 77 | + const mock = new LLMock({ |
| 78 | + port: 0, |
| 79 | + auth: { apiKeys: [key] }, |
| 80 | + record: { |
| 81 | + providers: { openai: upstreamUrl }, |
| 82 | + providerKeys: { openai: "synthetic-upstream-key" }, |
| 83 | + proxyOnly: true, |
| 84 | + }, |
| 85 | + }) |
| 86 | + try { |
| 87 | + await mock.start() |
| 88 | + const client = authenticatedMockFetch(fetch, mock.url, key) |
| 89 | + const response = await client(`${mock.url}/v1/chat/completions`, { |
| 90 | + method: "POST", |
| 91 | + headers: { "content-type": "application/json", authorization: "Bearer original" }, |
| 92 | + body: JSON.stringify({ model: "test", messages: [{ role: "user", content: "record" }] }), |
| 93 | + }) |
| 94 | + assert.equal(response.status, 200) |
| 95 | + await response.text() |
| 96 | + assert.equal(authorization, "Bearer synthetic-upstream-key") |
| 97 | + await client(upstreamUrl, { headers: { authorization: "Bearer unrelated" } }) |
| 98 | + assert.equal(authorization, "Bearer unrelated") |
| 99 | + } finally { |
| 100 | + await mock.stop() |
| 101 | + await new Promise<void>((resolve, reject) => upstream.close((error) => (error ? reject(error) : resolve()))) |
| 102 | + } |
| 103 | +}) |
0 commit comments