Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950import type { OAuthUserAgent } from "@atcute/oauth-browser-client";import { describe, expect, test, vi } from "vitest";import { hostForServiceDid, MissingGrant, mintServiceAuth, serviceDidForHost} from "#lib/auth/agent.ts";import { didOf, nsidOf } from "#lib/api/syntax.ts";
describe("serviceDidForHost", () => { test.each([ ["migrator.example.com", "did:web:migrator.example.com"], ["https://migrator.example.com/", "did:web:migrator.example.com"], ["http://knot.example.com:8443", "did:web:knot.example.com%3A8443"] ])("maps %s to %s", (input, expected) => { expect(serviceDidForHost(input)).toBe(expected); });
test("round-trips with hostForServiceDid", () => { const host = "migrator.example.com"; expect(hostForServiceDid(serviceDidForHost(host))).toBe(host); });});
describe("mintServiceAuth", () => { const aud = didOf("did:web:knot.test"); const lxm = nsidOf("com.atproto.repo.getRecord"); const agentWith = (scope: string) => { const handle = vi.fn(async () => Response.json({ token: "service-auth-jwt" })); return { agent: { session: { token: { scope } }, handle } as unknown as OAuthUserAgent, handle }; };
test("mints when the session was granted the call", async () => { const { agent } = agentWith(`atproto rpc:${lxm}?aud=*`); await expect(mintServiceAuth(agent, { aud, lxm })).resolves.toBe("service-auth-jwt"); });
test("names the missing grant instead of asking the pds, for a session signed in before it was asked for", async () => { const { agent, handle } = agentWith("atproto rpc:com.atproto.repo.createRecord?aud=*"); const minting = mintServiceAuth(agent, { aud, lxm }); await expect(minting).rejects.toBeInstanceOf(MissingGrant); await expect(minting).rejects.toMatchObject({ lxm, aud }); expect(handle).not.toHaveBeenCalled(); });});