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
@@ -109,6 +109,9 @@ describe("useCreateFlowFinalize", () => {
|
||||
sessionStorage.getItem(CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY),
|
||||
).toBeNull();
|
||||
expect(deleteServerDraft).toHaveBeenCalledTimes(1);
|
||||
expect(updateState).toHaveBeenCalledWith({
|
||||
editingPublishedRuleId: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("retries clearing the server draft when the first delete fails", async () => {
|
||||
@@ -450,7 +453,9 @@ describe("useCreateFlowFinalize", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("does not PATCH when a guest publishes an already-published rule", async () => {
|
||||
it("PATCHes when a guest finishes an edit of a rule this browser published", async () => {
|
||||
vi.mocked(updatePublishedRule).mockResolvedValue({ ok: true });
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useCreateFlowFinalize({
|
||||
state: {
|
||||
@@ -470,8 +475,16 @@ describe("useCreateFlowFinalize", () => {
|
||||
await result.current.finalize();
|
||||
});
|
||||
|
||||
expect(updatePublishedRule).not.toHaveBeenCalled();
|
||||
expect(updatePublishedRule).toHaveBeenCalledWith(
|
||||
"guest-rule-1",
|
||||
expect.objectContaining({
|
||||
title: "Published title",
|
||||
summary: "Published summary",
|
||||
document: {},
|
||||
}),
|
||||
);
|
||||
expect(publishRule).not.toHaveBeenCalled();
|
||||
expect(deleteServerDraft).not.toHaveBeenCalled();
|
||||
expect(openLogin).not.toHaveBeenCalled();
|
||||
expect(onGuestPublished).toHaveBeenCalledWith({ skippedInvites: false });
|
||||
expect(writeLastPublishedRule).toHaveBeenCalledWith({
|
||||
@@ -480,10 +493,92 @@ describe("useCreateFlowFinalize", () => {
|
||||
summary: "Published summary",
|
||||
document: {},
|
||||
});
|
||||
expect(updateState).toHaveBeenCalledWith({
|
||||
editingPublishedRuleId: undefined,
|
||||
});
|
||||
expect(sessionStorage.getItem(CREATE_FLOW_PENDING_KEEP_RULE_LOGIN_KEY)).toBeNull();
|
||||
expect(router.push).toHaveBeenCalledWith("/create/completed");
|
||||
});
|
||||
|
||||
it("publishes a new rule when a guest cannot update the leftover id", async () => {
|
||||
vi.mocked(updatePublishedRule).mockResolvedValue({
|
||||
ok: false,
|
||||
error: "Unauthorized",
|
||||
status: 401,
|
||||
});
|
||||
vi.mocked(publishRule).mockResolvedValue({
|
||||
ok: true,
|
||||
id: "new-guest-rule",
|
||||
title: "Published title",
|
||||
});
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useCreateFlowFinalize({
|
||||
state: {
|
||||
...emptyState,
|
||||
editingPublishedRuleId: "stale-rule",
|
||||
},
|
||||
router,
|
||||
openLogin,
|
||||
updateState,
|
||||
loginReturnPath: "/create/final-review?syncDraft=1",
|
||||
sessionUser: null,
|
||||
onGuestPublished,
|
||||
}),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.finalize();
|
||||
});
|
||||
|
||||
expect(updatePublishedRule).toHaveBeenCalledWith(
|
||||
"stale-rule",
|
||||
expect.anything(),
|
||||
);
|
||||
expect(publishRule).toHaveBeenCalled();
|
||||
expect(openLogin).not.toHaveBeenCalled();
|
||||
expect(writeLastPublishedRule).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ id: "new-guest-rule" }),
|
||||
);
|
||||
expect(updateState).toHaveBeenCalledWith({
|
||||
editingPublishedRuleId: undefined,
|
||||
});
|
||||
expect(router.push).toHaveBeenCalledWith(
|
||||
`/create/completed?${CREATE_FLOW_COMPLETED_CELEBRATE_QUERY}=${CREATE_FLOW_COMPLETED_CELEBRATE_VALUE}`,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not publish a second rule when a guest update fails on the server", async () => {
|
||||
vi.mocked(updatePublishedRule).mockResolvedValue({
|
||||
ok: false,
|
||||
error: "Database unavailable",
|
||||
status: 500,
|
||||
});
|
||||
|
||||
const { result } = renderHook(() =>
|
||||
useCreateFlowFinalize({
|
||||
state: {
|
||||
...emptyState,
|
||||
editingPublishedRuleId: "guest-rule-1",
|
||||
},
|
||||
router,
|
||||
openLogin,
|
||||
updateState,
|
||||
loginReturnPath: "/create/final-review?syncDraft=1",
|
||||
sessionUser: null,
|
||||
onGuestPublished,
|
||||
}),
|
||||
);
|
||||
|
||||
await act(async () => {
|
||||
await result.current.finalize();
|
||||
});
|
||||
|
||||
expect(publishRule).not.toHaveBeenCalled();
|
||||
expect(router.push).not.toHaveBeenCalled();
|
||||
expect(result.current.publishBannerMessage).toBe("Database unavailable");
|
||||
});
|
||||
|
||||
it("opens keepRule login when a guest publish is unauthorized", async () => {
|
||||
vi.mocked(publishRule).mockResolvedValue({
|
||||
ok: false,
|
||||
|
||||
@@ -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