From 9f9257cadfd75077a91ca16bdae26ff8dd624b1e Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Fri, 9 Oct 2026 13:14:35 -0400 Subject: [PATCH] test(notifications): add per-recipient read-scope test --- tests/int/notification-read-scope.int.spec.ts | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 tests/int/notification-read-scope.int.spec.ts diff --git a/tests/int/notification-read-scope.int.spec.ts b/tests/int/notification-read-scope.int.spec.ts new file mode 100644 index 00000000..c71cc0d4 --- /dev/null +++ b/tests/int/notification-read-scope.int.spec.ts @@ -0,0 +1,140 @@ +import { getPayload, Payload } from "payload"; +import config from "@/payload.config"; + +import { afterAll, beforeAll, describe, expect, it } from "vitest"; + +import type { Role, User } from "@/payload-types"; +import { invalidatePermissionCache } from "@/utils/access-control/loadUserPermissions"; +import { + markAllNotificationsReadForUser, + markNotificationsReadForUser, +} from "@/lib/notifications"; + +let payload: Payload; + +const RUN = `ntfread-${Date.now().toString(36)}`; +const TIMEOUT = 30_000; + +describe("notification read-state is personal to the recipient", () => { + const roleIds: number[] = []; + const userIds: number[] = []; + const notifIds: number[] = []; + + let alice: User; + let bob: User; + + const makeRole = async (label: string, extra: Partial = {}): Promise => { + 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 => { + 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 createNotification = async (userId: number): Promise => { + const doc = await payload.create({ + collection: "user-notifications", + data: { + user: userId, + type: "test:scope", + title: `${RUN} ${userId}`, + message: "test", + read: false, + }, + overrideAccess: true, + depth: 0, + }); + notifIds.push(doc.id as number); + return doc.id as number; + }; + + const isRead = async (id: number): Promise => { + const res = await payload.find({ + collection: "user-notifications", + where: { id: { equals: id } }, + limit: 1, + depth: 0, + overrideAccess: true, + }); + return Boolean(res.docs[0]?.read); + }; + + beforeAll(async () => { + const payloadConfig = await config; + payload = await getPayload({ config: payloadConfig }); + invalidatePermissionCache(); + + const plainRole = await makeRole("plain", { permissions: [] }); + alice = await makeUser("alice", plainRole.id); + bob = await makeUser("bob", plainRole.id); + }, TIMEOUT); + + afterAll(async () => { + if (!payload) return; + for (const id of notifIds) { + await payload.delete({ collection: "user-notifications", 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("markNotificationsReadForUser only flips the actor's own notifications", async () => { + const aliceId = await createNotification(alice.id); + const bobId = await createNotification(bob.id); + + // Alice attempts to mark BOTH reads. The bug: with overrideAccess the + // original code flipped bob's row too, hiding it from bob. + await markNotificationsReadForUser(payload, alice.id, [aliceId, bobId]); + + expect(await isRead(aliceId)).toBe(true); + expect(await isRead(bobId)).toBe(false); + }, TIMEOUT); + + it("markAllNotificationsReadForUser never touches another user's unread rows", async () => { + const aliceId = await createNotification(alice.id); + const bobId = await createNotification(bob.id); + + await markAllNotificationsReadForUser(payload, alice.id); + + expect(await isRead(aliceId)).toBe(true); + expect(await isRead(bobId)).toBe(false); + }, TIMEOUT); + + it("marks an actor's own notifications read when called for themselves", async () => { + const aliceId = await createNotification(alice.id); + const bobId = await createNotification(bob.id); + + const updated = await markAllNotificationsReadForUser(payload, alice.id); + + expect(updated).toEqual([aliceId]); + expect(await isRead(aliceId)).toBe(true); + expect(await isRead(bobId)).toBe(false); + }, TIMEOUT); +});