Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414import { expect } from "@playwright/test";
import { asKnot, editCob, getOk, knotRecord, knotRecords, knotReply, mirrorMerge} from "../harness/api";import { config, seeded } from "../harness/config";import { bobbin, collaboratorId, type Ctx, type Flow, flow, git, hydrant, knot, knotCopy, link, manifestId, not, page, patchId, pds, pullId, refsId, refusal, refusedTicketId, subscriptionId, target, testid, text, through, ticketId, where, within} from "../harness/flow";import { lsRemote, push } from "../harness/git";import { partsOf, selfUri, until, xrpc } from "../harness/net";import { base, branches, merges, onBranches, recall, remember, targetTookTheFeature} from "../harness/shared";
const COLLABORATOR = collaboratorId.nsid;const ADD = "com.atproto.repo.createRecord";const REMOVE = "com.atproto.repo.deleteRecord";
const access = where("the access settings", (ctx) => `${ctx.repo.path}/settings/access`);const addCollaboratorForm = where( "the add collaborator form", (ctx) => `${ctx.repo.path}/settings/access/new`);
const collaboration = (nsid: string, subject: (ctx: Ctx) => string) => (ctx: Ctx) => knotReply(ctx.actor, ctx.repo.did, nsid, { collection: COLLABORATOR, rkey: subject(ctx), ...(nsid === ADD ? { record: { $type: COLLABORATOR, createdAt: new Date().toISOString() } } : {}) });
const collaborate = (nsid: string, label: string) => async (ctx: Ctx) => { const reply = await collaboration(nsid, (c) => recall(c, label).did)(ctx); expect(reply.status, `${nsid} answered ${JSON.stringify(reply.body)}`).toBe(200);};
const withHelper = (flow: Flow) => flow .actor("helper") .step("remember the helper", remember("helper")) .actor("owner") .step("the owner adds the helper", collaborate(ADD, "helper"));
const bobbinSubjects = async (repoDid: string): Promise<string[]> => { const { items } = await getOk( xrpc(config.bobbinUrl, "org.tangled.repo.listCollaborators", { subject: repoDid }) ); return items.map((item: { subject: string }) => item.subject);};
const knotSubjects = async (repoDid: string): Promise<string[]> => (await knotRecords(repoDid, COLLABORATOR)).map((record) => partsOf(record.uri).rkey);
// the pages read bobbin, so a collaborator must show in bobbin's list tooconst listing = ( service: string, subjectsOf: (repoDid: string) => Promise<string[]>, label: string, wanted: boolean): [string, (ctx: Ctx) => Promise<unknown>] => [ `${service} ${wanted ? "lists" : "no longer lists"} ${label}`, async (ctx) => { const who = recall(ctx, label); await until( `${service} should ${wanted ? "list" : "drop"} ${label} on ${ctx.repo.did}`, 20_000, async () => { const now = await subjectsOf(ctx.repo.did); return [now.includes(who.did) === wanted, `it lists ${JSON.stringify(now)}`]; } ); }];const knotListing = (label: string, wanted: boolean) => listing("the knot", knotSubjects, label, wanted);const bobbinListing = (label: string, wanted: boolean) => listing("bobbin", bobbinSubjects, label, wanted);
const helperHandle = target("the helper's handle", (page, ctx) => testid("collaboratorHandle", { handle: recall(ctx, "helper").handle }).locate(page, ctx));const suggestion = target("the helper in the suggestions", (page, ctx) => testid("selectActor", { handle: recall(ctx, "helper").handle }).locate(page, ctx));
// only the knot's own refusals count, and it words them two ways depending on whether it knows whose key it isconst pushRefused = (label: string, branch: string) => async (ctx: Ctx) => { const who = recall(ctx, label); await expect( push(who, ctx.repo.did, { [`e2e/${label}.txt`]: `${label}\n` }, { branch, base: "main" }) ).rejects.toThrow( /you aren't authorized to push to this repository|doesn't match any key published by the accounts that may push here/ );};
const branchIsAt = (branch: string, label = "owner") => async (ctx: Ctx) => { const heads = await lsRemote(recall(ctx, label), ctx.repo.did); expect(heads[`refs/heads/${branch}`]).toBe(ctx.repo.heads[branch]); };
flow("the owner adds a collaborator from the access page") .actor("owner") .repo() .actor("helper") .step("remember the helper", remember("helper")) .actor("owner")
.open(access) .click(testid("addCollaborator")) .at(addCollaboratorForm) .fill(testid("collaborator"), (ctx) => recall(ctx, "helper").handle) .click(suggestion) .click(testid("invite")) .sends( collaboratorId, knot, (ctx) => ({ repo: ctx.repo.did, rkey: recall(ctx, "helper").did }), "create" ) .accepted(collaboratorId) .at(access) .step(...knotListing("helper", true)) .step(...bobbinListing("helper", true)) .reload() .sees(helperHandle) .sees(testid("remove", { standing: "collaborator" }));
flow("the owner removes a collaborator from the access page") .actor("owner") .repo() .use(withHelper) .step(...knotListing("helper", true))
.open(access) .sees(helperHandle) .click(testid("remove", { standing: "collaborator" })) .sends( collaboratorId, knot, (ctx) => ({ repo: ctx.repo.did, rkey: recall(ctx, "helper").did }), "delete" ) .accepted(collaboratorId) .step(...knotListing("helper", false)) .step(...bobbinListing("helper", false)) .reload() .sees(not(helperHandle)) .sees(not(testid("remove", { standing: "collaborator" })));
flow("a collaborator pushes a branch to the owner's repo") .actor("owner") .repo() .push(base) .step("remember the owner", remember("owner")) .use(withHelper)
.actor("helper") .push({ "e2e/helper.txt": "from the helper\n" }, { branch: "from-helper", base: "main" }) .follow(refsId, through(git, knotCopy, hydrant, bobbin)) .step("the owner sees the helper's branch over git", branchIsAt("from-helper"));
flow("a stranger's push is refused and the branch stays where it was") .actor("owner") .repo() .push(base) .step("remember the owner", remember("owner")) .actor("nobody") .step("remember the stranger", remember("nobody")) .step("the stranger's push is refused", pushRefused("nobody", "main")) .step("the stranger's new branch is refused too", pushRefused("nobody", "mine")) .step("the owner's branch didn't move", branchIsAt("main")) .step("and there is no new branch", async (ctx) => { const heads = await lsRemote(recall(ctx, "owner"), ctx.repo.did, "refs/heads/"); expect(Object.keys(heads)).toEqual(["refs/heads/main"]); }) .follow(refsId, through(git, knotCopy));
flow("a collaborator the owner removes can't push any more") .actor("owner") .repo() .push(base) .step("remember the owner", remember("owner")) .use(withHelper) .actor("helper") .push({ "e2e/first.txt": "while allowed\n" }, { branch: "while-allowed", base: "main" }) .follow(refsId, through(git, knotCopy, hydrant, bobbin))
.actor("owner") .step("the owner removes the helper", collaborate(REMOVE, "helper")) .step(...knotListing("helper", false)) .actor("helper") .step("the helper's push is refused", pushRefused("helper", "after-removal")) .step("the owner's repo has no such branch", async (ctx) => { const heads = await lsRemote(recall(ctx, "owner"), ctx.repo.did, "refs/heads/"); expect(Object.keys(heads)).not.toContain("refs/heads/after-removal"); });
const issueTitle = "a collaborator's issue";
flow("under the collaborators policy a collaborator opens an issue and a stranger is refused") .actor("owner") .repo() .policy("collaborators") .use(withHelper)
.actor("helper") .open(page.newIssue) .fill(testid("title"), issueTitle) .click(testid("submit")) .sends( ticketId, knot, (ctx) => ({ repo: ctx.repo.did, record: { state: "open", title: { text: issueTitle } } }), "create" ) .trace(ticketId, through(knot, hydrant, bobbin)) .trace(subscriptionId, through(pds)) .at(page.issues) .sees(link(issueTitle))
.actor(seeded.charlie) .open(page.newIssue) .fill(testid("title"), "let me in") .click(testid("submit")) .refused(refusedTicketId, 403) .sees(refusal) .open(page.issues) .sees(link(issueTitle)) .sees(not(link("let me in")));
onBranches("a collaborator closes and reopens the owner's pull request") .pull(pullId, patchId, branches) .step("remember the owner", remember("owner")) .use(withHelper) .actor("helper") .open(page.pull(pullId)) .sees(testid("requestClose")) .changeState(pullId, "closed", testid("requestClose"), testid("changeState")) .changeState(pullId, "open", testid("changeState"), testid("requestClose")) .reload() .sees(testid("requestClose"));
onBranches("a collaborator merges the owner's pull request") .pull(pullId, patchId, branches) .step("remember the owner", remember("owner")) .use(withHelper) .actor("helper") .open(page.pull(pullId)) .sees(testid("mergeChoice")) .use(merges) .knotStored(pullId, "completed") .step( "the target branch took the feature's commit", targetTookTheFeature((ctx) => recall(ctx, "owner")) );
const strangersPull = "a pull a stranger may only read";const closeThroughApi = async (ctx: Ctx) => { return editCob(ctx.actor, ctx.get(pullId).uri, "close", (stored) => ({ ...stored, state: "closed" }));};
onBranches("a stranger sees no close, merge or label controls on a pull and the knot refuses them") .pull(pullId, patchId, { ...branches, title: strangersPull }) .open(page.pull(pullId)) .sees(testid("requestClose")) .sees(testid("mergeChoice")) .sees(testid("editLabels"))
.actor(seeded.charlie) .open(page.pull(pullId)) .sees(within(testid("ticketTitle"), text(strangersPull))) .sees(not(testid("requestClose"))) .sees(not(testid("mergeChoice"))) .sees(not(testid("editLabels"))) .rejects( "charlie merging the pull", (ctx) => mirrorMerge( ctx.actor, ctx.repo.did, branches.into, ctx.repo.heads[branches.into], ctx.repo.heads[branches.from] ), [401, 403, 409] ) .rejects("charlie closing the pull", closeThroughApi, 403) .knotStored(pullId, "open");
flow("only the owner is offered the delete button in the repo settings") .actor("owner") .repo() .use(withHelper) .open(page.settings) .sees(testid("setConfirmDelete"))
.actor("helper") .open(page.settings) .sees(testid("formDefaultBranch")) .sees(not(testid("setConfirmDelete")))
.actor(seeded.alice) .open(page.settings) .sees(testid("errorMessage")) .sees(not(testid("setConfirmDelete")));
// the owner's declaration is still up, which only an admin's force gets pastconst aliceDeletes = async (ctx: Ctx) => { const reply = await asKnot(ctx.actor, "org.tangled.repo.deleteRepo", { repo: ctx.repo.did, force: true }); expect(reply.status, `the delete answered ${JSON.stringify(reply.body)}`).toBe(200);};
flow("a knot admin deletes a repo she does not own through the api") .actor("owner") .repo() .push(base) .actor(seeded.alice) .step("alice deletes it through the api", aliceDeletes) .step("the knot has dropped the repo", async (ctx) => { await until(`the knot should have dropped ${ctx.repo.did}`, 10_000, async () => [ (await knotRecord(selfUri(ctx.repo.did, manifestId.nsid))) === undefined, "it still has the manifest" ]); });
flow("a repo a knot admin deleted is gone from bobbin too") .actor("owner") .repo() .step("remember the owner", remember("owner")) .actor(seeded.alice) .step("alice deletes it through the api", aliceDeletes) .step("bobbin no longer knows the repo", async (ctx) => { await until(`bobbin should have dropped ${ctx.repo.name}`, 20_000, async () => { const { status } = await bobbin.repoByName(recall(ctx, "owner").did, ctx.repo.name); return [status !== 200, `getRepoByName answered ${status}`]; }); });
flow("a collaborator can't add or remove collaborators") .actor("owner") .repo() .use(withHelper)
.actor("helper") .rejects( "the helper adding charlie", collaboration(ADD, () => seeded.charlie.did), 403 ) .rejects( "the helper removing themself", collaboration(REMOVE, (ctx) => ctx.actor.did), 403 ) .step("the knot still lists only the helper", async (ctx) => { expect(await knotSubjects(ctx.repo.did)).toEqual([recall(ctx, "helper").did]); }) .open(access) .sees(testid("errorMessage")) .open(addCollaboratorForm) .sees(testid("errorMessage"));