174 lines
6 KiB
TypeScript
174 lines
6 KiB
TypeScript
import { getPayload, Payload, type Where } from "payload";
|
|
import config from "@/payload.config";
|
|
|
|
import { afterAll, beforeAll, describe, expect, it } from "vitest";
|
|
|
|
import type { Role, User, VoiceScript, VoiceSubmission } from "@/payload-types";
|
|
import { listScriptsForUser } from "@/lib/voiceover";
|
|
import { invalidatePermissionCache } from "@/utils/access-control/loadUserPermissions";
|
|
|
|
let payload: Payload;
|
|
|
|
const RUN = `vo-list-${Date.now().toString(36)}`;
|
|
const TIMEOUT = 30_000;
|
|
|
|
describe("voiceover listing scopes submissions to their script", () => {
|
|
const roleIds: number[] = [];
|
|
const userIds: number[] = [];
|
|
const scriptIds: number[] = [];
|
|
const submissionIds: number[] = [];
|
|
|
|
let plainUser: User;
|
|
let otherUser: User;
|
|
let scriptA: VoiceScript;
|
|
let scriptB: VoiceScript;
|
|
|
|
const makeRole = async (label: string, extra: Partial<Role> = {}): Promise<Role> => {
|
|
const role = (await payload.create({
|
|
collection: "roles",
|
|
data: { name: `${RUN}-${label}`, slug: `${RUN}-${label}`, ...extra },
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as Role;
|
|
roleIds.push(role.id);
|
|
return role;
|
|
};
|
|
|
|
const makeUser = async (label: string, roleId: number): Promise<User> => {
|
|
const user = (await payload.create({
|
|
collection: "users",
|
|
data: {
|
|
username: `${RUN}-${label}`,
|
|
discordUsername: `${RUN}-${label}`,
|
|
displayName: label.toUpperCase(),
|
|
steamId: `7656119${Math.floor(Math.random() * 1e9)}`,
|
|
password: "Test123",
|
|
roleDocs: [roleId],
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as User;
|
|
userIds.push(user.id);
|
|
return user;
|
|
};
|
|
|
|
const createScript = async (label: string): Promise<VoiceScript> => {
|
|
const script = (await payload.create({
|
|
collection: "voice-scripts",
|
|
data: {
|
|
title: `${RUN}-${label}`,
|
|
script: `${RUN} ${label} script text`,
|
|
xpAmount: 100,
|
|
status: "open",
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as VoiceScript;
|
|
scriptIds.push(script.id);
|
|
return script;
|
|
};
|
|
|
|
const createSubmission = async (
|
|
scriptId: number,
|
|
userId: number,
|
|
status: VoiceSubmission["status"],
|
|
): Promise<VoiceSubmission> => {
|
|
// voice-submissions is an upload collection, so create requires a file
|
|
// payload; a tiny buffer with an audio mimetype is enough to seed a row.
|
|
const submission = (await payload.create({
|
|
collection: "voice-submissions",
|
|
data: { script: scriptId, user: userId, status },
|
|
file: {
|
|
data: Buffer.from("test-audio-payload"),
|
|
mimetype: "audio/mpeg",
|
|
name: `${RUN}-take.mp3`,
|
|
size: 20,
|
|
},
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
})) as unknown as VoiceSubmission;
|
|
submissionIds.push(submission.id);
|
|
return submission;
|
|
};
|
|
|
|
beforeAll(async () => {
|
|
const payloadConfig = await config;
|
|
payload = await getPayload({ config: payloadConfig });
|
|
|
|
const plainRole = await makeRole("plain", { permissions: [] });
|
|
const otherRole = await makeRole("other", { permissions: [] });
|
|
|
|
plainUser = await makeUser("plain", plainRole.id);
|
|
otherUser = await makeUser("other", otherRole.id);
|
|
|
|
scriptA = await createScript("script-a");
|
|
scriptB = await createScript("script-b");
|
|
|
|
// The prod bug: a single take on script A was rendered under EVERY script
|
|
// card because the listing attributed submissions by submitter only.
|
|
await createSubmission(scriptA.id, plainUser.id, "rejected");
|
|
// A different user's take on script B, so the "other" count is per-script.
|
|
await createSubmission(scriptB.id, otherUser.id, "pending");
|
|
|
|
invalidatePermissionCache();
|
|
}, TIMEOUT);
|
|
|
|
afterAll(async () => {
|
|
if (!payload) return;
|
|
for (const id of submissionIds) {
|
|
await payload.delete({ collection: "voice-submissions", id, overrideAccess: true }).catch(
|
|
() => {},
|
|
);
|
|
}
|
|
for (const id of scriptIds) {
|
|
await payload.delete({ collection: "voice-scripts", id, overrideAccess: true }).catch(
|
|
() => {},
|
|
);
|
|
}
|
|
for (const id of userIds) {
|
|
await payload.delete({ collection: "users", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
for (const id of roleIds) {
|
|
await payload.delete({ collection: "roles", id, overrideAccess: true }).catch(() => {});
|
|
}
|
|
});
|
|
|
|
it("shows a user's take only on the script they submitted it to", async () => {
|
|
const views = await listScriptsForUser(payload, plainUser);
|
|
const viewA = views.find((s) => s.id === scriptA.id);
|
|
const viewB = views.find((s) => s.id === scriptB.id);
|
|
expect(viewA).toBeDefined();
|
|
expect(viewB).toBeDefined();
|
|
|
|
// The take lives on script A.
|
|
expect(viewA?.mySubmissions).toHaveLength(1);
|
|
expect(viewA?.mySubmissions[0]?.status).toBe("rejected");
|
|
|
|
// THE BUG FIX: script B must not inherit script A's take.
|
|
expect(viewB?.mySubmissions).toHaveLength(0);
|
|
});
|
|
|
|
it("counts other submissions per script, not across all scripts", async () => {
|
|
const views = await listScriptsForUser(payload, plainUser);
|
|
const viewA = views.find((s) => s.id === scriptA.id);
|
|
const viewB = views.find((s) => s.id === scriptB.id);
|
|
|
|
// Script A: only plainUser's own take exists -> no other submissions.
|
|
expect(viewA?.otherSubmissionCount).toBe(0);
|
|
// Script B: one take by the other user -> counted here, not on A.
|
|
expect(viewB?.otherSubmissionCount).toBe(1);
|
|
});
|
|
|
|
it("exposes the VoiceSubmissions read access as an owner scope for members", async () => {
|
|
const read = (await import("@/collections/voiceover/VoiceSubmissions")).VoiceSubmissions
|
|
.access?.read;
|
|
expect(typeof read).toBe("function");
|
|
const fn = read as (args: { req: unknown }) => Promise<Where | boolean>;
|
|
|
|
const memberClause = await fn({ req: { user: plainUser, payload } });
|
|
expect(typeof memberClause).toBe("object");
|
|
expect((memberClause as Where).user).toEqual({ equals: plainUser.id });
|
|
|
|
expect(await fn({ req: { user: null, payload } })).toBe(false);
|
|
}, TIMEOUT);
|
|
});
|