~james/core

core/web/src/lib/api/access.test.ts -rw-r--r-- 2.1 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
import { describe, expect, it, vi } from "vitest";
import type { Did } from "@atcute/lexicons/syntax";
import { createBobbinClient } from "#lib/api/client.ts";
import { NO_ACCESS, resolveRepoAccess } from "#lib/api/access.ts";
import type { Fetch } from "#lib/fetch.ts";

const owner = "did:plc:owner" as Did;
const repoDid = "did:plc:repo" as Did;

const bobbin = (items: { subject: string }[]) => {
	const fetch = vi.fn<Fetch>(
		async () =>
			new Response(JSON.stringify({ items }), {
				status: 200,
				headers: { "content-type": "application/json" }
			})
	);
	return { ctx: createBobbinClient({ serviceUrl: "https://bobbin.test", fetch }), fetch };
};

describe("resolveRepoAccess", () => {
	it("gives the owner everything without asking the knot", async () => {
		const { ctx, fetch } = bobbin([]);

		await expect(resolveRepoAccess(ctx, { ownerDid: owner, repoDid }, owner)).resolves.toEqual({
			role: "owner",
			canPush: true,
			canAdminister: true
		});
		expect(fetch).not.toHaveBeenCalled();
	});

	it("gives an accepted collaborator push but not administration", async () => {
		const { ctx } = bobbin([{ subject: "did:plc:friend" }]);

		await expect(
			resolveRepoAccess(ctx, { ownerDid: owner, repoDid }, "did:plc:friend")
		).resolves.toEqual({ role: "collaborator", canPush: true, canAdminister: false });
	});

	it("gives a stranger nothing", async () => {
		const { ctx } = bobbin([{ subject: "did:plc:friend" }]);

		await expect(
			resolveRepoAccess(ctx, { ownerDid: owner, repoDid }, "did:plc:stranger")
		).resolves.toEqual(NO_ACCESS);
	});

	it("gives a signed-out viewer nothing", async () => {
		const { ctx, fetch } = bobbin([]);

		await expect(
			resolveRepoAccess(ctx, { ownerDid: owner, repoDid }, undefined)
		).resolves.toEqual(NO_ACCESS);
		expect(fetch).not.toHaveBeenCalled();
	});

	it("has no collaborators to check on a repo without a DID", async () => {
		const { ctx, fetch } = bobbin([{ subject: "did:plc:friend" }]);

		await expect(
			resolveRepoAccess(ctx, { ownerDid: owner }, "did:plc:friend")
		).resolves.toEqual(NO_ACCESS);
		expect(fetch).not.toHaveBeenCalled();
	});
});