|
|
@@ -0,0 +1,108 @@ |
| 1 |
|
| 2 |
|
| 3 |
import { afterAll, beforeAll, describe, expect, test } from "bun:test"; |
| 4 |
import { mkdtemp, rm } from "node:fs/promises"; |
| 5 |
import { tmpdir } from "node:os"; |
| 6 |
import { join } from "node:path"; |
| 7 |
import { createApp, type App } from "../src/app.ts"; |
| 8 |
import { createToken } from "../src/core/tokens.ts"; |
| 9 |
import { createUser } from "../src/core/users.ts"; |
| 10 |
import { serve, type Servers } from "../src/http/server.ts"; |
| 11 |
import { quietLogs } from "./helpers/globals.ts"; |
| 12 |
|
| 13 |
quietLogs(); |
| 14 |
|
| 15 |
const PASS = "a-long-backups-passphrase"; |
| 16 |
const SECRET = "sekrit-key-1234"; |
| 17 |
|
| 18 |
describe("MCP backups_test", () => { |
| 19 |
let dataDir = ""; |
| 20 |
let app: App; |
| 21 |
let servers: Servers; |
| 22 |
let fake: ReturnType<typeof Bun.serve>; |
| 23 |
let admin = ""; |
| 24 |
let plain = ""; |
| 25 |
|
| 26 |
const rpc = async (token: string, method: string, params?: unknown) => { |
| 27 |
const res = await fetch(`${servers.url}/mcp`, { |
| 28 |
method: "POST", |
| 29 |
headers: { "content-type": "application/json", accept: "application/json, text/event-stream", authorization: `Bearer ${token}` }, |
| 30 |
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }), |
| 31 |
}); |
| 32 |
return (await res.json()) as any; |
| 33 |
}; |
| 34 |
const test_ = async (token: string, args: unknown) => { |
| 35 |
const body = await rpc(token, "tools/call", { name: "backups_test", arguments: args }); |
| 36 |
if (body.error) return { error: true, text: body.error.message as string }; |
| 37 |
return { error: body.result.isError === true, text: body.result.content[0].text as string }; |
| 38 |
}; |
| 39 |
|
| 40 |
beforeAll(async () => { |
| 41 |
dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-backups-")); |
| 42 |
app = createApp({ dataDir, port: 0 }); |
| 43 |
servers = await serve(app); |
| 44 |
fake = Bun.serve({ |
| 45 |
port: 0, |
| 46 |
hostname: "127.0.0.1", |
| 47 |
fetch: () => |
| 48 |
new Response( |
| 49 |
'<?xml version="1.0"?><ListBucketResult><IsTruncated>false</IsTruncated><Contents><Key>a.tar</Key></Contents></ListBucketResult>', |
| 50 |
{ headers: { "content-type": "application/xml" } }, |
| 51 |
), |
| 52 |
}); |
| 53 |
const made: Record<string, any> = {}; |
| 54 |
for (const [username, isAdmin] of [["alice", true], ["bob", false]] as const) { |
| 55 |
made[username] = await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@x.test`, password: PASS, isAdmin }); |
| 56 |
if (!made[username].ok) throw new Error(username); |
| 57 |
} |
| 58 |
admin = createToken(app.db, made["alice"].value.id, "t", "admin").token; |
| 59 |
plain = createToken(app.db, made["bob"].value.id, "t", "admin").token; |
| 60 |
}, 30_000); |
| 61 |
|
| 62 |
afterAll(async () => { |
| 63 |
fake.stop(true); |
| 64 |
servers.stop(); |
| 65 |
app.close(); |
| 66 |
await rm(dataDir, { recursive: true, force: true }); |
| 67 |
}); |
| 68 |
|
| 69 |
const fields = (endpoint: string) => ({ endpoint, bucket: "my-bucket", accessKey: "AKIAEXAMPLE", secretKey: SECRET }); |
| 70 |
|
| 71 |
test("a reachable local destination is reached, and says so", async () => { |
| 72 |
const r = await test_(admin, fields(`http://127.0.0.1:${fake.port}`)); |
| 73 |
expect(r.error).toBe(false); |
| 74 |
const out = JSON.parse(r.text); |
| 75 |
expect(out.reached).toBe(true); |
| 76 |
expect(out.says).toStartWith("Reached "); |
| 77 |
}, 20_000); |
| 78 |
|
| 79 |
test("an unreachable one says why, and never echoes a secret", async () => { |
| 80 |
const r = await test_(admin, fields("http://127.0.0.1:1")); |
| 81 |
const out = JSON.parse(r.text); |
| 82 |
expect(out.reached).toBe(false); |
| 83 |
expect(out.says).toStartWith("Could not list "); |
| 84 |
expect(r.text).not.toContain(SECRET); |
| 85 |
expect(r.text).not.toContain("AKIAEXAMPLE"); |
| 86 |
}, 20_000); |
| 87 |
|
| 88 |
test("a field the page would refuse is refused in the same words", async () => { |
| 89 |
const r = await test_(admin, { ...fields("ftp://x") }); |
| 90 |
const out = JSON.parse(r.text); |
| 91 |
expect(out.reached).toBe(false); |
| 92 |
expect(out.says).toContain("The endpoint must be an http(s) URL"); |
| 93 |
expect(r.text).not.toContain(SECRET); |
| 94 |
}); |
| 95 |
|
| 96 |
test("a non-admin is refused and does not see the tool", async () => { |
| 97 |
const r = await test_(plain, fields(`http://127.0.0.1:${fake.port}`)); |
| 98 |
expect(r.error).toBe(true); |
| 99 |
const listed = await rpc(plain, "tools/list"); |
| 100 |
expect(listed.result.tools.map((t: any) => t.name)).not.toContain("backups_test"); |
| 101 |
}); |
| 102 |
|
| 103 |
test("listed to an admin, not as a read", async () => { |
| 104 |
const listed = await rpc(admin, "tools/list"); |
| 105 |
const tool = listed.result.tools.find((t: any) => t.name === "backups_test"); |
| 106 |
expect(tool.annotations?.readOnlyHint).not.toBe(true); |
| 107 |
}); |
| 108 |
}); |