1
0
Fork 0
polaris-task-force/tests/int/wiki-draft.int.spec.ts
Z8MB1E b5f48de0b9 feat(wiki): add private drafts with a one-way publish
Wiki pages gain a Draft flag pinned to their original creator. A draft is
visible only to its creator, wiki moderators and admins: the collection's
read access returns a where clause so the REST/local API filters drafts,
and every frontend query ANDs the same clause (they run with overrideAccess).

Publishing is one-way. A page whose stored draft is false is rejected (403)
when sent draft: true, both in the service and in access.update.

UI: amber DraftBanner on the detail page, a Draft chip in WikiRow, and a
Draft switch in the editor that disables once the page is published.
2026-11-18 11:06:37 -05:00

271 lines
8.7 KiB
TypeScript

import { getTestPayload } from "../helpers/get-test-payload";
import { Payload } from "payload";
import type { AccessArgs } from "payload";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import type { User, WikiPage } from "@/payload-types";
import { WikiPages } from "@/collections/wiki/WikiPages";
import {
canViewAllDrafts,
createPage,
draftVisibilityWhere,
updatePage,
} from "@/lib/wiki/service";
import { mapWikiPageToEntry } from "@/lib/wiki/indexView";
let payload: Payload;
const RUN = `wiki-draft-${Date.now().toString(36)}`;
/** Users cascade to profiles/bank accounts, which block deletion first. */
const deleteUserWithRelations = async (pg: Payload, id: number): Promise<void> => {
const accounts = await pg
.find({
collection: "bank-accounts",
where: { ownerUser: { equals: id } },
limit: 5,
depth: 0,
overrideAccess: true,
})
.catch(() => null);
for (const account of accounts?.docs ?? []) {
await pg
.delete({ collection: "bank-accounts", id: account.id, overrideAccess: true })
.catch(() => {});
}
const profiles = await pg
.find({
collection: "profiles",
where: { user: { equals: id } },
limit: 5,
depth: 0,
overrideAccess: true,
})
.catch(() => null);
for (const profile of profiles?.docs ?? []) {
await pg
.delete({ collection: "profiles", id: profile.id, overrideAccess: true })
.catch(() => {});
}
await pg.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
};
describe("Wiki drafts", () => {
let author: User;
let other: User;
let admin: User;
const userIds: number[] = [];
const pageIds: number[] = [];
const makeUser = async (label: string, roles?: Array<"user" | "admin">): Promise<User> => {
const doc = (await payload.create({
collection: "users",
data: {
username: `${RUN}-${label}`,
discordUsername: `${RUN}-${label}`,
displayName: `WIKI DRAFT ${label}`,
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
password: "Test123",
...(roles ? { roles } : {}),
},
overrideAccess: true,
depth: 0,
})) as unknown as User;
userIds.push(doc.id);
return doc;
};
/** Loads a page the way the frontend does, through access control. */
const visibleTo = async (
user: User,
id: number,
): Promise<boolean> => {
const result = (await payload.find({
collection: "wiki-pages",
where: { id: { equals: id } },
limit: 1,
depth: 0,
user,
overrideAccess: false,
})) as unknown as { docs: WikiPage[] };
return result.docs.length > 0;
};
const makePage = async (
title: string,
draft: boolean,
creator: User = author,
): Promise<WikiPage> => {
const page = await createPage(payload, creator, {
title: `${RUN} ${title}`,
category: "Lore",
body: "draft body",
draft,
});
pageIds.push(page.id);
return page;
};
beforeAll(async () => {
payload = await getTestPayload();
author = await makeUser("author", ["user"]);
other = await makeUser("other", ["user"]);
admin = await makeUser("admin", ["admin"]);
});
afterAll(async () => {
for (const id of pageIds) {
const revisions = await payload
.find({
collection: "wiki-revisions",
where: { page: { equals: id } },
limit: 500,
depth: 0,
overrideAccess: true,
})
.catch(() => null);
for (const revision of revisions?.docs ?? []) {
await payload
.delete({ collection: "wiki-revisions", id: revision.id, overrideAccess: true })
.catch(() => {});
}
await payload
.delete({ collection: "wiki-pages", id, overrideAccess: true })
.catch(() => {});
}
for (const id of userIds) {
await deleteUserWithRelations(payload, id);
}
});
describe("service", () => {
it("records the original creator and publishes by default", async () => {
const page = await makePage("Default", false);
expect(page.draft).toBe(false);
expect(page.createdBy).toBe(author.id);
});
it("creates a draft when asked, and can publish it later", async () => {
const page = await makePage("Publishable", true);
expect(page.draft).toBe(true);
const published = await updatePage(payload, author, page, { draft: false });
expect(published.draft).toBe(false);
// Publishing does not move authorship: the creator stays put.
expect(published.createdBy).toBe(author.id);
});
it("refuses to set a published page back to draft", async () => {
const page = await makePage("OneWay", false);
await expect(updatePage(payload, author, page, { draft: true })).rejects.toThrow(
"This page has been published and cannot be set back to draft.",
);
});
});
describe("visibility", () => {
it("shows a draft to its creator but not to another member", async () => {
const page = await makePage("Private", true);
expect(await visibleTo(author, page.id)).toBe(true);
expect(await visibleTo(other, page.id)).toBe(false);
});
it("shows a draft to an admin", async () => {
const page = await makePage("AdminVisible", true);
expect(await visibleTo(admin, page.id)).toBe(true);
});
it("shows a published page to everyone", async () => {
const page = await makePage("Public", false);
expect(await visibleTo(other, page.id)).toBe(true);
});
it("hides a draft from the index query built for a plain member", async () => {
const page = await makePage("Indexed", true);
const where = draftVisibilityWhere(other, false);
const result = (await payload.find({
collection: "wiki-pages",
where: { AND: [{ id: { equals: page.id } }, where ?? {}] },
limit: 1,
depth: 0,
overrideAccess: true,
})) as unknown as { docs: WikiPage[] };
expect(result.docs).toHaveLength(0);
});
it("does not restrict the query for moderators/admins", async () => {
expect(draftVisibilityWhere(admin, true)).toBeUndefined();
expect(draftVisibilityWhere(null, false)).toEqual({ draft: { equals: false } });
});
it("classifies a plain member and an admin correctly", async () => {
expect(await canViewAllDrafts(payload, other)).toBe(false);
expect(await canViewAllDrafts(payload, admin)).toBe(true);
expect(await canViewAllDrafts(payload, null)).toBe(false);
});
});
describe("collection access", () => {
const readAccess = async (user: User | null) => {
const args = { req: { payload, user } } as unknown as AccessArgs;
return (await WikiPages.access?.read?.(args)) ?? false;
};
it("denies anonymous readers", async () => {
expect(await readAccess(null)).toBe(false);
});
it("returns a draft-hiding where clause for a plain member", async () => {
const result = await readAccess(other);
expect(result).toEqual({
OR: [
{ draft: { equals: false } },
{ draft: { equals: true }, createdBy: { equals: other.id } },
],
});
});
it("lets an admin read everything", async () => {
expect(await readAccess(admin)).toBe(true);
});
const updateAccess = async (user: User, id: number, data: Record<string, unknown>) => {
const args = { req: { payload, user }, id, data } as unknown as AccessArgs;
return (await WikiPages.access?.update?.(args)) ?? false;
};
it("allows publishing a draft", async () => {
const page = await makePage("AccessPublish", true);
expect(await updateAccess(author, page.id, { draft: false })).toBe(true);
});
it("refuses to unpublish a published page", async () => {
const page = await makePage("AccessOneWay", false);
expect(await updateAccess(author, page.id, { draft: true })).toBe(false);
});
it("tolerates an access check with no data payload", async () => {
// Payload calls update access without `data` on some paths; this used to
// throw and 500 every wiki page render.
const page = await makePage("AccessNoData", true);
expect(await updateAccess(author, page.id, {})).toBe(true);
});
});
describe("index view model", () => {
it("carries the draft flag through to the row model", () => {
const base = {
id: 1,
title: "Draft Page",
slug: "draft-page",
category: "Lore",
body: "text",
updatedAt: "2026-01-01T00:00:00.000Z",
createdAt: "2026-01-01T00:00:00.000Z",
lastEditor: null,
draft: true,
};
expect(mapWikiPageToEntry(base as never).draft).toBe(true);
});
});
});