Save a guest edit on the published Rule, and publish a new one when that update is refused.
A leftover editing id was treated as a successful finish in this browser only, so the public Rule stayed stale and a later Finish could skip creating a new Rule.
This commit is contained in:
1 parent
0535f6f167
commit
2644023b5a
6 files changed
+207
-44
No files matched your search
@@ -5,6 +5,7 @@ const isDatabaseConfiguredMock = vi.fn();
|
||||
const findUniqueMock = vi.fn();
|
||||
const updateMock = vi.fn();
|
||||
const getSessionUserMock = vi.fn();
|
||||
const browserHoldsClaimForHashMock = vi.fn();
|
||||
|
||||
vi.mock("../../lib/server/env", () => ({
|
||||
isDatabaseConfigured: () => isDatabaseConfiguredMock(),
|
||||
@@ -23,22 +24,37 @@ vi.mock("../../lib/server/session", () => ({
|
||||
getSessionUser: () => getSessionUserMock(),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/guestRuleClaim", () => ({
|
||||
browserHoldsClaimForHash: (...args: unknown[]) =>
|
||||
browserHoldsClaimForHashMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock("../../lib/server/uploads/linkUploads", () => ({
|
||||
linkUploadsToRule: vi.fn().mockResolvedValue(undefined),
|
||||
}));
|
||||
|
||||
import { PATCH } from "../../app/api/rules/[id]/route";
|
||||
import { linkUploadsToRule } from "../../lib/server/uploads/linkUploads";
|
||||
|
||||
function makeContext(id: string) {
|
||||
return { params: Promise.resolve({ id }) };
|
||||
}
|
||||
|
||||
const patchBody = {
|
||||
title: "Updated",
|
||||
summary: "Context",
|
||||
document: { sections: [], coreValues: [] },
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
isDatabaseConfiguredMock.mockReset();
|
||||
findUniqueMock.mockReset();
|
||||
updateMock.mockReset();
|
||||
getSessionUserMock.mockReset();
|
||||
browserHoldsClaimForHashMock.mockReset();
|
||||
vi.mocked(linkUploadsToRule).mockClear();
|
||||
getSessionUserMock.mockResolvedValue({ id: "user-1", email: "a@b.c" });
|
||||
browserHoldsClaimForHashMock.mockResolvedValue(false);
|
||||
});
|
||||
|
||||
describe("PATCH /api/rules/[id]", () => {
|
||||
@@ -114,4 +130,49 @@ describe("PATCH /api/rules/[id]", () => {
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("updates an unclaimed rule when this browser holds the claim", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
getSessionUserMock.mockResolvedValueOnce(null);
|
||||
browserHoldsClaimForHashMock.mockResolvedValueOnce(true);
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: "r1",
|
||||
userId: null,
|
||||
claimTokenHash: "claim-hash",
|
||||
});
|
||||
updateMock.mockResolvedValueOnce({});
|
||||
const res = await PATCH(
|
||||
new NextRequest("https://x.test/api/rules/r1", {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(patchBody),
|
||||
}),
|
||||
makeContext("r1"),
|
||||
);
|
||||
expect(res.status).toBe(200);
|
||||
expect(browserHoldsClaimForHashMock).toHaveBeenCalledWith("claim-hash");
|
||||
expect(updateMock).toHaveBeenCalled();
|
||||
expect(linkUploadsToRule).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ ruleId: "r1", userId: null }),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns 401 when a guest does not hold the claim", async () => {
|
||||
isDatabaseConfiguredMock.mockReturnValue(true);
|
||||
getSessionUserMock.mockResolvedValueOnce(null);
|
||||
browserHoldsClaimForHashMock.mockResolvedValueOnce(false);
|
||||
findUniqueMock.mockResolvedValueOnce({
|
||||
id: "r1",
|
||||
userId: null,
|
||||
claimTokenHash: "claim-hash",
|
||||
});
|
||||
const res = await PATCH(
|
||||
new NextRequest("https://x.test/api/rules/r1", {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify(patchBody),
|
||||
}),
|
||||
makeContext("r1"),
|
||||
);
|
||||
expect(res.status).toBe(401);
|
||||
expect(updateMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user