~james/core

core/web/src/lib/api/spindlewrite.test.ts -rw-r--r-- 3.2 KiB
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 15 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { describe, expect, it, vi } from "vitest";
import type { OAuthUserAgent } from "@atcute/oauth-browser-client";
import { mainSchema as addSecretSchema } from "#lib/api/lexicons/types/org/tangled/secret/addSecret.ts";
import { didOf, type Capability, type KnotOrigin } from "#lib/api/syntax.ts";
import type { Fetch } from "#lib/fetch.ts";

const knot = "knot.test" as KnotOrigin;
const minted = vi.hoisted(() => vi.fn());

vi.mock("#lib/auth/agent.ts", () => ({
	createClient: () => ({}),
	mintServiceAuth: async () => "user-token",
	serviceDidForHost: (host: string) => `did:web:${host}`,
	hostForServiceDid: () => null
}));

vi.mock("#lib/api/knot.ts", () => ({
	knotCapabilities: async (asked: KnotOrigin) =>
		asked === "knot.test"
			? (["knot-repo-token-mint"] as unknown as readonly Capability[])
			: null
}));

vi.mock("#lib/api/cob.ts", () => ({
	knotProcedure: minted
}));

const { spindleXrpc } = await import("#lib/api/spindlewrite.ts");

const agent = { sub: "did:plc:nel" } as unknown as OAuthUserAgent;
const target = { knot, repoDid: didOf("did:plc:repoproof") };

const stubbed = (body: unknown) => {
	const fetch = vi.fn<Fetch>(async () => new Response(JSON.stringify(body), { status: 200 }));
	vi.stubGlobal("fetch", fetch);
	return fetch;
};

describe("spindle write targets", () => {
	it("mints a repo token under the per-repo session when the knot advertises it", async () => {
		minted.mockResolvedValue({ token: "repo-token" });
		const fetch = stubbed({});
		const client = await spindleXrpc(agent, new URL("https://spindle.test"), target);

		await client.call(addSecretSchema, {
			input: { repo: target.repoDid, key: "K", value: "v" }
		});

		expect(minted).toHaveBeenCalledWith(
			agent,
			knot,
			target.repoDid,
			"org.tangled.repo.mintToken",
			{
				aud: "did:web:spindle.test",
				lxm: "org.tangled.secret.addSecret",
				exp: expect.any(Number)
			},
			{ signal: expect.any(AbortSignal), invalidate: false }
		);
		const sent = new URL(String(fetch.mock.calls[0][0]));
		expect(sent.origin).toBe("https://spindle.test");
		expect(sent.pathname).toBe("/xrpc/org.tangled.secret.addSecret");
		expect(new Headers(fetch.mock.calls[0][1]?.headers).get("authorization")).toBe(
			"Bearer repo-token"
		);
	});

	it("uses user token when the knot lacks the cap or mint will fail", async () => {
		minted.mockClear();
		const olderKnot = stubbed({});
		const incapable = await spindleXrpc(agent, new URL("https://spindle.test"), {
			repoDid: target.repoDid,
			knot: "older-knot.test" as KnotOrigin
		});
		await incapable.call(addSecretSchema, {
			input: { repo: target.repoDid, key: "K", value: "v" }
		});
		expect(minted).not.toHaveBeenCalled();
		expect(new Headers(olderKnot.mock.calls[0][1]?.headers).get("authorization")).toBe(
			"Bearer user-token"
		);

		minted.mockImplementation(() => {
			throw new Error("Exp too far in the future.");
		});
		const failing = stubbed({});
		const client = await spindleXrpc(agent, new URL("https://spindle.test"), target);
		await client.call(addSecretSchema, {
			input: { repo: target.repoDid, key: "K", value: "v" }
		});
		expect(minted).toHaveBeenCalledTimes(1);
		expect(new Headers(failing.mock.calls[0][1]?.headers).get("authorization")).toBe(
			"Bearer user-token"
		);
	});
});