~james/core

core/web/src/lib/api/secrets.test.ts -rw-r--r-- 2.0 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
import { describe, expect, it, vi } from "vitest";
import { didOf } from "#lib/api/syntax.ts";
import type { OAuthUserAgent } from "@atcute/oauth-browser-client";
import type { Fetch } from "#lib/fetch.ts";

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

vi.mock("#lib/api/knot.ts", () => ({
	knotCapabilities: async () => null
}));

const { addSecret, listSecrets, removeSecret } = await import("#lib/api/secrets.ts");
const agent = { sub: "did:plc:nel" } as unknown as OAuthUserAgent;
const target = { repoDid: didOf("did:plc:repoproof") };

const spindle = (body: unknown) => {
	const fetch = vi.fn<Fetch>(
		async () =>
			new Response(JSON.stringify(body), {
				status: 200,
				headers: { "content-type": "application/json" }
			})
	);
	vi.stubGlobal("fetch", fetch);
	return fetch;
};

describe("pipeline secrets", () => {
	it("asks the repo's spindle over https", async () => {
		const fetch = spindle({ secrets: [] });

		await expect(listSecrets(agent, new URL("https://spindle.test"), target)).resolves.toEqual(
			[]
		);
		const asked = new URL(String(fetch.mock.calls[0][0]));
		expect(new Headers(fetch.mock.calls[0][1]?.headers).get("authorization")).toBe(
			"Bearer token"
		);
		expect(asked.origin).toBe("https://spindle.test");
		expect(asked.pathname).toBe("/xrpc/org.tangled.secret.listSecrets");
		expect(asked.searchParams.get("repo")).toBe(target.repoDid);
	});

	it("keys writes by the repo did", async () => {
		const fetch = spindle({});

		await addSecret(agent, new URL("https://spindle.test"), target, "API_KEY", "value");
		expect(JSON.parse(String(fetch.mock.calls[0][1]?.body))).toEqual({
			repo: target.repoDid,
			key: "API_KEY",
			value: "value"
		});

		await removeSecret(agent, new URL("https://spindle.test"), target, "API_KEY");
		expect(JSON.parse(String(fetch.mock.calls[1][1]?.body))).toEqual({
			repo: target.repoDid,
			key: "API_KEY"
		});
	});
});