mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +00:00
fix(settings): list layout and preview switch serve the person, atomically
Both routes resolved through bare requireAuth, which refuses a session acting for another record, so the list view controls and the preview switch failed in exactly the context they are used in. They are the person's own presentation and now resolve through requireActorAuth, admitted on the actor-surface list with their reasons. The layout PUT and the preview PATCH read and write the stored blob under a row lock in one transaction, so two saves at once no longer start from the same value and drop each other's change.
This commit is contained in:
1 parent
557865acca
commit
90c9a4e2d0
4 files changed
+173
-35
No files matched your search
@@ -15,7 +15,7 @@ process.env.ENCRYPTION_KEY ??=
|
||||
"0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
|
||||
|
||||
import { cookieJar, headerJar } from "./mock-next-headers";
|
||||
import { getPrismaClient, truncateAllTables } from "./setup";
|
||||
import { getPrismaClient, switchSessionTo, truncateAllTables } from "./setup";
|
||||
|
||||
vi.mock("next/headers", async () => {
|
||||
const { cookieJar, headerJar } = await import("./mock-next-headers");
|
||||
@@ -214,3 +214,111 @@ describe("/api/settings/preview-features — integration", () => {
|
||||
expect(empty.status).toBe(422);
|
||||
});
|
||||
});
|
||||
|
||||
describe("settings presentation follows the person, also while acting for a record", () => {
|
||||
const LAYOUT = "http://localhost/api/settings/module-list-layout";
|
||||
const PREVIEW = "http://localhost/api/settings/preview-features";
|
||||
|
||||
async function actingForAnotherRecord() {
|
||||
const prisma = getPrismaClient();
|
||||
const owner = await prisma.user.create({
|
||||
data: {
|
||||
username: "prefs-record-owner",
|
||||
email: "prefs-owner@example.test",
|
||||
role: "USER",
|
||||
},
|
||||
});
|
||||
const actor = await prisma.user.create({
|
||||
data: {
|
||||
username: "prefs-delegate",
|
||||
email: "prefs-delegate@example.test",
|
||||
role: "USER",
|
||||
},
|
||||
});
|
||||
await prisma.accountGrant.create({
|
||||
data: {
|
||||
grantorId: owner.id,
|
||||
granteeId: actor.id,
|
||||
access: "WRITE",
|
||||
acceptedAt: new Date(),
|
||||
},
|
||||
});
|
||||
const session = await prisma.session.create({
|
||||
data: { userId: actor.id, expiresAt: new Date(Date.now() + 60_000) },
|
||||
});
|
||||
cookieJar.set("healthlog_session", session.id);
|
||||
await switchSessionTo(session.id, owner.id);
|
||||
return { ownerId: owner.id, actorId: actor.id };
|
||||
}
|
||||
|
||||
it("reads and saves the actor's own layout and preview switch", async () => {
|
||||
const { ownerId, actorId } = await actingForAnotherRecord();
|
||||
const layout = await import("@/app/api/settings/module-list-layout/route");
|
||||
const preview = await import("@/app/api/settings/preview-features/route");
|
||||
|
||||
expect((await (layout.GET as Handler)(new Request(LAYOUT))).status).toBe(
|
||||
200,
|
||||
);
|
||||
const put = await (layout.PUT as Handler)(
|
||||
json("PUT", LAYOUT, { version: 1, list: "labs", view: "cards" }),
|
||||
);
|
||||
expect(put.status).toBe(200);
|
||||
expect((await (preview.GET as Handler)(new Request(PREVIEW))).status).toBe(
|
||||
200,
|
||||
);
|
||||
const patch = await (preview.PATCH as Handler)(
|
||||
json("PATCH", PREVIEW, { topics: true }),
|
||||
);
|
||||
expect(patch.status).toBe(200);
|
||||
|
||||
const rows = await getPrismaClient().user.findMany({
|
||||
where: { id: { in: [ownerId, actorId] } },
|
||||
select: {
|
||||
id: true,
|
||||
moduleListLayoutJson: true,
|
||||
previewFeaturesJson: true,
|
||||
},
|
||||
});
|
||||
const actor = rows.find((r) => r.id === actorId)!;
|
||||
const owner = rows.find((r) => r.id === ownerId)!;
|
||||
expect(actor.moduleListLayoutJson).toMatchObject({
|
||||
labs: { view: "cards" },
|
||||
});
|
||||
expect(actor.previewFeaturesJson).toEqual({ topics: true });
|
||||
expect(owner.moduleListLayoutJson).toBeNull();
|
||||
expect(owner.previewFeaturesJson).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("layout saves for different lists do not overwrite each other", () => {
|
||||
const URL = "http://localhost/api/settings/module-list-layout";
|
||||
|
||||
it("keeps every list a burst of concurrent saves touched", async () => {
|
||||
const { userId } = await seedUser();
|
||||
const { PUT } = await import("@/app/api/settings/module-list-layout/route");
|
||||
const lists = ["labs", "illness", "vorsorge"] as const;
|
||||
await Promise.all(
|
||||
Array.from({ length: 9 }, (_, i) =>
|
||||
(PUT as Handler)(
|
||||
json("PUT", URL, {
|
||||
version: 1,
|
||||
list: lists[i % 3],
|
||||
...(i < 3 ? { view: "cards" } : { order: [`item-${i}`] }),
|
||||
}),
|
||||
),
|
||||
),
|
||||
);
|
||||
const row = await getPrismaClient().user.findUniqueOrThrow({
|
||||
where: { id: userId },
|
||||
select: { moduleListLayoutJson: true },
|
||||
});
|
||||
const stored = row.moduleListLayoutJson as Record<
|
||||
string,
|
||||
{ view?: string; order?: string[] }
|
||||
>;
|
||||
for (const list of lists) {
|
||||
expect(stored[list]?.view, list).toBe("cards");
|
||||
expect(stored[list]?.order, list).toBeDefined();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user